github.com/informationsea/shellflow@v0.1.3/executer_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/informationsea/shellflow/flowscript"
    11  )
    12  
    13  func TestGenerateTaskScripts(t *testing.T) {
    14  	builder, err := NewShellTaskBuilder()
    15  	if err != nil {
    16  		t.Fatalf("error: %s", err.Error())
    17  	}
    18  	env := NewEnvironment()
    19  	tempdir, err := ioutil.TempDir("", "generate_task_scripts")
    20  	if err != nil {
    21  		t.Fatalf("Cannot create temporary directory: %s", tempdir)
    22  	}
    23  	fmt.Printf("generate task scripts: %s\n", tempdir)
    24  
    25  	env.workDir = path.Join(tempdir, "workdir")
    26  	env.workflowRoot = path.Join(tempdir, "root")
    27  
    28  	err = os.MkdirAll(env.workDir, 0755)
    29  	if err != nil {
    30  		t.Fatalf("error: %s", err.Error())
    31  	}
    32  	err = os.MkdirAll(env.workflowRoot, 0755)
    33  	if err != nil {
    34  		t.Fatalf("error: %s", err.Error())
    35  	}
    36  	err = ioutil.WriteFile(path.Join(env.workDir, "hoge"), []byte("foo"), 0640)
    37  	if err != nil {
    38  		t.Fatalf("error: %s", err.Error())
    39  	}
    40  
    41  	builder.MissingCreatorFiles.Add("hoge")
    42  	builder.Tasks = append(builder.Tasks, &ShellTask{
    43  		LineNum:         1,
    44  		ID:              1,
    45  		ShellScript:     "cat hoge > foo",
    46  		DependentFiles:  flowscript.NewStringSetWithValues("hoge"),
    47  		CreatingFiles:   flowscript.NewStringSetWithValues("foo"),
    48  		DependentTaskID: []int{},
    49  	})
    50  	builder.Tasks = append(builder.Tasks, &ShellTask{
    51  		LineNum:         2,
    52  		ID:              2,
    53  		ShellScript:     "cat foo > bar",
    54  		DependentFiles:  flowscript.NewStringSetWithValues("foo"),
    55  		CreatingFiles:   flowscript.NewStringSetWithValues("bar"),
    56  		DependentTaskID: []int{1},
    57  	})
    58  	builder.Tasks = append(builder.Tasks, &ShellTask{
    59  		LineNum:         3,
    60  		ID:              3,
    61  		ShellScript:     "cat foo hoge > bar2",
    62  		DependentFiles:  flowscript.NewStringSetWithValues("hoge", "foo"),
    63  		CreatingFiles:   flowscript.NewStringSetWithValues("bar2"),
    64  		DependentTaskID: []int{1},
    65  	})
    66  
    67  	scripts, err := GenerateTaskScripts("testscript.sf", "", env, builder)
    68  	if err != nil {
    69  		t.Fatalf("cannot create task scripts: %s", err.Error())
    70  	}
    71  
    72  	for _, oneTask := range builder.Tasks {
    73  		jobPath := path.Join(scripts.workflowRoot, fmt.Sprintf("job%03d", oneTask.ID))
    74  		dirStat, err := os.Stat(jobPath)
    75  		if err != nil {
    76  			t.Fatalf("error: %s", err.Error())
    77  		}
    78  
    79  		if !dirStat.IsDir() {
    80  			t.Fatal("JOB directory should be directory")
    81  		}
    82  
    83  		scriptPath := path.Join(jobPath, "run.sh")
    84  		_, err = os.Stat(scriptPath)
    85  		if err != nil {
    86  			t.Fatalf("error: %s", err.Error())
    87  		}
    88  
    89  		scriptPath = path.Join(jobPath, "script.sh")
    90  		_, err = os.Stat(scriptPath)
    91  		if err != nil {
    92  			t.Fatalf("error: %s", err.Error())
    93  		}
    94  
    95  		scriptFile, err := os.Open(scriptPath)
    96  		if err != nil {
    97  			t.Fatalf("error: %s", err.Error())
    98  		}
    99  
   100  		data, err := ioutil.ReadAll(scriptFile)
   101  		if err != nil {
   102  			t.Fatalf("error: %s", err.Error())
   103  		}
   104  		if string(data) != oneTask.ShellScript {
   105  			t.Fatalf("bad script: %s", data)
   106  		}
   107  	}
   108  
   109  	fmt.Printf("dir: %s\n", scripts.workflowRoot)
   110  
   111  	os.RemoveAll(tempdir)
   112  }