github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/instance_deprovision_test.go (about) 1 package broker 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/kyma-project/kyma-environment-broker/internal" 8 "github.com/kyma-project/kyma-environment-broker/internal/broker/automock" 9 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 10 "github.com/kyma-project/kyma-environment-broker/internal/storage" 11 "github.com/pivotal-cf/brokerapi/v8/domain" 12 "github.com/sirupsen/logrus" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/mock" 15 "github.com/stretchr/testify/require" 16 ) 17 18 const ( 19 instanceID = "instance-001" 20 planID = "awesome-plan" 21 operationID = "1234" 22 ) 23 24 func TestDeprovisionEndpoint_DeprovisionNotExistingInstance(t *testing.T) { 25 // given 26 memoryStorage := storage.NewMemoryStorage() 27 queue := &automock.Queue{} 28 queue.On("Add", mock.AnythingOfType("string")) 29 30 svc := NewDeprovision(memoryStorage.Instances(), memoryStorage.Operations(), queue, logrus.StandardLogger()) 31 32 // when 33 _, err := svc.Deprovision(context.TODO(), "inst-0001", domain.DeprovisionDetails{}, true) 34 35 // then 36 assert.NoError(t, err) 37 } 38 39 func TestDeprovisionEndpoint_DeprovisionExistingInstance(t *testing.T) { 40 // given 41 memoryStorage := storage.NewMemoryStorage() 42 err := memoryStorage.Instances().Insert(fixInstance()) 43 require.NoError(t, err) 44 45 queue := &automock.Queue{} 46 queue.On("Add", mock.AnythingOfType("string")) 47 48 svc := NewDeprovision(memoryStorage.Instances(), memoryStorage.Operations(), queue, logrus.StandardLogger()) 49 50 // when 51 _, err = svc.Deprovision(context.TODO(), instanceID, domain.DeprovisionDetails{}, true) 52 53 // then 54 require.NoError(t, err) 55 operation, err := memoryStorage.Operations().GetDeprovisioningOperationByInstanceID(instanceID) 56 require.NoError(t, err) 57 assert.Equal(t, domain.LastOperationState("pending"), operation.State) 58 } 59 60 func TestDeprovisionEndpoint_DeprovisionExistingOperationInProgress(t *testing.T) { 61 // given 62 memoryStorage := storage.NewMemoryStorage() 63 err := memoryStorage.Instances().Insert(fixInstance()) 64 require.NoError(t, err) 65 66 err = memoryStorage.Operations().InsertDeprovisioningOperation(fixDeprovisioningOperation(domain.InProgress)) 67 require.NoError(t, err) 68 69 queue := &automock.Queue{} 70 queue.On("Add", mock.AnythingOfType("string")) 71 72 svc := NewDeprovision(memoryStorage.Instances(), memoryStorage.Operations(), queue, logrus.StandardLogger()) 73 74 // when 75 res, err := svc.Deprovision(context.TODO(), instanceID, domain.DeprovisionDetails{}, true) 76 77 // then 78 require.NoError(t, err) 79 assert.Equal(t, operationID, res.OperationData) 80 81 operation, err := memoryStorage.Operations().GetDeprovisioningOperationByInstanceID(instanceID) 82 require.NoError(t, err) 83 assert.Equal(t, domain.InProgress, operation.State) 84 assert.Equal(t, "", operation.ProvisionerOperationID) 85 } 86 87 func TestDeprovisionEndpoint_DeprovisionExistingOperationFailed(t *testing.T) { 88 // given 89 memoryStorage := storage.NewMemoryStorage() 90 err := memoryStorage.Instances().Insert(fixInstance()) 91 require.NoError(t, err) 92 93 err = memoryStorage.Operations().InsertDeprovisioningOperation(fixDeprovisioningOperation(domain.Failed)) 94 require.NoError(t, err) 95 96 queue := &automock.Queue{} 97 queue.On("Add", mock.Anything) 98 99 svc := NewDeprovision(memoryStorage.Instances(), memoryStorage.Operations(), queue, logrus.StandardLogger()) 100 101 // when 102 res, err := svc.Deprovision(context.TODO(), instanceID, domain.DeprovisionDetails{}, true) 103 104 // then 105 require.NoError(t, err) 106 assert.NotEqual(t, operationID, res.OperationData) 107 108 operation, err := memoryStorage.Operations().GetDeprovisioningOperationByInstanceID(instanceID) 109 require.NoError(t, err) 110 assert.Equal(t, domain.LastOperationState("pending"), operation.State) 111 } 112 113 func fixDeprovisioningOperation(state domain.LastOperationState) internal.DeprovisioningOperation { 114 deprovisioningOperation := fixture.FixDeprovisioningOperation(operationID, instanceID) 115 deprovisioningOperation.State = state 116 117 return deprovisioningOperation 118 } 119 120 func fixInstance() internal.Instance { 121 instance := fixture.FixInstance(instanceID) 122 instance.ServicePlanID = planID 123 124 return instance 125 }