@@ -11,114 +11,102 @@ import (
11
11
schema "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
12
12
)
13
13
14
+ // Return a specifed number of env attributes in a schema structure
14
15
func AddEnv (numEnv int ) []schema.EnvVar {
15
16
commandEnvs := make ([]schema.EnvVar , numEnv )
16
17
for i := 0 ; i < numEnv ; i ++ {
17
18
commandEnvs [i ].Name = "Name_" + GetRandomString (5 , false )
18
19
commandEnvs [i ].Value = "Value_" + GetRandomString (5 , false )
19
- LogMessage (fmt .Sprintf (" ....... Add Env: %s" , commandEnvs [i ]))
20
+ LogInfoMessage (fmt .Sprintf ("Add Env: %s" , commandEnvs [i ]))
20
21
}
21
22
return commandEnvs
22
23
}
23
24
25
+ // Return a specifed number of attributes in a schema structure
24
26
func AddAttributes (numAtrributes int ) map [string ]string {
25
27
attributes := make (map [string ]string )
26
28
for i := 0 ; i < numAtrributes ; i ++ {
27
29
AttributeName := "Name_" + GetRandomString (6 , false )
28
30
attributes [AttributeName ] = "Value_" + GetRandomString (6 , false )
29
- LogMessage (fmt .Sprintf (" ....... Add attribute : %s = %s" , AttributeName , attributes [AttributeName ]))
31
+ LogInfoMessage (fmt .Sprintf ("Add attribute : %s = %s" , AttributeName , attributes [AttributeName ]))
30
32
}
31
33
return attributes
32
34
}
33
35
36
+ // Create and return a group in a schema structure
34
37
func addGroup () * schema.CommandGroup {
35
38
36
39
commandGroup := schema.CommandGroup {}
37
-
38
40
commandGroup .Kind = GetRandomGroupKind ()
39
- LogMessage (fmt .Sprintf (" ....... group Kind: %s" , commandGroup .Kind ))
41
+ LogInfoMessage (fmt .Sprintf ("group Kind: %s" , commandGroup .Kind ))
40
42
commandGroup .IsDefault = GetBinaryDecision ()
41
- LogMessage (fmt .Sprintf (" ....... group isDefault: %t" , commandGroup .IsDefault ))
42
-
43
+ LogInfoMessage (fmt .Sprintf ("group isDefault: %t" , commandGroup .IsDefault ))
43
44
return & commandGroup
44
45
}
45
46
47
+ // Add a command of the specified type to the schema
46
48
func (devfile * TestDevfile ) addCommand (commandType schema.CommandType ) string {
47
-
48
- var commands []schema.Command
49
- index := 0
50
- if devfile .SchemaDevFile .Commands != nil {
51
- // Commands already exist so expand the current slice
52
- currentSize := len (devfile .SchemaDevFile .Commands )
53
- commands = make ([]schema.Command , currentSize + 1 )
54
- for _ , command := range devfile .SchemaDevFile .Commands {
55
- commands [index ] = command
56
- index ++
57
- }
58
- } else {
59
- commands = make ([]schema.Command , 1 )
60
- }
61
-
62
- generateCommand (& commands [index ], commandType )
63
- devfile .SchemaDevFile .Commands = commands
64
-
65
- return commands [index ].Id
66
-
49
+ command := generateCommand (commandType )
50
+ devfile .SchemaDevFile .Commands = append (devfile .SchemaDevFile .Commands , command )
51
+ return command .Id
67
52
}
68
53
69
- func generateCommand (command * schema.Command , commandType schema.CommandType ) {
54
+ // Create a command of a specified type in a schema structure
55
+ func generateCommand (commandType schema.CommandType ) schema.Command {
56
+ command := schema.Command {}
70
57
command .Id = GetRandomUniqueString (8 , true )
71
- LogMessage (fmt .Sprintf (" ....... id : %s" , command .Id ))
58
+ LogInfoMessage (fmt .Sprintf ("command Id : %s" , command .Id ))
72
59
73
60
if commandType == schema .ExecCommandType {
74
61
command .Exec = createExecCommand ()
75
62
} else if commandType == schema .CompositeCommandType {
76
63
command .Composite = createCompositeCommand ()
77
64
}
65
+ return command
78
66
}
79
67
68
+ // Update the values of a specified command
80
69
func (devfile * TestDevfile ) UpdateCommand (parserCommand * schema.Command ) error {
81
70
82
- errorString := ""
71
+ var errorString [] string
83
72
testCommand , found := getSchemaCommand (devfile .SchemaDevFile .Commands , parserCommand .Id )
84
73
if found {
85
- LogMessage (fmt .Sprintf (" ....... Updating command id: %s" , parserCommand .Id ))
74
+ LogInfoMessage (fmt .Sprintf ("Updating command id: %s" , parserCommand .Id ))
86
75
if testCommand .Exec != nil {
87
76
setExecCommandValues (parserCommand .Exec )
88
77
} else if testCommand .Composite != nil {
89
78
setCompositeCommandValues (parserCommand .Composite )
90
79
}
91
80
devfile .replaceSchemaCommand (* parserCommand )
92
81
} else {
93
- errorString += LogMessage ( fmt .Sprintf (" ....... Command not found in test : %s" , parserCommand .Id ))
82
+ errorString = append ( errorString , LogErrorMessage ( fmt .Sprintf ("Command not found in test : %s" , parserCommand .Id ) ))
94
83
}
95
84
96
- var err error
97
- if errorString != "" {
98
- err = errors .New (errorString )
85
+ var returnError error
86
+ if len ( errorString ) > 0 {
87
+ returnError = errors .New (fmt . Sprint ( errorString ) )
99
88
}
100
- return err
89
+ return returnError
101
90
}
102
91
92
+ // Create and return an exec command in a schema structure
103
93
func createExecCommand () * schema.ExecCommand {
104
94
105
- LogMessage ("Create an exec command :" )
106
-
95
+ LogInfoMessage ("Create an exec command :" )
107
96
execCommand := schema.ExecCommand {}
108
-
109
97
setExecCommandValues (& execCommand )
110
-
111
98
return & execCommand
112
99
113
100
}
114
101
102
+ // Set the attribute values of an exec command
115
103
func setExecCommandValues (execCommand * schema.ExecCommand ) {
116
104
117
105
execCommand .Component = GetRandomString (8 , false )
118
- LogMessage (fmt .Sprintf (" ....... component: %s" , execCommand .Component ))
106
+ LogInfoMessage (fmt .Sprintf ("....... component: %s" , execCommand .Component ))
119
107
120
108
execCommand .CommandLine = GetRandomString (4 , false ) + " " + GetRandomString (4 , false )
121
- LogMessage (fmt .Sprintf (" ....... commandLine: %s" , execCommand .CommandLine ))
109
+ LogInfoMessage (fmt .Sprintf ("....... commandLine: %s" , execCommand .CommandLine ))
122
110
123
111
if GetRandomDecision (2 , 1 ) {
124
112
execCommand .Group = addGroup ()
@@ -128,20 +116,20 @@ func setExecCommandValues(execCommand *schema.ExecCommand) {
128
116
129
117
if GetBinaryDecision () {
130
118
execCommand .Label = GetRandomString (12 , false )
131
- LogMessage (fmt .Sprintf (" ....... label: %s" , execCommand .Label ))
119
+ LogInfoMessage (fmt .Sprintf ("....... label: %s" , execCommand .Label ))
132
120
} else {
133
121
execCommand .Label = ""
134
122
}
135
123
136
124
if GetBinaryDecision () {
137
125
execCommand .WorkingDir = "./tmp"
138
- LogMessage (fmt .Sprintf (" ....... WorkingDir: %s" , execCommand .WorkingDir ))
126
+ LogInfoMessage (fmt .Sprintf ("....... WorkingDir: %s" , execCommand .WorkingDir ))
139
127
} else {
140
128
execCommand .WorkingDir = ""
141
129
}
142
130
143
131
execCommand .HotReloadCapable = GetBinaryDecision ()
144
- LogMessage (fmt .Sprintf (" ....... HotReloadCapable: %t" , execCommand .HotReloadCapable ))
132
+ LogInfoMessage (fmt .Sprintf ("....... HotReloadCapable: %t" , execCommand .HotReloadCapable ))
145
133
146
134
if GetBinaryDecision () {
147
135
execCommand .Env = AddEnv (GetRandomNumber (4 ))
@@ -151,6 +139,7 @@ func setExecCommandValues(execCommand *schema.ExecCommand) {
151
139
152
140
}
153
141
142
+ // Use the specified command to replace the command in the schema structure with the same Id.
154
143
func (devfile TestDevfile ) replaceSchemaCommand (command schema.Command ) {
155
144
for i := 0 ; i < len (devfile .SchemaDevFile .Commands ); i ++ {
156
145
if devfile .SchemaDevFile .Commands [i ].Id == command .Id {
@@ -160,6 +149,7 @@ func (devfile TestDevfile) replaceSchemaCommand(command schema.Command) {
160
149
}
161
150
}
162
151
152
+ // Get a command from the schema structure
163
153
func getSchemaCommand (commands []schema.Command , id string ) (* schema.Command , bool ) {
164
154
found := false
165
155
var schemaCommand schema.Command
@@ -173,22 +163,23 @@ func getSchemaCommand(commands []schema.Command, id string) (*schema.Command, bo
173
163
return & schemaCommand , found
174
164
}
175
165
166
+ // Create a composite command in a schema structure
176
167
func createCompositeCommand () * schema.CompositeCommand {
177
168
169
+ LogInfoMessage ("Create a composite command :" )
178
170
compositeCommand := schema.CompositeCommand {}
179
-
180
171
setCompositeCommandValues (& compositeCommand )
181
-
182
172
return & compositeCommand
183
173
}
184
174
175
+ // Set the attribute values for a composite command
185
176
func setCompositeCommandValues (compositeCommand * schema.CompositeCommand ) {
186
177
numCommands := GetRandomNumber (3 )
187
178
188
179
compositeCommand .Commands = make ([]string , numCommands )
189
180
for i := 0 ; i < numCommands ; i ++ {
190
181
compositeCommand .Commands [i ] = GetRandomUniqueString (8 , false )
191
- LogMessage (fmt .Sprintf (" ....... command %d of %d : %s" , i , numCommands , compositeCommand .Commands [i ]))
182
+ LogInfoMessage (fmt .Sprintf ("....... command %d of %d : %s" , i , numCommands , compositeCommand .Commands [i ]))
192
183
}
193
184
194
185
if GetRandomDecision (2 , 1 ) {
@@ -197,60 +188,71 @@ func setCompositeCommandValues(compositeCommand *schema.CompositeCommand) {
197
188
198
189
if GetBinaryDecision () {
199
190
compositeCommand .Label = GetRandomString (12 , false )
200
- LogMessage (fmt .Sprintf (" ....... label: %s" , compositeCommand .Label ))
191
+ LogInfoMessage (fmt .Sprintf ("....... label: %s" , compositeCommand .Label ))
201
192
}
202
193
203
194
if GetBinaryDecision () {
204
195
compositeCommand .Parallel = true
205
- LogMessage (fmt .Sprintf (" ....... Parallel: %t" , compositeCommand .Parallel ))
196
+ LogInfoMessage (fmt .Sprintf ("....... Parallel: %t" , compositeCommand .Parallel ))
206
197
}
207
198
}
208
199
200
+ // Verify commands returned by the parser match with those saved in the schema
209
201
func (devfile TestDevfile ) VerifyCommands (parserCommands []schema.Command ) error {
210
202
211
- LogMessage ("Enter VerifyCommands" )
212
- errorString := ""
203
+ LogInfoMessage ("Enter VerifyCommands" )
204
+ var errorString [] string
213
205
214
206
// Compare entire array of commands
215
207
if ! cmp .Equal (parserCommands , devfile .SchemaDevFile .Commands ) {
216
- errorString += LogMessage ( fmt .Sprintf (" --> ERROR: Command array compare failed." ))
208
+ errorString = append ( errorString , LogErrorMessage ( fmt .Sprintf ("Command array compare failed." ) ))
217
209
// Array compare failed. Narrow down by comparing indivdual commands
218
210
for _ , command := range parserCommands {
219
211
if testCommand , found := getSchemaCommand (devfile .SchemaDevFile .Commands , command .Id ); found {
220
212
if ! cmp .Equal (command , * testCommand ) {
221
213
parserFilename := AddSuffixToFileName (devfile .FileName , "_" + command .Id + "_Parser" )
222
214
testFilename := AddSuffixToFileName (devfile .FileName , "_" + command .Id + "_Test" )
223
- LogMessage (fmt .Sprintf (" .......marshall and write devfile %s" , devfile .FileName ))
215
+ LogInfoMessage (fmt .Sprintf (".......marshall and write devfile %s" , devfile .FileName ))
224
216
c , err := yaml .Marshal (command )
225
- if err == nil {
217
+ if err != nil {
218
+ errorString = append (errorString , LogErrorMessage (fmt .Sprintf (".......marshall devfile %s" , parserFilename )))
219
+ } else {
226
220
err = ioutil .WriteFile (parserFilename , c , 0644 )
221
+ if err != nil {
222
+ errorString = append (errorString , LogErrorMessage (fmt .Sprintf (".......write devfile %s" , parserFilename )))
223
+ }
227
224
}
228
- LogMessage (fmt .Sprintf (" .......marshall and write devfile %s" , devfile . FileName ))
225
+ LogInfoMessage (fmt .Sprintf (".......marshall and write devfile %s" , testFilename ))
229
226
c , err = yaml .Marshal (testCommand )
230
- if err == nil {
227
+ if err != nil {
228
+ errorString = append (errorString , LogErrorMessage (fmt .Sprintf (".......marshall devfile %s" , testFilename )))
229
+ } else {
231
230
err = ioutil .WriteFile (testFilename , c , 0644 )
231
+ if err != nil {
232
+ errorString = append (errorString , LogErrorMessage (fmt .Sprintf (".......write devfile %s" , testFilename )))
233
+ }
232
234
}
233
- errorString += LogMessage ( fmt .Sprintf (" --> ERROR: Command %s did not match, see files : %s and %s" , command .Id , parserFilename , testFilename ))
235
+ errorString = append ( errorString , LogInfoMessage ( fmt .Sprintf ("Command %s did not match, see files : %s and %s" , command .Id , parserFilename , testFilename ) ))
234
236
} else {
235
- LogMessage (fmt .Sprintf (" --> Command matched : %s" , command .Id ))
237
+ LogInfoMessage (fmt .Sprintf (" --> Command matched : %s" , command .Id ))
236
238
}
237
239
} else {
238
- errorString += LogMessage ( fmt .Sprintf (" --> ERROR: Command from parser not known to test - id : %s " , command .Id ))
240
+ errorString = append ( errorString , LogErrorMessage ( fmt .Sprintf ("Command from parser not known to test - id : %s " , command .Id ) ))
239
241
}
240
242
241
243
}
242
244
for _ , command := range devfile .SchemaDevFile .Commands {
243
245
if _ , found := getSchemaCommand (parserCommands , command .Id ); ! found {
244
- errorString += LogMessage ( fmt .Sprintf (" --> ERROR: Command from test not returned by parser : %s " , command .Id ))
246
+ errorString = append ( errorString , LogErrorMessage ( fmt .Sprintf ("Command from test not returned by parser : %s " , command .Id ) ))
245
247
}
246
248
}
247
249
} else {
248
- LogMessage (fmt .Sprintf (" --> Command structures matched" ))
250
+ LogInfoMessage (fmt .Sprintf (" --> Command structures matched" ))
249
251
}
250
252
251
253
var err error
252
- if errorString != "" {
253
- err = errors .New (errorString )
254
+ if len ( errorString ) > 0 {
255
+ err = errors .New (fmt . Sprint ( errorString ) )
254
256
}
255
257
return err
256
258
}
0 commit comments