github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/init_test.go (about) 1 package deprovisioning 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/kyma-project/kyma-environment-broker/common/orchestration" 8 "github.com/kyma-project/kyma-environment-broker/internal" 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 ) 15 16 const ( 17 fixOperationID = "17f3ddba-1132-466d-a3c5-920f544d7ea6" 18 fixInstanceID = "9d75a545-2e1e-4786-abd8-a37b14e185b9" 19 fixRuntimeID = "ef4e3210-652c-453e-8015-bba1c1cd1e1c" 20 fixGlobalAccountID = "abf73c71-a653-4951-b9c2-a26d6c2cccbd" 21 fixProvisionerOperationID = "e04de524-53b3-4890-b05a-296be393e4ba" 22 ) 23 24 func TestInitStep_happyPath(t *testing.T) { 25 // given 26 log := logrus.New() 27 memoryStorage := storage.NewMemoryStorage() 28 prepareProvisionedInstance(memoryStorage) 29 dOp := prepareDeprovisioningOperation(memoryStorage, orchestration.Pending) 30 31 svc := NewInitStep(memoryStorage.Operations(), memoryStorage.Instances(), 90*time.Second) 32 33 // when 34 op, d, err := svc.Run(dOp, log) 35 36 // then 37 assert.Equal(t, domain.InProgress, op.State) 38 assert.NoError(t, err) 39 assert.Zero(t, d) 40 dbOp, _ := memoryStorage.Operations().GetOperationByID(op.ID) 41 assert.Equal(t, domain.InProgress, dbOp.State) 42 } 43 44 func TestInitStep_existingUpdatingOperation(t *testing.T) { 45 // given 46 log := logrus.New() 47 memoryStorage := storage.NewMemoryStorage() 48 prepareProvisionedInstance(memoryStorage) 49 uOp := fixture.FixUpdatingOperation("uop-id", instanceID) 50 uOp.State = domain.InProgress 51 memoryStorage.Operations().InsertOperation(uOp.Operation) 52 dOp := prepareDeprovisioningOperation(memoryStorage, orchestration.Pending) 53 54 svc := NewInitStep(memoryStorage.Operations(), memoryStorage.Instances(), 90*time.Second) 55 56 // when 57 op, d, err := svc.Run(dOp, log) 58 59 // then 60 assert.Equal(t, orchestration.Pending, string(op.State)) 61 assert.NoError(t, err) 62 assert.NotZero(t, d) 63 dbOp, _ := memoryStorage.Operations().GetOperationByID(op.ID) 64 assert.Equal(t, orchestration.Pending, string(dbOp.State)) 65 } 66 67 func prepareProvisionedInstance(s storage.BrokerStorage) { 68 inst := fixture.FixInstance(instanceID) 69 s.Instances().Insert(inst) 70 pOp := fixture.FixProvisioningOperation("pop-id", instanceID) 71 s.Operations().InsertOperation(pOp) 72 } 73 74 func prepareDeprovisioningOperation(s storage.BrokerStorage, state domain.LastOperationState) internal.Operation { 75 dOperation := fixture.FixDeprovisioningOperation("dop-id", instanceID) 76 dOperation.State = state 77 s.Operations().InsertOperation(dOperation.Operation) 78 return dOperation.Operation 79 } 80 81 func fixDeprovisioningOperation() internal.DeprovisioningOperation { 82 deprovisioniningOperation := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID) 83 return deprovisioniningOperation 84 } 85 86 func fixProvisioningOperation() internal.Operation { 87 provisioningOperation := fixture.FixProvisioningOperation(fixOperationID, fixInstanceID) 88 return provisioningOperation 89 } 90 91 func fixInstanceRuntimeStatus() internal.Instance { 92 instance := fixture.FixInstance(fixInstanceID) 93 instance.RuntimeID = fixRuntimeID 94 instance.GlobalAccountID = fixGlobalAccountID 95 96 return instance 97 }