-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
367 lines (347 loc) · 14.1 KB
/
main_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) 2020 Wind River Systems, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY KIND, either express or implied.
package main
import (
"os"
"os/exec"
"strings"
"testing"
"time"
)
var logical_part_id string
var paths []string
var fvc []string
// TestPing is used to check the connection to the software part catalog's server
func TestPing(tester *testing.T) {
// ccli ping
cmd := exec.Command("ccli", "ping")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
result := string(output)
expected := "Ping Result: Success\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
} else {
paths = []string{"testdir/packages/openid-client-4.9.1_test.zip", "testdir/packages/busybox-1.35.0_test.zip"}
fvc = []string{"46564332008de01dcc150bcf6673a576d4c438b442afbb61d2cc98017234e44d9e338f19e8", "4656433200fe9e83718aa74cf8e3af8517fbaa0c273ed31d32363278759f86ec45251b5599"}
}
}
// TestUpload uploads the package present in the given path using the command line
// and checks if the command line output is as expected
func TestUpload(tester *testing.T) {
// set timer for upload to 20 seconds
duration := 180 * time.Second
for i := 0; i < 1; i = i + 1 {
// ccli upload testdir/packages/openid-client-4.9.1.zip
cmd := exec.Command("ccli", "upload", paths[i])
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
result := string(output)
expected := "Successfully uploaded package: " + paths[i] + "\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
var flag bool
flag = false
// Loop to check if the upload has been reflected on the catalog and
for start := time.Now(); time.Since(start) < duration; {
// ccli query 'query{part(file_verification_code:\"465643320044e55d9adee108307f5c274ecf14c4dd1442c43a66fc8955dcf7e40d6f8a50d1\"){size}}'
cmd := exec.Command("ccli", "query", "query{part(file_verification_code:\""+fvc[i]+"\"){size}}")
// capturing command line output
_, err = cmd.Output()
if err == nil {
flag = true
break
}
}
if !flag {
tester.Errorf("Timed out before upload could complete for %s", paths[i])
}
}
}
// TestAddPart adds a part based on the yml file present in the given path using the
// command line and checks if the command line output is as expected
func TestAddPart(tester *testing.T) {
// ccli add --part testdir/yml/busybox-1.35.0.yml
cmd := exec.Command("ccli", "add", "part", "testdir/yml/busybox-1.35.0.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked
result := strings.Split(string(output), "\n")[0]
// saving the part id outputted for it to be deleted later
logical_part_id = strings.Fields(strings.Split(string(output), "\n")[2])[1]
logical_part_id = logical_part_id[1 : len(logical_part_id)-2]
expected := "Successfully added part from: testdir/yml/busybox-1.35.0.yml"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestQuery runs a qraphql query using the command line
// and checks if the command line output is as expected
func TestQuery(tester *testing.T) {
// ccli query 'query{part(file_verification_code:\"465643320044e55d9adee108307f5c274ecf14c4dd1442c43a66fc8955dcf7e40d6f8a50d1\"){size}}'
cmd := exec.Command("ccli", "query", "query{part(file_verification_code:\""+fvc[0]+"\"){size}}")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
result := strings.Fields(string(output))[4]
expected := "164900"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestUpdate updates a part's information based on the yml file present in the given path using the
// command line and checks if the command line output is as expected
func TestUpdate(tester *testing.T) {
// ccli update --part testdir/yml/openid-client-4.9.1.yml
cmd := exec.Command("ccli", "update", "testdir/yml/openid-client-4.9.1.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked
result := strings.Split(string(output), "\n")[0]
expected := "Part successfully updated"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestAddLicenseProfile adds a part's licensing profile based on the yml file present in the given path using the
// command line and checks if the command line output is as expected
func TestAddLicenseProfile(tester *testing.T) {
// ccli add --profile testdir/yml/openid_license.yml
cmd := exec.Command("ccli", "add", "profile", "testdir/yml/openid_license.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
result := strings.Fields(string(output))[0]
expected := "Successfully"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestAddSecurityProfile adds a part's security profile based on the yml file present in the given path using the
// command line and checks if the command line output is as expected
func TestAddSecurityProfile(tester *testing.T) {
// ccli add --profile testdir/yml/openid_security.yml
cmd := exec.Command("ccli", "add", "profile", "testdir/yml/openid_security.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
result := strings.Fields(string(output))[0]
expected := "Successfully"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestAddQualityProfile adds a part's quality profile based on the yml file present in the given path using the
// command line and checks if the command line output is as expected
func TestAddQualityProfile(tester *testing.T) {
// ccli add --profile testdir/yml/openid_quality.yml
cmd := exec.Command("ccli", "add", "profile", "testdir/yml/openid_quality.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
result := strings.Fields(string(output))[0]
expected := "Successfully"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestExportPart exports a part to the given path in the form of a yml file using the
// command line and checks if the command line output is as expected
func TestExportPart(tester *testing.T) {
// ccli export -fvc 465643320044e55d9adee108307f5c274ecf14c4dd1442c43a66fc8955dcf7e40d6f8a50d1 -o testdir/testpart.yml
cmd := exec.Command("ccli", "export", "part", "fvc", fvc[0], "-o", "testdir/testpart.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
result := string(output)
expected := "Part successfully exported to path: testdir/testpart.yml\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
// remove the exported test yml files
os.RemoveAll("testdir/testpart.yml")
}
// TestExportTemplate exports a part or profile template to the given path in the form of a yml file using the
// command line and checks if the command line output is as expected
func TestExportTemplate(tester *testing.T) {
// ccli export --template licensing -o testdir/testlicense.yml
cmd := exec.Command("ccli", "export", "template", "license", "-o", "testdir/testlicense.yml")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
result := string(output)
expected := "Profile template successfully output to: testdir/testlicense.yml\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
// remove the exported test yml files
os.RemoveAll("testdir/testlicense.yml")
}
// TestDelete first finds out a part's unique part-id using the file verification code and then deletes the part using the
// part-id and command line. Finally checks if the command line output is as expected
func TestDelete(tester *testing.T) {
// ccli find -fvc 465643320044e55d9adee108307f5c274ecf14c4dd1442c43a66fc8955dcf7e40d6f8a50d1
cmd := exec.Command("ccli", "find", "fvc", fvc[0])
// capturing command line output
outputfind, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
part_Id := strings.Fields(string(outputfind))[2]
// ccli delete --id <part-id>
cmd = exec.Command("ccli", "delete", part_Id)
// capturing command line output
outputDeletePart1, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
// e.g., Hello World\nHi world
// split - Hi world
result := string(outputDeletePart1)
expected := "Successfully deleted id: " + part_Id + " from catalog\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
/*
func TestForceDelete(tester *testing.T) {
// ccli find -fvc 465643320044e55d9adee108307f5c274ecf14c4dd1442c43a66fc8955dcf7e40d6f8a50d1
cmd := exec.Command("ccli", "find", "-fvc", fvc[1])
// capturing command line output
outputfind, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
part_Id := strings.Fields(string(outputfind))[2]
// ccli delete --id <part-id>
cmd = exec.Command("ccli", "delete", "--id", part_Id, "--force")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
// e.g., Hello World\nHi world
// split - Hi world
result := string(output)
expected := "Successfully deleted id: " + part_Id + " from catalog\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
func TestRecursiveDelete(tester *testing.T) {
// ccli find -fvc 465643320044e55d9adee108307f5c274ecf14c4dd1442c43a66fc8955dcf7e40d6f8a50d1
cmd := exec.Command("ccli", "find", "fvc", fvc[1])
// capturing command line output
outputfind, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// splitting and extracting the output message to be checked. Fields func is used to divide the string by whitespaces to extract specific value
part_Id := strings.Fields(string(outputfind))[2]
// ccli delete --id <part-id>
cmd = exec.Command("ccli", "delete", part_Id, "--recursive")
// capturing command line output
output, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
// e.g., Hello World\nHi world
// split - Hi world
result := string(output)
expected := "Successfully deleted id: " + part_Id + " from catalog\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
*/
/*
func TestSharedFileDeletion(tester *testing.T) {
cmd := exec.Command("ccli", "query", "query{file(sha256:\"f29bc64a9d3732b4b9035125fdb3285f5b6455778edca72414671e0ca3b2e0de\"){names}}")
// capturing command line output
_, err := cmd.Output()
if err == nil {
tester.Error("shared file could not be deleted", err)
}
}
*/
func TestArchiveDeletion(tester *testing.T) {
cmd := exec.Command("ccli", "query", "query{archive(name:\"openid-client-4.9.1_test.zip\"){insert_date}}")
// capturing command line output
_, err := cmd.Output()
if err == nil {
tester.Error("archive could not be deleted", err)
}
cmd = exec.Command("ccli", "query", "query{archive(name:\"busybox-1.35.0_test.zip\"){insert_date}}")
// capturing command line output
_, err = cmd.Output()
if err == nil {
tester.Error("archive could not be deleted", err)
}
}
func TestLogicalPartDelete(tester *testing.T) {
// ccli delete --id <part-id>
cmd := exec.Command("ccli", "delete", logical_part_id)
// capturing command line output
outputDeletePart2, err := cmd.Output()
if err != nil {
tester.Error("failed to capture command line output", err)
}
// extracting the output message to be checked
result := string(outputDeletePart2)
expected := "Successfully deleted id: " + logical_part_id + " from catalog\n"
if result != expected {
tester.Errorf("Expected %s but got %s", expected, result)
}
}
// TestMain runs all the tests
func TestMain(mainTester *testing.M) {
exitCode := mainTester.Run()
os.Exit(exitCode)
}