Skip to content

Commit

Permalink
Fixed tests and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
slaskawi committed Oct 14, 2024
1 parent 50c1796 commit 56ab5ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
26 changes: 2 additions & 24 deletions test/e2e/util/mongotester/mongotester.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (m *Tester) HasKeyfileAuth(tries int, opts ...OptionApplier) func(t *testin
}

func (m *Tester) HasFCV(fcv string, tries int, opts ...OptionApplier) func(t *testing.T) {
return m.hasAdminParameter("featureCompatibilityVersion", bson.D{{"version", fcv}}, tries, opts...)
return m.hasAdminParameter("featureCompatibilityVersion", bson.D{{Key: "version", Value: fcv}}, tries, opts...)
}

func (m *Tester) ScramIsConfigured(tries int, opts ...OptionApplier) func(t *testing.T) {
Expand Down Expand Up @@ -168,7 +168,7 @@ func (m *Tester) VerifyRoles(expectedRoles []automationconfig.CustomRole, tries
t.Fatal(err)
return false
}
assert.ElementsMatch(t, result.Roles, expectedRoles)
assert.Subset(t, result.Roles, expectedRoles)
return true
}, tries, opts...)
}
Expand Down Expand Up @@ -422,28 +422,6 @@ func (c clientOptionAdder) ApplyOption(opts ...options.Lister[options.ClientOpti
return append(opts, c.option)
}

// clientOptionRemover is used if a value from the client array of options should be removed.
// assigning a nil value will not take precedence over an existing value, so we need a mechanism
// to remove elements that are present

// e.g. to disable TLS, you need to remove the options.ClientOption that has a non-nil tls config
// it is not enough to add a tls config that has a nil value.
type clientOptionRemover struct {
// removalPredicate is a function which returns a bool indicating
// if a given options.ClientOption should be removed.
removalPredicate func(opt *options.ClientOptions) bool
}

func (c clientOptionRemover) ApplyOption(opts ...*options.ClientOptions) []*options.ClientOptions {
newOpts := make([]*options.ClientOptions, 0)
for _, opt := range opts {
if !c.removalPredicate(opt) {
newOpts = append(newOpts, opt)
}
}
return newOpts
}

// WithScram provides a configuration option that will configure the MongoDB resource
// with the given username and password
func WithScram(username, password string) OptionApplier {
Expand Down
14 changes: 12 additions & 2 deletions test/e2e/util/mongotester/mongotester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ func TestTlsRemoval_RemovesCorrectConfig(t *testing.T) {
// remove the tls value
opts = removalOpt.ApplyOption(opts...)

var errs []error
raw := options.ClientOptions{}
for _, opt := range opts {
for _, fn := range opt.List() {
fn(&raw)
err := fn(&raw)
if err != nil {
errs = append(errs, err)
}
}
}

assert.Len(t, errs, 0)
assert.Len(t, opts, 3, "tls removal should remove an element")
assert.NotNil(t, raw.Hosts, "tls removal should not effect other configs")
assert.Len(t, raw.Hosts, 3, "original configs should not be changed")
Expand All @@ -39,12 +44,17 @@ func TestWithScram_AddsScramOption(t *testing.T) {
opts := WithScram("username", "password").ApplyOption()

raw := options.ClientOptions{}
var errs []error
for _, opt := range opts {
for _, fn := range opt.List() {
fn(&raw)
err := fn(&raw)
if err != nil {
errs = append(errs, err)
}
}
}

assert.Len(t, errs, 0)
assert.Len(t, opts, 1)
assert.NotNil(t, opts)
assert.Equal(t, raw.Auth.AuthMechanism, "SCRAM-SHA-256")
Expand Down

0 comments on commit 56ab5ed

Please sign in to comment.