Skip to content

Commit 9b1f8f7

Browse files
committed
Address Maysun comments
1 parent ad9fecd commit 9b1f8f7

8 files changed

+189
-145
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ main
1919
.vscode/
2020

2121
# File created running tests
22-
tests/src/tests/tmp/
22+
tests/tmp/
2323

2424
.DS_Store

tests/src/tests/README.md tests/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
The tests use the go language and are intended to test every apsect of the parser for every schema attribute. Some basic aspects of the tests:
66

7-
* A first test (parser_v200_schema_test.go) feeds pre-created devfiles to the parser to ensure the parser can parse all attribues and return an approproate error when the devfile contains an error.
7+
* A first test (parser_v200_schema_test.go) feeds pre-created devfiles to the parser to ensure the parser can parse all attribues and return an approproate error when the devfile contains an error. This test is not currently available.
88
* A second set of tests (parser_v200_verify_test.go) create devfile content at runtime:
99
* Devfile content is randomly generated and as a result the tests are designed to run multiple times.
1010
* Parser functions covered:
@@ -18,7 +18,7 @@ The tests use the go language and are intended to test every apsect of the parse
1818

1919
## Current tests:
2020

21-
The tests using pre-created devfiles are complete (but update in progress due to schema changes)
21+
The tests using pre-created devfiles are not currently available (update in progress due to schema changes)
2222

2323
The tests which generate devfiles with random content at run time currently cover the following properties and items.
2424

tests/src/tests/parser_v200_verify_test.go tests/api/parser_v200_verify_test.go

+49-39
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
package tests
1+
package api
22

33
import (
44
"fmt"
55
"strconv"
66
"testing"
77
"time"
88

9+
"github.com/devfile/library/tests/utils"
10+
911
schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
1012
)
1113

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+
)
1522

