Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the ability to tesh to mess with the processes' environment
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 20 May 2008 15:51:09 +0000 (15:51 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 20 May 2008 15:51:09 +0000 (15:51 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5450 48e7efb5-ca39-0410-a469-dd3cf9ba447f

tools/tesh/README.tesh
tools/tesh/run_context.c
tools/tesh/run_context.h
tools/tesh/setenv.tesh [new file with mode: 0644]

index f63a90f..5eb44e8 100644 (file)
@@ -18,6 +18,7 @@ blank and is ignored):
      `expect signal' <signal name>
      `expect return' <integer>
      `output' <ignore|display>
+     `setenv <key>=<val>'
  `p' a string to print
  `P' a string to print at the CRITICAL level (ease logging grepping)
 
@@ -87,4 +88,10 @@ OUTPUT
 By default, the commands output is matched against the one expected,
 and an error is raised on discrepency. Metacomands to change this:
  "output ignore"  -> output completely discarded 
- "output display" -> output displayed (but not verified) 
\ No newline at end of file
+ "output display" -> output displayed (but not verified)
+ENVIRONMENT
+-----------
+You can add some content to the tested processes environment with the
+setenv metacommand. It works as expected. For example:
+  "setenv PATH=/bin"
\ No newline at end of file
index 36547cc..7a91873 100644 (file)
@@ -103,11 +103,23 @@ void rctx_armageddon(rctx_t initiator, int exitcode) {
  */
 
 void rctx_empty(rctx_t rc) {
+  int i;
+  char **env_it=environ;
+   
   if (rc->cmd)
     free(rc->cmd);
   rc->cmd = NULL;
   if (rc->filepos)
     free(rc->filepos);
+  if (rc->env)
+     free(rc->env);
+   
+  for (i=0;*env_it;i++,env_it++);
+  i++;
+  rc->env_size = i;
+  rc->env = malloc(i*sizeof(char*));
+  memcpy(rc->env,environ,i*sizeof(char*)); 
+
   rc->filepos = NULL;
   rc->is_empty = 1;
   rc->is_background = 0;
@@ -121,6 +133,9 @@ void rctx_empty(rctx_t rc) {
   xbt_strbuff_empty(rc->output_got);
 }
 
+/* the environment, as specified by the opengroup */
+extern char **environ;
+
 rctx_t rctx_new() {
   rctx_t res = xbt_new0(s_rctx_t,1);
 
@@ -142,6 +157,8 @@ void rctx_free(rctx_t rctx) {
     free(rctx->cmd);
   if (rctx->filepos)
     free(rctx->filepos);
+  if (rctx->env)
+    free(rctx->env);
   xbt_os_mutex_destroy(rctx->interruption);
   xbt_strbuff_free(rctx->input);
   xbt_strbuff_free(rctx->output_got);
@@ -235,6 +252,12 @@ void rctx_pushline(const char* filepos, char kind, char *line) {
     } else if (!strncmp(line,"output display",strlen("output display"))) {
       rctx->output = e_output_display;
       VERB1("[%s] (ignore output of next command)", filepos);
+
+    } else if (!strncmp(line,"setenv ",strlen("setenv "))) {
+      rctx->env = realloc(rctx->env,++(rctx->env_size)*sizeof(char*));
+      rctx->env[rctx->env_size-2] = xbt_strdup(line+strlen("setenv "));
+      rctx->env[rctx->env_size-1] = NULL;
+      VERB1("[%s] (ignore output of next command)", filepos);
        
     } else {
       ERROR2("%s: Malformed metacommand: %s",filepos,line);
@@ -368,7 +391,7 @@ void rctx_start(void) {
     dup2(child_out[1],2);
     close(child_out[1]);
 
-    execlp ("/bin/sh", "sh", "-c", rctx->cmd, NULL);
+    execle ("/bin/sh", "sh", "-c", rctx->cmd, NULL, rctx->env);
   }
 
   rctx->is_stoppable = 1;
index bc76e3b..9b33472 100644 (file)
@@ -19,6 +19,8 @@ typedef enum {e_output_check, e_output_display, e_output_ignore} e_output_handli
 typedef struct {
   /* kind of job */
   char *cmd;
+  char **env;
+  int  env_size;
   char *filepos;
   int pid;
   int is_background:1;
diff --git a/tools/tesh/setenv.tesh b/tools/tesh/setenv.tesh
new file mode 100644 (file)
index 0000000..7ae9998
--- /dev/null
@@ -0,0 +1,31 @@
+#! ./tesh
+# This suite builds and uses a program returning 1.
+# tesh is instructed of this return code and must not whine.
+
+$ rm -rf temp_testdir
+$ mkdir temp_testdir
+
+$ cd temp_testdir
+
+< #include <string.h>
+< #include <stdio.h>
+< extern char **environ;
+< int main(void) {
+<   char **env_iter=environ;
+<   while (*env_iter) {
+<     if (!strncmp(*env_iter,"tesh_test_toto=",strlen("tesh_test_toto=")))
+<       printf("%s\n",*env_iter);
+<     env_iter++;
+<   }
+<   return 0;
+< }
+$ cat > getenv.c
+
+$ gcc -o getenv getenv.c -g
+
+! setenv tesh_test_toto=blah
+$ ./getenv
+> tesh_test_toto=blah
+
+$ cd ..
+$ rm -rf temp_testdir