Fix some unit tests

This commit is contained in:
Joshua Casey
2024-08-16 17:15:04 -05:00
parent c12402ee49
commit 504f0dc26f
5 changed files with 45 additions and 6 deletions

View File

@@ -20,7 +20,7 @@ func TestEnableAndDisableKubeFeatureGate(t *testing.T) {
require.True(t, feature.DefaultFeatureGate.Enabled(f))
// Set it back to its default value of true at the end of this test.
defer featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, f, true)()
featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, f, true)
EnableKubeFeatureGate(f)
require.True(t, feature.DefaultFeatureGate.Enabled(f))

View File

@@ -2300,11 +2300,12 @@ func TestController(t *testing.T) {
require.NoError(t, err)
}
if !assert.ElementsMatch(t, tt.wantActions(), pinnipedAPIClient.Actions()) {
scrubbedActions := testutil.ScrubListOptionsForActions(t, pinnipedAPIClient.Actions())
if !assert.ElementsMatch(t, tt.wantActions(), scrubbedActions) {
// cmp.Diff is superior to require.ElementsMatch in terms of readability here.
// require.ElementsMatch will handle pointers better than require.Equal, but
// the timestamps are still incredibly verbose.
require.Fail(t, cmp.Diff(tt.wantActions(), pinnipedAPIClient.Actions()), "actions should be exactly the expected number of actions and also contain the correct resources")
require.Fail(t, cmp.Diff(tt.wantActions(), scrubbedActions), "actions should be exactly the expected number of actions and also contain the correct resources")
}
require.Equal(t, len(tt.wantNamesOfJWTAuthenticatorsInCache), len(cache.Keys()), fmt.Sprintf("expected cache entries is incorrect. wanted:%d, got: %d, keys: %v", len(tt.wantNamesOfJWTAuthenticatorsInCache), len(cache.Keys()), cache.Keys()))

View File

@@ -1952,7 +1952,7 @@ func TestController(t *testing.T) {
}
require.NotEmpty(t, tt.wantActions, "wantActions is required for test %s", tt.name)
require.Equal(t, tt.wantActions(), pinnipedAPIClient.Actions())
require.Equal(t, tt.wantActions(), testutil.ScrubListOptionsForActions(t, pinnipedAPIClient.Actions()))
require.Equal(t, len(tt.wantNamesOfWebhookAuthenticatorsInCache), len(cache.Keys()), fmt.Sprintf("expected cache entries is incorrect. wanted:%d, got: %d, keys: %v", len(tt.wantNamesOfWebhookAuthenticatorsInCache), len(cache.Keys()), cache.Keys()))
actualLog, _ := strings.CutSuffix(log.String(), "\n")

View File

@@ -2596,7 +2596,7 @@ func TestController(t *testing.T) {
}
// This List action is coming from the test code when it pulls the GitHubIdentityProviders from K8s
wantActions[len(wantActions)-1] = coretesting.NewListAction(githubIDPGVR, githubIDPKind, namespace, metav1.ListOptions{})
require.Equal(t, wantActions, fakeSupervisorClient.Actions())
require.Equal(t, wantActions, testutil.ScrubListOptionsForActions(t, fakeSupervisorClient.Actions()))
})
}
}
@@ -2901,7 +2901,7 @@ func TestController_OnlyWantActions(t *testing.T) {
} else {
require.NoError(t, err)
}
require.Equal(t, tt.wantActions, fakeSupervisorClient.Actions())
require.Equal(t, tt.wantActions, testutil.ScrubListOptionsForActions(t, fakeSupervisorClient.Actions()))
})
}
}

View File

@@ -0,0 +1,38 @@
// Copyright 2024 the Pinniped contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package testutil
import (
"testing"
"github.com/stretchr/testify/require"
coretesting "k8s.io/client-go/testing"
)
// ScrubListOptionsForActions ignores certain aspects of Watch Actions which changed in K8s 1.31.
// Because of https://github.com/kubernetes/kubernetes/pull/125560 our test code is busted.
func ScrubListOptionsForActions(t *testing.T, actions []coretesting.Action) []coretesting.Action {
t.Helper()
scrubbedActions := make([]coretesting.Action, 0, len(actions))
for _, action := range actions {
if action.GetVerb() == "watch" {
watchAction, ok := action.(coretesting.WatchActionImpl)
require.True(t, ok)
watchAction.ListOptions.AllowWatchBookmarks = false
watchAction.ListOptions.TimeoutSeconds = nil
scrubbedActions = append(scrubbedActions, watchAction)
} else if action.GetVerb() == "list" {
listAction, ok := action.(coretesting.ListActionImpl)
require.True(t, ok)
listAction.ListOptions.ResourceVersion = ""
listAction.ListOptions.TimeoutSeconds = nil
listAction.ListOptions.Limit = 0
scrubbedActions = append(scrubbedActions, listAction)
} else {
scrubbedActions = append(scrubbedActions, action)
}
}
return scrubbedActions
}