forked from devtron-labs/ci-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptExecutor_test.go
286 lines (278 loc) · 9.79 KB
/
scriptExecutor_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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"fmt"
"github.com/devtron-labs/ci-runner/helper"
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
)
func TestRunScripts(t *testing.T) {
t.SkipNow()
type args struct {
workDirectory string
scriptFileName string
script string
envVars map[string]string
outputVars []string
}
tests := []struct {
name string
args args
wantErr bool
want map[string]string
}{
{name: "simple_success",
args: args{workDirectory: "/tmp/ci-test/", scriptFileName: "test", script: "echo hello", envVars: map[string]string{}, outputVars: nil},
wantErr: false,
want: map[string]string{}},
{name: "simple_script_fail",
args: args{workDirectory: "/tmp/ci-test/", scriptFileName: "test1", script: "err_cmd hello", envVars: map[string]string{}, outputVars: nil},
wantErr: true,
want: nil},
{name: "env_input_out",
args: args{workDirectory: "/tmp/ci-test/", scriptFileName: "test_2", script: "echo hello $name_1 \n export name_2=test_name2 \n echo $name_2", envVars: map[string]string{"name_1": "i am from env"}, outputVars: []string{"name_1", "name_2"}},
wantErr: false,
want: map[string]string{
"name_1": "i am from env",
"name_2": "test_name2",
},
},
{name: "empty_env_out",
args: args{workDirectory: "/tmp/ci-test/", scriptFileName: "test_3", script: "echo hello $name_1 \n export name_2=test_name2 \n echo $name_2", envVars: map[string]string{"name_1": "i am from env"}, outputVars: []string{"name_1", "empty_key", "name_2"}},
wantErr: false,
want: map[string]string{
"name_1": "i am from env",
"name_2": "test_name2",
"empty_key": "",
},
},
{name: "outValContains_specialChar",
args: args{workDirectory: "/tmp/ci-test/", scriptFileName: "test_4", script: "echo hello $name_1 \n export name_2=test_name2 \n echo $name_2", envVars: map[string]string{"name_1": "i am from \"env", "specialCharVal": "a=b"}, outputVars: []string{"name_1", "specialCharVal", "name_2"}},
wantErr: false,
want: map[string]string{
"name_1": "i am from \"env",
"name_2": "test_name2",
"specialCharVal": "a=b",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RunScripts(tt.args.workDirectory, tt.args.scriptFileName, tt.args.script, tt.args.envVars, tt.args.outputVars)
if (err != nil) != tt.wantErr {
t.Errorf("RunScripts() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("RunScripts() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_buildDockerRunCommand(t *testing.T) {
type args struct {
executionConf *executionConf
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "all_single",
args: args{executionConf: &executionConf{
DockerImage: "alpine:latest",
EnvInputFileName: "/tmp/ci-test/abc.env",
EntryScriptFileName: "/tmp/code-location/_entry.sh",
EnvOutFileName: "/tmp/ci-test/_env.out",
ExtraVolumeMounts: []*helper.MountPath{{SrcPath: "/src", DstPath: "/des"}},
SourceCodeMount: &helper.MountPath{SrcPath: "/tmp/code-location", DstPath: "/tmp/code-mount-location"},
CustomScriptMount: &helper.MountPath{SrcPath: "/tmp/custom-script-location", DstPath: "/tmp/script-mount-location"},
ExposedPorts: map[int]int{80: 8080},
}},
wantErr: false,
want: "docker run --network host \\\n--env-file /tmp/ci-test/abc.env \\\n-v /tmp/code-location/_entry.sh:/devtron_script/_entry.sh \\\n-v /tmp/ci-test/_env.out:/devtron_script/_out.env \\\n-v /tmp/code-location:/tmp/code-mount-location \\\n-v /src:/des \\\n-v /tmp/custom-script-location:/tmp/script-mount-location \\\n-p 80:8080 \\alpine:latest \\\n/bin/sh /devtron_script/_entry.sh\n",
},
{name: "all_multi",
args: args{executionConf: &executionConf{
DockerImage: "alpine:latest",
EnvInputFileName: "/tmp/ci-test/abc.env",
EntryScriptFileName: "/tmp/code-location/_entry.sh",
EnvOutFileName: "/tmp/ci-test/_env.out",
ExtraVolumeMounts: []*helper.MountPath{{SrcPath: "/src", DstPath: "/des"}, {SrcPath: "/src2", DstPath: "/des2"}},
SourceCodeMount: &helper.MountPath{SrcPath: "/tmp/code-location", DstPath: "/tmp/code-mount-location"},
CustomScriptMount: &helper.MountPath{SrcPath: "/tmp/custom-script-location", DstPath: "/tmp/script-mount-location"},
ExposedPorts: map[int]int{80: 8080, 90: 9090},
}},
wantErr: false,
want: "docker run --network host \\\n--env-file /tmp/ci-test/abc.env \\\n-v /tmp/code-location/_entry.sh:/devtron_script/_entry.sh \\\n-v /tmp/ci-test/_env.out:/devtron_script/_out.env \\\n-v /tmp/code-location:/tmp/code-mount-location \\\n-v /src:/des \\\n-v /src2:/des2 \\\n-v /tmp/custom-script-location:/tmp/script-mount-location \\\n-p 80:8080 \\\n-p 90:9090 \\alpine:latest \\\n/bin/sh /devtron_script/_entry.sh\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildDockerRunCommand(tt.args.executionConf)
if (err != nil) != tt.wantErr {
t.Errorf("buildDockerRunCommand() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("buildDockerRunCommand() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_buildDockerEntryScript(t *testing.T) {
type args struct {
command string
args []string
outputVars []string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{{name: "hello",
args: args{command: "ls"},
wantErr: false,
want: "#!/bin/sh\nset -e\nls \n> /devtron_script/_out.env\n"},
{name: "ls_dir",
args: args{command: "ls", args: []string{"\\tmp"}},
wantErr: false,
want: "#!/bin/sh\nset -e\nls \\tmp\n> /devtron_script/_out.env\n"},
{name: "ls_dir_with_out",
args: args{command: "ls", args: []string{"\\tmp"}, outputVars: []string{"HOME"}},
wantErr: false,
want: "#!/bin/sh\nset -e\nls \\tmp\n> /devtron_script/_out.env\nprintf \"\\nHOME=%s\" \"$HOME\" >> /devtron_script/_out.env\n"},
{name: "ls_dir_with_out_multi",
args: args{command: "ls", args: []string{"\\tmp"}, outputVars: []string{"HOME", "USER"}},
wantErr: false,
want: "#!/bin/sh\nset -e\nls \\tmp\n> /devtron_script/_out.env\nprintf \"\\nHOME=%s\" \"$HOME\" >> /devtron_script/_out.env\nprintf \"\\nUSER=%s\" \"$USER\" >> /devtron_script/_out.env\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildDockerEntryScript(tt.args.command, tt.args.args, tt.args.outputVars)
if (err != nil) != tt.wantErr {
t.Errorf("buildDockerEntryScript() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("buildDockerEntryScript() got = %v, want %v", got, tt.want)
}
})
}
fmt.Println("coverage:", testing.CoverMode(), testing.Coverage())
}
func TestRunScriptsInDocker(t *testing.T) {
t.SkipNow()
type args struct {
executionConf *executionConf
}
tests := []struct {
name string
args args
want map[string]string
wantErr bool
}{
{name: "hello",
args: args{
executionConf: &executionConf{
Script: "ls",
EnvInputVars: map[string]string{"KIND": "TEST"},
ExposedPorts: map[int]int{80: 8080, 90: 9090},
OutputVars: []string{"HOME", "PWD", "NAME", "KIND"},
DockerImage: "alpine:latest",
SourceCodeMount: &helper.MountPath{SrcPath: "/tmp/code-location", DstPath: "/tmp/code-mount-location"},
CustomScriptMount: &helper.MountPath{SrcPath: "/tmp/custom-script-location", DstPath: "/tmp/script-mount-location"},
command: "/bin/sh",
args: []string{"-c", "ls;sleep 1;export NAME=from-script;echo done;"},
scriptFileName: "",
workDirectory: "/tmp/ci-test",
},
},
want: map[string]string{"HOME": "/root", "PWD": "/", "NAME": "from-script", "KIND": "TEST"},
wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RunScriptsInDocker(tt.args.executionConf)
if (err != nil) != tt.wantErr {
t.Errorf("RunScriptsInDocker() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("RunScriptsInDocker() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_writeToEnvFile(t *testing.T) {
type args struct {
envMap map[string]string
filename string
}
tests := []struct {
name string
args args
wantErr bool
}{{
name: "empty_env_map",
args: args{
envMap: map[string]string{},
filename: "test.env",
},
wantErr: false,
}, {
name: "single_env_var",
args: args{
envMap: map[string]string{"FOO": "BAR"},
filename: "test.env",
},
wantErr: false,
}, {
name: "multiple_env_vars",
args: args{
envMap: map[string]string{"FOO": "BAR", "BAR": "FOO"},
filename: "test.env",
},
wantErr: false,
}, {
name: "error_creating_file",
args: args{
envMap: map[string]string{"FOO": "BAR"},
filename: "/dev/null/abcd",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := writeToEnvFile(tt.args.envMap, tt.args.filename)
if (err != nil) != tt.wantErr {
t.Errorf("writeToEnvFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
// Check the contents of the file
file, err := os.Open(tt.args.filename)
if err != nil {
t.Errorf("Error opening file: %v", err)
return
}
defer file.Close()
contents, err := ioutil.ReadAll(file)
if err != nil {
t.Errorf("Error reading file contents: %v", err)
return
}
for k, v := range tt.args.envMap {
if !strings.Contains(string(contents), fmt.Sprintf("%s=%s", k, v)) {
t.Errorf("Expected to find env var %s=%s in file, but it was not found", k, v)
}
}
}
})
}
}