-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathe2e.go
98 lines (85 loc) · 3.23 KB
/
e2e.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package e2e is created because:
//
// During test setup, SetupLocalRegistry is called,
// and container tests need to be run before VM tests
// so that the local registry won't be removed by VM tests,
// and container tests can use the images stored in it.
//
// However, by default Ginkgo does not guarantee the order in which specs run [1],
// and Ginkgo doc says that "You should only ever call RunSpecs once" [2],
// which means that we need two binaries that run VM tests and container tests respectively.
//
// We could add ginkgo.Ordered to the top level Ginkgo container,
// but that will make every single spec to run in the order they are defined:
// "Any container nodes nested within a container node will automatically be considered Ordered
// and there is no way to mark a node within an Ordered container as "not Ordered"" [1],
// and we don't want that because it can hide implicit dependencies among tests,
// while each test should be independent unless specially designed.
//
// As a result, we split the tests into `e2e/vm` and `e2e/container` packages
// and extract the common logic into this package.
//
// [1] https://onsi.github.io/ginkgo/#ordered-containers.
// [2] https://onsi.github.io/ginkgo/#mental-model-go-modules-packages-and-tests
package e2e
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"github.com/runfinch/common-tests/option"
)
// InstalledTestSubject is the test subject when Finch is installed.
const InstalledTestSubject = "finch"
var (
// Installed indicates whether the tests are run against installed application.
Installed = flag.Bool("installed", false, "the flag to show whether the tests are run against installed application")
// Registry indicates which container registry to pull from.
Registry = flag.String("registry", "", "used when pulling from registry to test credential helper")
)
// CreateOption creates an option for running e2e tests.
func CreateOption() (*option.Option, error) {
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get the current working directory: %w", err)
}
subject := filepath.Join(wd, "..", "..", "_output", "bin", "finch")
if *Installed {
subject = InstalledTestSubject
}
args := []string{subject}
debug := os.Getenv("DEBUG")
if len(debug) != 0 {
args = append(args, "--debug")
}
mods := []option.Modifier{}
if runtime.GOOS == "linux" {
nerdctlVersion, err := getNerdctlVersion(subject)
if err != nil {
return nil, err
}
mods = append(mods, option.WithNerdctlVersion(nerdctlVersion))
}
o, err := option.New(args, mods...)
if err != nil {
return nil, fmt.Errorf("failed to initialize a testing option: %w", err)
}
return o, nil
}
func getNerdctlVersion(subject string) (string, error) {
output, err := exec.Command(subject, "version").CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to run version command: %w", err)
}
re := regexp.MustCompile(`(?m)nerdctl:\n\s*Version:\s*v(.*)$`)
matches := re.FindStringSubmatch(string(output))
if matches == nil || len(matches) != 2 {
return "", fmt.Errorf("regexp did not match properly, output: %s", output)
}
return matches[1], nil
}