23+
// TestContent - structure used by a test to configure the tests to run
1624
type TestContent struct {
1725
CommandTypes []schema.CommandType
1826
ComponentTypes []schema.ComponentType
@@ -26,7 +34,7 @@ func Test_ExecCommand(t *testing.T) {
2634
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType}
2735
testContent.CreateWithParser = false
2836
testContent.EditContent = false
29-
testContent.FileName = GetDevFileName()
37+
testContent.FileName = utils.GetDevFileName()
3038
runTest(testContent, t)
3139
runMultiThreadTest(testContent, t)
3240
}
@@ -35,7 +43,7 @@ func Test_ExecCommandEdit(t *testing.T) {
3543
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType}
3644
testContent.CreateWithParser = false
3745
testContent.EditContent = true
38-
testContent.FileName = GetDevFileName()
46+
testContent.FileName = utils.GetDevFileName()
3947
runTest(testContent, t)
4048
runMultiThreadTest(testContent, t)
4149
}
@@ -45,7 +53,7 @@ func Test_ExecCommandParserCreate(t *testing.T) {
4553
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType}
4654
testContent.CreateWithParser = true
4755
testContent.EditContent = false
48-
testContent.FileName = GetDevFileName()
56+
testContent.FileName = utils.GetDevFileName()
4957
runTest(testContent, t)
5058
runMultiThreadTest(testContent, t)
5159
}
@@ -55,7 +63,7 @@ func Test_ExecCommandEditParserCreate(t *testing.T) {
5563
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType}
5664
testContent.CreateWithParser = true
5765
testContent.EditContent = true
58-
testContent.FileName = GetDevFileName()
66+
testContent.FileName = utils.GetDevFileName()
5967
runTest(testContent, t)
6068
runMultiThreadTest(testContent, t)
6169
}
@@ -65,7 +73,7 @@ func Test_CompositeCommand(t *testing.T) {
6573
testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType}
6674
testContent.CreateWithParser = false
6775
testContent.EditContent = false
68-
testContent.FileName = GetDevFileName()
76+
testContent.FileName = utils.GetDevFileName()
6977
runTest(testContent, t)
7078
runMultiThreadTest(testContent, t)
7179
}
@@ -74,7 +82,7 @@ func Test_CompositeCommandEdit(t *testing.T) {
7482
testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType}
7583
testContent.CreateWithParser = false
7684
testContent.EditContent = true
77-
testContent.FileName = GetDevFileName()
85+
testContent.FileName = utils.GetDevFileName()
7886
runTest(testContent, t)
7987
runMultiThreadTest(testContent, t)
8088
}
@@ -84,7 +92,7 @@ func Test_CompositeCommandParserCreate(t *testing.T) {
8492
testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType}
8593
testContent.CreateWithParser = true
8694
testContent.EditContent = false
87-
testContent.FileName = GetDevFileName()
95+
testContent.FileName = utils.GetDevFileName()
8896
runTest(testContent, t)
8997
runMultiThreadTest(testContent, t)
9098
}
@@ -94,7 +102,7 @@ func Test_CompositeCommandEditParserCreate(t *testing.T) {
94102
testContent.CommandTypes = []schema.CommandType{schema.CompositeCommandType}
95103
testContent.CreateWithParser = true
96104
testContent.EditContent = true
97-
testContent.FileName = GetDevFileName()
105+
testContent.FileName = utils.GetDevFileName()
98106
runTest(testContent, t)
99107
runMultiThreadTest(testContent, t)
100108
}
@@ -104,7 +112,7 @@ func Test_MultiCommand(t *testing.T) {
104112
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType, schema.CompositeCommandType}
105113
testContent.CreateWithParser = true
106114
testContent.EditContent = true
107-
testContent.FileName = GetDevFileName()
115+
testContent.FileName = utils.GetDevFileName()
108116
runTest(testContent, t)
109117
runMultiThreadTest(testContent, t)
110118
}
@@ -114,7 +122,7 @@ func Test_ContainerComponent(t *testing.T) {
114122
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType}
115123
testContent.CreateWithParser = false
116124
testContent.EditContent = false
117-
testContent.FileName = GetDevFileName()
125+
testContent.FileName = utils.GetDevFileName()
118126
runTest(testContent, t)
119127
runMultiThreadTest(testContent, t)
120128
}
@@ -124,7 +132,7 @@ func Test_ContainerComponentEdit(t *testing.T) {
124132
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType}
125133
testContent.CreateWithParser = false
126134
testContent.EditContent = true
127-
testContent.FileName = GetDevFileName()
135+
testContent.FileName = utils.GetDevFileName()
128136
runTest(testContent, t)
129137
runMultiThreadTest(testContent, t)
130138
}
@@ -134,7 +142,7 @@ func Test_ContainerComponentCreateWithParser(t *testing.T) {
134142
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType}
135143
testContent.CreateWithParser = true
136144
testContent.EditContent = false
137-
testContent.FileName = GetDevFileName()
145+
testContent.FileName = utils.GetDevFileName()
138146
runTest(testContent, t)
139147
runMultiThreadTest(testContent, t)
140148
}
@@ -144,7 +152,7 @@ func Test_ContainerComponentEditCreateWithParser(t *testing.T) {
144152
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType}
145153
testContent.CreateWithParser = true
146154
testContent.EditContent = true
147-
testContent.FileName = GetDevFileName()
155+
testContent.FileName = utils.GetDevFileName()
148156
runTest(testContent, t)
149157
runMultiThreadTest(testContent, t)
150158
}
@@ -154,7 +162,7 @@ func Test_VolumeComponent(t *testing.T) {
154162
testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType}
155163
testContent.CreateWithParser = false
156164
testContent.EditContent = false
157-
testContent.FileName = GetDevFileName()
165+
testContent.FileName = utils.GetDevFileName()
158166
runTest(testContent, t)
159167
runMultiThreadTest(testContent, t)
160168
}
@@ -164,7 +172,7 @@ func Test_VolumeComponentEdit(t *testing.T) {
164172
testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType}
165173
testContent.CreateWithParser = false
166174
testContent.EditContent = true
167-
testContent.FileName = GetDevFileName()
175+
testContent.FileName = utils.GetDevFileName()
168176
runTest(testContent, t)
169177
runMultiThreadTest(testContent, t)
170178
}
@@ -174,7 +182,7 @@ func Test_VolumeComponentCreateWithParser(t *testing.T) {
174182
testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType}
175183
testContent.CreateWithParser = true
176184
testContent.EditContent = false
177-
testContent.FileName = GetDevFileName()
185+
testContent.FileName = utils.GetDevFileName()
178186
runTest(testContent, t)
179187
runMultiThreadTest(testContent, t)
180188
}
@@ -184,7 +192,7 @@ func Test_VolumeComponentEditCreateWithParser(t *testing.T) {
184192
testContent.ComponentTypes = []schema.ComponentType{schema.VolumeComponentType}
185193
testContent.CreateWithParser = true
186194
testContent.EditContent = true
187-
testContent.FileName = GetDevFileName()
195+
testContent.FileName = utils.GetDevFileName()
188196
runTest(testContent, t)
189197
runMultiThreadTest(testContent, t)
190198
}
@@ -194,7 +202,7 @@ func Test_MultiComponent(t *testing.T) {
194202
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType, schema.VolumeComponentType}
195203
testContent.CreateWithParser = true
196204
testContent.EditContent = true
197-
testContent.FileName = GetDevFileName()
205+
testContent.FileName = utils.GetDevFileName()
198206
runTest(testContent, t)
199207
runMultiThreadTest(testContent, t)
200208
}
@@ -205,74 +213,76 @@ func Test_Everything(t *testing.T) {
205213
testContent.ComponentTypes = []schema.ComponentType{schema.ContainerComponentType, schema.VolumeComponentType}
206214
testContent.CreateWithParser = true
207215
testContent.EditContent = true
208-
testContent.FileName = GetDevFileName()
216+
testContent.FileName = utils.GetDevFileName()
209217
runTest(testContent, t)
210218
runMultiThreadTest(testContent, t)
211219
}
212220

