-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
run_test.go
205 lines (194 loc) · 5.57 KB
/
run_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package cmd
import (
"fmt"
variant "github.com/mumoshu/variant/pkg"
"github.com/mumoshu/variant/pkg/api/task"
"testing"
)
func TestVariant(t *testing.T) {
taskDef := &variant.TaskDef{
Name: "Variantfile",
Description: "",
Inputs: variant.InputConfigs{},
TaskDefs: variant.TaskDefs{
&variant.TaskDef{
Name: "duplicate",
Description: "",
Inputs: variant.InputConfigs{
&variant.InputConfig{
Name: "input",
Description: "",
ArgumentIndex: variant.Int(0),
Type: "string",
Default: "",
Properties: map[string]map[string]interface{}(nil),
Remainings: map[string]interface{}(nil)},
},
TaskDefs: variant.TaskDefs{},
Script: "echo \"{{.input}}_{{.input}}\"\n",
Steps: []variant.Step{
variant.ScriptStep{
Name: "script",
Code: "echo \"{{.input}}_{{.input}}\"\n",
Silent: false,
RunnerConfig: variant.RunnerConfig{
Image: "",
Command: "",
Entrypoint: (*string)(nil),
Artifacts: []variant.Artifact(nil),
Args: []string(nil),
Envfile: "",
Env: map[string]string(nil),
Volumes: []string(nil),
Net: "",
Workdir: "",
},
},
},
Autoenv: false,
Autodir: false,
Interactive: false,
Private: false,
},
&variant.TaskDef{
Name: "test",
Description: "",
Inputs: variant.InputConfigs{
&variant.InputConfig{
Name: "input",
Description: "",
ArgumentIndex: variant.Int(0),
Type: "string",
Default: nil,
Properties: map[string]map[string]interface{}(nil),
Remainings: map[string]interface{}(nil),
},
&variant.InputConfig{
Name: "expected",
Description: "",
ArgumentIndex: variant.Int(1),
Type: "string",
Default: nil,
Properties: map[string]map[string]interface{}(nil),
Remainings: map[string]interface{}(nil)}},
TaskDefs: variant.TaskDefs{},
Script: "",
Steps: []variant.Step{
variant.TaskStep{
Name: "res",
TaskKeyString: "duplicate",
Arguments: task.Arguments{"input": "{{.input}}"},
Silent: false,
},
variant.ScriptStep{
Name: "actual",
Code: "echo \"Double {{.input}} will be: {{.res}}\"\n",
Silent: false,
RunnerConfig: variant.RunnerConfig{
Image: "", Command: "",
Entrypoint: (*string)(nil),
Artifacts: []variant.Artifact(nil),
Args: []string(nil),
Envfile: "",
Env: map[string]string(nil),
Volumes: []string(nil), Net: "", Workdir: "",
},
},
variant.ScriptStep{
Name: "test",
Code: "echo \"{{ .actual }}\" | grep \"{{ .expected }}\"\n",
Silent: false,
RunnerConfig: variant.RunnerConfig{
Image: "",
Command: "",
Entrypoint: (*string)(nil),
Artifacts: []variant.Artifact(nil),
Args: []string(nil),
Envfile: "",
Env: map[string]string(nil),
Volumes: []string(nil),
Net: "",
Workdir: "",
},
},
},
Autoenv: false,
Autodir: false,
Interactive: false,
Private: false,
},
},
Script: "",
Steps: []variant.Step{
variant.TaskStep{
Name: "step-1",
TaskKeyString: "test",
Arguments: task.Arguments{
"expected": "Double FOO will be: FOO_FOO",
"input": "FOO",
},
Silent: false,
},
},
Autoenv: false,
Autodir: false,
Interactive: false,
Private: false,
}
adhocTaskInputs := variant.InputConfigs{&variant.InputConfig{
Name: "input",
Description: "",
ArgumentIndex: variant.Int(0),
Type: "string",
Default: nil,
Properties: map[string]map[string]interface{}(nil),
Remainings: map[string]interface{}(nil),
}}
adhocTaskDef := &variant.TaskDef{Inputs: adhocTaskInputs}
if err := taskDef.Add([]string{"should"}, &variant.TaskDef{Name: "should"}, func(_ variant.ExecutionContext) (string, error) {
return "", nil
}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := taskDef.Add([]string{"should", "succeed"}, adhocTaskDef, func(ctx variant.ExecutionContext) (string, error) {
params := ctx.Values()
in := params["input"]
return fmt.Sprintf("%s_%s_%s", in, in, in), nil
}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := taskDef.Add([]string{"should", "fail"}, adhocTaskDef, func(ctx variant.ExecutionContext) (string, error) {
return "", fmt.Errorf("simulated error")
}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
//bytes, err := json.MarshalIndent(taskDef, "", " ")
//panic(fmt.Sprintf("%s", string(bytes)))
cmd := New("variant", taskDef, variant.Opts{})
out1, err := cmd.Run([]string{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
t.Logf("outs1: %s", out1)
expected1 := `FOO_FOO
Double FOO will be: FOO_FOO
Double FOO will be: FOO_FOO`
if out1 != expected1 {
t.Fatalf("unexpected out1: %s", out1)
}
out2, err := cmd.Run([]string{"should", "succeed", "--input=SUCCEED"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
t.Logf("out2: %s", out2)
if out2 != "SUCCEED_SUCCEED_SUCCEED" {
t.Fatalf("unexpected out2: %s", out2)
}
out3, err := cmd.Run([]string{"should", "fail", "--input=FAIL"})
if err == nil {
t.Fatal("expected error, but succeeded")
}
if out3 != "" {
t.Fatalf("unexpected out3: %s", out3)
}
}