From 23536abb24f52b985055088d8fc7d48671806a76 Mon Sep 17 00:00:00 2001 From: ajanikow <12255597+ajanikow@users.noreply.github.com> Date: Thu, 23 Nov 2023 13:11:25 +0000 Subject: [PATCH] [Feature] Improve K8S Mock for UT --- CHANGELOG.md | 1 + .../ml/v1alpha1/extension_status_metadata.go | 33 +++ pkg/handlers/backup/backup_suite_test.go | 57 +---- pkg/handlers/backup/finalizer_test.go | 29 +-- pkg/handlers/backup/flow_test.go | 13 +- pkg/handlers/backup/handler_test.go | 4 +- pkg/handlers/backup/state_create_test.go | 25 +- pkg/handlers/backup/state_createerror_test.go | 17 +- pkg/handlers/backup/state_creating_test.go | 13 +- pkg/handlers/backup/state_deleted_test.go | 9 +- pkg/handlers/backup/state_download_test.go | 13 +- .../backup/state_downloaderror_test.go | 9 +- pkg/handlers/backup/state_downloading_test.go | 25 +- pkg/handlers/backup/state_failed_test.go | 5 +- pkg/handlers/backup/state_none_test.go | 5 +- pkg/handlers/backup/state_pending_test.go | 30 +-- pkg/handlers/backup/state_ready_test.go | 76 +++--- pkg/handlers/backup/state_scheduled_test.go | 13 +- pkg/handlers/backup/state_unavailable_test.go | 29 +-- pkg/handlers/backup/state_upload_test.go | 33 +-- pkg/handlers/backup/state_uploaderror_test.go | 13 +- pkg/handlers/backup/state_uploading_test.go | 33 +-- pkg/util/tests/gen.go | 59 ----- pkg/util/tests/kubernetes.go | 224 ++++++++++++++++++ pkg/util/tests/kubernetes_test.go | 56 +++++ 25 files changed, 533 insertions(+), 291 deletions(-) create mode 100644 pkg/apis/ml/v1alpha1/extension_status_metadata.go delete mode 100644 pkg/util/tests/gen.go create mode 100644 pkg/util/tests/kubernetes.go create mode 100644 pkg/util/tests/kubernetes_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 219f0c481..a53b81c3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - (Bugfix) Fix Early Connections for 3.10+ - (Maintenance) yamlfmt as CI Step - (Maintenance) Expose Context in OperatorV2 Item Handler +- (Feature) Improve K8S Mock for UT ## [1.2.35](https://github.com/arangodb/kube-arangodb/tree/1.2.35) (2023-11-06) - (Maintenance) Update go-driver to v1.6.0, update IsNotFound() checks diff --git a/pkg/apis/ml/v1alpha1/extension_status_metadata.go b/pkg/apis/ml/v1alpha1/extension_status_metadata.go new file mode 100644 index 000000000..beda764a0 --- /dev/null +++ b/pkg/apis/ml/v1alpha1/extension_status_metadata.go @@ -0,0 +1,33 @@ +// +// DISCLAIMER +// +// Copyright 2023 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package v1alpha1 + +type ArangoMLExtensionStatusMetadataService struct { + // Secret keeps details about secret with Metadata Service details + Secret *ArangoMLExtensionStatusMetadataSecret `json:"secret,omitempty"` +} + +type ArangoMLExtensionStatusMetadataSecret struct { + // Name specify the Secret Name + Name string `json:"name"` + // Namespace specify the Secret Namespace + Namespace *string `json:"namespace,omitempty"` +} diff --git a/pkg/handlers/backup/backup_suite_test.go b/pkg/handlers/backup/backup_suite_test.go index 2f72d0030..61c8c733e 100644 --- a/pkg/handlers/backup/backup_suite_test.go +++ b/pkg/handlers/backup/backup_suite_test.go @@ -41,6 +41,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/arangodb/kube-arangodb/pkg/util/errors" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) const ( @@ -70,33 +71,16 @@ func newErrorsFakeHandler(errors mockErrorsArangoClientBackup) (*handler, *mockA } } -func newObjectSet(state state.State) (*backupApi.ArangoBackup, *database.ArangoDeployment) { +func newObjectSet(t *testing.T, state state.State) (*backupApi.ArangoBackup, *database.ArangoDeployment) { name := string(uuid.NewUUID()) namespace := string(uuid.NewUUID()) obj := newArangoBackup(name, namespace, name, state) - arangoDeployment := newArangoDeployment(namespace, name) + arangoDeployment := tests.NewMetaObject[*database.ArangoDeployment](t, namespace, name) return obj, arangoDeployment } -func newItem(o operation.Operation, namespace, name string) operation.Item { - return operation.Item{ - Group: backupApi.SchemeGroupVersion.Group, - Version: backupApi.SchemeGroupVersion.Version, - Kind: backup.ArangoBackupResourceKind, - - Operation: o, - - Namespace: namespace, - Name: name, - } -} - -func newItemFromBackup(operation operation.Operation, backup *backupApi.ArangoBackup) operation.Item { - return newItem(operation, backup.Namespace, backup.Name) -} - func newArangoBackup(objectRef, namespace, name string, state state.State) *backupApi.ArangoBackup { return &backupApi.ArangoBackup{ TypeMeta: meta.TypeMeta{ @@ -140,25 +124,6 @@ func refreshArangoBackup(t *testing.T, h *handler, backup *backupApi.ArangoBacku return obj } -func newArangoDeployment(namespace, name string) *database.ArangoDeployment { - return &database.ArangoDeployment{ - TypeMeta: meta.TypeMeta{ - APIVersion: backupApi.SchemeGroupVersion.String(), - Kind: deployment.ArangoDeploymentResourceKind, - }, - ObjectMeta: meta.ObjectMeta{ - Name: name, - Namespace: namespace, - SelfLink: fmt.Sprintf("/api/%s/%s/%s/%s", - backupApi.SchemeGroupVersion.String(), - deployment.ArangoDeploymentResourcePlural, - namespace, - name), - UID: uuid.NewUUID(), - }, - } -} - func createArangoDeployment(t *testing.T, h *handler, deployments ...*database.ArangoDeployment) { for _, deployment := range deployments { _, err := h.client.DatabaseV1().ArangoDeployments(deployment.Namespace).Create(context.Background(), deployment, meta.CreateOptions{}) @@ -186,12 +151,12 @@ func wrapperUndefinedDeployment(t *testing.T, state state.State) { // Arrange handler := newFakeHandler() - obj, _ := newObjectSet(state) + obj, _ := newObjectSet(t, state) obj.Spec.Deployment.Name = "" // Act createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -204,11 +169,11 @@ func wrapperUndefinedDeployment(t *testing.T, state state.State) { // Arrange handler := newFakeHandler() - obj, _ := newObjectSet(state) + obj, _ := newObjectSet(t, state) // Act createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -226,12 +191,12 @@ func wrapperConnectionIssues(t *testing.T, state state.State) { f := newMockArangoClientBackupErrorFactory(errors.Newf(errorString)) handler.arangoClientFactory = f - obj, deployment := newObjectSet(state) + obj, deployment := newObjectSet(t, state) // Act createArangoBackup(t, handler, obj) createArangoDeployment(t, handler, deployment) - err := handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)) + err := handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)) // Assert require.Error(t, err) @@ -248,12 +213,12 @@ func wrapperProgressMissing(t *testing.T, state state.State) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(state) + obj, deployment := newObjectSet(t, state) // Act createArangoBackup(t, handler, obj) createArangoDeployment(t, handler, deployment) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/finalizer_test.go b/pkg/handlers/backup/finalizer_test.go index c58693e52..cb4f6616f 100644 --- a/pkg/handlers/backup/finalizer_test.go +++ b/pkg/handlers/backup/finalizer_test.go @@ -30,13 +30,14 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_Finalizer_PassThru(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, _ := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, _ := newObjectSet(t, backupApi.ArangoBackupStateCreate) time := meta.Time{ Time: time.Now(), } @@ -46,7 +47,7 @@ func Test_Finalizer_PassThru(t *testing.T) { //createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Delete, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Delete, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -58,7 +59,7 @@ func Test_Finalizer_RemoveObject(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Finalizers = []string{ backupApi.FinalizerArangoBackup, } @@ -80,7 +81,7 @@ func Test_Finalizer_RemoveObject(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Delete, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Delete, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -98,7 +99,7 @@ func Test_Finalizer_RemoveObject_WithoutFinalizer(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) time := meta.Now() obj.DeletionTimestamp = &time @@ -118,7 +119,7 @@ func Test_Finalizer_RemoveObject_WithoutFinalizer(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Delete, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Delete, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -136,7 +137,7 @@ func Test_Finalizer_RemoveObject_UnknownFinalizer(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Finalizers = []string{ "UNKNOWN", } @@ -158,7 +159,7 @@ func Test_Finalizer_RemoveObject_UnknownFinalizer(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Delete, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Delete, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -176,7 +177,7 @@ func Test_Finalizer_RemoveObject_MixedFinalizers(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Finalizers = []string{ "UNKNOWN", backupApi.FinalizerArangoBackup, @@ -199,7 +200,7 @@ func Test_Finalizer_RemoveObject_MixedFinalizers(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Delete, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Delete, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -217,7 +218,7 @@ func Test_Finalizer_AddDefault(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateNone) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateNone) obj.Finalizers = nil @@ -225,7 +226,7 @@ func Test_Finalizer_AddDefault(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -237,7 +238,7 @@ func Test_Finalizer_AppendDefault(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateNone) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateNone) obj.Finalizers = []string{ "RANDOM", @@ -248,7 +249,7 @@ func Test_Finalizer_AppendDefault(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/flow_test.go b/pkg/handlers/backup/flow_test.go index e052b1066..34413c56a 100644 --- a/pkg/handlers/backup/flow_test.go +++ b/pkg/handlers/backup/flow_test.go @@ -28,13 +28,14 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_Flow_SuccessHappyPath(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateNone) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateNone) // Act createArangoDeployment(t, handler, deployment) @@ -42,7 +43,7 @@ func Test_Flow_SuccessHappyPath(t *testing.T) { t.Run("Change from None to Pending", func(t *testing.T) { // Act - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -51,7 +52,7 @@ func Test_Flow_SuccessHappyPath(t *testing.T) { t.Run("Change from Pending to Scheduled", func(t *testing.T) { // Act - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -60,7 +61,7 @@ func Test_Flow_SuccessHappyPath(t *testing.T) { t.Run("Change from Scheduled to Create", func(t *testing.T) { // Act - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -69,7 +70,7 @@ func Test_Flow_SuccessHappyPath(t *testing.T) { t.Run("Change from Create to Ready", func(t *testing.T) { // Act - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -78,7 +79,7 @@ func Test_Flow_SuccessHappyPath(t *testing.T) { t.Run("Ensure Ready State Keeps", func(t *testing.T) { // Act - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/handler_test.go b/pkg/handlers/backup/handler_test.go index 4d41d9dc5..4031e2f29 100644 --- a/pkg/handlers/backup/handler_test.go +++ b/pkg/handlers/backup/handler_test.go @@ -27,14 +27,16 @@ import ( "github.com/stretchr/testify/require" apiErrors "k8s.io/apimachinery/pkg/api/errors" + backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_ObjectNotFound(t *testing.T) { // Arrange handler := newFakeHandler() - i := newItem(operation.Add, "test", "test") + i := tests.NewItem(t, operation.Add, tests.NewMetaObject[*backupApi.ArangoBackup](t, "none", "none")) actions := map[operation.Operation]bool{ operation.Add: false, diff --git a/pkg/handlers/backup/state_create_test.go b/pkg/handlers/backup/state_create_test.go index 43f3ab99a..b9fa893ce 100644 --- a/pkg/handlers/backup/state_create_test.go +++ b/pkg/handlers/backup/state_create_test.go @@ -32,6 +32,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/deployment/features" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/arangodb/kube-arangodb/pkg/util" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Create_Common(t *testing.T) { @@ -51,13 +52,13 @@ func Test_State_Create_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreate) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -77,13 +78,13 @@ func Test_State_Create_Success_Async(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreate) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -96,7 +97,7 @@ func Test_State_Create_SuccessForced(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreate) obj.Spec.Options = &backupApi.ArangoBackupSpecOptions{ AllowInconsistent: util.NewType[bool](true), } @@ -105,7 +106,7 @@ func Test_State_Create_SuccessForced(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -128,7 +129,7 @@ func Test_State_Create_Upload(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreate) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "test", } @@ -137,7 +138,7 @@ func Test_State_Create_Upload(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -160,13 +161,13 @@ func Test_State_Create_CreateError(t *testing.T) { createError: newFatalErrorf("error"), }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreate) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -183,13 +184,13 @@ func Test_State_Create_CreateError_Async(t *testing.T) { createError: newFatalErrorf("error"), }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreate) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreate) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_createerror_test.go b/pkg/handlers/backup/state_createerror_test.go index 4d021485b..2064f2997 100644 --- a/pkg/handlers/backup/state_createerror_test.go +++ b/pkg/handlers/backup/state_createerror_test.go @@ -31,13 +31,14 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/arangodb/kube-arangodb/pkg/util" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_CreateError_Retry_WhenBackoffEnabled(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreateError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreateError) backupMeta, err := mock.Create() require.NoError(t, err) @@ -58,7 +59,7 @@ func Test_State_CreateError_Retry_WhenBackoffEnabled(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -72,7 +73,7 @@ func Test_State_CreateError_Retry_WhenBackoffDisabled_C1(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreateError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreateError) backupMeta, err := mock.Create() require.NoError(t, err) @@ -91,7 +92,7 @@ func Test_State_CreateError_Retry_WhenBackoffDisabled_C1(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -103,7 +104,7 @@ func Test_State_CreateError_Retry_WhenBackoffDisabled_C2(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreateError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreateError) backupMeta, err := mock.Create() require.NoError(t, err) @@ -120,7 +121,7 @@ func Test_State_CreateError_Retry_WhenBackoffDisabled_C2(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -132,7 +133,7 @@ func Test_State_CreateError_Transfer_To_Failed(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreateError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreateError) backupMeta, err := mock.Create() require.NoError(t, err) @@ -157,7 +158,7 @@ func Test_State_CreateError_Transfer_To_Failed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_creating_test.go b/pkg/handlers/backup/state_creating_test.go index bf32bd64b..81d4a51c5 100644 --- a/pkg/handlers/backup/state_creating_test.go +++ b/pkg/handlers/backup/state_creating_test.go @@ -31,6 +31,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Creating_Common(t *testing.T) { @@ -42,7 +43,7 @@ func Test_State_Creating_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreating) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreating) obj.Status.Progress = &backupApi.ArangoBackupProgress{ JobID: "jobID", @@ -53,7 +54,7 @@ func Test_State_Creating_Success(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("Create in progress, then done", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -65,7 +66,7 @@ func Test_State_Creating_Success(t *testing.T) { require.Equal(t, obj.Status.Progress.JobID, newObj.Status.Progress.JobID) mock.state.createDone = true - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj = refreshArangoBackup(t, handler, obj) @@ -91,7 +92,7 @@ func Test_State_Creating_Failed(t *testing.T) { }, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateCreating) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateCreating) obj.Status.Progress = &backupApi.ArangoBackupProgress{ JobID: "jobID", @@ -102,14 +103,14 @@ func Test_State_Creating_Failed(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("Create Backup returns error", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Create error state should be set newObj := refreshArangoBackup(t, handler, obj) checkBackup(t, newObj, backupApi.ArangoBackupStateCreateError, false) require.Nil(t, newObj.Status.Progress) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // No retry - state should change to failed newObj = refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_deleted_test.go b/pkg/handlers/backup/state_deleted_test.go index f12d12108..1fc411ca0 100644 --- a/pkg/handlers/backup/state_deleted_test.go +++ b/pkg/handlers/backup/state_deleted_test.go @@ -28,19 +28,20 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Deleted(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDeleted) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDeleted) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -52,7 +53,7 @@ func Test_State_Deleted_Recover(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDeleted) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDeleted) createResponse, err := mock.Create() require.NoError(t, err) @@ -66,7 +67,7 @@ func Test_State_Deleted_Recover(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_download_test.go b/pkg/handlers/backup/state_download_test.go index 60bbe027b..ccb69233e 100644 --- a/pkg/handlers/backup/state_download_test.go +++ b/pkg/handlers/backup/state_download_test.go @@ -28,6 +28,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Download_Common(t *testing.T) { @@ -39,7 +40,7 @@ func Test_State_Download_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownload) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ @@ -52,7 +53,7 @@ func Test_State_Download_Success(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -74,7 +75,7 @@ func Test_State_Download_DownloadFailed(t *testing.T) { downloadError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownload) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ @@ -87,7 +88,7 @@ func Test_State_Download_DownloadFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -107,7 +108,7 @@ func Test_State_Download_TemporaryDownloadFailed(t *testing.T) { downloadError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownload) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ @@ -120,7 +121,7 @@ func Test_State_Download_TemporaryDownloadFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_downloaderror_test.go b/pkg/handlers/backup/state_downloaderror_test.go index 81a024a05..e3b54b9e9 100644 --- a/pkg/handlers/backup/state_downloaderror_test.go +++ b/pkg/handlers/backup/state_downloaderror_test.go @@ -29,13 +29,14 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_DownloadError_Reschedule(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloadError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloadError) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ @@ -50,7 +51,7 @@ func Test_State_DownloadError_Reschedule(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -63,7 +64,7 @@ func Test_State_DownloadError_Wait(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloadError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloadError) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ @@ -79,7 +80,7 @@ func Test_State_DownloadError_Wait(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_downloading_test.go b/pkg/handlers/backup/state_downloading_test.go index 315ad644f..a6095930f 100644 --- a/pkg/handlers/backup/state_downloading_test.go +++ b/pkg/handlers/backup/state_downloading_test.go @@ -31,6 +31,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Downloading_Common(t *testing.T) { @@ -42,7 +43,7 @@ func Test_State_Downloading_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloading) createResponse, err := mock.Create() require.NoError(t, err) @@ -71,7 +72,7 @@ func Test_State_Downloading_Success(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("Restore percent", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -87,7 +88,7 @@ func Test_State_Downloading_Success(t *testing.T) { Progress: p, } - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -102,7 +103,7 @@ func Test_State_Downloading_Success(t *testing.T) { Completed: true, } - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -119,7 +120,7 @@ func Test_State_Downloading_FailedDownload(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloading) createResponse, err := mock.Create() require.NoError(t, err) @@ -153,7 +154,7 @@ func Test_State_Downloading_FailedDownload(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -169,7 +170,7 @@ func Test_State_Downloading_FailedProgress(t *testing.T) { progressError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloading) createResponse, err := mock.Create() require.NoError(t, err) @@ -197,7 +198,7 @@ func Test_State_Downloading_FailedProgress(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.EqualError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)), error.Error()) + require.EqualError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)), error.Error()) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -211,7 +212,7 @@ func Test_State_Downloading_TemporaryFailedProgress(t *testing.T) { progressError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloading) createResponse, err := mock.Create() require.NoError(t, err) @@ -239,7 +240,7 @@ func Test_State_Downloading_TemporaryFailedProgress(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.EqualError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)), error.Error()) + require.EqualError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)), error.Error()) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -255,7 +256,7 @@ func Test_State_Downloading_NotFoundProgress(t *testing.T) { progressError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateDownloading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateDownloading) createResponse, err := mock.Create() require.NoError(t, err) @@ -283,7 +284,7 @@ func Test_State_Downloading_NotFoundProgress(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_failed_test.go b/pkg/handlers/backup/state_failed_test.go index 13083be6e..315417fc6 100644 --- a/pkg/handlers/backup/state_failed_test.go +++ b/pkg/handlers/backup/state_failed_test.go @@ -28,19 +28,20 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Failed(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateFailed) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateFailed) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_none_test.go b/pkg/handlers/backup/state_none_test.go index 6effe274f..61f7f0b7d 100644 --- a/pkg/handlers/backup/state_none_test.go +++ b/pkg/handlers/backup/state_none_test.go @@ -28,17 +28,18 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_None_Success(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, _ := newObjectSet(backupApi.ArangoBackupStateNone) + obj, _ := newObjectSet(t, backupApi.ArangoBackupStateNone) // Act createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_pending_test.go b/pkg/handlers/backup/state_pending_test.go index 4f7e7a86c..009e3d51d 100644 --- a/pkg/handlers/backup/state_pending_test.go +++ b/pkg/handlers/backup/state_pending_test.go @@ -31,7 +31,9 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" deploymentType "github.com/arangodb/kube-arangodb/pkg/apis/deployment" + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Pending_Common(t *testing.T) { @@ -42,14 +44,14 @@ func Test_State_Pending_CheckNamespaceIsolation(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStatePending) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStatePending) deployment.Namespace = "non-existent" // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -64,13 +66,13 @@ func Test_State_Pending_OneBackupObject(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStatePending) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStatePending) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -81,7 +83,7 @@ func Test_State_Pending_WithUploadRunning(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStatePending) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStatePending) uploading := newArangoBackup(deployment.GetName(), deployment.GetNamespace(), string(uuid.NewUUID()), backupApi.ArangoBackupStateUploading) @@ -89,7 +91,7 @@ func Test_State_Pending_WithUploadRunning(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj, uploading) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -100,7 +102,7 @@ func Test_State_Pending_WithScheduled(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStatePending) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStatePending) uploading := newArangoBackup(deployment.GetName(), deployment.GetNamespace(), string(uuid.NewUUID()), backupApi.ArangoBackupStateScheduled) @@ -108,7 +110,7 @@ func Test_State_Pending_WithScheduled(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj, uploading) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -119,8 +121,8 @@ func Test_State_Pending_MultipleBackupObjectWithLimitation(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStatePending) - obj2, _ := newObjectSet(backupApi.ArangoBackupStatePending) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStatePending) + obj2, _ := newObjectSet(t, backupApi.ArangoBackupStatePending) obj2.Namespace = obj.Namespace obj2.Spec.Deployment.Name = deployment.Name @@ -129,7 +131,7 @@ func Test_State_Pending_MultipleBackupObjectWithLimitation(t *testing.T) { createArangoBackup(t, handler, obj, obj2) t.Run("First backup object", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -137,7 +139,7 @@ func Test_State_Pending_MultipleBackupObjectWithLimitation(t *testing.T) { }) t.Run("Second backup object", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj2))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj2))) // Assert newObj := refreshArangoBackup(t, handler, obj2) @@ -152,7 +154,7 @@ func Test_State_Pending_KeepPendingWithForcedRunning(t *testing.T) { name := string(uuid.NewUUID()) - deployment := newArangoDeployment(name, name) + deployment := tests.NewMetaObject[*api.ArangoDeployment](t, name, name) size := 128 objects := make([]*backupApi.ArangoBackup, size) for id := range objects { @@ -168,7 +170,7 @@ func Test_State_Pending_KeepPendingWithForcedRunning(t *testing.T) { for _, backup := range objects { go func(b *backupApi.ArangoBackup) { defer w.Done() - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, b))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, b))) }(backup) } diff --git a/pkg/handlers/backup/state_ready_test.go b/pkg/handlers/backup/state_ready_test.go index b5b1a686a..81c1edbed 100644 --- a/pkg/handlers/backup/state_ready_test.go +++ b/pkg/handlers/backup/state_ready_test.go @@ -33,9 +33,11 @@ import ( "github.com/arangodb/go-driver" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util/globals" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Ready_Common(t *testing.T) { @@ -47,7 +49,7 @@ func Test_State_Ready_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) createResponse, err := mock.Create() require.NoError(t, err) @@ -61,7 +63,7 @@ func Test_State_Ready_Success(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -73,7 +75,7 @@ func Test_State_Ready_With_Lifetime(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Spec.Lifetime = &meta.Duration{Duration: 5 * time.Second} createResponse, err := mock.Create() @@ -89,7 +91,7 @@ func Test_State_Ready_With_Lifetime(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("First iteration", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -99,7 +101,7 @@ func Test_State_Ready_With_Lifetime(t *testing.T) { t.Run("Second iteration once Lifetime is expired", func(t *testing.T) { time.Sleep(10 * time.Second) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -112,7 +114,7 @@ func Test_State_Ready_Unavailable(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) createResponse, err := mock.Create() require.NoError(t, err) @@ -130,7 +132,7 @@ func Test_State_Ready_Unavailable(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -142,7 +144,7 @@ func Test_State_Ready_ServerDown(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) createResponse, err := mock.Create() require.NoError(t, err) @@ -160,7 +162,7 @@ func Test_State_Ready_ServerDown(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -172,7 +174,7 @@ func Test_State_Ready_Success_Update(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) createResponse, err := mock.Create() require.NoError(t, err) @@ -187,7 +189,7 @@ func Test_State_Ready_Success_Update(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("First iteration", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -199,7 +201,7 @@ func Test_State_Ready_Success_Update(t *testing.T) { backupMeta.SizeInBytes = 123 mock.state.backups[backupMeta.ID] = backupMeta - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -209,7 +211,7 @@ func Test_State_Ready_Success_Update(t *testing.T) { }) t.Run("Do nothing", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -225,7 +227,7 @@ func Test_State_Ready_TemporaryGetFailed(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Status.Backup = &backupApi.ArangoBackupDetails{} @@ -233,7 +235,7 @@ func Test_State_Ready_TemporaryGetFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -247,7 +249,7 @@ func Test_State_Ready_FatalGetFailed(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Status.Backup = &backupApi.ArangoBackupDetails{} @@ -255,7 +257,7 @@ func Test_State_Ready_FatalGetFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -271,7 +273,7 @@ func Test_State_Ready_MissingBackup(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Status.Backup = &backupApi.ArangoBackupDetails{} @@ -279,7 +281,7 @@ func Test_State_Ready_MissingBackup(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -290,7 +292,7 @@ func Test_State_Ready_Upload(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "Any", } @@ -307,7 +309,7 @@ func Test_State_Ready_Upload(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -319,7 +321,7 @@ func Test_State_Ready_DownloadDoNothing(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ RepositoryURL: "any", @@ -341,7 +343,7 @@ func Test_State_Ready_DownloadDoNothing(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -355,7 +357,7 @@ func Test_State_Ready_DoUploadDownloadedBackup(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "Any", } @@ -375,7 +377,7 @@ func Test_State_Ready_DoUploadDownloadedBackup(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -388,7 +390,7 @@ func Test_State_Ready_DoNotReUploadBackup(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "Any", } @@ -407,7 +409,7 @@ func Test_State_Ready_DoNotReUploadBackup(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -421,7 +423,7 @@ func Test_State_Ready_RemoveUploadedFlag(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) createResponse, err := mock.Create() require.NoError(t, err) @@ -437,7 +439,7 @@ func Test_State_Ready_RemoveUploadedFlag(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -451,7 +453,7 @@ func Test_State_Ready_KeepPendingWithForcedRunning(t *testing.T) { name := string(uuid.NewUUID()) - deployment := newArangoDeployment(name, name) + deployment := tests.NewMetaObject[*api.ArangoDeployment](t, name, name) size := 128 objects := make([]*backupApi.ArangoBackup, size) for id := range objects { @@ -482,7 +484,7 @@ func Test_State_Ready_KeepPendingWithForcedRunning(t *testing.T) { for _, backup := range objects { go func(b *backupApi.ArangoBackup) { defer w.Done() - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, b))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, b))) }(backup) } @@ -521,7 +523,7 @@ func Test_State_Ready_KeepPendingWithForcedRunningSameId(t *testing.T) { backupMeta, err := mock.Get(createResponse.ID) require.NoError(t, err) - deployment := newArangoDeployment(name, name) + deployment := tests.NewMetaObject[*api.ArangoDeployment](t, name, name) size := 128 objects := make([]*backupApi.ArangoBackup, size) for id := range objects { @@ -546,7 +548,7 @@ func Test_State_Ready_KeepPendingWithForcedRunningSameId(t *testing.T) { for _, backup := range objects { go func(b *backupApi.ArangoBackup) { defer w.Done() - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, b))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, b))) }(backup) } @@ -580,7 +582,7 @@ func Test_State_Ready_Concurrent_Queued(t *testing.T) { createResponse, err := mock.Create() require.NoError(t, err) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) backupMeta, err := mock.Get(createResponse.ID) require.NoError(t, err) @@ -615,7 +617,7 @@ func Test_State_Ready_Concurrent_Queued(t *testing.T) { createArangoBackup(t, handler, objects...) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -630,7 +632,7 @@ func Test_State_Ready_Concurrent_Started(t *testing.T) { createResponse, err := mock.Create() require.NoError(t, err) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateReady) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateReady) backupMeta, err := mock.Get(createResponse.ID) require.NoError(t, err) @@ -665,7 +667,7 @@ func Test_State_Ready_Concurrent_Started(t *testing.T) { createArangoBackup(t, handler, objects...) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_scheduled_test.go b/pkg/handlers/backup/state_scheduled_test.go index 5155b3389..b7aa256ad 100644 --- a/pkg/handlers/backup/state_scheduled_test.go +++ b/pkg/handlers/backup/state_scheduled_test.go @@ -28,6 +28,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Scheduled_Common(t *testing.T) { @@ -38,7 +39,7 @@ func Test_State_Scheduled_Download(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateScheduled) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateScheduled) obj.Spec.Download = &backupApi.ArangoBackupSpecDownload{ ArangoBackupSpecOperation: backupApi.ArangoBackupSpecOperation{ @@ -51,7 +52,7 @@ func Test_State_Scheduled_Download(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -62,13 +63,13 @@ func Test_State_Scheduled_Create(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateScheduled) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateScheduled) // Act createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -79,7 +80,7 @@ func Test_State_Scheduled_Upload(t *testing.T) { // Arrange handler, _ := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateScheduled) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateScheduled) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "test", @@ -89,7 +90,7 @@ func Test_State_Scheduled_Upload(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_unavailable_test.go b/pkg/handlers/backup/state_unavailable_test.go index b559e3992..236bfbb53 100644 --- a/pkg/handlers/backup/state_unavailable_test.go +++ b/pkg/handlers/backup/state_unavailable_test.go @@ -30,6 +30,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Unavailable_Common(t *testing.T) { @@ -41,7 +42,7 @@ func Test_State_Unavailable_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUnavailable) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUnavailable) createResponse, err := mock.Create() require.NoError(t, err) @@ -55,7 +56,7 @@ func Test_State_Unavailable_Success(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -67,7 +68,7 @@ func Test_State_Unavailable_Keep(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUnavailable) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUnavailable) createResponse, err := mock.Create() require.NoError(t, err) @@ -85,7 +86,7 @@ func Test_State_Unavailable_Keep(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -97,7 +98,7 @@ func Test_State_Unavailable_Update(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUnavailable) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUnavailable) createResponse, err := mock.Create() require.NoError(t, err) @@ -116,7 +117,7 @@ func Test_State_Unavailable_Update(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("First iteration", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -128,7 +129,7 @@ func Test_State_Unavailable_Update(t *testing.T) { backupMeta.SizeInBytes = 123 mock.state.backups[backupMeta.ID] = backupMeta - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -138,7 +139,7 @@ func Test_State_Unavailable_Update(t *testing.T) { }) t.Run("Do nothing", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -154,7 +155,7 @@ func Test_State_Unavailable_TemporaryGetFailed(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUnavailable) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUnavailable) obj.Status.Backup = &backupApi.ArangoBackupDetails{} @@ -162,7 +163,7 @@ func Test_State_Unavailable_TemporaryGetFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -176,7 +177,7 @@ func Test_State_Unavailable_FatalGetFailed(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUnavailable) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUnavailable) obj.Status.Backup = &backupApi.ArangoBackupDetails{} @@ -184,7 +185,7 @@ func Test_State_Unavailable_FatalGetFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -200,7 +201,7 @@ func Test_State_Unavailable_MissingBackup(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUnavailable) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUnavailable) obj.Status.Backup = &backupApi.ArangoBackupDetails{} @@ -208,7 +209,7 @@ func Test_State_Unavailable_MissingBackup(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_upload_test.go b/pkg/handlers/backup/state_upload_test.go index 20d68202c..0df0a5abe 100644 --- a/pkg/handlers/backup/state_upload_test.go +++ b/pkg/handlers/backup/state_upload_test.go @@ -30,6 +30,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Upload_Common(t *testing.T) { @@ -41,7 +42,7 @@ func Test_State_Upload_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -55,7 +56,7 @@ func Test_State_Upload_Success(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -76,7 +77,7 @@ func Test_State_Upload_TemporaryGetFailed(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -89,7 +90,7 @@ func Test_State_Upload_TemporaryGetFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.EqualError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)), error.Error()) + require.EqualError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)), error.Error()) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -103,7 +104,7 @@ func Test_State_Upload_FatalGetFailed(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -116,7 +117,7 @@ func Test_State_Upload_FatalGetFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.EqualError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)), error.Error()) + require.EqualError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)), error.Error()) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -132,7 +133,7 @@ func Test_State_Upload_BackupMissing(t *testing.T) { getError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -145,7 +146,7 @@ func Test_State_Upload_BackupMissing(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -159,7 +160,7 @@ func Test_State_Upload_TemporaryUploadFailed(t *testing.T) { uploadError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -172,7 +173,7 @@ func Test_State_Upload_TemporaryUploadFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -189,7 +190,7 @@ func Test_State_Upload_FatalUploadFailed(t *testing.T) { uploadError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -202,7 +203,7 @@ func Test_State_Upload_FatalUploadFailed(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -219,7 +220,7 @@ func Test_State_Upload_TemporaryUploadFailed_Backoff(t *testing.T) { uploadError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -235,7 +236,7 @@ func Test_State_Upload_TemporaryUploadFailed_Backoff(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -252,7 +253,7 @@ func Test_State_Upload_FatalUploadFailed_Backoff(t *testing.T) { uploadError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUpload) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUpload) createResponse, err := mock.Create() require.NoError(t, err) @@ -268,7 +269,7 @@ func Test_State_Upload_FatalUploadFailed_Backoff(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_uploaderror_test.go b/pkg/handlers/backup/state_uploaderror_test.go index 498038708..609ff75c9 100644 --- a/pkg/handlers/backup/state_uploaderror_test.go +++ b/pkg/handlers/backup/state_uploaderror_test.go @@ -31,13 +31,14 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/arangodb/kube-arangodb/pkg/util" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_UploadError_Reschedule(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploadError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploadError) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "S3 URL", @@ -59,7 +60,7 @@ func Test_State_UploadError_Reschedule(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -74,7 +75,7 @@ func Test_State_UploadError_Reschedule(t *testing.T) { func Test_State_UploadError_Wait(t *testing.T) { handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploadError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploadError) obj.Spec.Upload = &backupApi.ArangoBackupSpecOperation{ RepositoryURL: "S3 URL", @@ -99,7 +100,7 @@ func Test_State_UploadError_Wait(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -114,7 +115,7 @@ func Test_State_UploadError_Wait(t *testing.T) { func Test_State_UploadError_BackToReady(t *testing.T) { handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploadError) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploadError) backupMeta, err := mock.Create() require.NoError(t, err) @@ -132,7 +133,7 @@ func Test_State_UploadError_BackToReady(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/handlers/backup/state_uploading_test.go b/pkg/handlers/backup/state_uploading_test.go index b8bed24a3..adf730733 100644 --- a/pkg/handlers/backup/state_uploading_test.go +++ b/pkg/handlers/backup/state_uploading_test.go @@ -31,6 +31,7 @@ import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/tests" ) func Test_State_Uploading_Common(t *testing.T) { @@ -43,7 +44,7 @@ func Test_State_Uploading_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -69,7 +70,7 @@ func Test_State_Uploading_Success(t *testing.T) { createArangoBackup(t, handler, obj) t.Run("Restore percent", func(t *testing.T) { - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -84,7 +85,7 @@ func Test_State_Uploading_Success(t *testing.T) { Progress: p, } - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -98,7 +99,7 @@ func Test_State_Uploading_Success(t *testing.T) { Completed: true, } - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -114,7 +115,7 @@ func Test_State_Uploading_FailedUpload(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -141,7 +142,7 @@ func Test_State_Uploading_FailedUpload(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -157,7 +158,7 @@ func Test_StateUploading_FailedProgress(t *testing.T) { progressError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -182,7 +183,7 @@ func Test_StateUploading_FailedProgress(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.EqualError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)), error.Error()) + require.EqualError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)), error.Error()) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -196,7 +197,7 @@ func Test_State_Uploading_TemporaryFailedProgress(t *testing.T) { progressError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -221,7 +222,7 @@ func Test_State_Uploading_TemporaryFailedProgress(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.EqualError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj)), error.Error()) + require.EqualError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj)), error.Error()) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -237,7 +238,7 @@ func Test_State_Uploading_NotFoundProgress(t *testing.T) { progressError: error, }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -262,7 +263,7 @@ func Test_State_Uploading_NotFoundProgress(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -275,7 +276,7 @@ func Test_StateUploading_Abort_Success(t *testing.T) { // Arrange handler, mock := newErrorsFakeHandler(mockErrorsArangoClientBackup{}) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -296,7 +297,7 @@ func Test_StateUploading_Abort_Success(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) @@ -309,7 +310,7 @@ func Test_StateUploading_Abort_Fail(t *testing.T) { abortError: newTemporaryErrorf("error"), }) - obj, deployment := newObjectSet(backupApi.ArangoBackupStateUploading) + obj, deployment := newObjectSet(t, backupApi.ArangoBackupStateUploading) createResponse, err := mock.Create() require.NoError(t, err) @@ -330,7 +331,7 @@ func Test_StateUploading_Abort_Fail(t *testing.T) { createArangoDeployment(t, handler, deployment) createArangoBackup(t, handler, obj) - require.NoError(t, handler.Handle(context.Background(), newItemFromBackup(operation.Update, obj))) + require.NoError(t, handler.Handle(context.Background(), tests.NewItem(t, operation.Update, obj))) // Assert newObj := refreshArangoBackup(t, handler, obj) diff --git a/pkg/util/tests/gen.go b/pkg/util/tests/gen.go deleted file mode 100644 index 11b9bb61e..000000000 --- a/pkg/util/tests/gen.go +++ /dev/null @@ -1,59 +0,0 @@ -// -// DISCLAIMER -// -// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Copyright holder is ArangoDB GmbH, Cologne, Germany -// - -package tests - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - meta "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/uuid" - - api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" - "github.com/arangodb/kube-arangodb/pkg/util/kclient" -) - -func NewArangoDeployment(name string) *api.ArangoDeployment { - return &api.ArangoDeployment{ - ObjectMeta: meta.ObjectMeta{ - Name: name, - Namespace: FakeNamespace, - UID: uuid.NewUUID(), - }, - } -} - -func NewArangoClusterSynchronization(name string) *api.ArangoClusterSynchronization { - return &api.ArangoClusterSynchronization{ - ObjectMeta: meta.ObjectMeta{ - Name: name, - Namespace: FakeNamespace, - UID: uuid.NewUUID(), - }, - } -} - -func RefreshArangoClusterSynchronization(t *testing.T, client kclient.Client, acs *api.ArangoClusterSynchronization) *api.ArangoClusterSynchronization { - nacs, err := client.Arango().DatabaseV1().ArangoClusterSynchronizations(acs.GetNamespace()).Get(context.Background(), acs.GetName(), meta.GetOptions{}) - require.NoError(t, err) - return nacs -} diff --git a/pkg/util/tests/kubernetes.go b/pkg/util/tests/kubernetes.go new file mode 100644 index 000000000..e193e148a --- /dev/null +++ b/pkg/util/tests/kubernetes.go @@ -0,0 +1,224 @@ +// +// DISCLAIMER +// +// Copyright 2023 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package tests + +import ( + "context" + "fmt" + "reflect" + "testing" + + "github.com/stretchr/testify/require" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/uuid" + "k8s.io/client-go/kubernetes" + + "github.com/arangodb/kube-arangodb/pkg/apis/backup" + backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" + "github.com/arangodb/kube-arangodb/pkg/apis/deployment" + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" + "github.com/arangodb/kube-arangodb/pkg/apis/ml" + mlApi "github.com/arangodb/kube-arangodb/pkg/apis/ml/v1alpha1" + arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" +) + +type KubernetesObject interface { + meta.Object + meta.Type +} + +func CreateObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientSet.Interface, objects ...interface{}) func(t *testing.T) { + for _, object := range objects { + switch v := object.(type) { + case **api.ArangoDeployment: + require.NotNil(t, v) + + vl := *v + _, err := arango.DatabaseV1().ArangoDeployments(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) + require.NoError(t, err) + case **api.ArangoClusterSynchronization: + require.NotNil(t, v) + + vl := *v + _, err := arango.DatabaseV1().ArangoClusterSynchronizations(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) + require.NoError(t, err) + case **backupApi.ArangoBackup: + require.NotNil(t, v) + + vl := *v + _, err := arango.BackupV1().ArangoBackups(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) + require.NoError(t, err) + case **mlApi.ArangoMLExtension: + require.NotNil(t, v) + + vl := *v + _, err := arango.MlV1alpha1().ArangoMLExtensions(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) + require.NoError(t, err) + default: + require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) + } + } + + return func(t *testing.T) { + RefreshObjects(t, k8s, arango, objects...) + } +} + +func RefreshObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientSet.Interface, objects ...interface{}) { + for _, object := range objects { + switch v := object.(type) { + case **api.ArangoDeployment: + require.NotNil(t, v) + + vl := *v + + vn, err := arango.DatabaseV1().ArangoDeployments(vl.GetNamespace()).Get(context.Background(), vl.GetName(), meta.GetOptions{}) + require.NoError(t, err) + + *v = vn + case **api.ArangoClusterSynchronization: + require.NotNil(t, v) + + vl := *v + + vn, err := arango.DatabaseV1().ArangoClusterSynchronizations(vl.GetNamespace()).Get(context.Background(), vl.GetName(), meta.GetOptions{}) + require.NoError(t, err) + + *v = vn + case **backupApi.ArangoBackup: + require.NotNil(t, v) + + vl := *v + + vn, err := arango.BackupV1().ArangoBackups(vl.GetNamespace()).Get(context.Background(), vl.GetName(), meta.GetOptions{}) + require.NoError(t, err) + + *v = vn + case **mlApi.ArangoMLExtension: + require.NotNil(t, v) + + vl := *v + + vn, err := arango.MlV1alpha1().ArangoMLExtensions(vl.GetNamespace()).Get(context.Background(), vl.GetName(), meta.GetOptions{}) + require.NoError(t, err) + + *v = vn + default: + require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) + } + } +} + +type MetaObjectMod[T meta.Object] func(t *testing.T, obj T) + +func SetMetaBasedOnType(t *testing.T, object meta.Object) { + switch v := object.(type) { + case *api.ArangoDeployment: + v.Kind = deployment.ArangoDeploymentResourceKind + v.APIVersion = api.SchemeGroupVersion.String() + v.SetSelfLink(fmt.Sprintf("/api/%s/%s/%s/%s", + api.SchemeGroupVersion.String(), + deployment.ArangoDeploymentResourcePlural, + object.GetNamespace(), + object.GetName())) + case *api.ArangoClusterSynchronization: + v.Kind = deployment.ArangoClusterSynchronizationResourceKind + v.APIVersion = api.SchemeGroupVersion.String() + v.SetSelfLink(fmt.Sprintf("/api/%s/%s/%s/%s", + api.SchemeGroupVersion.String(), + deployment.ArangoClusterSynchronizationResourcePlural, + object.GetNamespace(), + object.GetName())) + case *backupApi.ArangoBackup: + v.Kind = backup.ArangoBackupResourceKind + v.APIVersion = backupApi.SchemeGroupVersion.String() + v.SetSelfLink(fmt.Sprintf("/api/%s/%s/%s/%s", + backupApi.SchemeGroupVersion.String(), + backup.ArangoBackupResourcePlural, + object.GetNamespace(), + object.GetName())) + case *mlApi.ArangoMLExtension: + v.Kind = ml.ArangoMLExtensionResourceKind + v.APIVersion = mlApi.SchemeGroupVersion.String() + v.SetSelfLink(fmt.Sprintf("/api/%s/%s/%s/%s", + mlApi.SchemeGroupVersion.String(), + ml.ArangoMLExtensionResourcePlural, + object.GetNamespace(), + object.GetName())) + default: + require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) + } +} + +func NewMetaObject[T meta.Object](t *testing.T, namespace, name string, mods ...MetaObjectMod[T]) T { + var obj T + + if objT := reflect.TypeOf(obj); objT.Kind() == reflect.Pointer { + newObj := reflect.New(objT.Elem()) + + reflect.ValueOf(&obj).Elem().Set(newObj) + } + + obj.SetNamespace(namespace) + obj.SetName(name) + obj.SetUID(uuid.NewUUID()) + + SetMetaBasedOnType(t, obj) + + for _, mod := range mods { + mod(t, obj) + } + + return obj +} + +func NewItem(t *testing.T, o operation.Operation, object meta.Object) operation.Item { + item := operation.Item{ + Operation: o, + + Namespace: object.GetNamespace(), + Name: object.GetName(), + } + + switch v := object.(type) { + case *api.ArangoDeployment: + item.Group = deployment.ArangoDeploymentGroupName + item.Version = api.ArangoDeploymentVersion + item.Kind = deployment.ArangoDeploymentResourceKind + case *api.ArangoClusterSynchronization: + item.Group = deployment.ArangoDeploymentGroupName + item.Version = api.ArangoDeploymentVersion + item.Kind = deployment.ArangoClusterSynchronizationResourceKind + case *backupApi.ArangoBackup: + item.Group = backup.ArangoBackupGroupName + item.Version = backupApi.ArangoBackupVersion + item.Kind = backup.ArangoBackupResourceKind + case *mlApi.ArangoMLExtension: + item.Group = ml.ArangoMLGroupName + item.Version = mlApi.ArangoMLVersion + item.Kind = ml.ArangoMLExtensionResourceKind + default: + require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) + } + + return item +} diff --git a/pkg/util/tests/kubernetes_test.go b/pkg/util/tests/kubernetes_test.go new file mode 100644 index 000000000..af26dce4f --- /dev/null +++ b/pkg/util/tests/kubernetes_test.go @@ -0,0 +1,56 @@ +// +// DISCLAIMER +// +// Copyright 2023 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package tests + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/require" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + + backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" + mlApi "github.com/arangodb/kube-arangodb/pkg/apis/ml/v1alpha1" + "github.com/arangodb/kube-arangodb/pkg/util/kclient" +) + +func NewMetaObjectRun[T meta.Object](t *testing.T) { + var obj T + t.Run(reflect.TypeOf(obj).String(), func(t *testing.T) { + c := kclient.NewFakeClient() + + obj := NewMetaObject[T](t, "test", "test") + + require.NotNil(t, obj) + + refresh := CreateObjects(t, c.Kubernetes(), c.Arango(), &obj) + + refresh(t) + }) +} + +func Test_NewMetaObject(t *testing.T) { + NewMetaObjectRun[*api.ArangoDeployment](t) + NewMetaObjectRun[*api.ArangoClusterSynchronization](t) + NewMetaObjectRun[*backupApi.ArangoBackup](t) + NewMetaObjectRun[*mlApi.ArangoMLExtension](t) +}