github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/instance_last_operation_test.go (about) 1 package broker_test 2 3 import ( 4 "context" 5 "testing" 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/broker" 10 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 11 "github.com/kyma-project/kyma-environment-broker/internal/storage" 12 "github.com/pivotal-cf/brokerapi/v8/domain" 13 "github.com/sirupsen/logrus" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 const ( 18 operationID = "23caac24-c317-47d0-bd2f-6b1bf4bdba99" 19 operationDescription = "some operation status description" 20 instID = "c39d9b98-5ed9-4a68-b786-f26ce93a734f" 21 ) 22 23 func TestLastOperation_LastOperation(t *testing.T) { 24 t.Run("Should return last operation when operation ID provided", func(t *testing.T) { 25 // given 26 memoryStorage := storage.NewMemoryStorage() 27 err := memoryStorage.Operations().InsertOperation(fixOperation()) 28 assert.NoError(t, err) 29 30 lastOperationEndpoint := broker.NewLastOperation(memoryStorage.Operations(), logrus.StandardLogger()) 31 32 // when 33 response, err := lastOperationEndpoint.LastOperation(context.TODO(), instID, domain.PollDetails{OperationData: operationID}) 34 assert.NoError(t, err) 35 36 // then 37 assert.Equal(t, domain.LastOperation{ 38 State: domain.Succeeded, 39 Description: operationDescription, 40 }, response) 41 }) 42 t.Run("Should return last operation when operation ID not provided", func(t *testing.T) { 43 // given 44 memoryStorage := storage.NewMemoryStorage() 45 err := memoryStorage.Operations().InsertOperation(fixOperation()) 46 assert.NoError(t, err) 47 48 lastOperationEndpoint := broker.NewLastOperation(memoryStorage.Operations(), logrus.StandardLogger()) 49 50 // when 51 response, err := lastOperationEndpoint.LastOperation(context.TODO(), instID, domain.PollDetails{OperationData: ""}) 52 assert.NoError(t, err) 53 54 // then 55 assert.Equal(t, domain.LastOperation{ 56 State: domain.Succeeded, 57 Description: operationDescription, 58 }, response) 59 }) 60 t.Run("Should convert operation's pending state to in progress", func(t *testing.T) { 61 // given 62 memoryStorage := storage.NewMemoryStorage() 63 updateOp := fixture.FixUpdatingOperation(operationID, instID) 64 updateOp.State = orchestration.Pending 65 err := memoryStorage.Operations().InsertUpdatingOperation(updateOp) 66 assert.NoError(t, err) 67 68 lastOperationEndpoint := broker.NewLastOperation(memoryStorage.Operations(), logrus.StandardLogger()) 69 70 // when 71 response, err := lastOperationEndpoint.LastOperation(context.TODO(), instID, domain.PollDetails{OperationData: ""}) 72 assert.Error(t, err, "instance operation with instance_id %s not found", instID) 73 74 // then 75 assert.Equal(t, domain.LastOperation{}, response) 76 77 // when 78 response, err = lastOperationEndpoint.LastOperation(context.TODO(), instID, 79 domain.PollDetails{OperationData: operationID}) 80 assert.NoError(t, err) 81 82 // then 83 assert.Equal(t, domain.LastOperation{ 84 State: domain.InProgress, 85 Description: updateOp.Description, 86 }, response) 87 }) 88 t.Run("Should convert operation's retrying state to in progress", func(t *testing.T) { 89 // given 90 memoryStorage := storage.NewMemoryStorage() 91 updateOp := fixture.FixUpdatingOperation(operationID, instID) 92 updateOp.State = orchestration.Retrying 93 err := memoryStorage.Operations().InsertUpdatingOperation(updateOp) 94 assert.NoError(t, err) 95 96 lastOperationEndpoint := broker.NewLastOperation(memoryStorage.Operations(), logrus.StandardLogger()) 97 98 // when 99 response, err := lastOperationEndpoint.LastOperation(context.TODO(), instID, domain.PollDetails{OperationData: ""}) 100 assert.NoError(t, err) 101 102 // then 103 assert.Equal(t, domain.LastOperation{ 104 State: domain.InProgress, 105 Description: updateOp.Description, 106 }, response) 107 108 // when 109 response, err = lastOperationEndpoint.LastOperation(context.TODO(), instID, 110 domain.PollDetails{OperationData: operationID}) 111 assert.NoError(t, err) 112 113 // then 114 assert.Equal(t, domain.LastOperation{ 115 State: domain.InProgress, 116 Description: updateOp.Description, 117 }, response) 118 }) 119 t.Run("Should convert operation's canceling state to succeeded", func(t *testing.T) { 120 // given 121 memoryStorage := storage.NewMemoryStorage() 122 updateOp := fixture.FixUpdatingOperation(operationID, instID) 123 updateOp.State = orchestration.Canceling 124 err := memoryStorage.Operations().InsertUpdatingOperation(updateOp) 125 assert.NoError(t, err) 126 127 lastOperationEndpoint := broker.NewLastOperation(memoryStorage.Operations(), logrus.StandardLogger()) 128 129 // when 130 response, err := lastOperationEndpoint.LastOperation(context.TODO(), instID, domain.PollDetails{OperationData: ""}) 131 assert.NoError(t, err) 132 133 // then 134 assert.Equal(t, domain.LastOperation{ 135 State: domain.Succeeded, 136 Description: updateOp.Description, 137 }, response) 138 139 // when 140 response, err = lastOperationEndpoint.LastOperation(context.TODO(), instID, 141 domain.PollDetails{OperationData: operationID}) 142 assert.NoError(t, err) 143 144 // then 145 assert.Equal(t, domain.LastOperation{ 146 State: domain.Succeeded, 147 Description: updateOp.Description, 148 }, response) 149 }) 150 t.Run("Should convert operation's canceled state to succeeded", func(t *testing.T) { 151 // given 152 memoryStorage := storage.NewMemoryStorage() 153 updateOp := fixture.FixUpdatingOperation(operationID, instID) 154 updateOp.State = orchestration.Canceled 155 err := memoryStorage.Operations().InsertUpdatingOperation(updateOp) 156 assert.NoError(t, err) 157 158 lastOperationEndpoint := broker.NewLastOperation(memoryStorage.Operations(), logrus.StandardLogger()) 159 160 // when 161 response, err := lastOperationEndpoint.LastOperation(context.TODO(), instID, domain.PollDetails{OperationData: ""}) 162 assert.NoError(t, err) 163 164 // then 165 assert.Equal(t, domain.LastOperation{ 166 State: domain.Succeeded, 167 Description: updateOp.Description, 168 }, response) 169 170 // when 171 response, err = lastOperationEndpoint.LastOperation(context.TODO(), instID, 172 domain.PollDetails{OperationData: operationID}) 173 assert.NoError(t, err) 174 175 // then 176 assert.Equal(t, domain.LastOperation{ 177 State: domain.Succeeded, 178 Description: updateOp.Description, 179 }, response) 180 }) 181 } 182 183 func fixOperation() internal.Operation { 184 provisioningOperation := fixture.FixProvisioningOperation(operationID, instID) 185 provisioningOperation.State = domain.Succeeded 186 provisioningOperation.Description = operationDescription 187 188 return provisioningOperation 189 }