Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Improve K8S Mock for UT #1503

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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