Skip to content

Commit d8a6c4a

Browse files
authored
test: add flags for augmenting commands which need to be run in the daemon-context to enable running tests from remote machines (runfinch#203)
* test: add flags for augmenting commands which need to be run in the daemon-context to enable running tests from remote machines Signed-off-by: Justin Alvarez <[email protected]> * fix copyright linting Signed-off-by: Justin Alvarez <[email protected]> --------- Signed-off-by: Justin Alvarez <[email protected]>
1 parent 49cdd07 commit d8a6c4a

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

e2e/e2e_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package e2e
55

66
import (
77
"flag"
8+
"log"
89
"os"
10+
"strings"
911
"testing"
1012

1113
"github.com/onsi/ginkgo/v2"
@@ -14,15 +16,24 @@ import (
1416
"github.com/runfinch/common-tests/option"
1517

1618
"github.com/runfinch/finch-daemon/e2e/tests"
19+
"github.com/runfinch/finch-daemon/e2e/util"
1720
)
1821

1922
// Subject defines which CLI the tests are run against, defaults to \"nerdctl\" in the user's PATH.
2023
var Subject = flag.String("subject", "nerdctl", `which CLI the tests are run against, defaults to "nerdctl" in the user's PATH.`)
24+
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.`)
25+
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"`)
2126

2227
func TestRun(t *testing.T) {
2328
if os.Getenv("TEST_E2E") != "1" {
2429
t.Skip("E2E tests skipped. Set TEST_E2E=1 to run these tests")
2530
}
31+
32+
if err := parseTestFlags(); err != nil {
33+
log.Println("failed to parse go test flags", err)
34+
os.Exit(1)
35+
}
36+
2637
opt, _ := option.New([]string{*Subject, "--namespace", "finch"})
2738

2839
ginkgo.SynchronizedBeforeSuite(func() []byte {
@@ -36,6 +47,15 @@ func TestRun(t *testing.T) {
3647
command.RemoveAll(opt)
3748
}, func() {})
3849

50+
var pOpt = option.New
51+
if *SubjectPrefix != "" {
52+
var modifiers []option.Modifier
53+
if *PrefixedSubjectEnv != "" {
54+
modifiers = append(modifiers, option.Env(strings.Split(*PrefixedSubjectEnv, " ")))
55+
}
56+
pOpt = util.WrappedOption(strings.Split(*SubjectPrefix, " "), modifiers...)
57+
}
58+
3959
const description = "Finch Daemon Functional test"
4060
ginkgo.Describe(description, func() {
4161
// functional test for container APIs
@@ -60,7 +80,7 @@ func TestRun(t *testing.T) {
6080
tests.VolumeRemove(opt)
6181

6282
// functional test for network APIs
63-
tests.NetworkCreate(opt)
83+
tests.NetworkCreate(opt, pOpt)
6484
tests.NetworkRemove(opt)
6585
tests.NetworkList(opt)
6686
tests.NetworkInspect(opt)
@@ -81,3 +101,17 @@ func TestRun(t *testing.T) {
81101
gomega.RegisterFailHandler(ginkgo.Fail)
82102
ginkgo.RunSpecs(t, description)
83103
}
104+
105+
// parseTestFlags parses go test flags because pflag package ignores flags with '-test.' prefix
106+
// Related issues:
107+
// https://github.com/spf13/pflag/issues/63
108+
// https://github.com/spf13/pflag/issues/238
109+
func parseTestFlags() error {
110+
var testFlags []string
111+
for _, f := range os.Args[1:] {
112+
if strings.HasPrefix(f, "-test.") {
113+
testFlags = append(testFlags, f)
114+
}
115+
}
116+
return flag.CommandLine.Parse(testFlags)
117+
}

e2e/tests/network_create.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717

1818
"github.com/runfinch/finch-daemon/api/types"
1919
"github.com/runfinch/finch-daemon/e2e/client"
20+
"github.com/runfinch/finch-daemon/e2e/util"
2021
)
2122

22-
func NetworkCreate(opt *option.Option) {
23+
func NetworkCreate(opt *option.Option, pOpt util.NewOpt) {
2324
Describe("create a network", func() {
2425
const (
2526
path = "/networks/create"
@@ -202,7 +203,7 @@ func NetworkCreate(opt *option.Option) {
202203
Expect(stdout).To(ContainSubstring(`"finch.network.bridge.enable_icc.ipv4": "false"`))
203204

204205
// check iptables rules exists
205-
iptOpt, _ := option.New([]string{"iptables"})
206+
iptOpt, _ := pOpt([]string{"iptables"})
206207
command.Run(iptOpt, "-C", "FINCH-ISOLATE-CHAIN",
207208
"-i", testBridge, "-o", testBridge, "-j", "DROP")
208209
})
@@ -226,7 +227,7 @@ func NetworkCreate(opt *option.Option) {
226227
Expect(stdout).ShouldNot(ContainSubstring(`"finch.network.bridge.enable_icc.ipv4"`))
227228

228229
// check iptables rules does not exist
229-
iptOpt, _ := option.New([]string{"iptables"})
230+
iptOpt, _ := pOpt([]string{"iptables"})
230231
command.RunWithoutSuccessfulExit(iptOpt, "-C", "FINCH-ISOLATE-CHAIN",
231232
"-i", testBridge, "-o", testBridge, "-j", "DROP")
232233
})

e2e/util/option_wrapper.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package testutil holds code that may be useful for any of the e2e subpackages (including e2e itself).
5+
// It is useful to avoid import loops between the various e2e test pacakges.
6+
package util
7+
8+
import (
9+
"github.com/runfinch/common-tests/option"
10+
)
11+
12+
// NewOpt is a helper to make it easier for functions to accept wrapped option creators.
13+
type NewOpt func(subject []string, modifiers ...option.Modifier) (*option.Option, error)
14+
15+
// WrappedOption allows injection of new prefixed option creator function into tests.
16+
// This is useful for scenarios where CLI commands must be run in an environment which is
17+
// not the same as the system running the tests, like inside a SSH shell.
18+
func WrappedOption(prefix []string, wModifiers ...option.Modifier) NewOpt {
19+
return func(subject []string, modifiers ...option.Modifier) (*option.Option, error) {
20+
prefix = append(prefix, subject...)
21+
return option.New(prefix, append(wModifiers, modifiers...)...)
22+
}
23+
}

0 commit comments

Comments
 (0)