Skip to content

Commit 9167b13

Browse files
committed
chore: update e2e_test.go
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent f70fc9c commit 9167b13

File tree

1 file changed

+116
-82
lines changed

1 file changed

+116
-82
lines changed

e2e/e2e_test.go

Lines changed: 116 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package e2e
55

66
import (
77
"flag"
8-
"fmt"
98
"log"
109
"os"
1110
"strings"
@@ -20,125 +19,160 @@ import (
2019
"github.com/runfinch/finch-daemon/e2e/util"
2120
)
2221

23-
// Subject defines which CLI the tests are run against, defaults to \"nerdctl\" in the user's PATH.
24-
var Subject = flag.String("subject", "nerdctl", `which CLI the tests are run against, defaults to "nerdctl" in the user's PATH.`)
25-
var SubjectPrefix = flag.String("daemon-context-subject-prefix", "", `A string which prefixes the command the tests are run against, defaults to "". This string will be split by spaces.`)
26-
var PrefixedSubjectEnv = flag.String("daemon-context-subject-env", "", `Environment to add when running a prefixed subject, in the form of a string like "EXAMPLE=foo EXAMPLE2=bar"`)
22+
const (
23+
defaultNamespace = "finch"
24+
testE2EEnv = "TEST_E2E"
25+
middlewareE2EEnv = "MIDDLEWARE_E2E"
26+
opaTestDescription = "Finch Daemon OPA E2E Tests"
27+
e2eTestDescription = "Finch Daemon Functional test"
28+
)
29+
30+
var (
31+
Subject = flag.String("subject", "nerdctl", `which CLI the tests are run against, defaults to "nerdctl" in the user's PATH.`)
32+
SubjectPrefix = flag.String("daemon-context-subject-prefix", "", `A string which prefixes the command the tests are run against, defaults to "". This string will be split by spaces.`)
33+
PrefixedSubjectEnv = flag.String("daemon-context-subject-env", "", `Environment to add when running a prefixed subject, in the form of a string like "EXAMPLE=foo EXAMPLE2=bar"`)
34+
)
2735

2836
func TestRun(t *testing.T) {
29-
if os.Getenv("MIDDLEWARE_E2E") == "1" {
37+
switch {
38+
case os.Getenv(middlewareE2EEnv) == "1":
3039
runOPATests(t)
31-
} else if os.Getenv("TEST_E2E") == "1" {
40+
case os.Getenv(testE2EEnv) == "1":
3241
runE2ETests(t)
33-
} else {
42+
default:
3443
t.Skip("E2E tests skipped. Set TEST_E2E=1 to run regular E2E tests or MIDDLEWARE_E2E=1 to run OPA middleware tests")
3544
}
3645
}
3746

38-
func runOPATests(t *testing.T) {
39-
if err := parseTestFlags(); err != nil {
40-
log.Println("failed to parse go test flags", err)
41-
os.Exit(1)
42-
}
43-
44-
opt, _ := option.New([]string{*Subject, "--namespace", "finch"})
47+
func createTestOption() (*option.Option, error) {
48+
return option.New([]string{*Subject, "--namespace", defaultNamespace})
49+
}
4550

51+
func setupTestSuite(opt *option.Option) {
4652
ginkgo.SynchronizedBeforeSuite(func() []byte {
4753
tests.SetupLocalRegistry(opt)
4854
return nil
4955
}, func(bytes []byte) {})
5056

5157
ginkgo.SynchronizedAfterSuite(func() {
5258
tests.CleanupLocalRegistry(opt)
53-
// clean up everything after the local registry is cleaned up
5459
command.RemoveAll(opt)
5560
}, func() {})
61+
}
62+
63+
func runOPATests(t *testing.T) {
64+
if err := parseTestFlags(); err != nil {
65+
log.Fatal("failed to parse go test flags:", err)
66+
}
67+
68+
opt, err := createTestOption()
69+
if err != nil {
70+
log.Fatal("failed to create test option:", err)
71+
}
5672

57-
const description = "Finch Daemon OPA E2E Tests"
58-
ginkgo.Describe(description, func() {
73+
setupTestSuite(opt)
74+
75+
ginkgo.Describe(opaTestDescription, func() {
5976
tests.OpaMiddlewareTest(opt)
60-
fmt.Print(opt)
6177
})
6278

63-
gomega.RegisterFailHandler(ginkgo.Fail)
64-
ginkgo.RunSpecs(t, description)
79+
runTests(t, opaTestDescription)
6580
}
6681

6782
func runE2ETests(t *testing.T) {
6883
if err := parseTestFlags(); err != nil {
69-
log.Println("failed to parse go test flags", err)
70-
os.Exit(1)
84+
log.Fatal("failed to parse go test flags:", err)
7185
}
7286

73-
opt, _ := option.New([]string{*Subject, "--namespace", "finch"})
87+
opt, err := createTestOption()
88+
if err != nil {
89+
log.Fatal("failed to create test option:", err)
90+
}
7491

75-
ginkgo.SynchronizedBeforeSuite(func() []byte {
76-
tests.SetupLocalRegistry(opt)
77-
return nil
78-
}, func(bytes []byte) {})
92+
setupTestSuite(opt)
7993

80-
ginkgo.SynchronizedAfterSuite(func() {
81-
tests.CleanupLocalRegistry(opt)
82-
// clean up everything after the local registry is cleaned up
83-
command.RemoveAll(opt)
84-
}, func() {})
94+
pOpt := createPrefixedOption()
8595

86-
var pOpt = option.New
87-
if *SubjectPrefix != "" {
88-
var modifiers []option.Modifier
89-
if *PrefixedSubjectEnv != "" {
90-
modifiers = append(modifiers, option.Env(strings.Split(*PrefixedSubjectEnv, " ")))
91-
}
92-
pOpt = util.WrappedOption(strings.Split(*SubjectPrefix, " "), modifiers...)
96+
ginkgo.Describe(e2eTestDescription, func() {
97+
runContainerTests(opt)
98+
runVolumeTests(opt)
99+
runNetworkTests(opt, pOpt)
100+
runImageTests(opt)
101+
runSystemTests(opt)
102+
runDistributionTests(opt)
103+
})
104+
105+
runTests(t, e2eTestDescription)
106+
}
107+
108+
func createPrefixedOption() func([]string, ...option.Modifier) (*option.Option, error) {
109+
if *SubjectPrefix == "" {
110+
return option.New
93111
}
94112

95-
const description = "Finch Daemon Functional test"
96-
ginkgo.Describe(description, func() {
97-
// functional test for container APIs
98-
tests.ContainerCreate(opt, pOpt)
99-
tests.ContainerStart(opt)
100-
tests.ContainerStop(opt)
101-
tests.ContainerRestart(opt)
102-
tests.ContainerRemove(opt)
103-
tests.ContainerList(opt)
104-
tests.ContainerRename(opt)
105-
tests.ContainerStats(opt)
106-
tests.ContainerAttach(opt)
107-
tests.ContainerLogs(opt)
108-
tests.ContainerKill(opt)
109-
tests.ContainerInspect(opt)
110-
tests.ContainerWait(opt)
111-
tests.ContainerPause(opt)
112-
tests.ContainerUnpause(opt)
113-
114-
// functional test for volume APIs
115-
tests.VolumeList(opt)
116-
tests.VolumeInspect(opt)
117-
tests.VolumeRemove(opt)
118-
119-
// functional test for network APIs
120-
tests.NetworkCreate(opt, pOpt)
121-
tests.NetworkRemove(opt)
122-
tests.NetworkList(opt)
123-
tests.NetworkInspect(opt)
124-
125-
// functional test for image APIs
126-
tests.ImageRemove(opt)
127-
tests.ImagePush(opt)
128-
tests.ImagePull(opt)
129-
130-
// functional test for system api
131-
tests.SystemVersion(opt)
132-
tests.SystemEvents(opt)
133-
134-
// functional test for distribution api
135-
tests.DistributionInspect(opt)
136-
})
113+
var modifiers []option.Modifier
114+
if *PrefixedSubjectEnv != "" {
115+
modifiers = append(modifiers, option.Env(strings.Split(*PrefixedSubjectEnv, " ")))
116+
}
117+
return util.WrappedOption(strings.Split(*SubjectPrefix, " "), modifiers...)
118+
}
137119

120+
func runTests(t *testing.T, description string) {
138121
gomega.RegisterFailHandler(ginkgo.Fail)
139122
ginkgo.RunSpecs(t, description)
140123
}
141124

125+
// functional test for container APIs.
126+
func runContainerTests(opt *option.Option) {
127+
tests.ContainerCreate(opt)
128+
tests.ContainerStart(opt)
129+
tests.ContainerStop(opt)
130+
tests.ContainerRestart(opt)
131+
tests.ContainerRemove(opt)
132+
tests.ContainerList(opt)
133+
tests.ContainerRename(opt)
134+
tests.ContainerStats(opt)
135+
tests.ContainerAttach(opt)
136+
tests.ContainerLogs(opt)
137+
tests.ContainerKill(opt)
138+
tests.ContainerInspect(opt)
139+
tests.ContainerWait(opt)
140+
tests.ContainerPause(opt)
141+
}
142+
143+
// functional test for volume APIs.
144+
func runVolumeTests(opt *option.Option) {
145+
tests.VolumeList(opt)
146+
tests.VolumeInspect(opt)
147+
tests.VolumeRemove(opt)
148+
}
149+
150+
// functional test for network APIs.
151+
func runNetworkTests(opt *option.Option, pOpt func([]string, ...option.Modifier) (*option.Option, error)) {
152+
tests.NetworkCreate(opt, pOpt)
153+
tests.NetworkRemove(opt)
154+
tests.NetworkList(opt)
155+
tests.NetworkInspect(opt)
156+
}
157+
158+
// functional test for image APIs.
159+
func runImageTests(opt *option.Option) {
160+
tests.ImageRemove(opt)
161+
tests.ImagePush(opt)
162+
tests.ImagePull(opt)
163+
}
164+
165+
// .
166+
func runSystemTests(opt *option.Option) {
167+
tests.SystemVersion(opt)
168+
tests.SystemEvents(opt)
169+
}
170+
171+
// functional test for distribution api.
172+
func runDistributionTests(opt *option.Option) {
173+
tests.DistributionInspect(opt)
174+
}
175+
142176
// parseTestFlags parses go test flags because pflag package ignores flags with '-test.' prefix
143177
// Related issues:
144178
// https://github.com/spf13/pflag/issues/63

0 commit comments

Comments
 (0)