Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connectivity: Support multiple conditions #2732

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions connectivity/check/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewTest(name string, verbose bool, debug bool) *Test {
cegps: make(map[string]*ciliumv2.CiliumEgressGatewayPolicy),
clrps: make(map[string]*ciliumv2.CiliumLocalRedirectPolicy),
logBuf: &bytes.Buffer{}, // maintain internal buffer by default
conditionFn: func() bool { return true },
conditionFn: nil,
}
// Setting the internal buffer to nil causes the logger to
// write directly to stdout in verbose or debug mode.
Expand Down Expand Up @@ -153,7 +153,7 @@ type Test struct {
// conditionFn is a function that returns true if the test needs to run,
// and false otherwise. By default, it's set to a function that returns
// true.
conditionFn func() bool
conditionFn []func() bool

// List of functions to be called when Run() returns.
finalizers []func(ctx context.Context) error
Expand Down Expand Up @@ -252,6 +252,15 @@ func (t *Test) versionInRange(version semver.Version) (bool, string) {
return true, "running version within range"
}

func (t *Test) checkConditions() bool {
for _, fn := range t.conditionFn {
if !fn() {
return false
}
}
return true
}

// willRun returns false if all of the Test's Scenarios are skipped by the user,
// if any of its FeatureRequirements are not met, or if the running Cilium
// version is not within range of the one specified using WithCiliumVersion.
Expand All @@ -262,7 +271,7 @@ func (t *Test) versionInRange(version semver.Version) (bool, string) {
// excluding tests, they're most likely interested in other reasons why their
// test is not being executed.
func (t *Test) willRun() (bool, string) {
if !t.conditionFn() {
if !t.checkConditions() {
return false, "skipped by condition"
}

Expand Down Expand Up @@ -428,9 +437,11 @@ func configureNamespaceInPolicySpec(spec *api.Rule, namespace string) {
}

// WithCondition takes a function containing condition check logic that
// returns true if the test needs to be run, and false otherwise.
// returns true if the test needs to be run, and false otherwise. If
// WithCondition gets called multiple times, all the conditions need to be
// satisfied for the test to run.
func (t *Test) WithCondition(fn func() bool) *Test {
t.conditionFn = fn
t.conditionFn = append(t.conditionFn, fn)
return t
}

Expand Down
25 changes: 25 additions & 0 deletions connectivity/check/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"github.com/cilium/cilium-cli/utils/features"
)

Expand Down Expand Up @@ -52,3 +54,26 @@ func TestWithFeatureRequirements(t *testing.T) {
})
}
}

func TestWithCondition(t *testing.T) {
mytest := NewTest("my-test", false, false)
assert.True(t, mytest.checkConditions())

mytest = NewTest("my-test", false, false).
WithCondition(func() bool { return true })
assert.True(t, mytest.checkConditions())

mytest = NewTest("my-test", false, false).
WithCondition(func() bool { return false })
assert.False(t, mytest.checkConditions())

mytest = NewTest("my-test", false, false).
WithCondition(func() bool { return true }).
WithCondition(func() bool { return false })
assert.False(t, mytest.checkConditions())

mytest = NewTest("my-test", false, false).
WithCondition(func() bool { return false }).
WithCondition(func() bool { return true })
assert.False(t, mytest.checkConditions())
}
Loading