From 909ca745d7536a3f3d87ebd24ccfc734f692ca17 Mon Sep 17 00:00:00 2001 From: Salah Aldeen Al Saleh Date: Tue, 26 Sep 2023 22:28:22 +0000 Subject: [PATCH] update tests --- pkg/operator/client/client.go | 5 +-- pkg/operator/client/client_interface.go | 2 +- pkg/operator/client/mock/mock.go | 24 ++++++------- pkg/operator/operator.go | 5 ++- pkg/operator/operator_test.go | 46 +++++++++++++++++++++---- 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/pkg/operator/client/client.go b/pkg/operator/client/client.go index 652b019b8d..59dacf3c70 100644 --- a/pkg/operator/client/client.go +++ b/pkg/operator/client/client.go @@ -124,7 +124,7 @@ func (c *Client) runAppStateMonitor() error { return errors.New("app state monitor shutdown") } -func (c *Client) RestartNamespacesInformer(namespaces []string, imagePullSecrets []string) { +func (c *Client) ApplyNamespacesInformer(namespaces []string, imagePullSecrets []string) { for _, ns := range namespaces { if ns == "*" { continue @@ -180,9 +180,6 @@ func (c *Client) DeployApp(deployArgs operatortypes.DeployAppArgs) (deployed boo } }() - c.RestartNamespacesInformer(deployArgs.AdditionalNamespaces, deployArgs.ImagePullSecrets) - c.ApplyHooksInformer(deployArgs.AdditionalNamespaces) - deployRes, deployError = c.deployManifests(deployArgs) if deployError != nil { deployRes = &deployResult{} diff --git a/pkg/operator/client/client_interface.go b/pkg/operator/client/client_interface.go index b5d987ca0c..19af0c3e25 100644 --- a/pkg/operator/client/client_interface.go +++ b/pkg/operator/client/client_interface.go @@ -10,6 +10,6 @@ type ClientInterface interface { DeployApp(deployArgs operatortypes.DeployAppArgs) (deployed bool, finalError error) UndeployApp(undeployArgs operatortypes.UndeployAppArgs) error ApplyAppInformers(args operatortypes.AppInformersArgs) - RestartNamespacesInformer(namespaces []string, imagePullSecrets []string) + ApplyNamespacesInformer(namespaces []string, imagePullSecrets []string) ApplyHooksInformer(namespaces []string) } diff --git a/pkg/operator/client/mock/mock.go b/pkg/operator/client/mock/mock.go index f6860fac0e..1569a58bd1 100644 --- a/pkg/operator/client/mock/mock.go +++ b/pkg/operator/client/mock/mock.go @@ -58,6 +58,18 @@ func (mr *MockClientInterfaceMockRecorder) ApplyHooksInformer(namespaces interfa return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyHooksInformer", reflect.TypeOf((*MockClientInterface)(nil).ApplyHooksInformer), namespaces) } +// ApplyNamespacesInformer mocks base method. +func (m *MockClientInterface) ApplyNamespacesInformer(namespaces, imagePullSecrets []string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "ApplyNamespacesInformer", namespaces, imagePullSecrets) +} + +// ApplyNamespacesInformer indicates an expected call of ApplyNamespacesInformer. +func (mr *MockClientInterfaceMockRecorder) ApplyNamespacesInformer(namespaces, imagePullSecrets interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyNamespacesInformer", reflect.TypeOf((*MockClientInterface)(nil).ApplyNamespacesInformer), namespaces, imagePullSecrets) +} + // DeployApp mocks base method. func (m *MockClientInterface) DeployApp(deployArgs types.DeployAppArgs) (bool, error) { m.ctrl.T.Helper() @@ -87,18 +99,6 @@ func (mr *MockClientInterfaceMockRecorder) Init() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockClientInterface)(nil).Init)) } -// RestartNamespacesInformer mocks base method. -func (m *MockClientInterface) RestartNamespacesInformer(namespaces, imagePullSecrets []string) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "RestartNamespacesInformer", namespaces, imagePullSecrets) -} - -// RestartNamespacesInformer indicates an expected call of RestartNamespacesInformer. -func (mr *MockClientInterfaceMockRecorder) RestartNamespacesInformer(namespaces, imagePullSecrets interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RestartNamespacesInformer", reflect.TypeOf((*MockClientInterface)(nil).RestartNamespacesInformer), namespaces, imagePullSecrets) -} - // Shutdown mocks base method. func (m *MockClientInterface) Shutdown() { m.ctrl.T.Helper() diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index dd5dbc13e4..eb0b698bc0 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -382,6 +382,9 @@ func (o *Operator) DeployApp(appID string, sequence int64) (deployed bool, deplo return false, errors.Wrap(err, "failed to apply status informers") } + o.client.ApplyNamespacesInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces, imagePullSecrets) + o.client.ApplyHooksInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces) + deployArgs := operatortypes.DeployAppArgs{ AppID: app.ID, AppSlug: app.Slug, @@ -527,7 +530,7 @@ func (o *Operator) resumeInformersForApp(app *apptypes.App) error { return errors.Wrapf(err, "failed to apply status informers for app %s", app.ID) } - o.client.RestartNamespacesInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces, imagePullSecrets) + o.client.ApplyNamespacesInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces, imagePullSecrets) o.client.ApplyHooksInformer(kotsKinds.KotsApplication.Spec.AdditionalNamespaces) return nil diff --git a/pkg/operator/operator_test.go b/pkg/operator/operator_test.go index e9981f4590..f3ebc02006 100644 --- a/pkg/operator/operator_test.go +++ b/pkg/operator/operator_test.go @@ -63,7 +63,7 @@ spec: Expect(err).ToNot(HaveOccurred()) }) - It("starts the status informers", func() { + It("resumes informers on (re)start", func() { mockClient.EXPECT().Init().Return(nil) mockStore.EXPECT().GetClusterIDFromDeployToken(clusterToken).Return("", nil) @@ -106,6 +106,16 @@ spec: wg.Done() }) + wg.Add(1) + mockClient.EXPECT().ApplyNamespacesInformer(gomock.Any(), gomock.Any()).Times(1).Do(func(namespaces []string, imagePullSecrets []string) { + wg.Done() + }) + + wg.Add(1) + mockClient.EXPECT().ApplyHooksInformer(gomock.Any()).Times(1).Do(func(namespaces []string) { + wg.Done() + }) + err := testOperator.Start() Expect(err).ToNot(HaveOccurred()) @@ -169,6 +179,16 @@ spec: wg.Done() }) + wg.Add(1) + mockClient.EXPECT().ApplyNamespacesInformer(gomock.Any(), gomock.Any()).Times(0).Do(func(namespaces []string, imagePullSecrets []string) { + wg.Done() + }) + + wg.Add(1) + mockClient.EXPECT().ApplyHooksInformer(gomock.Any()).Times(0).Do(func(namespaces []string) { + wg.Done() + }) + err := testOperator.Start() Expect(err).ToNot(HaveOccurred()) @@ -299,9 +319,13 @@ spec: mockStore.EXPECT().GetPreviouslyDeployedSequence(appID, "").Return(previouslyDeployedSequence, nil) - mockClient.EXPECT().DeployApp(gomock.Any()).Return(true, nil) + mockClient.EXPECT().ApplyAppInformers(gomock.Any()).Times(1) + + mockClient.EXPECT().ApplyNamespacesInformer(gomock.Any(), gomock.Any()).Times(1) - mockClient.EXPECT().ApplyAppInformers(gomock.Any()) + mockClient.EXPECT().ApplyHooksInformer(gomock.Any()).Times(1) + + mockClient.EXPECT().DeployApp(gomock.Any()).Return(true, nil) deployed, err := testOperator.DeployApp(appID, sequence) Expect(err).ToNot(HaveOccurred()) @@ -390,13 +414,17 @@ spec: mockStore.EXPECT().GetParentSequenceForSequence(appID, "", previouslyDeployedSequence).Return(int64(0), nil) + mockClient.EXPECT().ApplyAppInformers(gomock.Any()).Times(1) + + mockClient.EXPECT().ApplyNamespacesInformer(gomock.Any(), gomock.Any()).Times(1) + + mockClient.EXPECT().ApplyHooksInformer(gomock.Any()).Times(1) + mockClient.EXPECT().DeployApp(gomock.Any()).DoAndReturn(func(deployArgs operatortypes.DeployAppArgs) (bool, error) { Expect(deployArgs.PreviousManifests).To(BeEmpty()) return true, nil }) - mockClient.EXPECT().ApplyAppInformers(gomock.Any()) - deployed, err := testOperator.DeployApp(appID, sequence) Expect(err).ToNot(HaveOccurred()) Expect(deployed).To(BeTrue()) @@ -596,6 +624,12 @@ spec: mockStore.EXPECT().GetPreviouslyDeployedSequence(appID, "").Return(previouslyDeployedSequence, nil) + mockClient.EXPECT().ApplyAppInformers(gomock.Any()).Times(1) + + mockClient.EXPECT().ApplyNamespacesInformer(gomock.Any(), gomock.Any()).Times(1) + + mockClient.EXPECT().ApplyHooksInformer(gomock.Any()).Times(1) + mockClient.EXPECT().DeployApp(gomock.Any()).Do(func(deployArgs operatortypes.DeployAppArgs) (bool, error) { // validate that the namespace and helm upgrade flags are templated when deploying Expect(deployArgs.KotsKinds.V1Beta1HelmCharts.Items[0].Spec.Namespace).To(Equal(expectedNamespace)) @@ -605,8 +639,6 @@ spec: return true, nil }) - mockClient.EXPECT().ApplyAppInformers(gomock.Any()) - _, err := testOperator.DeployApp(appID, sequence) Expect(err).ToNot(HaveOccurred()) })