github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtime/converter_test.go (about) 1 package runtime 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/kyma-project/kyma-environment-broker/common/orchestration" 9 "github.com/kyma-project/kyma-environment-broker/common/runtime" 10 "github.com/kyma-project/kyma-environment-broker/internal" 11 "github.com/pivotal-cf/brokerapi/v8/domain" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestConverting_Provisioning(t *testing.T) { 16 // given 17 instance := fixInstance() 18 svc := NewConverter("eu") 19 20 // when 21 dto, _ := svc.NewDTO(instance) 22 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.InProgress, time.Now())) 23 24 // then 25 assert.Equal(t, runtime.StateProvisioning, dto.Status.State) 26 } 27 28 func TestConverting_Provisioned(t *testing.T) { 29 // given 30 instance := fixInstance() 31 svc := NewConverter("eu") 32 33 // when 34 dto, _ := svc.NewDTO(instance) 35 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 36 37 // then 38 assert.Equal(t, runtime.StateSucceeded, dto.Status.State) 39 } 40 41 func TestConverting_ProvisioningFailed(t *testing.T) { 42 // given 43 instance := fixInstance() 44 svc := NewConverter("eu") 45 46 // when 47 dto, _ := svc.NewDTO(instance) 48 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Failed, time.Now())) 49 50 // then 51 assert.Equal(t, runtime.StateFailed, dto.Status.State) 52 } 53 54 func TestConverting_Updating(t *testing.T) { 55 // given 56 instance := fixInstance() 57 svc := NewConverter("eu") 58 59 // when 60 dto, _ := svc.NewDTO(instance) 61 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 62 svc.ApplyUpdateOperations(&dto, []internal.UpdatingOperation{{ 63 Operation: internal.Operation{ 64 CreatedAt: time.Now().Add(time.Second), 65 ID: "prov-id", 66 State: domain.InProgress, 67 }, 68 }}, 1) 69 70 // then 71 assert.Equal(t, runtime.StateUpdating, dto.Status.State) 72 } 73 74 func TestConverting_UpdateFailed(t *testing.T) { 75 // given 76 instance := fixInstance() 77 svc := NewConverter("eu") 78 79 // when 80 dto, _ := svc.NewDTO(instance) 81 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 82 svc.ApplyUpdateOperations(&dto, []internal.UpdatingOperation{{ 83 Operation: internal.Operation{ 84 CreatedAt: time.Now().Add(time.Second), 85 ID: "prov-id", 86 State: domain.Failed, 87 }, 88 }}, 1) 89 90 // then 91 assert.Equal(t, runtime.StateError, dto.Status.State) 92 } 93 94 func TestConverting_Suspending(t *testing.T) { 95 t.Run("last operation should be deprovisioning", func(t *testing.T) { 96 // given 97 instance := fixInstance() 98 svc := NewConverter("eu") 99 100 // when 101 dto, _ := svc.NewDTO(instance) 102 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 103 svc.ApplySuspensionOperations(&dto, fixSuspensionOperation(domain.InProgress, time.Now().Add(time.Second))) 104 105 // then 106 assert.Equal(t, runtime.StateDeprovisioning, dto.Status.State) 107 }) 108 109 t.Run("last operation should not be deprovisioning when it is pending", func(t *testing.T) { 110 // given 111 instance := fixInstance() 112 svc := NewConverter("eu") 113 114 // when 115 dto, _ := svc.NewDTO(instance) 116 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.InProgress, time.Now())) 117 svc.ApplySuspensionOperations(&dto, fixSuspensionOperation(orchestration.Pending, time.Now().Add(time.Second))) 118 119 // then 120 assert.Equal(t, runtime.StateProvisioning, dto.Status.State) 121 }) 122 } 123 124 func TestConverting_Deprovisioning(t *testing.T) { 125 t.Run("last operation should be deprovisioning", func(t *testing.T) { 126 // given 127 instance := fixInstance() 128 svc := NewConverter("eu") 129 130 // when 131 dto, _ := svc.NewDTO(instance) 132 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 133 svc.ApplyDeprovisioningOperation(&dto, fixDeprovisionOperation(domain.InProgress, time.Now().Add(time.Second))) 134 135 // then 136 assert.Equal(t, runtime.StateDeprovisioning, dto.Status.State) 137 }) 138 139 t.Run("last operation should not be deprovisioning when it is pending", func(t *testing.T) { 140 // given 141 instance := fixInstance() 142 svc := NewConverter("eu") 143 144 // when 145 dto, _ := svc.NewDTO(instance) 146 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.InProgress, time.Now())) 147 svc.ApplyDeprovisioningOperation(&dto, fixDeprovisionOperation(orchestration.Pending, time.Now().Add(time.Second))) 148 149 // then 150 assert.Equal(t, runtime.StateProvisioning, dto.Status.State) 151 }) 152 } 153 154 func TestConverting_DeprovisionFailed(t *testing.T) { 155 // given 156 instance := fixInstance() 157 svc := NewConverter("eu") 158 159 // when 160 dto, _ := svc.NewDTO(instance) 161 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 162 svc.ApplyDeprovisioningOperation(&dto, fixDeprovisionOperation(domain.Failed, time.Now().Add(time.Second))) 163 164 // then 165 assert.Equal(t, runtime.StateFailed, dto.Status.State) 166 } 167 168 func TestConverting_SuspendFailed(t *testing.T) { 169 // given 170 instance := fixInstance() 171 svc := NewConverter("eu") 172 173 // when 174 dto, _ := svc.NewDTO(instance) 175 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 176 svc.ApplySuspensionOperations(&dto, fixSuspensionOperation(domain.Failed, time.Now().Add(time.Second))) 177 178 // then 179 assert.Equal(t, runtime.StateFailed, dto.Status.State) 180 } 181 182 func TestConverting_SuspendedAndUpdated(t *testing.T) { 183 // given 184 instance := fixInstance() 185 svc := NewConverter("eu") 186 187 // when 188 dto, _ := svc.NewDTO(instance) 189 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 190 svc.ApplySuspensionOperations(&dto, fixSuspensionOperation(domain.Succeeded, time.Now().Add(time.Second))) 191 svc.ApplyUpdateOperations(&dto, []internal.UpdatingOperation{{ 192 Operation: internal.Operation{ 193 CreatedAt: time.Now().Add(2 * time.Second), 194 ID: "prov-id", 195 State: domain.Succeeded, 196 }, 197 }}, 1) 198 199 // then 200 assert.Equal(t, runtime.StateSuspended, dto.Status.State) 201 } 202 203 func TestConverting_SuspendedAndUpdateFAiled(t *testing.T) { 204 // given 205 instance := fixInstance() 206 svc := NewConverter("eu") 207 208 // when 209 dto, _ := svc.NewDTO(instance) 210 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 211 svc.ApplySuspensionOperations(&dto, fixSuspensionOperation(domain.Succeeded, time.Now().Add(time.Second))) 212 svc.ApplyUpdateOperations(&dto, []internal.UpdatingOperation{{ 213 Operation: internal.Operation{ 214 CreatedAt: time.Now().Add(2 * time.Second), 215 ID: "prov-id", 216 State: domain.Failed, 217 }, 218 }}, 1) 219 220 // then 221 assert.Equal(t, runtime.StateSuspended, dto.Status.State) 222 } 223 224 func TestConverting_ProvisioningOperationConverter(t *testing.T) { 225 // given 226 instance := fixInstance() 227 svc := NewConverter("eu") 228 229 // when 230 dto, _ := svc.NewDTO(instance) 231 232 //expected stages in order 233 expected := []string{"start", "create_runtime", "check_kyma", "post_actions"} 234 235 t.Run("runtime and finished orders should be not set", func(t *testing.T) { 236 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperation(domain.Succeeded, time.Now())) 237 238 // then 239 assert.Equal(t, []string(nil), dto.Status.Provisioning.FinishedStages) 240 assert.Equal(t, "", dto.Status.Provisioning.RuntimeVersion) 241 }) 242 243 t.Run("runtime and finished orders should be set in order", func(t *testing.T) { 244 svc.ApplyProvisioningOperation(&dto, fixProvisioningOperationWithStagesAndVersion(domain.Succeeded, time.Now())) 245 246 // then 247 assert.True(t, reflect.DeepEqual(expected, dto.Status.Provisioning.FinishedStages)) 248 assert.Equal(t, "2.0", dto.Status.Provisioning.RuntimeVersion) 249 }) 250 } 251 252 func fixSuspensionOperation(state domain.LastOperationState, createdAt time.Time) []internal.DeprovisioningOperation { 253 return []internal.DeprovisioningOperation{{ 254 Operation: internal.Operation{ 255 CreatedAt: createdAt, 256 ID: "s-id", 257 State: state, 258 Temporary: true, 259 }, 260 }} 261 } 262 263 func fixDeprovisionOperation(state domain.LastOperationState, createdAt time.Time) *internal.DeprovisioningOperation { 264 return &internal.DeprovisioningOperation{ 265 Operation: internal.Operation{ 266 CreatedAt: createdAt, 267 ID: "s-id", 268 State: state, 269 }, 270 } 271 } 272 273 func fixInstance() internal.Instance { 274 return internal.Instance{ 275 InstanceID: "instance-id", 276 RuntimeID: "runtime-id", 277 GlobalAccountID: "global-account-id", 278 SubscriptionGlobalAccountID: "subgid", 279 SubAccountID: "sub-account-id", 280 } 281 } 282 283 func fixProvisioningOperation(state domain.LastOperationState, createdAt time.Time) *internal.ProvisioningOperation { 284 return &internal.ProvisioningOperation{ 285 Operation: internal.Operation{ 286 CreatedAt: createdAt, 287 ID: "prov-id", 288 State: state, 289 }, 290 } 291 } 292 293 func fixProvisioningOperationWithStagesAndVersion(state domain.LastOperationState, createdAt time.Time) *internal.ProvisioningOperation { 294 return &internal.ProvisioningOperation{ 295 Operation: internal.Operation{ 296 CreatedAt: createdAt, 297 ID: "prov-id", 298 State: state, 299 FinishedStages: []string{"start", "create_runtime", "check_kyma", "post_actions"}, 300 RuntimeVersion: internal.RuntimeVersionData{ 301 Version: "2.0", 302 Origin: "default", 303 MajorVersion: 2, 304 }, 305 }, 306 } 307 }