Skip to content

Commit

Permalink
[Feature] Improve K8S Mock for UT
Browse files Browse the repository at this point in the history
  • Loading branch information
ajanikow committed Nov 23, 2023
1 parent 8cdc6b9 commit 23536ab
Show file tree
Hide file tree
Showing 25 changed files with 533 additions and 291 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/ml/v1alpha1/extension_status_metadata.go
Original file line number Diff line number Diff line change
@@ -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"`
}
57 changes: 11 additions & 46 deletions pkg/handlers/backup/backup_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
29 changes: 15 additions & 14 deletions pkg/handlers/backup/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand All @@ -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)
Expand All @@ -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,
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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",
}
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -217,15 +218,15 @@ 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

// 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)
Expand All @@ -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",
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions pkg/handlers/backup/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ 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)
createArangoBackup(t, handler, obj)

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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/handlers/backup/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 23536ab

Please sign in to comment.