github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/cis/account_cleanup_test.go (about) 1 package cis 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/kyma-project/kyma-environment-broker/internal" 8 mocks "github.com/kyma-project/kyma-environment-broker/internal/cis/automock" 9 "github.com/kyma-project/kyma-environment-broker/internal/logger" 10 "github.com/kyma-project/kyma-environment-broker/internal/storage" 11 12 "github.com/sirupsen/logrus" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 // subAccountTestIDs contains test data in form: InstanceID : SubAccountID 17 var subAccountTestIDs = map[string]string{ 18 "7b84e7e7-62df-412a-9e09-b4581253efba": "e65a807a-488f-4062-9e02-b39090ec0258", 19 "c2cf9bf5-948e-4c65-baed-6b92f9da9c8e": "c9200e87-e43a-4988-b058-ab99c73e20cb", 20 "ab0addc7-de76-4f2e-a6a7-bfcbd3d4eefe": "b45f63bd-750c-4ede-920f-e2b7c86ca15f", 21 "eb3508ca-eec1-4ae1-8968-e78a45daa741": "33cfc39a-a974-4d8d-964f-5be7b1907fbb", 22 "07d368f2-c294-47e7-8d66-20b73ef46342": "967f897e-29a0-4576-8bfa-98e532ba04a7", 23 "d7348ffa-a7d3-41cf-8a7f-17c92c7b2794": "c5090aad-af2e-406b-bddc-ece72bfa0b7a", 24 "cb6034fc-2bae-466f-a98d-5af492edeca2": "f1672806-136e-4b40-9e71-d93a04391274", 25 "9e3cbd53-5c5a-410f-8eaa-1c2d75345814": "c249745a-d52f-4be1-ab02-6d59834e50d4", 26 "2c448504-6c8a-4d82-bdc2-fa4bd0534f25": "95490855-cf3c-4333-884c-4084f591aa27", 27 "ad6af000-e647-44ea-a3bb-db8672d5bc7e": "8664da71-8e3c-47bb-9c29-128038f8a959", 28 } 29 30 func TestSubAccountCleanupService_Run(t *testing.T) { 31 t.Run("all instances should be deprovisioned", func(t *testing.T) { 32 // Given 33 cisClient := &mocks.CisClient{} 34 cisClient.On("FetchSubaccountsToDelete").Return(fixSubAccountIDs(), nil) 35 defer cisClient.AssertExpectations(t) 36 37 brokerClient := &mocks.BrokerClient{} 38 for _, instance := range fixInstances() { 39 brokerClient.On("Deprovision", instance).Return("<operationUUID>", nil).Once() 40 } 41 defer brokerClient.AssertExpectations(t) 42 43 memoryStorage := storage.NewMemoryStorage() 44 for _, instance := range fixInstances() { 45 err := memoryStorage.Instances().Insert(instance) 46 assert.NoError(t, err) 47 } 48 49 service := NewSubAccountCleanupService(cisClient, brokerClient, memoryStorage.Instances(), logrus.New()) 50 service.chunksAmount = 2 51 52 // When 53 err := service.Run() 54 55 // Then 56 assert.NoError(t, err) 57 }) 58 59 t.Run("some deprovisioning should failed and warnings should be displayed", func(t *testing.T) { 60 // Given 61 brokenInstanceIDOne := "07d368f2-c294-47e7-8d66-20b73ef46342" 62 brokenInstanceIDTwo := "ad6af000-e647-44ea-a3bb-db8672d5bc7e" 63 64 cisClient := &mocks.CisClient{} 65 cisClient.On("FetchSubaccountsToDelete").Return(fixSubAccountIDs(), nil) 66 defer cisClient.AssertExpectations(t) 67 68 brokerClient := &mocks.BrokerClient{} 69 for _, instance := range fixInstances() { 70 if instance.InstanceID == brokenInstanceIDOne || instance.InstanceID == brokenInstanceIDTwo { 71 brokerClient.On("Deprovision", instance).Return("", fmt.Errorf("cannot deprovision")).Once() 72 } else { 73 brokerClient.On("Deprovision", instance).Return("<operationUUID>", nil).Once() 74 } 75 } 76 defer brokerClient.AssertExpectations(t) 77 78 memoryStorage := storage.NewMemoryStorage() 79 for _, instance := range fixInstances() { 80 err := memoryStorage.Instances().Insert(instance) 81 assert.NoError(t, err) 82 } 83 84 log := logger.NewLogSpy() 85 service := NewSubAccountCleanupService(cisClient, brokerClient, memoryStorage.Instances(), log.Logger) 86 service.chunksAmount = 5 87 88 // When 89 err := service.Run() 90 91 // Then 92 assert.NoError(t, err) 93 log.AssertLogged(t, logrus.WarnLevel, "part of deprovisioning process failed with error: error occurred during deprovisioning instance with ID ad6af000-e647-44ea-a3bb-db8672d5bc7e: cannot deprovision") 94 log.AssertLogged(t, logrus.WarnLevel, "part of deprovisioning process failed with error: error occurred during deprovisioning instance with ID 07d368f2-c294-47e7-8d66-20b73ef46342: cannot deprovision") 95 }) 96 97 t.Run("process should return with error", func(t *testing.T) { 98 // Given 99 cisClient := &mocks.CisClient{} 100 cisClient.On("FetchSubaccountsToDelete").Return([]string{}, fmt.Errorf("cannot fetch subaccounts")) 101 defer cisClient.AssertExpectations(t) 102 103 brokerClient := &mocks.BrokerClient{} 104 memoryStorage := storage.NewMemoryStorage() 105 106 service := NewSubAccountCleanupService(cisClient, brokerClient, memoryStorage.Instances(), logrus.New()) 107 service.chunksAmount = 7 108 109 // When 110 err := service.Run() 111 112 // Then 113 assert.Error(t, err) 114 }) 115 } 116 117 func fixSubAccountIDs() []string { 118 subAccountIDs := make([]string, 0) 119 120 for _, subAccountID := range subAccountTestIDs { 121 subAccountIDs = append(subAccountIDs, subAccountID) 122 } 123 124 return subAccountIDs 125 } 126 127 func fixInstances() []internal.Instance { 128 instances := make([]internal.Instance, 0) 129 130 for instanceID, subAccountTestID := range subAccountTestIDs { 131 instances = append(instances, internal.Instance{ 132 InstanceID: instanceID, 133 SubAccountID: subAccountTestID, 134 }) 135 } 136 137 return instances 138 }