-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathodo_test.go
545 lines (445 loc) · 15.2 KB
/
odo_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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
run 5 test in paralel:
ginkgo run --procs 5
specifying flags:
ginkgo run -v -- -stacksPath <path>
test only one stack with a specific stack id:
ginkgo run --focus "stack: java-vertx starter: vertx-http-example" -- -stacksPath ../../stacks
test all starter project in a specific stack:
ginkgo run --focus "stack: java-vertx" -- -stacksPath ../../stacks
specifying odo path:
ginkgo run -v -- -odoPath <path/to/odo/binary>
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/devfile/library/v2/pkg/devfile"
"github.com/devfile/library/v2/pkg/devfile/parser"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var stacksPath string
var stackDirs string
var odoPath string
func init() {
flag.StringVar(&stacksPath, "stacksPath", "../../stacks", "The path to the directory containing the stacks")
flag.StringVar(&stackDirs, "stackDirs", "", "The stacks to test as a string separated by spaces")
flag.StringVar(&odoPath, "odoPath", "odo", "The path to the odo binary, defaults to path symbol")
}
// all stacks to be tested
var stacks []Stack
func TestOdo(t *testing.T) {
RegisterFailHandler(Fail)
// perform work that needs to be done before the Tree Construction Phase here
// note that we wrap `t` with a new Gomega instance to make assertions about the fixtures here.
// more at: https://onsi.github.io/ginkgo/#dynamically-generating-specs
g := NewGomegaWithT(t)
var dirs []string
if stackDirs != "" {
dirs = strings.Split(stackDirs, " ")
}
for _, dir := range dirs {
base := filepath.Join(stacksPath, dir)
path := filepath.Join(base, "devfile.yaml")
parserArgs := parser.ParserArgs{
Path: path,
}
devfile, _, err := devfile.ParseDevfileAndValidate(parserArgs)
g.Expect(err).To(BeNil())
schemaVersion := devfile.Data.GetSchemaVersion()
name := devfile.Data.GetMetadata().Name
version := devfile.Data.GetMetadata().Version
starterProjects, err := devfile.Data.GetStarterProjects(common.DevfileOptions{})
g.Expect(err).To(BeNil())
stack := Stack{name: name, schemaVersion: schemaVersion, version: version, path: path, starterProjects: starterProjects, base: base}
stacks = append(stacks, stack)
}
GinkgoWriter.Println("Total stacks found", len(stacks), "stacks")
RunSpecs(t, "odo suite")
}
var _ = Describe("test starter projects from devfile stacks", func() {
var namespace string
var tmpDir string
var oldDir string
var oldKubeConfig string
var _ = BeforeEach(func() {
// save current working directory and make sure that we go back
var err error
oldDir, err = os.Getwd()
Expect(err).To(BeNil())
// create new temporary directory and change to it
tmpDir, err = os.MkdirTemp("", "")
Expect(err).To(BeNil())
// use copy of KUBECONFIG to make sure that we don't change the original one
oldKubeConfig = os.Getenv("KUBECONFIG")
tmpKubeConfig := filepath.Join(tmpDir, "kubeconfig")
if oldKubeConfig == "" {
home, err := os.UserHomeDir()
Expect(err).To(BeNil())
copyFile(filepath.Join(home, ".kube", "config"), tmpKubeConfig)
} else {
copyFile(oldKubeConfig, tmpKubeConfig)
}
os.Setenv("KUBECONFIG", tmpKubeConfig)
GinkgoWriter.Printf("KUBECONFIG=%s\n", os.Getenv("KUBECONFIG"))
// use dedicated odo preference file for tests
os.Setenv("GLOBALODOCONFIG", filepath.Join(tmpDir, "preference.yaml"))
// disable telemetry
_, _, err = runOdo("preference", "set", "ConsentTelemetry", "false")
Expect(err).To(BeNil())
// use Ephemeral (emptyDir) volumes instead of PVC
_, _, err = runOdo("preference", "set", "ephemeral", "true")
Expect(err).To(BeNil())
// create new directory for the application and set it as the current working directory
appDir := filepath.Join(tmpDir, "app")
os.Mkdir(appDir, 0755)
os.Chdir(appDir)
namespace = randomString(8)
_, _, err = runOdo("create", "namespace", namespace)
Expect(err).To(BeNil())
GinkgoWriter.Println("Using namespace:", namespace)
})
var _ = AfterEach(func() {
runOdo("delete", "namespace", "-f", namespace)
os.Setenv("KUBECONFIG", oldKubeConfig)
os.Chdir(oldDir)
os.RemoveAll(tmpDir)
})
for _, stack := range stacks {
stack := stack
if len(stack.starterProjects) == 0 {
It(fmt.Sprintf("stack: %s version: %s no_starter", stack.name, stack.version), func() {
// No starter projects defined in Devfile => let's start a Dev Session with --no-commands
// (i.e., without implicitly executing any build and/or run commands), since there won't be any source code.
// So here, we just want to make sure that odo dev starts properly (to cover cases for example where the dev container does not end up running).
cmpName := fmt.Sprintf("cmp-%s-%s", stack.name, randomString(3))
_, _, err := runOdo("init", "--devfile-path", stack.path, "--name", cmpName)
Expect(err).To(BeNil())
// Copy all additional resources found inside the stack directory.
err = copyDir(stack.base, filepath.Join(tmpDir, "app"))
Expect(err).To(BeNil())
devStdout, devStderr, devProcess, err := runOdoDev("--no-commands")
Expect(err).To(BeNil())
var stopped bool
defer func() {
if !stopped {
_ = devProcess.Process.Kill()
_ = devProcess.Wait()
}
_, _, _ = runOdo("delete", "component", "--force")
}()
var stdoutContentRead []string
Eventually(func(g Gomega) string {
tmpBuffer := make([]byte, 4096)
_, rErr := devStdout.Read(tmpBuffer)
if rErr != io.EOF {
g.Expect(rErr).ShouldNot(HaveOccurred())
}
stdoutReadSoFar := string(tmpBuffer)
fmt.Fprintln(GinkgoWriter, stdoutReadSoFar)
stdoutContentRead = append(stdoutContentRead, stdoutReadSoFar)
return strings.Join(stdoutContentRead, "\n")
}).WithTimeout(3*time.Minute).WithPolling(10*time.Second).Should(
SatisfyAll(
ContainSubstring("Keyboard Commands:"),
// The matcher below is to prevent flakiness (case for example of a Pod with a terminating command,
// where the pod could still run briefly but stop afterward); odo would try to sync files when it detects
// the pod is running but not succeed to do so because the pod ended up being restarted.
Not(ContainSubstring("failed to sync to component with name %s", cmpName)),
),
func() string {
// Stopping the dev process to be able to read its stderr output without blocking
// (devStderr is a pipe, and we can read only as long as something is writing to it).
_ = devProcess.Process.Kill()
_ = devProcess.Wait()
stopped = true
dataStderr, _ := io.ReadAll(devStderr)
return fmt.Sprintf(`Dev Session not started properly. See logs below:
*** STDOUT ****
%s
**** STDERR ****
%s
`, strings.Join(stdoutContentRead, "\n"), string(dataStderr))
})
})
} else {
for _, starterProject := range stack.starterProjects {
starterProject := starterProject
It(fmt.Sprintf("stack: %s version: %s starter: %s", stack.name, stack.version, starterProject.Name), func() {
_, _, err := runOdo("init", "--devfile-path", stack.path, "--starter", starterProject.Name, "--name", starterProject.Name)
Expect(err).To(BeNil())
// Copy all additional resources found inside the stack directory.
err = copyDir(stack.base, filepath.Join(tmpDir, "app"))
Expect(err).To(BeNil())
devStdout, devStderr, devProcess, err := runOdoDev()
Expect(err).To(BeNil())
// if odo dev command failed send error to this chanel to interrupt waitForPort()
devError := make(chan error)
go func() {
dataStdout, err := io.ReadAll(devStdout)
Expect(err).To(BeNil())
dataStderr, err := io.ReadAll(devStderr)
Expect(err).To(BeNil())
err = devProcess.Wait()
PrintIfNotEmpty("'odo dev' stdout:", string(dataStdout))
PrintIfNotEmpty("'odo dev' stderr:", string(dataStderr))
devError <- err
}()
ports, err := waitForPort(devError)
Expect(err).To(BeNil())
for _, port := range ports {
err := waitForHttp(fmt.Sprintf("http://%s:%d", port.LocalAddress, port.LocalPort), 200)
Expect(err).To(BeNil())
}
devProcess.Process.Kill()
runOdo("delete", "component", "--force")
})
}
}
}
})
func waitForHttp(url string, expectedCode int) error {
maxTries := 12
delay := 10 * time.Second
for i := 0; i < maxTries; i++ {
GinkgoWriter.Printf("Waiting for %s to return %d. Try %d of %d\n", url, expectedCode, i+1, maxTries)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
GinkgoWriter.Printf("Unable to create HTTP Request %s. Trying again in %f\n", err, delay.Seconds())
time.Sleep(delay)
continue
}
req.Header.Set("Accept", "*/*")
resp, err := client.Do(req)
if err != nil {
GinkgoWriter.Printf("Unable to get %s. Trying again in %f\n", err, delay.Seconds())
time.Sleep(delay)
continue
}
defer resp.Body.Close()
if resp.StatusCode == expectedCode {
GinkgoWriter.Printf("%s returned %d\n", url, expectedCode)
return nil
}
GinkgoWriter.Printf("Unexpected return code %d. Trying again in %s\n", resp.StatusCode, delay)
time.Sleep(delay)
}
// try to retrieve logs to see what happened
logsStdout, logsStderr, logsErr := runOdo("logs")
if logsErr != nil {
GinkgoWriter.Printf("Unable to get odo logs: %s\n", logsErr)
}
GinkgoWriter.Printf("odo logs stdout: %s\n", logsStdout)
GinkgoWriter.Printf("odo logs stderr: %s\n", logsStderr)
return fmt.Errorf("%s did not return %d", url, expectedCode)
}
// uses `odo describe component` to get the forwarded ports of the component
// returns list of forwarded ports
func waitForPort(devError chan error) ([]ForwardedPort, error) {
args := []string{"describe", "component", "-o", "json"}
maxTries := 50
delay := 10 * time.Second
stdout := []byte{}
stderr := []byte{}
var lastError error
for i := 0; i < maxTries; i++ {
// check if odo dev command failed, if yes stop and return error
select {
case err := <-devError:
GinkgoWriter.Printf("'odo dev' failed with %q\n", err)
return nil, fmt.Errorf("'odo dev' failed with %q", err)
default:
}
GinkgoWriter.Println("Waiting for odo to setup port-forwarding. Try", i+1, "of", maxTries)
var component Component
var err error
stdout, stderr, err = runOdo(args...)
if err != nil {
GinkgoWriter.Println("odo command failed")
PrintIfNotEmpty("stdout:", string(stdout))
PrintIfNotEmpty("stderr:", string(stderr))
return nil, err
}
err = json.Unmarshal(stdout, &component)
if err != nil {
lastError = err
time.Sleep(delay)
continue
}
// get list ports that we should wait for
// this ignores ports that have exposure set to "none" or "internal"
ports := []int{}
for _, component := range component.DevfileData.Devfile.Components {
if component.Container != nil {
for _, endpoint := range component.Container.Endpoints {
if endpoint.Exposure == "none" || endpoint.Exposure == "internal" {
continue
}
ports = append(ports, endpoint.TargetPort)
}
}
}
GinkgoWriter.Printf("Checking if following %v ports have port-forwarding setup.\n", ports)
if len(component.DevForwardedPorts) >= len(ports) {
GinkgoWriter.Println("Found ports", component.DevForwardedPorts)
out := []ForwardedPort{}
// return only ports that we were waiting for
for _, forwardedPort := range component.DevForwardedPorts {
for _, port := range ports {
if forwardedPort.ContainerPort == port {
out = append(out, forwardedPort)
}
}
}
return out, nil
}
delay += 10 * time.Second
time.Sleep(delay)
}
GinkgoWriter.Println("No ports found")
GinkgoWriter.Printf("Last error: %v", lastError)
PrintIfNotEmpty("Last stdout:", string(stdout))
PrintIfNotEmpty("Last stderr:", string(stderr))
// try to retrieve logs to see what happened
logsStdout, logsStderr, logsErr := runOdo("logs")
if logsErr != nil {
GinkgoWriter.Printf("Unable to get odo logs: %s\n", logsErr)
}
PrintIfNotEmpty("odo logs stdout:", string(logsStdout))
PrintIfNotEmpty("odo logs stderr:", string(logsStderr))
return nil, fmt.Errorf("No ports found")
}
// run `odo dev` on the background
// returned cmd can be used to kill the process
// returns stdout pipe, stderr pipe, cmd
func runOdoDev(additionalArgs ...string) (io.ReadCloser, io.ReadCloser, *exec.Cmd, error) {
args := []string{"dev", "--random-ports"}
args = append(args, additionalArgs...)
GinkgoWriter.Println("Executing odo " + strings.Join(args, " "))
cmd := exec.Command(odoPath, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, nil, err
}
return stdout, stderr, cmd, nil
}
// run odo commands
// returns stdout, stderr, and error
func runOdo(args ...string) ([]byte, []byte, error) {
// Clean given path
filepath.Clean(odoPath)
GinkgoWriter.Println("Executing: odo", strings.Join(args, " "))
cmd := exec.Command(odoPath, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, nil, err
}
if err := cmd.Start(); err != nil {
return nil, nil, err
}
dataStdout, err := io.ReadAll(stdout)
if err != nil {
return nil, nil, err
}
dataStderr, err := io.ReadAll(stderr)
if err != nil {
return nil, nil, err
}
err = cmd.Wait()
if err != nil {
return nil, nil, err
}
return dataStdout, dataStderr, nil
}
const letters = "abcdefghijklmnopqrstuvwxyz"
func randomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
// copyDir: Copies all items inside given stack's directory to the given destination.
// If the item exists it skips it.
func copyDir(src string, dst string) error {
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
sourceEntryPath := filepath.Join(src, entry.Name())
destEntryPath := filepath.Join(dst, entry.Name())
sourceEntryInfo, err := os.Stat(sourceEntryPath)
if err != nil {
return err
}
_, err = os.Stat(destEntryPath)
// check if destination exists
if err == nil {
// if it exists continue
continue
} else if !os.IsNotExist(err) {
// for every other err return it
return err
}
// if entry is a directory, create a dir in destination and go one level down.
if sourceEntryInfo.IsDir() {
os.Mkdir(destEntryPath, 0755)
err = copyDir(sourceEntryPath, destEntryPath)
if err != nil {
return err
}
continue
}
// if is not a dir copy file
err = copyFile(sourceEntryPath, destEntryPath)
if err != nil {
return err
}
}
return nil
}
// this is not the best way to copy files
func copyFile(src string, dst string) error {
GinkgoWriter.Printf("Copying file %s to %s\n", src, dst)
input, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, input, 0644)
if err != nil {
return err
}
return nil
}
// write to GinkgoWeiter if str is not empty.
// format will be "decription str"
func PrintIfNotEmpty(description string, str string) {
if str != "" {
GinkgoWriter.Printf("%s %s\n", description, str)
}
}