1
- package tests
1
+ package api
2
2
3
3
import (
4
4
"fmt"
5
5
"strconv"
6
6
"testing"
7
7
"time"
8
8
9
+ "github.com/devfile/library/tests/utils"
10
+
9
11
schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
10
12
)
11
13
12
- const numThreads = 5 // Number of threads used by multi-thread tests
13
- const maxCommands = 10 // The maximum number of commands to include in a generated devfile
14
- const maxComponents = 10 // The maximum number of components to include in a generated devfile
14
+ const (
15
+ // numThreads : Number of threads used by multi-thread tests
16
+ numThreads = 5
17
+ // maxCommands : The maximum number of commands to include in a generated devfile
18
+ maxCommands = 10
19
+ // maxComponents : The maximum number of components to include in a generated devfile
20
+ maxComponents = 10
21
+ )
15
22
23
+ // TestContent - structure used by a test to configure the tests to run
16
24
type TestContent struct {
17
25
CommandTypes []schema.CommandType
18
26
ComponentTypes []schema.ComponentType
@@ -26,7 +34,7 @@ func Test_ExecCommand(t *testing.T) {
26
34
testContent .CommandTypes = []schema.CommandType {schema .ExecCommandType }
27
35
testContent .CreateWithParser = false
28
36
testContent .EditContent = false
29
- testContent .FileName = GetDevFileName ()
37
+ testContent .FileName = utils . GetDevFileName ()
30
38
runTest (testContent , t )
31
39
runMultiThreadTest (testContent , t )
32
40
}
@@ -35,7 +43,7 @@ func Test_ExecCommandEdit(t *testing.T) {
35
43
testContent .CommandTypes = []schema.CommandType {schema .ExecCommandType }
36
44
testContent .CreateWithParser = false
37
45
testContent .EditContent = true
38
- testContent .FileName = GetDevFileName ()
46
+ testContent .FileName = utils . GetDevFileName ()
39
47
runTest (testContent , t )
40
48
runMultiThreadTest (testContent , t )
41
49
}
@@ -45,7 +53,7 @@ func Test_ExecCommandParserCreate(t *testing.T) {
45
53
testContent .CommandTypes = []schema.CommandType {schema .ExecCommandType }
46
54
testContent .CreateWithParser = true
47
55
testContent .EditContent = false
48
- testContent .FileName = GetDevFileName ()
56
+ testContent .FileName = utils . GetDevFileName ()
49
57
runTest (testContent , t )
50
58
runMultiThreadTest (testContent , t )
51
59
}
@@ -55,7 +63,7 @@ func Test_ExecCommandEditParserCreate(t *testing.T) {
55
63
testContent .CommandTypes = []schema.CommandType {schema .ExecCommandType }
56
64
testContent .CreateWithParser = true
57
65
testContent .EditContent = true
58
- testContent .FileName = GetDevFileName ()
66
+ testContent .FileName = utils . GetDevFileName ()
59
67
runTest (testContent , t )
60
68
runMultiThreadTest (testContent , t )
61
69
}
@@ -65,7 +73,7 @@ func Test_CompositeCommand(t *testing.T) {
65
73
testContent .CommandTypes = []schema.CommandType {schema .CompositeCommandType }
66
74
testContent .CreateWithParser = false
67
75
testContent .EditContent = false
68
- testContent .FileName = GetDevFileName ()
76
+ testContent .FileName = utils . GetDevFileName ()
69
77
runTest (testContent , t )
70
78
runMultiThreadTest (testContent , t )
71
79
}
@@ -74,7 +82,7 @@ func Test_CompositeCommandEdit(t *testing.T) {
74
82
testContent .CommandTypes = []schema.CommandType {schema .CompositeCommandType }
75
83
testContent .CreateWithParser = false
76
84
testContent .EditContent = true
77
- testContent .FileName = GetDevFileName ()
85
+ testContent .FileName = utils . GetDevFileName ()
78
86
runTest (testContent , t )
79
87
runMultiThreadTest (testContent , t )
80
88
}
@@ -84,7 +92,7 @@ func Test_CompositeCommandParserCreate(t *testing.T) {
84
92
testContent .CommandTypes = []schema.CommandType {schema .CompositeCommandType }
85
93
testContent .CreateWithParser = true
86
94
testContent .EditContent = false
87
- testContent .FileName = GetDevFileName ()
95
+ testContent .FileName = utils . GetDevFileName ()
88
96
runTest (testContent , t )
89
97
runMultiThreadTest (testContent , t )
90
98
}
@@ -94,7 +102,7 @@ func Test_CompositeCommandEditParserCreate(t *testing.T) {
94
102
testContent .CommandTypes = []schema.CommandType {schema .CompositeCommandType }
95
103
testContent .CreateWithParser = true
96
104
testContent .EditContent = true
97
- testContent .FileName = GetDevFileName ()
105
+ testContent .FileName = utils . GetDevFileName ()
98
106
runTest (testContent , t )
99
107
runMultiThreadTest (testContent , t )
100
108
}
@@ -104,7 +112,7 @@ func Test_MultiCommand(t *testing.T) {
104
112
testContent .CommandTypes = []schema.CommandType {schema .ExecCommandType , schema .CompositeCommandType }
105
113
testContent .CreateWithParser = true
106
114
testContent .EditContent = true
107
- testContent .FileName = GetDevFileName ()
115
+ testContent .FileName = utils . GetDevFileName ()
108
116
runTest (testContent , t )
109
117
runMultiThreadTest (testContent , t )
110
118
}
@@ -114,7 +122,7 @@ func Test_ContainerComponent(t *testing.T) {
114
122
testContent .ComponentTypes = []schema.ComponentType {schema .ContainerComponentType }
115
123
testContent .CreateWithParser = false
116
124
testContent .EditContent = false
117
- testContent .FileName = GetDevFileName ()
125
+ testContent .FileName = utils . GetDevFileName ()
118
126
runTest (testContent , t )
119
127
runMultiThreadTest (testContent , t )
120
128
}
@@ -124,7 +132,7 @@ func Test_ContainerComponentEdit(t *testing.T) {
124
132
testContent .ComponentTypes = []schema.ComponentType {schema .ContainerComponentType }
125
133
testContent .CreateWithParser = false
126
134
testContent .EditContent = true
127
- testContent .FileName = GetDevFileName ()
135
+ testContent .FileName = utils . GetDevFileName ()
128
136
runTest (testContent , t )
129
137
runMultiThreadTest (testContent , t )
130
138
}
@@ -134,7 +142,7 @@ func Test_ContainerComponentCreateWithParser(t *testing.T) {
134
142
testContent .ComponentTypes = []schema.ComponentType {schema .ContainerComponentType }
135
143
testContent .CreateWithParser = true
136
144
testContent .EditContent = false
137
- testContent .FileName = GetDevFileName ()
145
+ testContent .FileName = utils . GetDevFileName ()
138
146
runTest (testContent , t )
139
147
runMultiThreadTest (testContent , t )
140
148
}
@@ -144,7 +152,7 @@ func Test_ContainerComponentEditCreateWithParser(t *testing.T) {
144
152
testContent .ComponentTypes = []schema.ComponentType {schema .ContainerComponentType }
145
153
testContent .CreateWithParser = true
146
154
testContent .EditContent = true
147
- testContent .FileName = GetDevFileName ()
155
+ testContent .FileName = utils . GetDevFileName ()
148
156
runTest (testContent , t )
149
157
runMultiThreadTest (testContent , t )
150
158
}
@@ -154,7 +162,7 @@ func Test_VolumeComponent(t *testing.T) {
154
162
testContent .ComponentTypes = []schema.ComponentType {schema .VolumeComponentType }
155
163
testContent .CreateWithParser = false
156
164
testContent .EditContent = false
157
- testContent .FileName = GetDevFileName ()
165
+ testContent .FileName = utils . GetDevFileName ()
158
166
runTest (testContent , t )
159
167
runMultiThreadTest (testContent , t )
160
168
}
@@ -164,7 +172,7 @@ func Test_VolumeComponentEdit(t *testing.T) {
164
172
testContent .ComponentTypes = []schema.ComponentType {schema .VolumeComponentType }
165
173
testContent .CreateWithParser = false
166
174
testContent .EditContent = true
167
- testContent .FileName = GetDevFileName ()
175
+ testContent .FileName = utils . GetDevFileName ()
168
176
runTest (testContent , t )
169
177
runMultiThreadTest (testContent , t )
170
178
}
@@ -174,7 +182,7 @@ func Test_VolumeComponentCreateWithParser(t *testing.T) {
174
182
testContent .ComponentTypes = []schema.ComponentType {schema .VolumeComponentType }
175
183
testContent .CreateWithParser = true
176
184
testContent .EditContent = false
177
- testContent .FileName = GetDevFileName ()
185
+ testContent .FileName = utils . GetDevFileName ()
178
186
runTest (testContent , t )
179
187
runMultiThreadTest (testContent , t )
180
188
}
@@ -184,7 +192,7 @@ func Test_VolumeComponentEditCreateWithParser(t *testing.T) {
184
192
testContent .ComponentTypes = []schema.ComponentType {schema .VolumeComponentType }
185
193
testContent .CreateWithParser = true
186
194
testContent .EditContent = true
187
- testContent .FileName = GetDevFileName ()
195
+ testContent .FileName = utils . GetDevFileName ()
188
196
runTest (testContent , t )
189
197
runMultiThreadTest (testContent , t )
190
198
}
@@ -194,7 +202,7 @@ func Test_MultiComponent(t *testing.T) {
194
202
testContent .ComponentTypes = []schema.ComponentType {schema .ContainerComponentType , schema .VolumeComponentType }
195
203
testContent .CreateWithParser = true
196
204
testContent .EditContent = true
197
- testContent .FileName = GetDevFileName ()
205
+ testContent .FileName = utils . GetDevFileName ()
198
206
runTest (testContent , t )
199
207
runMultiThreadTest (testContent , t )
200
208
}
@@ -205,74 +213,76 @@ func Test_Everything(t *testing.T) {
205
213
testContent .ComponentTypes = []schema.ComponentType {schema .ContainerComponentType , schema .VolumeComponentType }
206
214
testContent .CreateWithParser = true
207
215
testContent .EditContent = true
208
- testContent .FileName = GetDevFileName ()
216
+ testContent .FileName = utils . GetDevFileName ()
209
217
runTest (testContent , t )
210
218
runMultiThreadTest (testContent , t )
211
219
}
212
220
221
+ // runMultiThreadTest : Runs the same test on multiple threads, the test is based on the content of the specified TestContent
213
222
func runMultiThreadTest (testContent TestContent , t * testing.T ) {
214
223
215
- LogMessage (fmt .Sprintf ("Start Threaded test for %s" , testContent .FileName ))
224
+ utils . LogMessage (fmt .Sprintf ("Start Threaded test for %s" , testContent .FileName ))
216
225
217
226
devfileName := testContent .FileName
218
227
var i int
219
228
for i = 1 ; i < numThreads ; i ++ {
220
- testContent .FileName = AddSuffixToFileName (devfileName , strconv .Itoa (i ))
229
+ testContent .FileName = utils . AddSuffixToFileName (devfileName , strconv .Itoa (i ))
221
230
go runTest (testContent , t )
222
231
}
223
- testContent .FileName = AddSuffixToFileName (devfileName , strconv .Itoa (i ))
232
+ testContent .FileName = utils . AddSuffixToFileName (devfileName , strconv .Itoa (i ))
224
233
runTest (testContent , t )
225
234
226
- LogMessage (fmt .Sprintf ("Sleep 2 seconds to allow all threads to complete : %s" , devfileName ))
235
+ utils . LogMessage (fmt .Sprintf ("Sleep 2 seconds to allow all threads to complete : %s" , devfileName ))
227
236
time .Sleep (2 * time .Second )
228
- LogMessage (fmt .Sprintf ("Sleep complete : %s" , devfileName ))
237
+ utils . LogMessage (fmt .Sprintf ("Sleep complete : %s" , devfileName ))
229
238
230
239
}
231
240
241
+ // runTest : Runs a test beased on the content of the specified TestContent
232
242
func runTest (testContent TestContent , t * testing.T ) {
233
243
234
- LogMessage (fmt .Sprintf ("Start test for %s" , testContent .FileName ))
235
- testDevfile := GetDevfile (testContent .FileName )
244
+ utils . LogMessage (fmt .Sprintf ("Start test for %s" , testContent .FileName ))
245
+ testDevfile := utils . GetDevfile (testContent .FileName )
236
246
237
247
if len (testContent .CommandTypes ) > 0 {
238
- numCommands := GetRandomNumber (maxCommands )
248
+ numCommands := utils . GetRandomNumber (maxCommands )
239
249
for i := 0 ; i < numCommands ; i ++ {
240
- commandIndex := GetRandomNumber (len (testContent .CommandTypes ))
241
- testDevfile .addCommand (testContent .CommandTypes [commandIndex - 1 ])
250
+ commandIndex := utils . GetRandomNumber (len (testContent .CommandTypes ))
251
+ testDevfile .AddCommand (testContent .CommandTypes [commandIndex - 1 ])
242
252
}
243
253
}
244
254
245
255
if len (testContent .ComponentTypes ) > 0 {
246
- numComponents := GetRandomNumber (maxComponents )
256
+ numComponents := utils . GetRandomNumber (maxComponents )
247
257
for i := 0 ; i < numComponents ; i ++ {
248
- componentIndex := GetRandomNumber (len (testContent .ComponentTypes ))
258
+ componentIndex := utils . GetRandomNumber (len (testContent .ComponentTypes ))
249
259
testDevfile .AddComponent (testContent .ComponentTypes [componentIndex - 1 ])
250
260
}
251
261
}
252
262
253
263
err := testDevfile .CreateDevfile (testContent .CreateWithParser )
254
264
if err != nil {
255
- t .Fatalf (LogErrorMessage (fmt .Sprintf ("ERROR creating devfile : %s : %v" , testContent .FileName , err )))
265
+ t .Fatalf (utils . LogErrorMessage (fmt .Sprintf ("ERROR creating devfile : %s : %v" , testContent .FileName , err )))
256
266
}
257
267
258
268
if testContent .EditContent {
259
269
if len (testContent .CommandTypes ) > 0 {
260
270
err = testDevfile .EditCommands ()
261
271
if err != nil {
262
- t .Fatalf (LogErrorMessage (fmt .Sprintf ("ERROR editing commands : %s : %v" , testContent .FileName , err )))
272
+ t .Fatalf (utils . LogErrorMessage (fmt .Sprintf ("ERROR editing commands : %s : %v" , testContent .FileName , err )))
263
273
}
264
274
}
265
275
if len (testContent .ComponentTypes ) > 0 {
266
276
err = testDevfile .EditComponents ()
267
277
if err != nil {
268
- t .Fatalf (LogErrorMessage (fmt .Sprintf ("ERROR editing components : %s : %v" , testContent .FileName , err )))
278
+ t .Fatalf (utils . LogErrorMessage (fmt .Sprintf ("ERROR editing components : %s : %v" , testContent .FileName , err )))
269
279
}
270
280
}
271
281
}
272
282
273
283
err = testDevfile .Verify ()
274
284
if err != nil {
275
- t .Fatalf (LogErrorMessage (fmt .Sprintf ("ERROR verifying devfile content : %s : %v" , testContent .FileName , err )))
285
+ t .Fatalf (utils . LogErrorMessage (fmt .Sprintf ("ERROR verifying devfile content : %s : %v" , testContent .FileName , err )))
276
286
}
277
287
278
288
}
0 commit comments