Use existing clock test double to get kubecertagent units passing
Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
parent
eb0d9a15fc
commit
a55e9de4fc
@ -9,6 +9,7 @@ import (
|
|||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/clock"
|
||||||
corev1informers "k8s.io/client-go/informers/core/v1"
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
@ -21,28 +22,13 @@ import (
|
|||||||
"go.pinniped.dev/internal/provider"
|
"go.pinniped.dev/internal/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CurrentTimeProvider interface {
|
|
||||||
Now() metav1.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
type currentTimeProvider struct{}
|
|
||||||
|
|
||||||
// TODO use this as the currentTimeProvider argument when calling NewExecerController() from prepare_controllers.go.
|
|
||||||
func NewCurrentTimeProvider() CurrentTimeProvider {
|
|
||||||
return ¤tTimeProvider{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *currentTimeProvider) Now() metav1.Time {
|
|
||||||
return metav1.Now()
|
|
||||||
}
|
|
||||||
|
|
||||||
type execerController struct {
|
type execerController struct {
|
||||||
agentInfo *Info
|
agentInfo *Info
|
||||||
credentialIssuerConfigNamespaceName string
|
credentialIssuerConfigNamespaceName string
|
||||||
credentialIssuerConfigResourceName string
|
credentialIssuerConfigResourceName string
|
||||||
dynamicCertProvider provider.DynamicTLSServingCertProvider
|
dynamicCertProvider provider.DynamicTLSServingCertProvider
|
||||||
podCommandExecutor kubecertauthority.PodCommandExecutor
|
podCommandExecutor kubecertauthority.PodCommandExecutor
|
||||||
currentTimeProvider CurrentTimeProvider
|
clock clock.Clock
|
||||||
pinnipedAPIClient pinnipedclientset.Interface
|
pinnipedAPIClient pinnipedclientset.Interface
|
||||||
agentPodInformer corev1informers.PodInformer
|
agentPodInformer corev1informers.PodInformer
|
||||||
}
|
}
|
||||||
@ -54,7 +40,7 @@ func NewExecerController(
|
|||||||
dynamicCertProvider provider.DynamicTLSServingCertProvider,
|
dynamicCertProvider provider.DynamicTLSServingCertProvider,
|
||||||
podCommandExecutor kubecertauthority.PodCommandExecutor,
|
podCommandExecutor kubecertauthority.PodCommandExecutor,
|
||||||
pinnipedAPIClient pinnipedclientset.Interface,
|
pinnipedAPIClient pinnipedclientset.Interface,
|
||||||
currentTimeProvider CurrentTimeProvider,
|
clock clock.Clock,
|
||||||
agentPodInformer corev1informers.PodInformer,
|
agentPodInformer corev1informers.PodInformer,
|
||||||
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
||||||
) controllerlib.Controller {
|
) controllerlib.Controller {
|
||||||
@ -67,8 +53,8 @@ func NewExecerController(
|
|||||||
credentialIssuerConfigResourceName: credentialIssuerConfigResourceName,
|
credentialIssuerConfigResourceName: credentialIssuerConfigResourceName,
|
||||||
dynamicCertProvider: dynamicCertProvider,
|
dynamicCertProvider: dynamicCertProvider,
|
||||||
podCommandExecutor: podCommandExecutor,
|
podCommandExecutor: podCommandExecutor,
|
||||||
currentTimeProvider: currentTimeProvider,
|
|
||||||
pinnipedAPIClient: pinnipedAPIClient,
|
pinnipedAPIClient: pinnipedAPIClient,
|
||||||
|
clock: clock,
|
||||||
agentPodInformer: agentPodInformer,
|
agentPodInformer: agentPodInformer,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -145,7 +131,7 @@ func (c *execerController) strategySuccess() configv1alpha1.CredentialIssuerConf
|
|||||||
Status: configv1alpha1.SuccessStrategyStatus,
|
Status: configv1alpha1.SuccessStrategyStatus,
|
||||||
Reason: configv1alpha1.FetchedKeyStrategyReason,
|
Reason: configv1alpha1.FetchedKeyStrategyReason,
|
||||||
Message: "Key was fetched successfully",
|
Message: "Key was fetched successfully",
|
||||||
LastUpdateTime: c.currentTimeProvider.Now(),
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +141,7 @@ func (c *execerController) strategyError(err error) configv1alpha1.CredentialIss
|
|||||||
Status: configv1alpha1.ErrorStrategyStatus,
|
Status: configv1alpha1.ErrorStrategyStatus,
|
||||||
Reason: configv1alpha1.CouldNotFetchKeyStrategyReason,
|
Reason: configv1alpha1.CouldNotFetchKeyStrategyReason,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
LastUpdateTime: c.currentTimeProvider.Now(),
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/apimachinery/pkg/util/clock"
|
||||||
kubeinformers "k8s.io/client-go/informers"
|
kubeinformers "k8s.io/client-go/informers"
|
||||||
kubernetesfake "k8s.io/client-go/kubernetes/fake"
|
kubernetesfake "k8s.io/client-go/kubernetes/fake"
|
||||||
coretesting "k8s.io/client-go/testing"
|
coretesting "k8s.io/client-go/testing"
|
||||||
@ -27,18 +28,6 @@ import (
|
|||||||
"go.pinniped.dev/internal/testutil"
|
"go.pinniped.dev/internal/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeCurrentTimeProvider struct {
|
|
||||||
frozenNow *metav1.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeCurrentTimeProvider) Now() metav1.Time {
|
|
||||||
if f.frozenNow == nil {
|
|
||||||
realNow := metav1.Now()
|
|
||||||
f.frozenNow = &realNow
|
|
||||||
}
|
|
||||||
return *f.frozenNow
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExecerControllerOptions(t *testing.T) {
|
func TestExecerControllerOptions(t *testing.T) {
|
||||||
spec.Run(t, "options", func(t *testing.T, when spec.G, it spec.S) {
|
spec.Run(t, "options", func(t *testing.T, when spec.G, it spec.S) {
|
||||||
var r *require.Assertions
|
var r *require.Assertions
|
||||||
@ -68,10 +57,10 @@ func TestExecerControllerOptions(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"credentialIssuerConfigNamespaceName",
|
"credentialIssuerConfigNamespaceName",
|
||||||
"credentialIssuerConfigResourceName",
|
"credentialIssuerConfigResourceName",
|
||||||
nil, // not needed for this test
|
nil, // dynamicCertProvider, not needed for this test
|
||||||
nil, // not needed for this test
|
nil, // podCommandExecutor, not needed for this test
|
||||||
nil, // not needed for this test
|
nil, // pinnipedAPIClient, not needed for this test
|
||||||
&fakeCurrentTimeProvider{},
|
nil, // clock, not needed for this test
|
||||||
agentPodsInformer,
|
agentPodsInformer,
|
||||||
observableWithInformerOption.WithInformer,
|
observableWithInformerOption.WithInformer,
|
||||||
)
|
)
|
||||||
@ -174,8 +163,8 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
var agentPodTemplate *corev1.Pod
|
var agentPodTemplate *corev1.Pod
|
||||||
var dynamicCertProvider provider.DynamicTLSServingCertProvider
|
var dynamicCertProvider provider.DynamicTLSServingCertProvider
|
||||||
var fakeCertPEM, fakeKeyPEM string
|
var fakeCertPEM, fakeKeyPEM string
|
||||||
var fakeNow *fakeCurrentTimeProvider
|
|
||||||
var credentialIssuerConfigGVR schema.GroupVersionResource
|
var credentialIssuerConfigGVR schema.GroupVersionResource
|
||||||
|
var frozenNow time.Time
|
||||||
|
|
||||||
// Defer starting the informers until the last possible moment so that the
|
// Defer starting the informers until the last possible moment so that the
|
||||||
// nested Before's can keep adding things to the informer caches.
|
// nested Before's can keep adding things to the informer caches.
|
||||||
@ -192,7 +181,7 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
dynamicCertProvider,
|
dynamicCertProvider,
|
||||||
fakeExecutor,
|
fakeExecutor,
|
||||||
pinnipedAPIClient,
|
pinnipedAPIClient,
|
||||||
fakeNow,
|
clock.NewFakeClock(frozenNow),
|
||||||
agentPodInformer.Core().V1().Pods(),
|
agentPodInformer.Core().V1().Pods(),
|
||||||
controllerlib.WithInformer,
|
controllerlib.WithInformer,
|
||||||
)
|
)
|
||||||
@ -251,8 +240,7 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
agentPodInformerClient = kubernetesfake.NewSimpleClientset()
|
agentPodInformerClient = kubernetesfake.NewSimpleClientset()
|
||||||
agentPodInformer = kubeinformers.NewSharedInformerFactory(agentPodInformerClient, 0)
|
agentPodInformer = kubeinformers.NewSharedInformerFactory(agentPodInformerClient, 0)
|
||||||
fakeExecutor = &fakePodExecutor{r: r}
|
fakeExecutor = &fakePodExecutor{r: r}
|
||||||
fakeNow = &fakeCurrentTimeProvider{}
|
frozenNow = time.Date(2020, time.September, 23, 7, 42, 0, 0, time.Local)
|
||||||
fakeNow.Now() // call once to initialize
|
|
||||||
dynamicCertProvider = provider.NewDynamicTLSServingCertProvider()
|
dynamicCertProvider = provider.NewDynamicTLSServingCertProvider()
|
||||||
dynamicCertProvider.Set([]byte(defaultDynamicCertProviderCert), []byte(defaultDynamicCertProviderKey))
|
dynamicCertProvider.Set([]byte(defaultDynamicCertProviderCert), []byte(defaultDynamicCertProviderKey))
|
||||||
|
|
||||||
@ -402,7 +390,7 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
Status: configv1alpha1.SuccessStrategyStatus,
|
Status: configv1alpha1.SuccessStrategyStatus,
|
||||||
Reason: configv1alpha1.FetchedKeyStrategyReason,
|
Reason: configv1alpha1.FetchedKeyStrategyReason,
|
||||||
Message: "Key was fetched successfully",
|
Message: "Key was fetched successfully",
|
||||||
LastUpdateTime: fakeNow.Now(),
|
LastUpdateTime: metav1.NewTime(frozenNow),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
expectedGetAction := coretesting.NewGetAction(credentialIssuerConfigGVR, credentialIssuerConfigNamespaceName, credentialIssuerConfigResourceName)
|
expectedGetAction := coretesting.NewGetAction(credentialIssuerConfigGVR, credentialIssuerConfigNamespaceName, credentialIssuerConfigResourceName)
|
||||||
@ -432,7 +420,7 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
Status: configv1alpha1.SuccessStrategyStatus,
|
Status: configv1alpha1.SuccessStrategyStatus,
|
||||||
Reason: configv1alpha1.FetchedKeyStrategyReason,
|
Reason: configv1alpha1.FetchedKeyStrategyReason,
|
||||||
Message: "Key was fetched successfully",
|
Message: "Key was fetched successfully",
|
||||||
LastUpdateTime: fakeNow.Now(),
|
LastUpdateTime: metav1.NewTime(frozenNow),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -475,7 +463,7 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
Status: configv1alpha1.ErrorStrategyStatus,
|
Status: configv1alpha1.ErrorStrategyStatus,
|
||||||
Reason: configv1alpha1.CouldNotFetchKeyStrategyReason,
|
Reason: configv1alpha1.CouldNotFetchKeyStrategyReason,
|
||||||
Message: podExecErrorMessage,
|
Message: podExecErrorMessage,
|
||||||
LastUpdateTime: metav1.Now(),
|
LastUpdateTime: metav1.NewTime(frozenNow),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -517,7 +505,7 @@ func TestManagerControllerSync(t *testing.T) {
|
|||||||
Status: configv1alpha1.ErrorStrategyStatus,
|
Status: configv1alpha1.ErrorStrategyStatus,
|
||||||
Reason: configv1alpha1.CouldNotFetchKeyStrategyReason,
|
Reason: configv1alpha1.CouldNotFetchKeyStrategyReason,
|
||||||
Message: podExecErrorMessage,
|
Message: podExecErrorMessage,
|
||||||
LastUpdateTime: metav1.Now(),
|
LastUpdateTime: metav1.NewTime(frozenNow),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user