github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/check_kyma_resource_deleted_step_test.go (about) 1 package deprovisioning 2 3 import ( 4 "context" 5 "testing" 6 7 "k8s.io/apimachinery/pkg/runtime/schema" 8 9 "github.com/kyma-project/kyma-environment-broker/internal/process/steps" 10 11 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 12 "github.com/kyma-project/kyma-environment-broker/internal/logger" 13 "github.com/kyma-project/kyma-environment-broker/internal/storage" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 "k8s.io/apimachinery/pkg/api/errors" 17 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 18 "sigs.k8s.io/controller-runtime/pkg/client" 19 "sigs.k8s.io/controller-runtime/pkg/client/fake" 20 ) 21 22 func TestCheckKymaResourceDeleted_HappyFlow(t *testing.T) { 23 // Given 24 operation := fixture.FixDeprovisioningOperationAsOperation(fixOperationID, fixInstanceID) 25 operation.KymaResourceNamespace = "kyma-system" 26 operation.KymaTemplate = kymaTemplate 27 28 kcpClient := fake.NewClientBuilder().Build() 29 30 err := fixKymaResourceWithGivenRuntimeID(kcpClient, "kyma-system", "some-other-Runtime-ID") 31 require.NoError(t, err) 32 33 memoryStorage := storage.NewMemoryStorage() 34 err = memoryStorage.Operations().InsertOperation(operation) 35 assert.NoError(t, err) 36 37 step := NewCheckKymaResourceDeletedStep(memoryStorage.Operations(), kcpClient) 38 39 // When 40 _, backoff, err := step.Run(operation, logger.NewLogSpy().Logger) 41 42 // Then 43 assert.Zero(t, backoff) 44 assertNoKymaResourceWithGivenRuntimeID(t, kcpClient, operation.KymaResourceNamespace, steps.KymaName(operation)) 45 } 46 47 func TestCheckKymaResourceDeleted_EmptyKymaResourceName(t *testing.T) { 48 // Given 49 operation := fixture.FixDeprovisioningOperationAsOperation(fixOperationID, fixInstanceID) 50 operation.KymaResourceNamespace = "kyma-system" 51 operation.RuntimeID = "" 52 operation.KymaResourceName = "" 53 operation.KymaTemplate = kymaTemplate 54 55 kcpClient := fake.NewClientBuilder().Build() 56 57 err := fixKymaResourceWithGivenRuntimeID(kcpClient, "kyma-system", "some-other-Runtime-ID") 58 require.NoError(t, err) 59 60 memoryStorage := storage.NewMemoryStorage() 61 err = memoryStorage.Operations().InsertOperation(operation) 62 assert.NoError(t, err) 63 64 step := NewCheckKymaResourceDeletedStep(memoryStorage.Operations(), kcpClient) 65 66 // When 67 _, backoff, err := step.Run(operation, logger.NewLogSpy().Logger) 68 69 // Then 70 assert.Zero(t, backoff) 71 assertNoKymaResourceWithGivenRuntimeID(t, kcpClient, operation.KymaResourceNamespace, steps.KymaName(operation)) 72 } 73 74 func TestCheckKymaResourceDeleted_RetryWhenStillExists(t *testing.T) { 75 // Given 76 operation := fixture.FixDeprovisioningOperationAsOperation(fixOperationID, fixInstanceID) 77 operation.KymaResourceNamespace = "kyma-system" 78 operation.KymaTemplate = kymaTemplate 79 80 kcpClient := fake.NewClientBuilder().Build() 81 82 err := fixKymaResourceWithGivenRuntimeID(kcpClient, operation.KymaResourceNamespace, operation.RuntimeID) 83 require.NoError(t, err) 84 85 memoryStorage := storage.NewMemoryStorage() 86 err = memoryStorage.Operations().InsertOperation(operation) 87 require.NoError(t, err) 88 89 step := NewCheckKymaResourceDeletedStep(memoryStorage.Operations(), kcpClient) 90 91 // When 92 _, backoff, err := step.Run(operation, logger.NewLogSpy().Logger) 93 94 // Then 95 require.NoError(t, err) 96 assert.NotZero(t, backoff) 97 } 98 99 func assertNoKymaResourceWithGivenRuntimeID(t *testing.T, kcpClient client.Client, kymaResourceNamespace string, resourceName string) { 100 kymaUnstructured := &unstructured.Unstructured{} 101 kymaUnstructured.SetGroupVersionKind(schema.GroupVersionKind{ 102 Group: "operator.kyma-project.io", 103 Version: "v1beta2", 104 Kind: "kyma", 105 }) 106 err := kcpClient.Get(context.Background(), client.ObjectKey{ 107 Namespace: kymaResourceNamespace, 108 Name: resourceName, 109 }, kymaUnstructured) 110 assert.True(t, errors.IsNotFound(err)) 111 } 112 113 func fixKymaResourceWithGivenRuntimeID(kcpClient client.Client, kymaResourceNamespace string, resourceName string) error { 114 return kcpClient.Create(context.Background(), &unstructured.Unstructured{Object: map[string]interface{}{ 115 "apiVersion": "operator.kyma-project.io/v1beta2", 116 "kind": "Kyma", 117 "metadata": map[string]interface{}{ 118 "name": resourceName, 119 "namespace": kymaResourceNamespace, 120 }, 121 "spec": map[string]interface{}{ 122 "channel": "stable", 123 }, 124 }}) 125 }