221+
// runMultiThreadTest : Runs the same test on multiple threads, the test is based on the content of the specified TestContent
213222
func runMultiThreadTest(testContent TestContent, t *testing.T) {
214223

215-
LogMessage(fmt.Sprintf("Start Threaded test for %s", testContent.FileName))
224+
utils.LogMessage(fmt.Sprintf("Start Threaded test for %s", testContent.FileName))
216225

217226
devfileName := testContent.FileName
218227
var i int
219228
for i = 1; i < numThreads; i++ {
220-
testContent.FileName = AddSuffixToFileName(devfileName, strconv.Itoa(i))
229+
testContent.FileName = utils.AddSuffixToFileName(devfileName, strconv.Itoa(i))
221230
go runTest(testContent, t)
222231
}
223-
testContent.FileName = AddSuffixToFileName(devfileName, strconv.Itoa(i))
232+
testContent.FileName = utils.AddSuffixToFileName(devfileName, strconv.Itoa(i))
224233
runTest(testContent, t)
225234

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))
227236
time.Sleep(2 * time.Second)
228-
LogMessage(fmt.Sprintf("Sleep complete : %s", devfileName))
237+
utils.LogMessage(fmt.Sprintf("Sleep complete : %s", devfileName))
229238

230239
}
231240

241+
// runTest : Runs a test beased on the content of the specified TestContent
232242
func runTest(testContent TestContent, t *testing.T) {
233243

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)
236246

237247
if len(testContent.CommandTypes) > 0 {
238-
numCommands := GetRandomNumber(maxCommands)
248+
numCommands := utils.GetRandomNumber(maxCommands)
239249
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])
242252
}
243253
}
244254

245255
if len(testContent.ComponentTypes) > 0 {
246-
numComponents := GetRandomNumber(maxComponents)
256+
numComponents := utils.GetRandomNumber(maxComponents)
247257
for i := 0; i < numComponents; i++ {
248-
componentIndex := GetRandomNumber(len(testContent.ComponentTypes))
258+
componentIndex := utils.GetRandomNumber(len(testContent.ComponentTypes))
249259
testDevfile.AddComponent(testContent.ComponentTypes[componentIndex-1])
250260
}
251261
}
252262

253263
err := testDevfile.CreateDevfile(testContent.CreateWithParser)
254264
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)))
256266
}
257267

258268
if testContent.EditContent {
259269
if len(testContent.CommandTypes) > 0 {
260270
err = testDevfile.EditCommands()
261271
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)))
263273
}
264274
}
265275
if len(testContent.ComponentTypes) > 0 {
266276
err = testDevfile.EditComponents()
267277
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)))
269279
}
270280
}
271281
}
272282

273283
err = testDevfile.Verify()
274284
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)))
276286
}
277287

278288
}

0 commit comments

Comments
 (0)