github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/runtime/resolver_test.go (about) 1 package runtime_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/kyma-incubator/compass/components/director/internal/domain/scenarioassignment" 9 10 dataloader "github.com/kyma-incubator/compass/components/director/internal/dataloaders" 11 pkgmodel "github.com/kyma-incubator/compass/components/director/pkg/model" 12 13 "github.com/google/uuid" 14 15 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant" 16 17 "github.com/stretchr/testify/require" 18 19 "github.com/kyma-incubator/compass/components/director/internal/labelfilter" 20 21 "github.com/kyma-incubator/compass/components/director/internal/domain/runtime/rtmtest" 22 23 "github.com/kyma-incubator/compass/components/director/internal/domain/label" 24 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 25 "github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest" 26 "github.com/kyma-incubator/compass/components/director/pkg/resource" 27 "github.com/kyma-incubator/compass/components/director/pkg/str" 28 29 "github.com/kyma-incubator/compass/components/director/internal/domain/runtime/automock" 30 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 31 32 "github.com/stretchr/testify/mock" 33 34 "github.com/kyma-incubator/compass/components/director/internal/domain/runtime" 35 "github.com/kyma-incubator/compass/components/director/internal/model" 36 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 37 persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock" 38 "github.com/pkg/errors" 39 "github.com/stretchr/testify/assert" 40 ) 41 42 const ( 43 RegionKey = "region" 44 testRegion = "test-region" 45 distinguishLabelValue = "distinguishLabelValue" 46 ) 47 48 var contextParam = mock.MatchedBy(func(ctx context.Context) bool { 49 persistenceOp, err := persistence.FromCtx(ctx) 50 return err == nil && persistenceOp != nil 51 }) 52 53 func TestResolver_CreateRuntime(t *testing.T) { 54 // GIVEN 55 const ( 56 accountTenantID = "c6dea850-ce93-4d65-8e10-432d85f0a244" 57 accountExternalID = "aa02436e-c2f1-44b8-8bf0-2e8519c27e40" 58 extSubaccountID = "extSubaccountID" 59 desc = "Lorem ipsum" 60 ) 61 62 ctx := tenant.SaveToContext(context.TODO(), accountTenantID, accountExternalID) 63 64 modelRuntime := fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Lorem ipsum", "test.ns") 65 gqlRuntime := fixGQLRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns") 66 testErr := errors.New("Test error") 67 68 gqlInput := graphql.RuntimeRegisterInput{ 69 Name: "Foo", 70 Description: str.Ptr(desc), 71 Webhooks: []*graphql.WebhookInput{{ 72 Type: "test webhook", 73 }}, 74 } 75 gqlInputWithSubaccountLabel := graphql.RuntimeRegisterInput{ 76 Name: "Foo", 77 Description: str.Ptr(desc), 78 Labels: graphql.Labels{scenarioassignment.SubaccountIDKey: extSubaccountID}, 79 Webhooks: []*graphql.WebhookInput{{ 80 Type: "test webhook", 81 }}, 82 } 83 gqlInputWithInvalidLabel := graphql.RuntimeRegisterInput{ 84 Name: "Foo", 85 Description: str.Ptr(desc), 86 Labels: graphql.Labels{scenarioassignment.SubaccountIDKey: []string{"firstValue", "secondValue"}}, 87 Webhooks: []*graphql.WebhookInput{{ 88 Type: "test webhook", 89 }}, 90 } 91 modelInput := model.RuntimeRegisterInput{ 92 Name: "Foo", 93 Description: str.Ptr(desc), 94 Webhooks: []*model.WebhookInput{{ 95 Type: "test webhook", 96 }}, 97 } 98 selfRegModelInput := model.RuntimeRegisterInput{ 99 Name: "Foo", 100 Description: str.Ptr(desc), 101 Labels: graphql.Labels{rtmtest.TestDistinguishLabel: "selfRegVal"}, 102 Webhooks: []*model.WebhookInput{{ 103 Type: "test webhook", 104 }}, 105 } 106 selfRegLabels := map[string]interface{}{"xsappnameCMPClone": "clone", RegionKey: testRegion, rtmtest.TestDistinguishLabel: distinguishLabelValue} 107 108 subscriptionFilters := []*labelfilter.LabelFilter{ 109 labelfilter.NewForKeyWithQuery(rtmtest.TestDistinguishLabel, fmt.Sprintf("\"%s\"", distinguishLabelValue)), 110 labelfilter.NewForKeyWithQuery(RegionKey, fmt.Sprintf("\"%s\"", testRegion)), 111 } 112 113 testCases := []struct { 114 Name string 115 PersistenceFn func() *persistenceautomock.PersistenceTx 116 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 117 ServiceFn func() *automock.RuntimeService 118 ConverterFn func() *automock.RuntimeConverter 119 SelfRegManagerFn func() *automock.SelfRegisterManager 120 UUIDSvcFn func() *automock.UidService 121 TenantFetcherFn func() *automock.TenantFetcher 122 123 Input graphql.RuntimeRegisterInput 124 ExpectedRuntime *graphql.Runtime 125 ExpectedErr error 126 }{ 127 { 128 Name: "Success", 129 PersistenceFn: func() *persistenceautomock.PersistenceTx { 130 persistTx := &persistenceautomock.PersistenceTx{} 131 persistTx.On("Commit").Return(nil).Once() 132 return persistTx 133 }, 134 TransactionerFn: txtest.TransactionerThatSucceeds, 135 ServiceFn: func() *automock.RuntimeService { 136 svc := &automock.RuntimeService{} 137 svc.On("Get", contextParam, testUUID).Return(modelRuntime, nil).Once() 138 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 139 svc.On("CreateWithMandatoryLabels", contextParam, modelInput, testUUID, selfRegLabels).Return(nil).Once() 140 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 141 return svc 142 }, 143 ConverterFn: func() *automock.RuntimeConverter { 144 conv := &automock.RuntimeConverter{} 145 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 146 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 147 return conv 148 }, 149 UUIDSvcFn: func() *automock.UidService { 150 svc := &automock.UidService{} 151 svc.On("Generate").Return(testUUID).Once() 152 return svc 153 }, 154 TenantFetcherFn: func() *automock.TenantFetcher { 155 return &automock.TenantFetcher{} 156 }, 157 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesPrepWithNoErrorsAndGetSelfRegDistinguishingLabelKey(selfRegLabels), 158 Input: gqlInput, 159 ExpectedRuntime: gqlRuntime, 160 ExpectedErr: nil, 161 }, 162 { 163 Name: "Success with subaccount label", 164 PersistenceFn: func() *persistenceautomock.PersistenceTx { 165 persistTx := &persistenceautomock.PersistenceTx{} 166 persistTx.On("Commit").Return(nil).Once() 167 return persistTx 168 }, 169 TransactionerFn: txtest.TransactionerThatSucceeds, 170 ServiceFn: func() *automock.RuntimeService { 171 svc := &automock.RuntimeService{} 172 svc.On("Get", contextParam, testUUID).Return(modelRuntime, nil).Once() 173 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 174 svc.On("CreateWithMandatoryLabels", contextParam, modelInput, testUUID, selfRegLabels).Return(nil).Once() 175 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 176 return svc 177 }, 178 ConverterFn: func() *automock.RuntimeConverter { 179 conv := &automock.RuntimeConverter{} 180 conv.On("RegisterInputFromGraphQL", gqlInputWithSubaccountLabel).Return(modelInput, nil).Once() 181 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 182 return conv 183 }, 184 UUIDSvcFn: func() *automock.UidService { 185 svc := &automock.UidService{} 186 svc.On("Generate").Return(testUUID).Once() 187 return svc 188 }, 189 TenantFetcherFn: func() *automock.TenantFetcher { 190 svc := &automock.TenantFetcher{} 191 svc.On("FetchOnDemand", extSubaccountID, accountTenantID).Return(nil).Once() 192 return svc 193 }, 194 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesPrepWithNoErrorsAndGetSelfRegDistinguishingLabelKey(selfRegLabels), 195 Input: gqlInputWithSubaccountLabel, 196 ExpectedRuntime: gqlRuntime, 197 ExpectedErr: nil, 198 }, 199 { 200 Name: "Returns error when runtime converting fails", 201 PersistenceFn: func() *persistenceautomock.PersistenceTx { 202 return &persistenceautomock.PersistenceTx{} 203 }, 204 TransactionerFn: txtest.NoopTransactioner, 205 ServiceFn: func() *automock.RuntimeService { 206 return &automock.RuntimeService{} 207 }, 208 ConverterFn: func() *automock.RuntimeConverter { 209 conv := &automock.RuntimeConverter{} 210 conv.On("RegisterInputFromGraphQL", gqlInputWithInvalidLabel).Return(model.RuntimeRegisterInput{}, testErr).Once() 211 return conv 212 }, 213 UUIDSvcFn: func() *automock.UidService { 214 return &automock.UidService{} 215 }, 216 TenantFetcherFn: func() *automock.TenantFetcher { 217 return &automock.TenantFetcher{} 218 }, 219 SelfRegManagerFn: func() *automock.SelfRegisterManager { 220 return &automock.SelfRegisterManager{} 221 }, 222 Input: gqlInputWithInvalidLabel, 223 ExpectedRuntime: nil, 224 ExpectedErr: testErr, 225 }, 226 { 227 Name: "Returns error when subaccount id label has invalid value", 228 PersistenceFn: func() *persistenceautomock.PersistenceTx { 229 persistTx := &persistenceautomock.PersistenceTx{} 230 return persistTx 231 }, 232 TransactionerFn: txtest.NoopTransactioner, 233 ServiceFn: func() *automock.RuntimeService { 234 svc := &automock.RuntimeService{} 235 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 236 return svc 237 }, 238 ConverterFn: func() *automock.RuntimeConverter { 239 conv := &automock.RuntimeConverter{} 240 conv.On("RegisterInputFromGraphQL", gqlInputWithInvalidLabel).Return(modelInput, nil).Once() 241 return conv 242 }, 243 UUIDSvcFn: func() *automock.UidService { 244 svc := &automock.UidService{} 245 svc.On("Generate").Return(testUUID).Once() 246 return svc 247 }, 248 TenantFetcherFn: func() *automock.TenantFetcher { 249 return &automock.TenantFetcher{} 250 }, 251 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesPrepWithNoErrors(selfRegLabels), 252 Input: gqlInputWithInvalidLabel, 253 ExpectedRuntime: nil, 254 ExpectedErr: errors.New("while converting global_subaccount_id label: cannot cast label value"), 255 }, 256 { 257 Name: "Returns error when cannot fetch tenant details from tenant provider", 258 PersistenceFn: func() *persistenceautomock.PersistenceTx { 259 persistTx := &persistenceautomock.PersistenceTx{} 260 return persistTx 261 }, 262 TransactionerFn: txtest.NoopTransactioner, 263 ServiceFn: func() *automock.RuntimeService { 264 svc := &automock.RuntimeService{} 265 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 266 return svc 267 }, 268 ConverterFn: func() *automock.RuntimeConverter { 269 conv := &automock.RuntimeConverter{} 270 conv.On("RegisterInputFromGraphQL", gqlInputWithSubaccountLabel).Return(modelInput, nil).Once() 271 return conv 272 }, 273 UUIDSvcFn: func() *automock.UidService { 274 svc := &automock.UidService{} 275 svc.On("Generate").Return(testUUID).Once() 276 return svc 277 }, 278 TenantFetcherFn: func() *automock.TenantFetcher { 279 svc := &automock.TenantFetcher{} 280 svc.On("FetchOnDemand", extSubaccountID, accountTenantID).Return(testErr).Once() 281 return svc 282 }, 283 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesPrepWithNoErrors(selfRegLabels), 284 Input: gqlInputWithSubaccountLabel, 285 ExpectedRuntime: nil, 286 ExpectedErr: testErr, 287 }, 288 { 289 Name: "Returns error when runtime creation failed", 290 PersistenceFn: func() *persistenceautomock.PersistenceTx { 291 return &persistenceautomock.PersistenceTx{} 292 }, 293 TransactionerFn: txtest.TransactionerThatDoesARollback, 294 ServiceFn: func() *automock.RuntimeService { 295 svc := &automock.RuntimeService{} 296 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 297 svc.On("CreateWithMandatoryLabels", contextParam, modelInput, testUUID, selfRegLabels).Return(testErr).Once() 298 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 299 return svc 300 }, 301 ConverterFn: func() *automock.RuntimeConverter { 302 conv := &automock.RuntimeConverter{} 303 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 304 return conv 305 }, 306 UUIDSvcFn: func() *automock.UidService { 307 svc := &automock.UidService{} 308 svc.On("Generate").Return(testUUID).Once() 309 return svc 310 }, 311 TenantFetcherFn: func() *automock.TenantFetcher { 312 return &automock.TenantFetcher{} 313 }, 314 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanupFunc(selfRegLabels), 315 Input: gqlInput, 316 ExpectedRuntime: nil, 317 ExpectedErr: testErr, 318 }, 319 { 320 Name: "Returns error when runtime self registration fails", 321 PersistenceFn: func() *persistenceautomock.PersistenceTx { 322 persistTx := &persistenceautomock.PersistenceTx{} 323 return persistTx 324 }, 325 TransactionerFn: txtest.TransactionerThatDoesARollback, 326 ServiceFn: func() *automock.RuntimeService { 327 svc := &automock.RuntimeService{} 328 svc.On("CreateWithMandatoryLabels", contextParam, selfRegModelInput, testUUID, selfRegLabels).Return(testErr).Once() 329 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 330 svc.On("UnsafeExtractModifiableLabels", selfRegModelInput.Labels).Return(selfRegModelInput.Labels, nil).Once() 331 return svc 332 }, 333 ConverterFn: func() *automock.RuntimeConverter { 334 conv := &automock.RuntimeConverter{} 335 conv.On("RegisterInputFromGraphQL", gqlInput).Return(selfRegModelInput, nil).Once() 336 return conv 337 }, 338 UUIDSvcFn: func() *automock.UidService { 339 svc := &automock.UidService{} 340 svc.On("Generate").Return(testUUID).Once() 341 return svc 342 }, 343 TenantFetcherFn: func() *automock.TenantFetcher { 344 return &automock.TenantFetcher{} 345 }, 346 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsNoErrors(selfRegLabels), 347 Input: gqlInput, 348 ExpectedRuntime: nil, 349 ExpectedErr: testErr, 350 }, 351 { 352 Name: "Returns error when runtime cleanup fails", 353 PersistenceFn: func() *persistenceautomock.PersistenceTx { 354 persistTx := &persistenceautomock.PersistenceTx{} 355 return persistTx 356 }, 357 TransactionerFn: txtest.TransactionerThatDoesARollback, 358 ServiceFn: func() *automock.RuntimeService { 359 svc := &automock.RuntimeService{} 360 svc.On("Get", contextParam, testUUID).Return(nil, testErr).Once() 361 svc.On("CreateWithMandatoryLabels", contextParam, selfRegModelInput, testUUID, selfRegLabels).Return(nil).Once() 362 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 363 svc.On("UnsafeExtractModifiableLabels", selfRegModelInput.Labels).Return(selfRegModelInput.Labels, nil).Once() 364 return svc 365 }, 366 ConverterFn: func() *automock.RuntimeConverter { 367 conv := &automock.RuntimeConverter{} 368 conv.On("RegisterInputFromGraphQL", gqlInput).Return(selfRegModelInput, nil).Once() 369 return conv 370 }, 371 UUIDSvcFn: func() *automock.UidService { 372 svc := &automock.UidService{} 373 svc.On("Generate").Return(testUUID).Once() 374 return svc 375 }, 376 TenantFetcherFn: func() *automock.TenantFetcher { 377 return &automock.TenantFetcher{} 378 }, 379 SelfRegManagerFn: rtmtest.SelfRegManagerThatFailsOnCleanup(selfRegLabels), 380 Input: gqlInput, 381 ExpectedRuntime: nil, 382 ExpectedErr: testErr, 383 }, 384 { 385 Name: "Returns error when runtime retrieval failed", 386 PersistenceFn: func() *persistenceautomock.PersistenceTx { 387 persistTx := &persistenceautomock.PersistenceTx{} 388 return persistTx 389 }, 390 TransactionerFn: txtest.TransactionerThatDoesARollback, 391 ServiceFn: func() *automock.RuntimeService { 392 svc := &automock.RuntimeService{} 393 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 394 svc.On("CreateWithMandatoryLabels", contextParam, modelInput, testUUID, selfRegLabels).Return(nil).Once() 395 svc.On("Get", contextParam, testUUID).Return(nil, testErr).Once() 396 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 397 return svc 398 }, 399 ConverterFn: func() *automock.RuntimeConverter { 400 conv := &automock.RuntimeConverter{} 401 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 402 return conv 403 }, 404 UUIDSvcFn: func() *automock.UidService { 405 svc := &automock.UidService{} 406 svc.On("Generate").Return(testUUID).Once() 407 return svc 408 }, 409 TenantFetcherFn: func() *automock.TenantFetcher { 410 return &automock.TenantFetcher{} 411 }, 412 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanupFunc(selfRegLabels), 413 Input: gqlInput, 414 ExpectedRuntime: nil, 415 ExpectedErr: testErr, 416 }, 417 { 418 Name: "Returns error when self registered runtime retrieval failed", 419 PersistenceFn: func() *persistenceautomock.PersistenceTx { 420 persistTx := &persistenceautomock.PersistenceTx{} 421 return persistTx 422 }, 423 TransactionerFn: txtest.TransactionerThatDoesARollback, 424 ServiceFn: func() *automock.RuntimeService { 425 svc := &automock.RuntimeService{} 426 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, apperrors.NewNotFoundErrorWithType(resource.Runtime)).Once() 427 svc.On("CreateWithMandatoryLabels", contextParam, selfRegModelInput, testUUID, selfRegLabels).Return(nil).Once() 428 svc.On("Get", contextParam, testUUID).Return(nil, testErr).Once() 429 svc.On("UnsafeExtractModifiableLabels", selfRegModelInput.Labels).Return(selfRegModelInput.Labels, nil).Once() 430 return svc 431 }, 432 ConverterFn: func() *automock.RuntimeConverter { 433 conv := &automock.RuntimeConverter{} 434 conv.On("RegisterInputFromGraphQL", gqlInput).Return(selfRegModelInput, nil).Once() 435 return conv 436 }, 437 UUIDSvcFn: func() *automock.UidService { 438 svc := &automock.UidService{} 439 svc.On("Generate").Return(testUUID).Once() 440 return svc 441 }, 442 TenantFetcherFn: func() *automock.TenantFetcher { 443 return &automock.TenantFetcher{} 444 }, 445 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsNoErrors(selfRegLabels), 446 Input: gqlInput, 447 ExpectedRuntime: nil, 448 ExpectedErr: testErr, 449 }, 450 { 451 Name: "Returns error when preparation for self-registration failed", 452 PersistenceFn: func() *persistenceautomock.PersistenceTx { 453 return &persistenceautomock.PersistenceTx{} 454 }, 455 TransactionerFn: txtest.NoopTransactioner, 456 ServiceFn: func() *automock.RuntimeService { 457 svc := &automock.RuntimeService{} 458 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 459 return svc 460 }, 461 ConverterFn: func() *automock.RuntimeConverter { 462 conv := &automock.RuntimeConverter{} 463 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 464 return conv 465 }, 466 UUIDSvcFn: func() *automock.UidService { 467 svc := &automock.UidService{} 468 svc.On("Generate").Return(testUUID).Once() 469 return svc 470 }, 471 TenantFetcherFn: func() *automock.TenantFetcher { 472 return &automock.TenantFetcher{} 473 }, 474 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsErrorOnPrep, 475 Input: gqlInput, 476 ExpectedRuntime: nil, 477 ExpectedErr: errors.New(rtmtest.SelfRegErrorMsg), 478 }, 479 { 480 Name: "Returns error when listing provider runtimes by filters", 481 PersistenceFn: func() *persistenceautomock.PersistenceTx { 482 return &persistenceautomock.PersistenceTx{} 483 }, 484 TransactionerFn: txtest.TransactionerThatDoesARollback, 485 ServiceFn: func() *automock.RuntimeService { 486 svc := &automock.RuntimeService{} 487 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(nil, testErr).Once() 488 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 489 return svc 490 }, 491 ConverterFn: func() *automock.RuntimeConverter { 492 conv := &automock.RuntimeConverter{} 493 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 494 return conv 495 }, 496 UUIDSvcFn: func() *automock.UidService { 497 svc := &automock.UidService{} 498 svc.On("Generate").Return(testUUID).Once() 499 return svc 500 }, 501 TenantFetcherFn: func() *automock.TenantFetcher { 502 return &automock.TenantFetcher{} 503 }, 504 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanupFunc(selfRegLabels), 505 Input: gqlInput, 506 ExpectedRuntime: nil, 507 ExpectedErr: testErr, 508 }, 509 { 510 Name: "Returns error when there is already registered provider runtime with the same region and distinguish label", 511 PersistenceFn: func() *persistenceautomock.PersistenceTx { 512 return &persistenceautomock.PersistenceTx{} 513 }, 514 TransactionerFn: txtest.TransactionerThatDoesARollback, 515 ServiceFn: func() *automock.RuntimeService { 516 svc := &automock.RuntimeService{} 517 svc.On("GetByFilters", contextParam, subscriptionFilters).Return(modelRuntime, nil).Once() 518 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(modelInput.Labels, nil).Once() 519 return svc 520 }, 521 ConverterFn: func() *automock.RuntimeConverter { 522 conv := &automock.RuntimeConverter{} 523 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 524 return conv 525 }, 526 UUIDSvcFn: func() *automock.UidService { 527 svc := &automock.UidService{} 528 svc.On("Generate").Return(testUUID).Once() 529 return svc 530 }, 531 TenantFetcherFn: func() *automock.TenantFetcher { 532 return &automock.TenantFetcher{} 533 }, 534 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanupFunc(selfRegLabels), 535 Input: gqlInput, 536 ExpectedRuntime: nil, 537 ExpectedErr: errors.New(fmt.Sprintf("cannot have more than one runtime with labels %q: %q and %q: %q", RegionKey, testRegion, rtmtest.TestDistinguishLabel, distinguishLabelValue)), 538 }, 539 { 540 Name: "Returns error when can't extract modifiable labels", 541 PersistenceFn: func() *persistenceautomock.PersistenceTx { 542 return &persistenceautomock.PersistenceTx{} 543 }, 544 TransactionerFn: txtest.NoopTransactioner, 545 ServiceFn: func() *automock.RuntimeService { 546 svc := &automock.RuntimeService{} 547 svc.On("UnsafeExtractModifiableLabels", modelInput.Labels).Return(nil, testErr).Once() 548 return svc 549 }, 550 ConverterFn: func() *automock.RuntimeConverter { 551 conv := &automock.RuntimeConverter{} 552 conv.On("RegisterInputFromGraphQL", gqlInput).Return(modelInput, nil).Once() 553 return conv 554 }, 555 UUIDSvcFn: func() *automock.UidService { 556 svc := &automock.UidService{} 557 return svc 558 }, 559 TenantFetcherFn: func() *automock.TenantFetcher { 560 return &automock.TenantFetcher{} 561 }, 562 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 563 Input: gqlInput, 564 ExpectedRuntime: nil, 565 ExpectedErr: testErr, 566 }, 567 } 568 569 for _, testCase := range testCases { 570 t.Run(testCase.Name, func(t *testing.T) { 571 persistTx := testCase.PersistenceFn() 572 transact := testCase.TransactionerFn(persistTx) 573 svc := testCase.ServiceFn() 574 converter := testCase.ConverterFn() 575 selfRegManager := testCase.SelfRegManagerFn() 576 uuidSvc := testCase.UUIDSvcFn() 577 fetcher := testCase.TenantFetcherFn() 578 579 defer mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx, selfRegManager, fetcher) 580 581 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, fetcher, nil) 582 583 // WHEN 584 result, err := resolver.RegisterRuntime(ctx, testCase.Input) 585 586 // then 587 assert.Equal(t, testCase.ExpectedRuntime, result) 588 if testCase.ExpectedErr != nil { 589 assert.Contains(t, err.Error(), testCase.ExpectedErr.Error()) 590 } else { 591 assert.NoError(t, err) 592 } 593 }) 594 } 595 } 596 597 func TestResolver_UpdateRuntime(t *testing.T) { 598 // GIVEN 599 modelRuntime := fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Lorem ipsum", "test.ns") 600 gqlRuntime := fixGQLRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns") 601 testErr := errors.New("Test error") 602 603 desc := "Lorem ipsum" 604 gqlInput := graphql.RuntimeUpdateInput{ 605 Name: "Foo", 606 Description: str.Ptr(desc), 607 } 608 modelInput := model.RuntimeUpdateInput{ 609 Name: "Foo", 610 Description: str.Ptr(desc), 611 } 612 runtimeID := "foo" 613 emptyLabels := make(map[string]interface{}) 614 615 testCases := []struct { 616 Name string 617 PersistenceFn func() *persistenceautomock.PersistenceTx 618 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 619 ServiceFn func() *automock.RuntimeService 620 ConverterFn func() *automock.RuntimeConverter 621 SelfRegManagerFn func() *automock.SelfRegisterManager 622 RuntimeID string 623 Input graphql.RuntimeUpdateInput 624 ExpectedRuntime *graphql.Runtime 625 ExpectedErr error 626 }{ 627 { 628 Name: "Success", 629 PersistenceFn: func() *persistenceautomock.PersistenceTx { 630 persistTx := &persistenceautomock.PersistenceTx{} 631 persistTx.On("Commit").Return(nil).Once() 632 return persistTx 633 }, 634 TransactionerFn: txtest.TransactionerThatSucceeds, 635 ServiceFn: func() *automock.RuntimeService { 636 svc := &automock.RuntimeService{} 637 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 638 svc.On("Update", contextParam, runtimeID, modelInput).Return(nil).Once() 639 return svc 640 }, 641 ConverterFn: func() *automock.RuntimeConverter { 642 conv := &automock.RuntimeConverter{} 643 conv.On("UpdateInputFromGraphQL", gqlInput).Return(modelInput).Once() 644 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 645 return conv 646 }, 647 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsNoErrors(emptyLabels), 648 RuntimeID: runtimeID, 649 Input: gqlInput, 650 ExpectedRuntime: gqlRuntime, 651 ExpectedErr: nil, 652 }, 653 { 654 Name: "Returns error when runtime update failed", 655 PersistenceFn: func() *persistenceautomock.PersistenceTx { 656 persistTx := &persistenceautomock.PersistenceTx{} 657 return persistTx 658 }, 659 TransactionerFn: txtest.TransactionerThatDoesARollback, 660 ServiceFn: func() *automock.RuntimeService { 661 svc := &automock.RuntimeService{} 662 svc.On("Update", contextParam, runtimeID, modelInput).Return(testErr).Once() 663 return svc 664 }, 665 ConverterFn: func() *automock.RuntimeConverter { 666 conv := &automock.RuntimeConverter{} 667 conv.On("UpdateInputFromGraphQL", gqlInput).Return(modelInput).Once() 668 return conv 669 }, 670 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsNoErrors(emptyLabels), 671 RuntimeID: runtimeID, 672 Input: gqlInput, 673 ExpectedRuntime: nil, 674 ExpectedErr: testErr, 675 }, 676 { 677 Name: "Returns error when runtime retrieval failed", 678 PersistenceFn: func() *persistenceautomock.PersistenceTx { 679 persistTx := &persistenceautomock.PersistenceTx{} 680 return persistTx 681 }, 682 TransactionerFn: txtest.TransactionerThatDoesARollback, 683 ServiceFn: func() *automock.RuntimeService { 684 svc := &automock.RuntimeService{} 685 svc.On("Update", contextParam, runtimeID, modelInput).Return(nil).Once() 686 svc.On("Get", contextParam, "foo").Return(nil, testErr).Once() 687 return svc 688 }, 689 ConverterFn: func() *automock.RuntimeConverter { 690 conv := &automock.RuntimeConverter{} 691 conv.On("UpdateInputFromGraphQL", gqlInput).Return(modelInput).Once() 692 return conv 693 }, 694 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsNoErrors(emptyLabels), 695 RuntimeID: runtimeID, 696 Input: gqlInput, 697 ExpectedRuntime: nil, 698 ExpectedErr: testErr, 699 }, 700 } 701 702 for _, testCase := range testCases { 703 t.Run(testCase.Name, func(t *testing.T) { 704 persistTx := testCase.PersistenceFn() 705 transact := testCase.TransactionerFn(persistTx) 706 svc := testCase.ServiceFn() 707 converter := testCase.ConverterFn() 708 selfRegMng := testCase.SelfRegManagerFn() 709 uuidSvc := &automock.UidService{} 710 711 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegMng, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 712 713 // WHEN 714 result, err := resolver.UpdateRuntime(context.TODO(), testCase.RuntimeID, testCase.Input) 715 716 // then 717 assert.Equal(t, testCase.ExpectedRuntime, result) 718 assert.Equal(t, testCase.ExpectedErr, err) 719 720 mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx) 721 }) 722 } 723 } 724 725 func TestResolver_DeleteRuntime(t *testing.T) { 726 // GIVEN 727 modelRuntime := fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Bar", "test.ns") 728 gqlRuntime := fixGQLRuntime(t, "foo", "Foo", "Bar", "test.ns") 729 testErr := errors.New("Test error") 730 labelNotFoundErr := apperrors.NewNotFoundError(resource.Label, "") 731 scenarioAssignmentNotFoundErr := apperrors.NewNotFoundError(resource.AutomaticScenarioAssigment, "") 732 txGen := txtest.NewTransactionContextGenerator(testErr) 733 testAuths := fixOAuths() 734 emptyScenariosLabel := &model.Label{Key: model.ScenariosKey, Value: []interface{}{}} 735 singleScenarioLabel := &model.Label{Key: model.ScenariosKey, Value: []interface{}{"scenario-0"}} 736 multiScenariosLabel := &model.Label{Key: model.ScenariosKey, Value: []interface{}{"scenario-0", "scenario-1", "scenario-2", "scenario-3"}} 737 738 testCases := []struct { 739 Name string 740 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 741 ServiceFn func() *automock.RuntimeService 742 ScenarioAssignmentFn func() *automock.ScenarioAssignmentService 743 SysAuthServiceFn func() *automock.SystemAuthService 744 OAuth20ServiceFn func() *automock.OAuth20Service 745 ConverterFn func() *automock.RuntimeConverter 746 BundleInstanceAuthSvcFn func() *automock.BundleInstanceAuthService 747 SelfRegManagerFn func() *automock.SelfRegisterManager 748 FormationsSvcFn func() *automock.FormationService 749 InputID string 750 ExpectedRuntime *graphql.Runtime 751 ExpectedErr error 752 }{ 753 { 754 Name: "Success", 755 TransactionerFn: txGen.ThatSucceeds, 756 ServiceFn: func() *automock.RuntimeService { 757 svc := &automock.RuntimeService{} 758 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 759 svc.On("Delete", contextParam, "foo").Return(nil).Once() 760 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, labelNotFoundErr).Once() 761 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 762 return svc 763 }, 764 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 765 ConverterFn: func() *automock.RuntimeConverter { 766 conv := &automock.RuntimeConverter{} 767 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 768 return conv 769 }, 770 SysAuthServiceFn: func() *automock.SystemAuthService { 771 svc := &automock.SystemAuthService{} 772 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 773 return svc 774 }, 775 OAuth20ServiceFn: func() *automock.OAuth20Service { 776 svc := &automock.OAuth20Service{} 777 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 778 return svc 779 }, 780 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 781 svc := &automock.BundleInstanceAuthService{} 782 auth := &model.BundleInstanceAuth{ 783 RuntimeID: &modelRuntime.ID, 784 Status: &model.BundleInstanceAuthStatus{ 785 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 786 }, 787 } 788 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 789 svc.On("Update", contextParam, auth).Return(nil) 790 return svc 791 }, 792 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 793 FormationsSvcFn: UnusedFormationService, 794 InputID: "foo", 795 ExpectedRuntime: gqlRuntime, 796 ExpectedErr: nil, 797 }, 798 { 799 Name: "Success for self registered runtime", 800 TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) { 801 persistTx := &persistenceautomock.PersistenceTx{} 802 persistTx.On("Commit").Return(nil).Times(2) 803 804 transact := &persistenceautomock.Transactioner{} 805 transact.On("Begin").Return(persistTx, nil).Times(2) 806 transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once() 807 808 return persistTx, transact 809 }, 810 ServiceFn: func() *automock.RuntimeService { 811 svc := &automock.RuntimeService{} 812 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 813 svc.On("Delete", contextParam, "foo").Return(nil).Once() 814 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, labelNotFoundErr).Once() 815 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, nil).Once() 816 svc.On("GetLabel", contextParam, "foo", RegionKey).Return(&model.Label{Value: testRegion}, nil).Once() 817 return svc 818 }, 819 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 820 ConverterFn: func() *automock.RuntimeConverter { 821 conv := &automock.RuntimeConverter{} 822 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 823 return conv 824 }, 825 SysAuthServiceFn: func() *automock.SystemAuthService { 826 svc := &automock.SystemAuthService{} 827 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 828 return svc 829 }, 830 OAuth20ServiceFn: func() *automock.OAuth20Service { 831 svc := &automock.OAuth20Service{} 832 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 833 return svc 834 }, 835 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 836 svc := &automock.BundleInstanceAuthService{} 837 auth := &model.BundleInstanceAuth{ 838 RuntimeID: &modelRuntime.ID, 839 Status: &model.BundleInstanceAuthStatus{ 840 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 841 }, 842 } 843 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 844 svc.On("Update", contextParam, auth).Return(nil) 845 return svc 846 }, 847 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesCleanupWithNoErrors, 848 FormationsSvcFn: UnusedFormationService, 849 InputID: "foo", 850 ExpectedRuntime: gqlRuntime, 851 ExpectedErr: nil, 852 }, 853 { 854 Name: "Returns error when second transaction fails", 855 TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) { 856 persistTx := &persistenceautomock.PersistenceTx{} 857 persistTxSecond := &persistenceautomock.PersistenceTx{} 858 persistTx.On("Commit").Return(nil).Once() 859 persistTxSecond.On("Commit").Return(testErr).Once() 860 861 transact := &persistenceautomock.Transactioner{} 862 transact.On("Begin").Return(persistTx, nil).Once() 863 transact.On("Begin").Return(persistTxSecond, nil).Once() 864 transact.On("RollbackUnlessCommitted", mock.Anything, persistTxSecond).Return(false).Once() 865 866 return persistTx, transact 867 }, 868 ServiceFn: func() *automock.RuntimeService { 869 svc := &automock.RuntimeService{} 870 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 871 svc.On("Delete", contextParam, "foo").Return(nil).Once() 872 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, labelNotFoundErr).Once() 873 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, nil).Once() 874 svc.On("GetLabel", contextParam, "foo", RegionKey).Return(&model.Label{Value: testRegion}, nil).Once() 875 return svc 876 }, 877 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 878 ConverterFn: func() *automock.RuntimeConverter { 879 conv := &automock.RuntimeConverter{} 880 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 881 return conv 882 }, 883 SysAuthServiceFn: func() *automock.SystemAuthService { 884 svc := &automock.SystemAuthService{} 885 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 886 return svc 887 }, 888 OAuth20ServiceFn: func() *automock.OAuth20Service { 889 svc := &automock.OAuth20Service{} 890 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 891 return svc 892 }, 893 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 894 svc := &automock.BundleInstanceAuthService{} 895 auth := &model.BundleInstanceAuth{ 896 RuntimeID: &modelRuntime.ID, 897 Status: &model.BundleInstanceAuthStatus{ 898 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 899 }, 900 } 901 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 902 svc.On("Update", contextParam, auth).Return(nil) 903 return svc 904 }, 905 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesCleanupWithNoErrors, 906 FormationsSvcFn: UnusedFormationService, 907 InputID: "foo", 908 ExpectedRuntime: nil, 909 ExpectedErr: testErr, 910 }, 911 { 912 Name: "Returns error when self-registration had been done but cleanup fails", 913 TransactionerFn: txGen.ThatSucceeds, 914 ServiceFn: func() *automock.RuntimeService { 915 svc := &automock.RuntimeService{} 916 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 917 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, nil).Once() 918 svc.On("GetLabel", contextParam, "foo", RegionKey).Return(&model.Label{Value: testRegion}, nil).Once() 919 return svc 920 }, 921 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 922 ConverterFn: UnusedRuntimeConverter, 923 SysAuthServiceFn: UnusedSysAuthService, 924 OAuth20ServiceFn: UnusedOauth20Service, 925 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 926 svc := &automock.BundleInstanceAuthService{} 927 auth := &model.BundleInstanceAuth{ 928 RuntimeID: &modelRuntime.ID, 929 Status: &model.BundleInstanceAuthStatus{ 930 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 931 }, 932 } 933 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 934 svc.On("Update", contextParam, auth).Return(nil) 935 return svc 936 }, 937 SelfRegManagerFn: rtmtest.SelfRegManagerThatReturnsErrorOnCleanup, 938 FormationsSvcFn: UnusedFormationService, 939 InputID: "foo", 940 ExpectedRuntime: nil, 941 ExpectedErr: errors.New("An error occurred during cleanup of self-registered runtime"), 942 }, 943 { 944 Name: "Returns error when region label retrieval fails", 945 TransactionerFn: txGen.ThatDoesntExpectCommit, 946 ServiceFn: func() *automock.RuntimeService { 947 svc := &automock.RuntimeService{} 948 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 949 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, nil).Once() 950 svc.On("GetLabel", contextParam, "foo", RegionKey).Return(nil, testErr).Once() 951 return svc 952 }, 953 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 954 ConverterFn: UnusedRuntimeConverter, 955 SysAuthServiceFn: UnusedSysAuthService, 956 OAuth20ServiceFn: UnusedOauth20Service, 957 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 958 svc := &automock.BundleInstanceAuthService{} 959 auth := &model.BundleInstanceAuth{ 960 RuntimeID: &modelRuntime.ID, 961 Status: &model.BundleInstanceAuthStatus{ 962 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 963 }, 964 } 965 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 966 return svc 967 }, 968 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 969 FormationsSvcFn: UnusedFormationService, 970 InputID: "foo", 971 ExpectedRuntime: nil, 972 ExpectedErr: testErr, 973 }, 974 { 975 Name: "Returns error when runtime deletion failed", 976 TransactionerFn: txGen.ThatDoesntExpectCommit, 977 ServiceFn: func() *automock.RuntimeService { 978 svc := &automock.RuntimeService{} 979 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 980 svc.On("Delete", contextParam, "foo").Return(testErr).Once() 981 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, labelNotFoundErr).Once() 982 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 983 return svc 984 }, 985 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 986 ConverterFn: func() *automock.RuntimeConverter { 987 conv := &automock.RuntimeConverter{} 988 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 989 return conv 990 }, 991 SysAuthServiceFn: func() *automock.SystemAuthService { 992 svc := &automock.SystemAuthService{} 993 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 994 return svc 995 }, 996 OAuth20ServiceFn: UnusedOauth20Service, 997 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 998 svc := &automock.BundleInstanceAuthService{} 999 auth := &model.BundleInstanceAuth{ 1000 RuntimeID: &modelRuntime.ID, 1001 Status: &model.BundleInstanceAuthStatus{ 1002 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 1003 }, 1004 } 1005 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 1006 svc.On("Update", contextParam, auth).Return(nil) 1007 return svc 1008 }, 1009 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1010 FormationsSvcFn: UnusedFormationService, 1011 InputID: "foo", 1012 ExpectedRuntime: nil, 1013 ExpectedErr: testErr, 1014 }, 1015 { 1016 Name: "Returns error when runtime retrieval failed", 1017 TransactionerFn: txGen.ThatDoesntExpectCommit, 1018 ServiceFn: func() *automock.RuntimeService { 1019 svc := &automock.RuntimeService{} 1020 svc.On("Get", contextParam, "foo").Return(nil, testErr).Once() 1021 return svc 1022 }, 1023 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1024 ConverterFn: UnusedRuntimeConverter, 1025 SysAuthServiceFn: UnusedSysAuthService, 1026 OAuth20ServiceFn: UnusedOauth20Service, 1027 BundleInstanceAuthSvcFn: UnusedBundleInstanceAuthService, 1028 FormationsSvcFn: UnusedFormationService, 1029 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1030 InputID: "foo", 1031 ExpectedRuntime: nil, 1032 ExpectedErr: testErr, 1033 }, 1034 { 1035 Name: "Returns error when transaction starting failed", 1036 TransactionerFn: txGen.ThatFailsOnBegin, 1037 ServiceFn: func() *automock.RuntimeService { 1038 svc := &automock.RuntimeService{} 1039 return svc 1040 }, 1041 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1042 ConverterFn: UnusedRuntimeConverter, 1043 SysAuthServiceFn: UnusedSysAuthService, 1044 OAuth20ServiceFn: UnusedOauth20Service, 1045 BundleInstanceAuthSvcFn: UnusedBundleInstanceAuthService, 1046 FormationsSvcFn: UnusedFormationService, 1047 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1048 InputID: "foo", 1049 ExpectedRuntime: nil, 1050 ExpectedErr: testErr, 1051 }, 1052 { 1053 Name: "Returns error when transaction commit failed", 1054 TransactionerFn: txGen.ThatFailsOnCommit, 1055 ServiceFn: func() *automock.RuntimeService { 1056 svc := &automock.RuntimeService{} 1057 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1058 svc.On("Delete", contextParam, modelRuntime.ID).Return(nil) 1059 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, labelNotFoundErr).Once() 1060 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1061 return svc 1062 }, 1063 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1064 ConverterFn: func() *automock.RuntimeConverter { 1065 conv := &automock.RuntimeConverter{} 1066 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1067 return conv 1068 }, 1069 SysAuthServiceFn: func() *automock.SystemAuthService { 1070 svc := &automock.SystemAuthService{} 1071 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1072 return svc 1073 }, 1074 OAuth20ServiceFn: func() *automock.OAuth20Service { 1075 svc := &automock.OAuth20Service{} 1076 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1077 return svc 1078 }, 1079 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1080 svc := &automock.BundleInstanceAuthService{} 1081 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1082 return svc 1083 }, 1084 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1085 FormationsSvcFn: UnusedFormationService, 1086 InputID: "foo", 1087 ExpectedRuntime: nil, 1088 ExpectedErr: testErr, 1089 }, 1090 { 1091 Name: "Return error when listing all system auths failed", 1092 TransactionerFn: txGen.ThatDoesntExpectCommit, 1093 ServiceFn: func() *automock.RuntimeService { 1094 svc := &automock.RuntimeService{} 1095 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1096 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1097 return svc 1098 }, 1099 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1100 ConverterFn: UnusedRuntimeConverter, 1101 SysAuthServiceFn: func() *automock.SystemAuthService { 1102 svc := &automock.SystemAuthService{} 1103 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(nil, testErr) 1104 return svc 1105 }, 1106 OAuth20ServiceFn: UnusedOauth20Service, 1107 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1108 svc := &automock.BundleInstanceAuthService{} 1109 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1110 return svc 1111 }, 1112 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1113 FormationsSvcFn: UnusedFormationService, 1114 InputID: "foo", 1115 ExpectedRuntime: nil, 1116 ExpectedErr: testErr, 1117 }, 1118 { 1119 Name: "Fails when cannot list bundle instance auths by runtime id", 1120 TransactionerFn: txGen.ThatDoesntExpectCommit, 1121 ServiceFn: func() *automock.RuntimeService { 1122 svc := &automock.RuntimeService{} 1123 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1124 return svc 1125 }, 1126 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1127 ConverterFn: UnusedRuntimeConverter, 1128 SysAuthServiceFn: UnusedSysAuthService, 1129 OAuth20ServiceFn: UnusedOauth20Service, 1130 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1131 svc := &automock.BundleInstanceAuthService{} 1132 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return(nil, testErr) 1133 return svc 1134 }, 1135 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1136 FormationsSvcFn: UnusedFormationService, 1137 InputID: "foo", 1138 ExpectedRuntime: nil, 1139 ExpectedErr: testErr, 1140 }, 1141 { 1142 Name: "Fails when cannot list self-register distinguishing label because of error which is other than not found", 1143 TransactionerFn: txGen.ThatDoesntExpectCommit, 1144 ServiceFn: func() *automock.RuntimeService { 1145 svc := &automock.RuntimeService{} 1146 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1147 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, testErr).Once() 1148 return svc 1149 }, 1150 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1151 ConverterFn: UnusedRuntimeConverter, 1152 SysAuthServiceFn: UnusedSysAuthService, 1153 OAuth20ServiceFn: UnusedOauth20Service, 1154 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1155 svc := &automock.BundleInstanceAuthService{} 1156 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1157 return svc 1158 }, 1159 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1160 FormationsSvcFn: UnusedFormationService, 1161 InputID: "foo", 1162 ExpectedRuntime: nil, 1163 ExpectedErr: testErr, 1164 }, 1165 { 1166 Name: "Fails when cannot update bundle instance auths status to unused", 1167 TransactionerFn: txGen.ThatDoesntExpectCommit, 1168 ServiceFn: func() *automock.RuntimeService { 1169 svc := &automock.RuntimeService{} 1170 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1171 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1172 return svc 1173 }, 1174 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1175 ConverterFn: UnusedRuntimeConverter, 1176 SysAuthServiceFn: UnusedSysAuthService, 1177 OAuth20ServiceFn: UnusedOauth20Service, 1178 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1179 svc := &automock.BundleInstanceAuthService{} 1180 auth := &model.BundleInstanceAuth{ 1181 RuntimeID: &modelRuntime.ID, 1182 Status: &model.BundleInstanceAuthStatus{ 1183 Condition: model.BundleInstanceAuthStatusConditionSucceeded, 1184 }, 1185 } 1186 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{auth}, nil) 1187 svc.On("Update", contextParam, auth).Return(testErr) 1188 return svc 1189 }, 1190 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1191 FormationsSvcFn: UnusedFormationService, 1192 InputID: "foo", 1193 ExpectedRuntime: nil, 1194 ExpectedErr: testErr, 1195 }, 1196 { 1197 Name: "Return error when removing oauth from hydra", 1198 TransactionerFn: txGen.ThatDoesntExpectCommit, 1199 ServiceFn: func() *automock.RuntimeService { 1200 svc := &automock.RuntimeService{} 1201 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1202 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1203 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, labelNotFoundErr).Once() 1204 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1205 return svc 1206 }, 1207 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1208 ConverterFn: func() *automock.RuntimeConverter { 1209 conv := &automock.RuntimeConverter{} 1210 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1211 return conv 1212 }, 1213 SysAuthServiceFn: func() *automock.SystemAuthService { 1214 svc := &automock.SystemAuthService{} 1215 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1216 return svc 1217 }, 1218 OAuth20ServiceFn: func() *automock.OAuth20Service { 1219 svc := &automock.OAuth20Service{} 1220 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(testErr) 1221 return svc 1222 }, 1223 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1224 svc := &automock.BundleInstanceAuthService{} 1225 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1226 return svc 1227 }, 1228 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1229 FormationsSvcFn: UnusedFormationService, 1230 InputID: "foo", 1231 ExpectedRuntime: nil, 1232 ExpectedErr: testErr, 1233 }, 1234 { 1235 Name: "Returns error when listing scenarios label", 1236 TransactionerFn: txGen.ThatDoesntExpectCommit, 1237 ServiceFn: func() *automock.RuntimeService { 1238 svc := &automock.RuntimeService{} 1239 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1240 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(nil, testErr) 1241 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1242 return svc 1243 }, 1244 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1245 ConverterFn: UnusedRuntimeConverter, 1246 SysAuthServiceFn: func() *automock.SystemAuthService { 1247 svc := &automock.SystemAuthService{} 1248 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1249 return svc 1250 }, 1251 OAuth20ServiceFn: UnusedOauth20Service, 1252 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1253 svc := &automock.BundleInstanceAuthService{} 1254 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1255 return svc 1256 }, 1257 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1258 FormationsSvcFn: UnusedFormationService, 1259 InputID: "foo", 1260 ExpectedRuntime: nil, 1261 ExpectedErr: testErr, 1262 }, 1263 { 1264 Name: "Returns empty scenarios when listing scenarios label should succeed", 1265 TransactionerFn: txGen.ThatSucceeds, 1266 ServiceFn: func() *automock.RuntimeService { 1267 svc := &automock.RuntimeService{} 1268 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1269 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(emptyScenariosLabel, nil) 1270 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1271 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1272 return svc 1273 }, 1274 ScenarioAssignmentFn: UnusedScenarioAssignmentService, 1275 ConverterFn: func() *automock.RuntimeConverter { 1276 conv := &automock.RuntimeConverter{} 1277 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1278 1279 return conv 1280 }, 1281 SysAuthServiceFn: func() *automock.SystemAuthService { 1282 svc := &automock.SystemAuthService{} 1283 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1284 return svc 1285 }, 1286 OAuth20ServiceFn: func() *automock.OAuth20Service { 1287 svc := &automock.OAuth20Service{} 1288 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1289 1290 return svc 1291 }, 1292 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1293 svc := &automock.BundleInstanceAuthService{} 1294 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1295 return svc 1296 }, 1297 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1298 FormationsSvcFn: UnusedFormationService, 1299 InputID: "foo", 1300 ExpectedRuntime: gqlRuntime, 1301 ExpectedErr: nil, 1302 }, 1303 { 1304 Name: "Returns scenario when listing scenarios label and error when listing scenario assignments should fail", 1305 TransactionerFn: txGen.ThatDoesntExpectCommit, 1306 ServiceFn: func() *automock.RuntimeService { 1307 svc := &automock.RuntimeService{} 1308 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1309 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(singleScenarioLabel, nil) 1310 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1311 return svc 1312 }, 1313 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1314 svc := &automock.ScenarioAssignmentService{} 1315 scenarios, err := label.ValueToStringsSlice(singleScenarioLabel.Value) 1316 assert.NoError(t, err) 1317 1318 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(model.AutomaticScenarioAssignment{}, testErr) 1319 return svc 1320 }, 1321 ConverterFn: UnusedRuntimeConverter, 1322 SysAuthServiceFn: func() *automock.SystemAuthService { 1323 svc := &automock.SystemAuthService{} 1324 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1325 return svc 1326 }, 1327 OAuth20ServiceFn: UnusedOauth20Service, 1328 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1329 svc := &automock.BundleInstanceAuthService{} 1330 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1331 return svc 1332 }, 1333 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1334 FormationsSvcFn: UnusedFormationService, 1335 InputID: "foo", 1336 ExpectedRuntime: nil, 1337 ExpectedErr: testErr, 1338 }, 1339 { 1340 Name: "Returns scenario when listing scenarios label and not found when listing scenario assignments should succeed", 1341 TransactionerFn: txGen.ThatSucceeds, 1342 ServiceFn: func() *automock.RuntimeService { 1343 svc := &automock.RuntimeService{} 1344 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1345 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(singleScenarioLabel, nil) 1346 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1347 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1348 return svc 1349 }, 1350 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1351 svc := &automock.ScenarioAssignmentService{} 1352 scenarios, err := label.ValueToStringsSlice(singleScenarioLabel.Value) 1353 assert.NoError(t, err) 1354 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(model.AutomaticScenarioAssignment{}, scenarioAssignmentNotFoundErr) 1355 return svc 1356 }, 1357 ConverterFn: func() *automock.RuntimeConverter { 1358 conv := &automock.RuntimeConverter{} 1359 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1360 return conv 1361 }, 1362 SysAuthServiceFn: func() *automock.SystemAuthService { 1363 svc := &automock.SystemAuthService{} 1364 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1365 return svc 1366 }, 1367 OAuth20ServiceFn: func() *automock.OAuth20Service { 1368 svc := &automock.OAuth20Service{} 1369 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1370 return svc 1371 }, 1372 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1373 svc := &automock.BundleInstanceAuthService{} 1374 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1375 return svc 1376 }, 1377 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1378 FormationsSvcFn: UnusedFormationService, 1379 InputID: "foo", 1380 ExpectedRuntime: gqlRuntime, 1381 ExpectedErr: nil, 1382 }, 1383 { 1384 Name: "Returns scenario when listing scenarios label and scenario assignment when listing scenario assignments but fails on deletion of scenario assignment should fail", 1385 TransactionerFn: txGen.ThatDoesntExpectCommit, 1386 ServiceFn: func() *automock.RuntimeService { 1387 svc := &automock.RuntimeService{} 1388 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1389 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(singleScenarioLabel, nil) 1390 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1391 return svc 1392 }, 1393 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1394 svc := &automock.ScenarioAssignmentService{} 1395 scenarios, err := label.ValueToStringsSlice(singleScenarioLabel.Value) 1396 assert.NoError(t, err) 1397 scenarioAssignment := model.AutomaticScenarioAssignment{} 1398 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(scenarioAssignment, nil) 1399 return svc 1400 }, 1401 FormationsSvcFn: func() *automock.FormationService { 1402 svc := &automock.FormationService{} 1403 1404 scenarioAssignment := model.AutomaticScenarioAssignment{} 1405 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment).Return(testErr) 1406 return svc 1407 }, 1408 ConverterFn: UnusedRuntimeConverter, 1409 SysAuthServiceFn: func() *automock.SystemAuthService { 1410 svc := &automock.SystemAuthService{} 1411 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1412 return svc 1413 }, 1414 OAuth20ServiceFn: UnusedOauth20Service, 1415 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1416 svc := &automock.BundleInstanceAuthService{} 1417 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1418 return svc 1419 }, 1420 SelfRegManagerFn: rtmtest.SelfRegManagerReturnsDistinguishingLabel, 1421 InputID: "foo", 1422 ExpectedRuntime: nil, 1423 ExpectedErr: testErr, 1424 }, 1425 { 1426 Name: "Returns scenario when listing scenarios label and scenario assignment when listing scenario assignments and succeeds on deletion of scenario assignment should succeed", 1427 TransactionerFn: txGen.ThatSucceeds, 1428 ServiceFn: func() *automock.RuntimeService { 1429 svc := &automock.RuntimeService{} 1430 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1431 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(singleScenarioLabel, nil) 1432 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1433 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1434 return svc 1435 }, 1436 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1437 svc := &automock.ScenarioAssignmentService{} 1438 scenarios, err := label.ValueToStringsSlice(singleScenarioLabel.Value) 1439 assert.NoError(t, err) 1440 scenarioAssignment := model.AutomaticScenarioAssignment{} 1441 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(scenarioAssignment, nil) 1442 return svc 1443 }, 1444 FormationsSvcFn: func() *automock.FormationService { 1445 svc := &automock.FormationService{} 1446 1447 scenarioAssignment := model.AutomaticScenarioAssignment{} 1448 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment).Return(nil) 1449 return svc 1450 }, 1451 ConverterFn: func() *automock.RuntimeConverter { 1452 conv := &automock.RuntimeConverter{} 1453 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1454 return conv 1455 }, 1456 SysAuthServiceFn: func() *automock.SystemAuthService { 1457 svc := &automock.SystemAuthService{} 1458 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1459 return svc 1460 }, 1461 OAuth20ServiceFn: func() *automock.OAuth20Service { 1462 svc := &automock.OAuth20Service{} 1463 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1464 return svc 1465 }, 1466 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1467 svc := &automock.BundleInstanceAuthService{} 1468 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1469 return svc 1470 }, 1471 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1472 InputID: "foo", 1473 ExpectedRuntime: gqlRuntime, 1474 ExpectedErr: nil, 1475 }, 1476 { 1477 Name: "Returns multiple scenarios when listing scenarios label and only some are created by a scenario assignment should succeed", 1478 TransactionerFn: txGen.ThatSucceeds, 1479 ServiceFn: func() *automock.RuntimeService { 1480 svc := &automock.RuntimeService{} 1481 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1482 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(multiScenariosLabel, nil) 1483 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1484 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1485 return svc 1486 }, 1487 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1488 svc := &automock.ScenarioAssignmentService{} 1489 scenarios, err := label.ValueToStringsSlice(multiScenariosLabel.Value) 1490 assert.NoError(t, err) 1491 1492 emptyAssignment := model.AutomaticScenarioAssignment{} 1493 scenarioAssignment1 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[1]} 1494 scenarioAssignment2 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[2]} 1495 1496 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1497 svc.On("GetForScenarioName", contextParam, scenarios[1]).Return(scenarioAssignment1, nil) 1498 svc.On("GetForScenarioName", contextParam, scenarios[2]).Return(scenarioAssignment2, nil) 1499 svc.On("GetForScenarioName", contextParam, scenarios[3]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1500 1501 return svc 1502 }, 1503 FormationsSvcFn: func() *automock.FormationService { 1504 svc := &automock.FormationService{} 1505 scenarios, err := label.ValueToStringsSlice(multiScenariosLabel.Value) 1506 assert.NoError(t, err) 1507 1508 scenarioAssignment1 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[1]} 1509 scenarioAssignment2 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[2]} 1510 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment1).Return(nil).Once() 1511 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment2).Return(nil).Once() 1512 return svc 1513 }, 1514 ConverterFn: func() *automock.RuntimeConverter { 1515 conv := &automock.RuntimeConverter{} 1516 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1517 return conv 1518 }, 1519 SysAuthServiceFn: func() *automock.SystemAuthService { 1520 svc := &automock.SystemAuthService{} 1521 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1522 return svc 1523 }, 1524 OAuth20ServiceFn: func() *automock.OAuth20Service { 1525 svc := &automock.OAuth20Service{} 1526 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1527 return svc 1528 }, 1529 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1530 svc := &automock.BundleInstanceAuthService{} 1531 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1532 return svc 1533 }, 1534 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1535 InputID: "foo", 1536 ExpectedRuntime: gqlRuntime, 1537 ExpectedErr: nil, 1538 }, 1539 { 1540 Name: "Returns multiple scenarios when listing scenarios label and all are created by a scenario assignment should succeed", 1541 TransactionerFn: txGen.ThatSucceeds, 1542 ServiceFn: func() *automock.RuntimeService { 1543 svc := &automock.RuntimeService{} 1544 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1545 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(multiScenariosLabel, nil) 1546 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1547 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1548 return svc 1549 }, 1550 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1551 svc := &automock.ScenarioAssignmentService{} 1552 scenarios, err := label.ValueToStringsSlice(multiScenariosLabel.Value) 1553 assert.NoError(t, err) 1554 1555 scenarioAssignment0 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[0]} 1556 scenarioAssignment1 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[1]} 1557 scenarioAssignment2 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[2]} 1558 scenarioAssignment3 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[3]} 1559 1560 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(scenarioAssignment0, nil) 1561 svc.On("GetForScenarioName", contextParam, scenarios[1]).Return(scenarioAssignment1, nil) 1562 svc.On("GetForScenarioName", contextParam, scenarios[2]).Return(scenarioAssignment2, nil) 1563 svc.On("GetForScenarioName", contextParam, scenarios[3]).Return(scenarioAssignment3, nil) 1564 1565 return svc 1566 }, 1567 ConverterFn: func() *automock.RuntimeConverter { 1568 conv := &automock.RuntimeConverter{} 1569 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1570 return conv 1571 }, 1572 SysAuthServiceFn: func() *automock.SystemAuthService { 1573 svc := &automock.SystemAuthService{} 1574 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1575 return svc 1576 }, 1577 OAuth20ServiceFn: func() *automock.OAuth20Service { 1578 svc := &automock.OAuth20Service{} 1579 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1580 return svc 1581 }, 1582 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1583 svc := &automock.BundleInstanceAuthService{} 1584 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1585 return svc 1586 }, 1587 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1588 FormationsSvcFn: func() *automock.FormationService { 1589 svc := &automock.FormationService{} 1590 scenarios, err := label.ValueToStringsSlice(multiScenariosLabel.Value) 1591 assert.NoError(t, err) 1592 1593 scenarioAssignment0 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[0]} 1594 scenarioAssignment1 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[1]} 1595 scenarioAssignment2 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[2]} 1596 scenarioAssignment3 := model.AutomaticScenarioAssignment{ScenarioName: scenarios[3]} 1597 1598 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment0).Return(nil).Once() 1599 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment1).Return(nil).Once() 1600 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment2).Return(nil).Once() 1601 svc.On("DeleteAutomaticScenarioAssignment", contextParam, scenarioAssignment3).Return(nil).Once() 1602 1603 return svc 1604 }, 1605 InputID: "foo", 1606 ExpectedRuntime: gqlRuntime, 1607 ExpectedErr: nil, 1608 }, 1609 { 1610 Name: "Returns multiple scenarios when listing scenarios label and none are created by a scenario assignment should succeed", 1611 TransactionerFn: txGen.ThatSucceeds, 1612 ServiceFn: func() *automock.RuntimeService { 1613 svc := &automock.RuntimeService{} 1614 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1615 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(multiScenariosLabel, nil) 1616 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1617 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1618 return svc 1619 }, 1620 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1621 svc := &automock.ScenarioAssignmentService{} 1622 scenarios, err := label.ValueToStringsSlice(multiScenariosLabel.Value) 1623 assert.NoError(t, err) 1624 1625 emptyAssignment := model.AutomaticScenarioAssignment{} 1626 1627 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1628 svc.On("GetForScenarioName", contextParam, scenarios[1]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1629 svc.On("GetForScenarioName", contextParam, scenarios[2]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1630 svc.On("GetForScenarioName", contextParam, scenarios[3]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1631 1632 return svc 1633 }, 1634 ConverterFn: func() *automock.RuntimeConverter { 1635 conv := &automock.RuntimeConverter{} 1636 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1637 return conv 1638 }, 1639 SysAuthServiceFn: func() *automock.SystemAuthService { 1640 svc := &automock.SystemAuthService{} 1641 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1642 return svc 1643 }, 1644 OAuth20ServiceFn: func() *automock.OAuth20Service { 1645 svc := &automock.OAuth20Service{} 1646 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1647 return svc 1648 }, 1649 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1650 svc := &automock.BundleInstanceAuthService{} 1651 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1652 return svc 1653 }, 1654 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1655 FormationsSvcFn: UnusedFormationService, 1656 InputID: "foo", 1657 ExpectedRuntime: gqlRuntime, 1658 ExpectedErr: nil, 1659 }, 1660 { 1661 Name: "Returns multiple scenarios when listing scenarios label and none are created by a scenario assignment should succeed", 1662 TransactionerFn: txGen.ThatSucceeds, 1663 ServiceFn: func() *automock.RuntimeService { 1664 svc := &automock.RuntimeService{} 1665 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1666 svc.On("GetLabel", contextParam, "foo", model.ScenariosKey).Return(multiScenariosLabel, nil) 1667 svc.On("GetLabel", contextParam, "foo", rtmtest.TestDistinguishLabel).Return(nil, labelNotFoundErr).Once() 1668 svc.On("Delete", contextParam, "foo").Return(nil).Once() 1669 return svc 1670 }, 1671 ScenarioAssignmentFn: func() *automock.ScenarioAssignmentService { 1672 svc := &automock.ScenarioAssignmentService{} 1673 scenarios, err := label.ValueToStringsSlice(multiScenariosLabel.Value) 1674 assert.NoError(t, err) 1675 1676 emptyAssignment := model.AutomaticScenarioAssignment{} 1677 1678 svc.On("GetForScenarioName", contextParam, scenarios[0]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1679 svc.On("GetForScenarioName", contextParam, scenarios[1]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1680 svc.On("GetForScenarioName", contextParam, scenarios[2]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1681 svc.On("GetForScenarioName", contextParam, scenarios[3]).Return(emptyAssignment, scenarioAssignmentNotFoundErr) 1682 1683 return svc 1684 }, 1685 ConverterFn: func() *automock.RuntimeConverter { 1686 conv := &automock.RuntimeConverter{} 1687 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1688 return conv 1689 }, 1690 SysAuthServiceFn: func() *automock.SystemAuthService { 1691 svc := &automock.SystemAuthService{} 1692 svc.On("ListForObject", contextParam, pkgmodel.RuntimeReference, modelRuntime.ID).Return(testAuths, nil) 1693 return svc 1694 }, 1695 OAuth20ServiceFn: func() *automock.OAuth20Service { 1696 svc := &automock.OAuth20Service{} 1697 svc.On("DeleteMultipleClientCredentials", contextParam, testAuths).Return(nil) 1698 return svc 1699 }, 1700 BundleInstanceAuthSvcFn: func() *automock.BundleInstanceAuthService { 1701 svc := &automock.BundleInstanceAuthService{} 1702 svc.On("ListByRuntimeID", contextParam, modelRuntime.ID).Return([]*model.BundleInstanceAuth{}, nil) 1703 return svc 1704 }, 1705 SelfRegManagerFn: rtmtest.SelfRegManagerThatDoesNotCleanup, 1706 FormationsSvcFn: UnusedFormationService, 1707 InputID: "foo", 1708 ExpectedRuntime: gqlRuntime, 1709 ExpectedErr: nil, 1710 }, 1711 } 1712 for _, testCase := range testCases { 1713 t.Run(testCase.Name, func(t *testing.T) { 1714 persistTx, transact := testCase.TransactionerFn() 1715 svc := testCase.ServiceFn() 1716 scenarioAssignmentSvc := testCase.ScenarioAssignmentFn() 1717 converter := testCase.ConverterFn() 1718 sysAuthSvc := testCase.SysAuthServiceFn() 1719 oAuth20Svc := testCase.OAuth20ServiceFn() 1720 bundleInstanceAuthSvc := testCase.BundleInstanceAuthSvcFn() 1721 selfRegisterManager := testCase.SelfRegManagerFn() 1722 formationSvc := testCase.FormationsSvcFn() 1723 uuidSvc := &automock.UidService{} 1724 1725 resolver := runtime.NewResolver(transact, svc, scenarioAssignmentSvc, sysAuthSvc, oAuth20Svc, converter, nil, nil, bundleInstanceAuthSvc, selfRegisterManager, uuidSvc, nil, nil, nil, nil, nil, nil, formationSvc) 1726 1727 // WHEN 1728 result, err := resolver.DeleteRuntime(context.TODO(), testCase.InputID) 1729 1730 // then 1731 assert.Equal(t, testCase.ExpectedRuntime, result) 1732 if testCase.ExpectedErr != nil { 1733 assert.Contains(t, err.Error(), testCase.ExpectedErr.Error()) 1734 } else { 1735 assert.NoError(t, err) 1736 } 1737 1738 mock.AssertExpectationsForObjects(t, svc, scenarioAssignmentSvc, converter, transact, persistTx, sysAuthSvc, oAuth20Svc, selfRegisterManager, formationSvc) 1739 }) 1740 } 1741 } 1742 1743 func TestResolver_Runtime(t *testing.T) { 1744 // GIVEN 1745 modelRuntime := fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Bar", "test.ns") 1746 gqlRuntime := fixGQLRuntime(t, "foo", "Foo", "Bar", "test.ns") 1747 testErr := errors.New("Test error") 1748 1749 testCases := []struct { 1750 Name string 1751 PersistenceFn func() *persistenceautomock.PersistenceTx 1752 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 1753 ServiceFn func() *automock.RuntimeService 1754 ConverterFn func() *automock.RuntimeConverter 1755 SelfRegManagerFn func() *automock.SelfRegisterManager 1756 InputID string 1757 ExpectedRuntime *graphql.Runtime 1758 ExpectedErr error 1759 }{ 1760 { 1761 Name: "Success", 1762 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1763 persistTx := &persistenceautomock.PersistenceTx{} 1764 persistTx.On("Commit").Return(nil).Once() 1765 return persistTx 1766 }, 1767 TransactionerFn: txtest.TransactionerThatSucceeds, 1768 ServiceFn: func() *automock.RuntimeService { 1769 svc := &automock.RuntimeService{} 1770 svc.On("Get", contextParam, "foo").Return(modelRuntime, nil).Once() 1771 1772 return svc 1773 }, 1774 ConverterFn: func() *automock.RuntimeConverter { 1775 conv := &automock.RuntimeConverter{} 1776 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1777 return conv 1778 }, 1779 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1780 InputID: "foo", 1781 ExpectedRuntime: gqlRuntime, 1782 ExpectedErr: nil, 1783 }, 1784 { 1785 Name: "Success when runtime not found returns nil", 1786 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1787 persistTx := &persistenceautomock.PersistenceTx{} 1788 persistTx.On("Commit").Return(nil).Once() 1789 return persistTx 1790 }, 1791 TransactionerFn: txtest.TransactionerThatSucceeds, 1792 ServiceFn: func() *automock.RuntimeService { 1793 svc := &automock.RuntimeService{} 1794 svc.On("Get", contextParam, "foo").Return(modelRuntime, apperrors.NewNotFoundError(resource.Runtime, "foo")).Once() 1795 1796 return svc 1797 }, 1798 ConverterFn: UnusedRuntimeConverter, 1799 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1800 InputID: "foo", 1801 ExpectedRuntime: nil, 1802 ExpectedErr: nil, 1803 }, 1804 { 1805 Name: "Returns error when runtime retrieval failed", 1806 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1807 persistTx := &persistenceautomock.PersistenceTx{} 1808 return persistTx 1809 }, 1810 TransactionerFn: txtest.TransactionerThatDoesARollback, 1811 ServiceFn: func() *automock.RuntimeService { 1812 svc := &automock.RuntimeService{} 1813 svc.On("Get", contextParam, "foo").Return(nil, testErr).Once() 1814 1815 return svc 1816 }, 1817 ConverterFn: UnusedRuntimeConverter, 1818 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1819 InputID: "foo", 1820 ExpectedRuntime: nil, 1821 ExpectedErr: testErr, 1822 }, 1823 } 1824 1825 for _, testCase := range testCases { 1826 t.Run(testCase.Name, func(t *testing.T) { 1827 persistTx := testCase.PersistenceFn() 1828 transact := testCase.TransactionerFn(persistTx) 1829 svc := testCase.ServiceFn() 1830 converter := testCase.ConverterFn() 1831 selfRegManager := testCase.SelfRegManagerFn() 1832 uuidSvc := &automock.UidService{} 1833 1834 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 1835 1836 // WHEN 1837 result, err := resolver.Runtime(context.TODO(), testCase.InputID) 1838 1839 // then 1840 assert.Equal(t, testCase.ExpectedRuntime, result) 1841 assert.Equal(t, testCase.ExpectedErr, err) 1842 1843 mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx) 1844 }) 1845 } 1846 } 1847 1848 func TestResolver_Runtimes(t *testing.T) { 1849 // GIVEN 1850 modelRuntimes := []*model.Runtime{ 1851 fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Lorem Ipsum", "foo.ns"), 1852 fixModelRuntime(t, "bar", "tenant-bar", "Bar", "Lorem Ipsum", "bar.ns"), 1853 } 1854 1855 gqlRuntimes := []*graphql.Runtime{ 1856 fixGQLRuntime(t, "foo", "Foo", "Lorem Ipsum", "foo.ns"), 1857 fixGQLRuntime(t, "bar", "Bar", "Lorem Ipsum", "bar.ns"), 1858 } 1859 1860 first := 2 1861 gqlAfter := graphql.PageCursor("test") 1862 after := "test" 1863 filter := []*labelfilter.LabelFilter{{Key: ""}} 1864 gqlFilter := []*graphql.LabelFilter{{Key: ""}} 1865 testErr := errors.New("Test error") 1866 1867 testCases := []struct { 1868 Name string 1869 PersistenceFn func() *persistenceautomock.PersistenceTx 1870 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 1871 ServiceFn func() *automock.RuntimeService 1872 ConverterFn func() *automock.RuntimeConverter 1873 SelfRegManagerFn func() *automock.SelfRegisterManager 1874 InputLabelFilters []*graphql.LabelFilter 1875 InputFirst *int 1876 InputAfter *graphql.PageCursor 1877 ExpectedResult *graphql.RuntimePage 1878 ExpectedErr error 1879 }{ 1880 { 1881 Name: "Success", 1882 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1883 persistTx := &persistenceautomock.PersistenceTx{} 1884 persistTx.On("Commit").Return(nil).Once() 1885 return persistTx 1886 }, 1887 TransactionerFn: txtest.TransactionerThatSucceeds, 1888 ServiceFn: func() *automock.RuntimeService { 1889 svc := &automock.RuntimeService{} 1890 svc.On("List", contextParam, filter, first, after).Return(fixRuntimePage(modelRuntimes), nil).Once() 1891 return svc 1892 }, 1893 ConverterFn: func() *automock.RuntimeConverter { 1894 conv := &automock.RuntimeConverter{} 1895 conv.On("MultipleToGraphQL", modelRuntimes).Return(gqlRuntimes).Once() 1896 return conv 1897 }, 1898 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1899 InputFirst: &first, 1900 InputAfter: &gqlAfter, 1901 InputLabelFilters: gqlFilter, 1902 ExpectedResult: fixGQLRuntimePage(gqlRuntimes), 1903 ExpectedErr: nil, 1904 }, 1905 { 1906 Name: "Returns error when runtime listing failed", 1907 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1908 persistTx := &persistenceautomock.PersistenceTx{} 1909 return persistTx 1910 }, 1911 TransactionerFn: txtest.TransactionerThatDoesARollback, 1912 ServiceFn: func() *automock.RuntimeService { 1913 svc := &automock.RuntimeService{} 1914 svc.On("List", contextParam, filter, first, after).Return(nil, testErr).Once() 1915 return svc 1916 }, 1917 ConverterFn: UnusedRuntimeConverter, 1918 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1919 InputFirst: &first, 1920 InputAfter: &gqlAfter, 1921 InputLabelFilters: gqlFilter, 1922 ExpectedResult: nil, 1923 ExpectedErr: testErr, 1924 }, 1925 } 1926 1927 for _, testCase := range testCases { 1928 t.Run(testCase.Name, func(t *testing.T) { 1929 persistTx := testCase.PersistenceFn() 1930 transact := testCase.TransactionerFn(persistTx) 1931 svc := testCase.ServiceFn() 1932 converter := testCase.ConverterFn() 1933 selfRegManager := testCase.SelfRegManagerFn() 1934 uuidSvc := &automock.UidService{} 1935 1936 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 1937 1938 // WHEN 1939 result, err := resolver.Runtimes(context.TODO(), testCase.InputLabelFilters, testCase.InputFirst, testCase.InputAfter) 1940 1941 // then 1942 assert.Equal(t, testCase.ExpectedResult, result) 1943 assert.Equal(t, testCase.ExpectedErr, err) 1944 1945 mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx) 1946 }) 1947 } 1948 } 1949 1950 func TestResolver_RuntimeByTokenIssuer(t *testing.T) { 1951 // GIVEN 1952 modelRuntime := fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Bar", "test.ns") 1953 gqlRuntime := fixGQLRuntime(t, "foo", "Foo", "Bar", "test.ns") 1954 testErr := errors.New("Test error") 1955 1956 testCases := []struct { 1957 Name string 1958 PersistenceFn func() *persistenceautomock.PersistenceTx 1959 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 1960 ServiceFn func() *automock.RuntimeService 1961 ConverterFn func() *automock.RuntimeConverter 1962 SelfRegManagerFn func() *automock.SelfRegisterManager 1963 InputID string 1964 ExpectedRuntime *graphql.Runtime 1965 ExpectedErr error 1966 }{ 1967 { 1968 Name: "Success", 1969 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1970 persistTx := &persistenceautomock.PersistenceTx{} 1971 persistTx.On("Commit").Return(nil).Once() 1972 return persistTx 1973 }, 1974 TransactionerFn: txtest.TransactionerThatSucceeds, 1975 ServiceFn: func() *automock.RuntimeService { 1976 svc := &automock.RuntimeService{} 1977 svc.On("GetByTokenIssuer", contextParam, "foo").Return(modelRuntime, nil).Once() 1978 1979 return svc 1980 }, 1981 ConverterFn: func() *automock.RuntimeConverter { 1982 conv := &automock.RuntimeConverter{} 1983 conv.On("ToGraphQL", modelRuntime).Return(gqlRuntime).Once() 1984 return conv 1985 }, 1986 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 1987 InputID: "foo", 1988 ExpectedRuntime: gqlRuntime, 1989 ExpectedErr: nil, 1990 }, 1991 { 1992 Name: "Success when runtime not found returns nil", 1993 PersistenceFn: func() *persistenceautomock.PersistenceTx { 1994 persistTx := &persistenceautomock.PersistenceTx{} 1995 persistTx.On("Commit").Return(nil).Once() 1996 return persistTx 1997 }, 1998 TransactionerFn: txtest.TransactionerThatSucceeds, 1999 ServiceFn: func() *automock.RuntimeService { 2000 svc := &automock.RuntimeService{} 2001 svc.On("GetByTokenIssuer", contextParam, "foo").Return(modelRuntime, apperrors.NewNotFoundError(resource.Runtime, "foo")).Once() 2002 2003 return svc 2004 }, 2005 ConverterFn: UnusedRuntimeConverter, 2006 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2007 InputID: "foo", 2008 ExpectedRuntime: nil, 2009 ExpectedErr: nil, 2010 }, 2011 { 2012 Name: "Returns error when runtime retrieval failed", 2013 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2014 persistTx := &persistenceautomock.PersistenceTx{} 2015 return persistTx 2016 }, 2017 TransactionerFn: txtest.TransactionerThatDoesARollback, 2018 ServiceFn: func() *automock.RuntimeService { 2019 svc := &automock.RuntimeService{} 2020 svc.On("GetByTokenIssuer", contextParam, "foo").Return(nil, testErr).Once() 2021 2022 return svc 2023 }, 2024 ConverterFn: UnusedRuntimeConverter, 2025 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2026 InputID: "foo", 2027 ExpectedRuntime: nil, 2028 ExpectedErr: testErr, 2029 }, 2030 } 2031 2032 for _, testCase := range testCases { 2033 t.Run(testCase.Name, func(t *testing.T) { 2034 persistTx := testCase.PersistenceFn() 2035 transact := testCase.TransactionerFn(persistTx) 2036 svc := testCase.ServiceFn() 2037 converter := testCase.ConverterFn() 2038 selfRegManager := testCase.SelfRegManagerFn() 2039 uuidSvc := &automock.UidService{} 2040 2041 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 2042 2043 // WHEN 2044 result, err := resolver.RuntimeByTokenIssuer(context.TODO(), testCase.InputID) 2045 2046 // then 2047 assert.Equal(t, testCase.ExpectedRuntime, result) 2048 assert.Equal(t, testCase.ExpectedErr, err) 2049 2050 mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx) 2051 }) 2052 } 2053 } 2054 2055 func TestResolver_SetRuntimeLabel(t *testing.T) { 2056 // GIVEN 2057 testErr := errors.New("Test error") 2058 2059 runtimeID := "foo" 2060 labelKey := "key" 2061 labelValue := []string{"foo", "bar"} 2062 gqlLabel := &graphql.Label{ 2063 Key: labelKey, 2064 Value: labelValue, 2065 } 2066 modelLabelInput := &model.LabelInput{ 2067 Key: labelKey, 2068 Value: labelValue, 2069 ObjectID: runtimeID, 2070 ObjectType: model.RuntimeLabelableObject, 2071 } 2072 2073 modelLabel := &model.Label{ 2074 ID: "baz", 2075 Key: labelKey, 2076 Value: labelValue, 2077 ObjectID: runtimeID, 2078 ObjectType: model.RuntimeLabelableObject, 2079 } 2080 2081 testCases := []struct { 2082 Name string 2083 PersistenceFn func() *persistenceautomock.PersistenceTx 2084 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 2085 ServiceFn func() *automock.RuntimeService 2086 ConverterFn func() *automock.RuntimeConverter 2087 SelfRegManagerFn func() *automock.SelfRegisterManager 2088 InputRuntimeID string 2089 InputKey string 2090 InputValue interface{} 2091 ExpectedLabel *graphql.Label 2092 ExpectedErr error 2093 }{ 2094 { 2095 Name: "Success", 2096 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2097 persistTx := &persistenceautomock.PersistenceTx{} 2098 persistTx.On("Commit").Return(nil).Once() 2099 return persistTx 2100 }, 2101 TransactionerFn: txtest.TransactionerThatSucceeds, 2102 ServiceFn: func() *automock.RuntimeService { 2103 svc := &automock.RuntimeService{} 2104 svc.On("SetLabel", contextParam, modelLabelInput).Return(nil).Once() 2105 svc.On("GetLabel", contextParam, runtimeID, modelLabelInput.Key).Return(modelLabel, nil).Once() 2106 return svc 2107 }, 2108 ConverterFn: UnusedRuntimeConverter, 2109 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2110 InputRuntimeID: runtimeID, 2111 InputKey: gqlLabel.Key, 2112 InputValue: gqlLabel.Value, 2113 ExpectedLabel: gqlLabel, 2114 ExpectedErr: nil, 2115 }, 2116 { 2117 Name: "Returns error when adding label to runtime failed", 2118 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2119 persistTx := &persistenceautomock.PersistenceTx{} 2120 return persistTx 2121 }, 2122 TransactionerFn: txtest.TransactionerThatDoesARollback, 2123 ServiceFn: func() *automock.RuntimeService { 2124 svc := &automock.RuntimeService{} 2125 svc.On("SetLabel", contextParam, modelLabelInput).Return(testErr).Once() 2126 return svc 2127 }, 2128 ConverterFn: UnusedRuntimeConverter, 2129 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2130 InputRuntimeID: runtimeID, 2131 InputKey: gqlLabel.Key, 2132 InputValue: gqlLabel.Value, 2133 ExpectedLabel: nil, 2134 ExpectedErr: testErr, 2135 }, 2136 } 2137 2138 for _, testCase := range testCases { 2139 t.Run(testCase.Name, func(t *testing.T) { 2140 persistTx := testCase.PersistenceFn() 2141 transact := testCase.TransactionerFn(persistTx) 2142 svc := testCase.ServiceFn() 2143 converter := testCase.ConverterFn() 2144 selfRegManager := testCase.SelfRegManagerFn() 2145 uuidSvc := &automock.UidService{} 2146 2147 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 2148 2149 // WHEN 2150 result, err := resolver.SetRuntimeLabel(context.TODO(), testCase.InputRuntimeID, testCase.InputKey, testCase.InputValue) 2151 2152 // then 2153 assert.Equal(t, testCase.ExpectedLabel, result) 2154 assert.Equal(t, testCase.ExpectedErr, err) 2155 2156 mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx) 2157 }) 2158 } 2159 2160 t.Run("Returns error when Label input validation failed", func(t *testing.T) { 2161 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 2162 2163 // WHEN 2164 result, err := resolver.SetRuntimeLabel(context.TODO(), "", "", "") 2165 2166 // then 2167 require.Nil(t, result) 2168 require.Error(t, err) 2169 assert.Contains(t, err.Error(), "value=cannot be blank") 2170 assert.Contains(t, err.Error(), "key=cannot be blank") 2171 }) 2172 } 2173 2174 func TestResolver_DeleteRuntimeLabel(t *testing.T) { 2175 // GIVEN 2176 testErr := errors.New("Test error") 2177 2178 runtimeID := "foo" 2179 2180 gqlLabel := &graphql.Label{ 2181 Key: "key", 2182 Value: []string{"foo", "bar"}, 2183 } 2184 modelLabel := &model.Label{ 2185 Key: "key", 2186 Value: []string{"foo", "bar"}, 2187 } 2188 2189 testCases := []struct { 2190 Name string 2191 PersistenceFn func() *persistenceautomock.PersistenceTx 2192 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 2193 ServiceFn func() *automock.RuntimeService 2194 ConverterFn func() *automock.RuntimeConverter 2195 SelfRegManagerFn func() *automock.SelfRegisterManager 2196 InputRuntimeID string 2197 InputKey string 2198 ExpectedLabel *graphql.Label 2199 ExpectedErr error 2200 }{ 2201 { 2202 Name: "Success", 2203 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2204 persistTx := &persistenceautomock.PersistenceTx{} 2205 persistTx.On("Commit").Return(nil).Once() 2206 return persistTx 2207 }, 2208 TransactionerFn: txtest.TransactionerThatSucceeds, 2209 ServiceFn: func() *automock.RuntimeService { 2210 svc := &automock.RuntimeService{} 2211 svc.On("GetLabel", contextParam, runtimeID, gqlLabel.Key).Return(modelLabel, nil).Once() 2212 svc.On("DeleteLabel", contextParam, runtimeID, gqlLabel.Key).Return(nil).Once() 2213 return svc 2214 }, 2215 ConverterFn: UnusedRuntimeConverter, 2216 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2217 InputRuntimeID: runtimeID, 2218 InputKey: gqlLabel.Key, 2219 ExpectedLabel: gqlLabel, 2220 ExpectedErr: nil, 2221 }, 2222 { 2223 Name: "Returns error when label retrieval failed", 2224 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2225 persistTx := &persistenceautomock.PersistenceTx{} 2226 return persistTx 2227 }, 2228 TransactionerFn: txtest.TransactionerThatDoesARollback, 2229 ServiceFn: func() *automock.RuntimeService { 2230 svc := &automock.RuntimeService{} 2231 svc.On("GetLabel", contextParam, runtimeID, gqlLabel.Key).Return(nil, testErr).Once() 2232 return svc 2233 }, 2234 ConverterFn: UnusedRuntimeConverter, 2235 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2236 InputRuntimeID: runtimeID, 2237 InputKey: gqlLabel.Key, 2238 ExpectedLabel: nil, 2239 ExpectedErr: testErr, 2240 }, 2241 { 2242 Name: "Returns error when deleting runtime's label failed", 2243 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2244 persistTx := &persistenceautomock.PersistenceTx{} 2245 return persistTx 2246 }, 2247 TransactionerFn: txtest.TransactionerThatDoesARollback, 2248 ServiceFn: func() *automock.RuntimeService { 2249 svc := &automock.RuntimeService{} 2250 svc.On("GetLabel", contextParam, runtimeID, gqlLabel.Key).Return(modelLabel, nil).Once() 2251 svc.On("DeleteLabel", contextParam, runtimeID, gqlLabel.Key).Return(testErr).Once() 2252 return svc 2253 }, 2254 ConverterFn: UnusedRuntimeConverter, 2255 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2256 InputRuntimeID: runtimeID, 2257 InputKey: gqlLabel.Key, 2258 ExpectedLabel: nil, 2259 ExpectedErr: testErr, 2260 }, 2261 } 2262 2263 for _, testCase := range testCases { 2264 t.Run(testCase.Name, func(t *testing.T) { 2265 persistTx := testCase.PersistenceFn() 2266 transact := testCase.TransactionerFn(persistTx) 2267 svc := testCase.ServiceFn() 2268 converter := testCase.ConverterFn() 2269 selfRegManager := testCase.SelfRegManagerFn() 2270 uuidSvc := &automock.UidService{} 2271 2272 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 2273 2274 // WHEN 2275 result, err := resolver.DeleteRuntimeLabel(context.TODO(), testCase.InputRuntimeID, testCase.InputKey) 2276 2277 // then 2278 assert.Equal(t, testCase.ExpectedLabel, result) 2279 assert.Equal(t, testCase.ExpectedErr, err) 2280 2281 mock.AssertExpectationsForObjects(t, svc, converter, transact, persistTx) 2282 }) 2283 } 2284 } 2285 2286 func TestResolver_Webhooks(t *testing.T) { 2287 // GIVEN 2288 id := "foo" 2289 2290 gqlRuntime := fixGQLRuntime(t, id, "name", "desc", "test.ns") 2291 2292 modelWebhooks := []*model.Webhook{{Type: "test webhook"}} 2293 gqlWebhooks := []*graphql.Webhook{{Type: "test webhook"}} 2294 2295 testErr := errors.New("Test error") 2296 txGen := txtest.NewTransactionContextGenerator(testErr) 2297 2298 testCases := []struct { 2299 Name string 2300 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 2301 ServiceFn func() *automock.WebhookService 2302 ConverterFn func() *automock.WebhookConverter 2303 ExpectedResult []*graphql.Webhook 2304 ExpectedErr error 2305 }{ 2306 { 2307 Name: "Success", 2308 TransactionerFn: txGen.ThatSucceeds, 2309 ServiceFn: func() *automock.WebhookService { 2310 svc := &automock.WebhookService{} 2311 svc.On("ListForRuntime", contextParam, gqlRuntime.ID).Return(modelWebhooks, nil).Once() 2312 return svc 2313 }, 2314 ConverterFn: func() *automock.WebhookConverter { 2315 converter := &automock.WebhookConverter{} 2316 converter.Mock.On("MultipleToGraphQL", modelWebhooks).Return(gqlWebhooks, nil).Once() 2317 return converter 2318 }, 2319 ExpectedResult: gqlWebhooks, 2320 ExpectedErr: nil, 2321 }, 2322 { 2323 Name: "Returns error when transaction begin failed", 2324 TransactionerFn: txGen.ThatFailsOnBegin, 2325 ServiceFn: func() *automock.WebhookService { 2326 return &automock.WebhookService{} 2327 }, 2328 ConverterFn: func() *automock.WebhookConverter { 2329 return &automock.WebhookConverter{} 2330 }, 2331 ExpectedResult: nil, 2332 ExpectedErr: testErr, 2333 }, 2334 { 2335 Name: "Returns error when listing webhooks failed", 2336 TransactionerFn: txGen.ThatDoesntExpectCommit, 2337 ServiceFn: func() *automock.WebhookService { 2338 svc := &automock.WebhookService{} 2339 svc.On("ListForRuntime", contextParam, gqlRuntime.ID).Return([]*model.Webhook{}, testErr).Once() 2340 return svc 2341 }, 2342 ConverterFn: func() *automock.WebhookConverter { 2343 return &automock.WebhookConverter{} 2344 }, 2345 ExpectedResult: nil, 2346 ExpectedErr: testErr, 2347 }, 2348 { 2349 Name: "Returns error when transaction commit failed", 2350 TransactionerFn: txGen.ThatFailsOnCommit, 2351 ServiceFn: func() *automock.WebhookService { 2352 svc := &automock.WebhookService{} 2353 svc.On("ListForRuntime", contextParam, gqlRuntime.ID).Return(modelWebhooks, nil).Once() 2354 return svc 2355 }, 2356 ConverterFn: func() *automock.WebhookConverter { 2357 return &automock.WebhookConverter{} 2358 }, 2359 ExpectedResult: nil, 2360 ExpectedErr: testErr, 2361 }, 2362 { 2363 Name: "Returns error when webhook conversion failed", 2364 TransactionerFn: txGen.ThatSucceeds, 2365 ServiceFn: func() *automock.WebhookService { 2366 svc := &automock.WebhookService{} 2367 svc.On("ListForRuntime", contextParam, gqlRuntime.ID).Return(modelWebhooks, nil).Once() 2368 return svc 2369 }, 2370 ConverterFn: func() *automock.WebhookConverter { 2371 converter := &automock.WebhookConverter{} 2372 converter.Mock.On("MultipleToGraphQL", modelWebhooks).Return(nil, testErr).Once() 2373 return converter 2374 }, 2375 ExpectedResult: nil, 2376 ExpectedErr: testErr, 2377 }, 2378 } 2379 2380 for _, testCase := range testCases { 2381 t.Run(testCase.Name, func(t *testing.T) { 2382 persist, transact := testCase.TransactionerFn() 2383 svc := testCase.ServiceFn() 2384 converter := testCase.ConverterFn() 2385 2386 resolver := runtime.NewResolver(transact, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, svc, converter, nil, nil) 2387 2388 // WHEN 2389 result, err := resolver.Webhooks(context.TODO(), gqlRuntime) 2390 2391 // then 2392 assert.Equal(t, testCase.ExpectedResult, result) 2393 assert.Equal(t, testCase.ExpectedErr, err) 2394 2395 mock.AssertExpectationsForObjects(t, svc, transact, persist, converter) 2396 }) 2397 } 2398 t.Run("Returns error when Runtime object is missing", func(t *testing.T) { 2399 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 2400 // WHEN 2401 _, err := resolver.Webhooks(context.TODO(), nil) 2402 // THEN 2403 require.Error(t, err) 2404 assert.Contains(t, err.Error(), "Runtime cannot be empty") 2405 }) 2406 } 2407 2408 func TestResolver_Labels(t *testing.T) { 2409 // GIVEN 2410 id := "foo" 2411 labelKey1 := "key1" 2412 labelValue1 := "val1" 2413 labelKey2 := "key2" 2414 labelValue2 := "val2" 2415 2416 gqlRuntime := fixGQLRuntime(t, id, "name", "desc", "test.ns") 2417 2418 modelLabels := map[string]*model.Label{ 2419 "abc": { 2420 ID: "abc", 2421 Key: labelKey1, 2422 Value: labelValue1, 2423 ObjectID: id, 2424 ObjectType: model.RuntimeLabelableObject, 2425 }, 2426 "def": { 2427 ID: "def", 2428 Key: labelKey2, 2429 Value: labelValue2, 2430 ObjectID: id, 2431 ObjectType: model.RuntimeLabelableObject, 2432 }, 2433 } 2434 2435 gqlLabels := graphql.Labels{ 2436 labelKey1: labelValue1, 2437 labelKey2: labelValue2, 2438 } 2439 2440 gqlLabels1 := graphql.Labels{ 2441 labelKey1: labelValue1, 2442 } 2443 2444 testErr := errors.New("Test error") 2445 2446 testCases := []struct { 2447 Name string 2448 PersistenceFn func() *persistenceautomock.PersistenceTx 2449 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 2450 ServiceFn func() *automock.RuntimeService 2451 SelfRegManagerFn func() *automock.SelfRegisterManager 2452 InputRuntime *graphql.Runtime 2453 InputKey *string 2454 ExpectedResult graphql.Labels 2455 ExpectedErr error 2456 }{ 2457 { 2458 Name: "Success", 2459 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2460 persistTx := &persistenceautomock.PersistenceTx{} 2461 persistTx.On("Commit").Return(nil).Once() 2462 return persistTx 2463 }, 2464 TransactionerFn: txtest.TransactionerThatSucceeds, 2465 ServiceFn: func() *automock.RuntimeService { 2466 svc := &automock.RuntimeService{} 2467 svc.On("ListLabels", contextParam, id).Return(modelLabels, nil).Once() 2468 return svc 2469 }, 2470 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2471 InputKey: nil, 2472 ExpectedResult: gqlLabels, 2473 ExpectedErr: nil, 2474 }, 2475 { 2476 Name: "Success when labels are filtered", 2477 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2478 persistTx := &persistenceautomock.PersistenceTx{} 2479 persistTx.On("Commit").Return(nil).Once() 2480 return persistTx 2481 }, 2482 TransactionerFn: txtest.TransactionerThatSucceeds, 2483 ServiceFn: func() *automock.RuntimeService { 2484 svc := &automock.RuntimeService{} 2485 svc.On("ListLabels", contextParam, id).Return(modelLabels, nil).Once() 2486 return svc 2487 }, 2488 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2489 InputKey: &labelKey1, 2490 ExpectedResult: gqlLabels1, 2491 ExpectedErr: nil, 2492 }, 2493 { 2494 Name: "Success returns nil when labels not found", 2495 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2496 persistTx := &persistenceautomock.PersistenceTx{} 2497 persistTx.On("Commit").Return(nil).Once() 2498 return persistTx 2499 }, 2500 TransactionerFn: txtest.TransactionerThatSucceeds, 2501 ServiceFn: func() *automock.RuntimeService { 2502 svc := &automock.RuntimeService{} 2503 svc.On("ListLabels", contextParam, id).Return(nil, errors.New("doesn't exist")).Once() 2504 return svc 2505 }, 2506 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2507 InputKey: &labelKey1, 2508 ExpectedResult: nil, 2509 ExpectedErr: nil, 2510 }, 2511 { 2512 Name: "Returns error when label listing failed", 2513 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2514 persistTx := &persistenceautomock.PersistenceTx{} 2515 return persistTx 2516 }, 2517 TransactionerFn: txtest.TransactionerThatDoesARollback, 2518 ServiceFn: func() *automock.RuntimeService { 2519 svc := &automock.RuntimeService{} 2520 svc.On("ListLabels", contextParam, id).Return(nil, testErr).Once() 2521 return svc 2522 }, 2523 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2524 InputKey: &labelKey1, 2525 ExpectedResult: nil, 2526 ExpectedErr: testErr, 2527 }, 2528 } 2529 2530 for _, testCase := range testCases { 2531 t.Run(testCase.Name, func(t *testing.T) { 2532 persistTx := testCase.PersistenceFn() 2533 svc := testCase.ServiceFn() 2534 transact := testCase.TransactionerFn(persistTx) 2535 selfRegManager := testCase.SelfRegManagerFn() 2536 uuidSvc := &automock.UidService{} 2537 2538 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, nil, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 2539 2540 // WHEN 2541 result, err := resolver.Labels(context.TODO(), gqlRuntime, testCase.InputKey) 2542 2543 // then 2544 assert.Equal(t, testCase.ExpectedResult, result) 2545 assert.Equal(t, testCase.ExpectedErr, err) 2546 2547 mock.AssertExpectationsForObjects(t, svc, transact, persistTx) 2548 }) 2549 } 2550 } 2551 2552 func TestResolver_GetLabel(t *testing.T) { 2553 // GIVEN 2554 runtimeID := "37e89317-9ace-441d-9dc0-badf09b035b4" 2555 labelKey := runtime.IsNormalizedLabel 2556 labelValue := "true" 2557 2558 modelLabel := &model.Label{ 2559 ID: "abc", 2560 Key: labelKey, 2561 Value: labelValue, 2562 ObjectID: runtimeID, 2563 ObjectType: model.RuntimeLabelableObject, 2564 } 2565 2566 gqlLabels := &graphql.Labels{ 2567 labelKey: labelValue, 2568 } 2569 2570 testErr := errors.New("Test error") 2571 2572 testCases := []struct { 2573 Name string 2574 PersistenceFn func() *persistenceautomock.PersistenceTx 2575 TransactionerFn func(persistTx *persistenceautomock.PersistenceTx) *persistenceautomock.Transactioner 2576 ServiceFn func() *automock.RuntimeService 2577 SelfRegManagerFn func() *automock.SelfRegisterManager 2578 InputRuntime *graphql.Runtime 2579 InputKey string 2580 ExpectedResult *graphql.Labels 2581 ExpectedErr error 2582 }{ 2583 { 2584 Name: "Success", 2585 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2586 persistTx := &persistenceautomock.PersistenceTx{} 2587 persistTx.On("Commit").Return(nil).Once() 2588 return persistTx 2589 }, 2590 TransactionerFn: txtest.TransactionerThatSucceeds, 2591 ServiceFn: func() *automock.RuntimeService { 2592 svc := &automock.RuntimeService{} 2593 svc.On("GetLabel", contextParam, runtimeID, labelKey).Return(modelLabel, nil).Once() 2594 return svc 2595 }, 2596 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2597 InputKey: labelKey, 2598 ExpectedResult: gqlLabels, 2599 ExpectedErr: nil, 2600 }, 2601 { 2602 Name: "Success returns nil when label not found", 2603 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2604 persistTx := &persistenceautomock.PersistenceTx{} 2605 persistTx.On("Commit").Return(nil).Once() 2606 return persistTx 2607 }, 2608 TransactionerFn: txtest.TransactionerThatSucceeds, 2609 ServiceFn: func() *automock.RuntimeService { 2610 svc := &automock.RuntimeService{} 2611 svc.On("GetLabel", contextParam, runtimeID, labelKey).Return(nil, apperrors.NewNotFoundError(resource.Runtime, runtimeID)).Once() 2612 return svc 2613 }, 2614 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2615 InputKey: labelKey, 2616 ExpectedResult: nil, 2617 ExpectedErr: nil, 2618 }, 2619 { 2620 Name: "Returns error when label listing fails", 2621 PersistenceFn: func() *persistenceautomock.PersistenceTx { 2622 persistTx := &persistenceautomock.PersistenceTx{} 2623 return persistTx 2624 }, 2625 TransactionerFn: txtest.TransactionerThatSucceeds, 2626 ServiceFn: func() *automock.RuntimeService { 2627 svc := &automock.RuntimeService{} 2628 svc.On("GetLabel", contextParam, runtimeID, labelKey).Return(nil, testErr).Once() 2629 return svc 2630 }, 2631 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 2632 InputKey: labelKey, 2633 ExpectedResult: nil, 2634 ExpectedErr: testErr, 2635 }, 2636 } 2637 2638 for _, testCase := range testCases { 2639 t.Run(testCase.Name, func(t *testing.T) { 2640 persistTx := testCase.PersistenceFn() 2641 svc := testCase.ServiceFn() 2642 transact := testCase.TransactionerFn(persistTx) 2643 selfRegManager := testCase.SelfRegManagerFn() 2644 uuidSvc := &automock.UidService{} 2645 2646 resolver := runtime.NewResolver(transact, svc, nil, nil, nil, nil, nil, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 2647 2648 // WHEN 2649 result, err := resolver.GetLabel(context.TODO(), runtimeID, labelKey) 2650 2651 // then 2652 assert.Equal(t, testCase.ExpectedResult, result) 2653 assert.Equal(t, testCase.ExpectedErr, err) 2654 2655 mock.AssertExpectationsForObjects(t, svc, transact, persistTx) 2656 }) 2657 } 2658 } 2659 2660 func TestResolver_RuntimeContext(t *testing.T) { 2661 // GIVEN 2662 id := "foo" 2663 key := "key" 2664 val := "value" 2665 runtimeID := "runtime_id" 2666 2667 modelRuntimeContext := fixModelRuntimeContext(id, runtimeID, key, val) 2668 gqlRuntimeContext := fixGqlRuntimeContext(id, key, val) 2669 gqlRuntime := fixGQLRuntime(t, runtimeID, "runtime", "description", "test.ns") 2670 testErr := errors.New("Test error") 2671 txGen := txtest.NewTransactionContextGenerator(testErr) 2672 2673 testCases := []struct { 2674 Name string 2675 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 2676 ServiceFn func() *automock.RuntimeContextService 2677 ConverterFn func() *automock.RuntimeContextConverter 2678 InputID string 2679 Runtime *graphql.Runtime 2680 ExpectedRuntimeContext *graphql.RuntimeContext 2681 ExpectedErr error 2682 }{ 2683 { 2684 Name: "Success", 2685 TransactionerFn: txGen.ThatSucceeds, 2686 ServiceFn: func() *automock.RuntimeContextService { 2687 svc := &automock.RuntimeContextService{} 2688 svc.On("GetForRuntime", contextParam, "foo", runtimeID).Return(modelRuntimeContext, nil).Once() 2689 2690 return svc 2691 }, 2692 ConverterFn: func() *automock.RuntimeContextConverter { 2693 conv := &automock.RuntimeContextConverter{} 2694 conv.On("ToGraphQL", modelRuntimeContext).Return(gqlRuntimeContext).Once() 2695 return conv 2696 }, 2697 InputID: id, 2698 Runtime: gqlRuntime, 2699 ExpectedRuntimeContext: gqlRuntimeContext, 2700 ExpectedErr: nil, 2701 }, 2702 { 2703 Name: "Returns when runtime context for runtime not found", 2704 TransactionerFn: txGen.ThatSucceeds, 2705 ServiceFn: func() *automock.RuntimeContextService { 2706 svc := &automock.RuntimeContextService{} 2707 svc.On("GetForRuntime", contextParam, "foo", runtimeID).Return(modelRuntimeContext, apperrors.NewNotFoundError(resource.RuntimeContext, "foo")).Once() 2708 2709 return svc 2710 }, 2711 ConverterFn: func() *automock.RuntimeContextConverter { 2712 conv := &automock.RuntimeContextConverter{} 2713 return conv 2714 }, 2715 InputID: id, 2716 Runtime: gqlRuntime, 2717 ExpectedRuntimeContext: nil, 2718 ExpectedErr: nil, 2719 }, 2720 { 2721 Name: "Returns error when runtime context retrieval failed", 2722 TransactionerFn: txGen.ThatDoesntExpectCommit, 2723 ServiceFn: func() *automock.RuntimeContextService { 2724 svc := &automock.RuntimeContextService{} 2725 svc.On("GetForRuntime", contextParam, "foo", runtimeID).Return(nil, testErr).Once() 2726 2727 return svc 2728 }, 2729 ConverterFn: func() *automock.RuntimeContextConverter { 2730 conv := &automock.RuntimeContextConverter{} 2731 return conv 2732 }, 2733 InputID: id, 2734 Runtime: gqlRuntime, 2735 ExpectedRuntimeContext: nil, 2736 ExpectedErr: testErr, 2737 }, 2738 { 2739 Name: "Returns error when commit begin error", 2740 TransactionerFn: txGen.ThatFailsOnBegin, 2741 ServiceFn: func() *automock.RuntimeContextService { 2742 svc := &automock.RuntimeContextService{} 2743 return svc 2744 }, 2745 ConverterFn: func() *automock.RuntimeContextConverter { 2746 conv := &automock.RuntimeContextConverter{} 2747 return conv 2748 }, 2749 InputID: "foo", 2750 Runtime: gqlRuntime, 2751 ExpectedRuntimeContext: nil, 2752 ExpectedErr: testErr, 2753 }, 2754 { 2755 Name: "Returns error when commit failed", 2756 TransactionerFn: txGen.ThatFailsOnCommit, 2757 ServiceFn: func() *automock.RuntimeContextService { 2758 svc := &automock.RuntimeContextService{} 2759 svc.On("GetForRuntime", contextParam, "foo", runtimeID).Return(modelRuntimeContext, nil).Once() 2760 2761 return svc 2762 }, 2763 ConverterFn: func() *automock.RuntimeContextConverter { 2764 conv := &automock.RuntimeContextConverter{} 2765 return conv 2766 }, 2767 InputID: "foo", 2768 Runtime: gqlRuntime, 2769 ExpectedRuntimeContext: nil, 2770 ExpectedErr: testErr, 2771 }, 2772 } 2773 2774 for _, testCase := range testCases { 2775 t.Run(testCase.Name, func(t *testing.T) { 2776 persist, transact := testCase.TransactionerFn() 2777 svc := testCase.ServiceFn() 2778 converter := testCase.ConverterFn() 2779 2780 resolver := runtime.NewResolver(transact, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, svc, converter, nil, nil, nil, nil) 2781 2782 // WHEN 2783 result, err := resolver.RuntimeContext(context.TODO(), testCase.Runtime, testCase.InputID) 2784 2785 // then 2786 assert.Equal(t, testCase.ExpectedRuntimeContext, result) 2787 assert.Equal(t, testCase.ExpectedErr, err) 2788 2789 mock.AssertExpectationsForObjects(t, svc, converter, transact, persist) 2790 }) 2791 } 2792 t.Run("Returns error when Runtime object is missing", func(t *testing.T) { 2793 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 2794 // WHEN 2795 _, err := resolver.RuntimeContext(context.TODO(), nil, "foo") 2796 // THEN 2797 require.Error(t, err) 2798 assert.Contains(t, err.Error(), "Runtime cannot be empty") 2799 }) 2800 } 2801 2802 func TestResolver_RuntimeContexts(t *testing.T) { 2803 // GIVEN 2804 id := "foo" 2805 key := "key" 2806 val := "value" 2807 firstRuntimeID := "runtime_id" 2808 secondRuntimeID := "runtime2_id" 2809 runtimeIDs := []string{firstRuntimeID, secondRuntimeID} 2810 2811 testErr := errors.New("Test error") 2812 2813 rtmCtxFirstRuntime := fixModelRuntimeContext(id, firstRuntimeID, key, val) 2814 rtmCtxSecondRuntime := fixModelRuntimeContext(id+"2", secondRuntimeID, key+"2", val+"2") 2815 2816 rtmCtxsFirstRuntime := []*model.RuntimeContext{rtmCtxFirstRuntime} 2817 rtmCtxsSecondRuntime := []*model.RuntimeContext{rtmCtxSecondRuntime} 2818 2819 gqlRtmCtxFirstRuntime := fixGqlRuntimeContext(id, key, val) 2820 gqlRtmCtxSecondRuntime := fixGqlRuntimeContext(id+"2", key+"2", val+"2") 2821 2822 gqlRtmCtxsFirstRuntime := []*graphql.RuntimeContext{gqlRtmCtxFirstRuntime} 2823 gqlRtmCtxsSecondRuntime := []*graphql.RuntimeContext{gqlRtmCtxSecondRuntime} 2824 2825 rtmCtxPageFirstRuntime := fixRtmCtxPage(rtmCtxsFirstRuntime) 2826 rtmCtxPageSecondRuntime := fixRtmCtxPage(rtmCtxsSecondRuntime) 2827 rtmCtxPages := []*model.RuntimeContextPage{rtmCtxPageFirstRuntime, rtmCtxPageSecondRuntime} 2828 2829 gqlrtmCtxPageFirstApp := fixGQLRtmCtxPage(gqlRtmCtxsFirstRuntime) 2830 gqlrtmCtxPageSecondApp := fixGQLRtmCtxPage(gqlRtmCtxsSecondRuntime) 2831 gqlrtmCtxPages := []*graphql.RuntimeContextPage{gqlrtmCtxPageFirstApp, gqlrtmCtxPageSecondApp} 2832 2833 txGen := txtest.NewTransactionContextGenerator(testErr) 2834 2835 first := 2 2836 gqlAfter := graphql.PageCursor("test") 2837 after := "test" 2838 2839 testCases := []struct { 2840 Name string 2841 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 2842 ServiceFn func() *automock.RuntimeContextService 2843 ConverterFn func() *automock.RuntimeContextConverter 2844 ExpectedResult []*graphql.RuntimeContextPage 2845 ExpectedErr []error 2846 }{ 2847 { 2848 Name: "Success", 2849 TransactionerFn: txGen.ThatSucceeds, 2850 ServiceFn: func() *automock.RuntimeContextService { 2851 svc := &automock.RuntimeContextService{} 2852 svc.On("ListByRuntimeIDs", txtest.CtxWithDBMatcher(), runtimeIDs, first, after).Return(rtmCtxPages, nil).Once() 2853 return svc 2854 }, 2855 ConverterFn: func() *automock.RuntimeContextConverter { 2856 conv := &automock.RuntimeContextConverter{} 2857 conv.On("MultipleToGraphQL", rtmCtxsFirstRuntime).Return(gqlRtmCtxsFirstRuntime).Once() 2858 conv.On("MultipleToGraphQL", rtmCtxsSecondRuntime).Return(gqlRtmCtxsSecondRuntime).Once() 2859 return conv 2860 }, 2861 ExpectedResult: gqlrtmCtxPages, 2862 ExpectedErr: nil, 2863 }, 2864 { 2865 Name: "Returns error when transaction begin failed", 2866 TransactionerFn: txGen.ThatFailsOnBegin, 2867 ServiceFn: func() *automock.RuntimeContextService { 2868 svc := &automock.RuntimeContextService{} 2869 return svc 2870 }, 2871 ConverterFn: func() *automock.RuntimeContextConverter { 2872 conv := &automock.RuntimeContextConverter{} 2873 return conv 2874 }, 2875 ExpectedResult: nil, 2876 ExpectedErr: []error{testErr}, 2877 }, 2878 { 2879 Name: "Returns error when runtime context listing failed", 2880 TransactionerFn: txGen.ThatDoesntExpectCommit, 2881 ServiceFn: func() *automock.RuntimeContextService { 2882 svc := &automock.RuntimeContextService{} 2883 svc.On("ListByRuntimeIDs", txtest.CtxWithDBMatcher(), runtimeIDs, first, after).Return(nil, testErr).Once() 2884 return svc 2885 }, 2886 ConverterFn: func() *automock.RuntimeContextConverter { 2887 conv := &automock.RuntimeContextConverter{} 2888 return conv 2889 }, 2890 ExpectedResult: nil, 2891 ExpectedErr: []error{testErr}, 2892 }, 2893 { 2894 Name: "Returns error when transaction commit failed", 2895 TransactionerFn: txGen.ThatFailsOnCommit, 2896 ServiceFn: func() *automock.RuntimeContextService { 2897 svc := &automock.RuntimeContextService{} 2898 svc.On("ListByRuntimeIDs", txtest.CtxWithDBMatcher(), runtimeIDs, first, after).Return(rtmCtxPages, nil).Once() 2899 return svc 2900 }, 2901 ConverterFn: func() *automock.RuntimeContextConverter { 2902 conv := &automock.RuntimeContextConverter{} 2903 conv.On("MultipleToGraphQL", rtmCtxsFirstRuntime).Return(gqlRtmCtxsFirstRuntime).Once() 2904 conv.On("MultipleToGraphQL", rtmCtxsSecondRuntime).Return(gqlRtmCtxsSecondRuntime).Once() 2905 return conv 2906 }, 2907 ExpectedResult: nil, 2908 ExpectedErr: []error{testErr}, 2909 }, 2910 } 2911 2912 for _, testCase := range testCases { 2913 t.Run(testCase.Name, func(t *testing.T) { 2914 // GIVEN 2915 persist, transact := testCase.TransactionerFn() 2916 svc := testCase.ServiceFn() 2917 converter := testCase.ConverterFn() 2918 2919 resolver := runtime.NewResolver(transact, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, svc, converter, nil, nil, nil, nil) 2920 firstRuntimeParams := dataloader.ParamRuntimeContext{ID: firstRuntimeID, Ctx: context.TODO(), First: &first, After: &gqlAfter} 2921 secondRuntimeParams := dataloader.ParamRuntimeContext{ID: secondRuntimeID, Ctx: context.TODO(), First: &first, After: &gqlAfter} 2922 keys := []dataloader.ParamRuntimeContext{firstRuntimeParams, secondRuntimeParams} 2923 2924 // WHEN 2925 result, err := resolver.RuntimeContextsDataLoader(keys) 2926 2927 // then 2928 assert.Equal(t, testCase.ExpectedResult, result) 2929 assert.Equal(t, testCase.ExpectedErr, err) 2930 2931 mock.AssertExpectationsForObjects(t, svc, converter, transact, persist) 2932 }) 2933 } 2934 2935 t.Run("Returns error when there are no Runtimes", func(t *testing.T) { 2936 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 2937 // WHEN 2938 _, err := resolver.RuntimeContextsDataLoader([]dataloader.ParamRuntimeContext{}) 2939 // THEN 2940 require.Error(t, err[0]) 2941 assert.EqualError(t, err[0], apperrors.NewInternalError("No Runtimes found").Error()) 2942 }) 2943 2944 t.Run("Returns error when start cursor is nil", func(t *testing.T) { 2945 firstRuntimeParams := dataloader.ParamRuntimeContext{ID: firstRuntimeID, Ctx: context.TODO(), First: nil, After: &gqlAfter} 2946 keys := []dataloader.ParamRuntimeContext{firstRuntimeParams} 2947 2948 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 2949 // WHEN 2950 _, err := resolver.RuntimeContextsDataLoader(keys) 2951 // THEN 2952 require.Error(t, err[0]) 2953 assert.EqualError(t, err[0], apperrors.NewInvalidDataError("missing required parameter 'first'").Error()) 2954 }) 2955 } 2956 2957 func TestResolver_Auths(t *testing.T) { 2958 // GIVEN 2959 tnt := "tnt" 2960 externalTnt := "external-tnt" 2961 ctx := context.TODO() 2962 ctx = tenant.SaveToContext(ctx, tnt, externalTnt) 2963 2964 parentRuntime := fixGQLRuntime(t, "foo", "bar", "baz", "test.ns") 2965 2966 modelSysAuths := []pkgmodel.SystemAuth{ 2967 fixModelSystemAuth("bar", tnt, parentRuntime.ID, fixModelAuth()), 2968 fixModelSystemAuth("baz", tnt, parentRuntime.ID, fixModelAuth()), 2969 fixModelSystemAuth("faz", tnt, parentRuntime.ID, fixModelAuth()), 2970 } 2971 2972 gqlSysAuths := []*graphql.RuntimeSystemAuth{ 2973 fixGQLSystemAuth("bar", fixGQLAuth()), 2974 fixGQLSystemAuth("baz", fixGQLAuth()), 2975 fixGQLSystemAuth("faz", fixGQLAuth()), 2976 } 2977 2978 testErr := errors.New("this is a test error") 2979 txGen := txtest.NewTransactionContextGenerator(testErr) 2980 2981 testCases := []struct { 2982 Name string 2983 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 2984 SysAuthSvcFn func() *automock.SystemAuthService 2985 SysAuthConvFn func() *automock.SystemAuthConverter 2986 SelfRegManagerFn func() *automock.SelfRegisterManager 2987 ExpectedOutput []*graphql.RuntimeSystemAuth 2988 ExpectedError error 2989 }{ 2990 { 2991 Name: "Success", 2992 TransactionerFn: txGen.ThatSucceeds, 2993 SysAuthSvcFn: func() *automock.SystemAuthService { 2994 sysAuthSvc := &automock.SystemAuthService{} 2995 sysAuthSvc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.RuntimeReference, parentRuntime.ID).Return(modelSysAuths, nil).Once() 2996 return sysAuthSvc 2997 }, 2998 SysAuthConvFn: func() *automock.SystemAuthConverter { 2999 sysAuthConv := &automock.SystemAuthConverter{} 3000 sysAuthConv.On("ToGraphQL", &modelSysAuths[0]).Return(gqlSysAuths[0], nil).Once() 3001 sysAuthConv.On("ToGraphQL", &modelSysAuths[1]).Return(gqlSysAuths[1], nil).Once() 3002 sysAuthConv.On("ToGraphQL", &modelSysAuths[2]).Return(gqlSysAuths[2], nil).Once() 3003 return sysAuthConv 3004 }, 3005 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3006 ExpectedOutput: gqlSysAuths, 3007 ExpectedError: nil, 3008 }, 3009 { 3010 Name: "Error when listing for object", 3011 TransactionerFn: txGen.ThatDoesntExpectCommit, 3012 SysAuthSvcFn: func() *automock.SystemAuthService { 3013 sysAuthSvc := &automock.SystemAuthService{} 3014 sysAuthSvc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.RuntimeReference, parentRuntime.ID).Return(nil, testErr).Once() 3015 return sysAuthSvc 3016 }, 3017 SysAuthConvFn: func() *automock.SystemAuthConverter { 3018 sysAuthConv := &automock.SystemAuthConverter{} 3019 return sysAuthConv 3020 }, 3021 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3022 ExpectedOutput: nil, 3023 ExpectedError: testErr, 3024 }, 3025 { 3026 Name: "Error when beginning transaction", 3027 TransactionerFn: txGen.ThatFailsOnBegin, 3028 SysAuthSvcFn: func() *automock.SystemAuthService { 3029 sysAuthSvc := &automock.SystemAuthService{} 3030 return sysAuthSvc 3031 }, 3032 SysAuthConvFn: func() *automock.SystemAuthConverter { 3033 sysAuthConv := &automock.SystemAuthConverter{} 3034 return sysAuthConv 3035 }, 3036 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3037 ExpectedOutput: nil, 3038 ExpectedError: testErr, 3039 }, 3040 { 3041 Name: "Error when committing transaction", 3042 TransactionerFn: txGen.ThatFailsOnCommit, 3043 SysAuthSvcFn: func() *automock.SystemAuthService { 3044 sysAuthSvc := &automock.SystemAuthService{} 3045 sysAuthSvc.On("ListForObject", txtest.CtxWithDBMatcher(), pkgmodel.RuntimeReference, parentRuntime.ID).Return(modelSysAuths, nil).Once() 3046 return sysAuthSvc 3047 }, 3048 SysAuthConvFn: func() *automock.SystemAuthConverter { 3049 sysAuthConv := &automock.SystemAuthConverter{} 3050 return sysAuthConv 3051 }, 3052 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3053 ExpectedOutput: nil, 3054 ExpectedError: testErr, 3055 }, 3056 } 3057 3058 for _, testCase := range testCases { 3059 t.Run(testCase.Name, func(t *testing.T) { 3060 persist, transact := testCase.TransactionerFn() 3061 sysAuthSvc := testCase.SysAuthSvcFn() 3062 sysAuthConv := testCase.SysAuthConvFn() 3063 selfRegManager := testCase.SelfRegManagerFn() 3064 uuidSvc := &automock.UidService{} 3065 3066 resolver := runtime.NewResolver(transact, nil, nil, sysAuthSvc, nil, nil, sysAuthConv, nil, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 3067 3068 // WHEN 3069 result, err := resolver.Auths(ctx, parentRuntime) 3070 3071 // THEN 3072 if testCase.ExpectedError != nil { 3073 require.Error(t, err) 3074 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 3075 } else { 3076 assert.NoError(t, err) 3077 } 3078 assert.Equal(t, testCase.ExpectedOutput, result) 3079 3080 mock.AssertExpectationsForObjects(t, sysAuthSvc, sysAuthConv, transact, persist) 3081 }) 3082 } 3083 3084 t.Run("Error when parent object is nil", func(t *testing.T) { 3085 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 3086 3087 // WHEN 3088 result, err := resolver.Auths(context.TODO(), nil) 3089 3090 // THEN 3091 require.Error(t, err) 3092 assert.Contains(t, err.Error(), "Runtime cannot be empty") 3093 assert.Nil(t, result) 3094 }) 3095 } 3096 3097 func TestResolver_EventingConfiguration(t *testing.T) { 3098 // GIVEN 3099 tnt := "tnt" 3100 externalTnt := "external-tnt" 3101 ctx := context.TODO() 3102 ctx = tenant.SaveToContext(ctx, tnt, externalTnt) 3103 3104 runtimeID := uuid.New() 3105 gqlRuntime := fixGQLRuntime(t, runtimeID.String(), "bar", "baz", "test.ns") 3106 3107 testErr := errors.New("this is a test error") 3108 txGen := txtest.NewTransactionContextGenerator(testErr) 3109 3110 defaultEveningURL := "https://eventing.domain.local" 3111 modelRuntimeEventingCfg := fixModelRuntimeEventingConfiguration(t, defaultEveningURL) 3112 gqlRuntimeEventingCfg := fixGQLRuntimeEventingConfiguration(defaultEveningURL) 3113 3114 testCases := []struct { 3115 Name string 3116 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 3117 EventingSvcFn func() *automock.EventingService 3118 SelfRegManagerFn func() *automock.SelfRegisterManager 3119 ExpectedOutput *graphql.RuntimeEventingConfiguration 3120 ExpectedError error 3121 }{ 3122 { 3123 Name: "Success", 3124 TransactionerFn: txGen.ThatSucceeds, 3125 EventingSvcFn: func() *automock.EventingService { 3126 eventingSvc := &automock.EventingService{} 3127 eventingSvc.On("GetForRuntime", txtest.CtxWithDBMatcher(), runtimeID).Return(modelRuntimeEventingCfg, nil).Once() 3128 3129 return eventingSvc 3130 }, 3131 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3132 ExpectedOutput: gqlRuntimeEventingCfg, 3133 ExpectedError: nil, 3134 }, { 3135 Name: "Error when getting the configuration for runtime failed", 3136 TransactionerFn: txGen.ThatDoesntExpectCommit, 3137 EventingSvcFn: func() *automock.EventingService { 3138 eventingSvc := &automock.EventingService{} 3139 eventingSvc.On("GetForRuntime", txtest.CtxWithDBMatcher(), runtimeID).Return(nil, testErr).Once() 3140 3141 return eventingSvc 3142 }, 3143 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3144 ExpectedOutput: nil, 3145 ExpectedError: testErr, 3146 }, { 3147 Name: "Error when beginning transaction", 3148 TransactionerFn: txGen.ThatFailsOnBegin, 3149 EventingSvcFn: func() *automock.EventingService { 3150 eventingSvc := &automock.EventingService{} 3151 return eventingSvc 3152 }, 3153 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3154 ExpectedOutput: nil, 3155 ExpectedError: testErr, 3156 }, { 3157 Name: "Error when committing transaction", 3158 TransactionerFn: txGen.ThatFailsOnCommit, 3159 EventingSvcFn: func() *automock.EventingService { 3160 eventingSvc := &automock.EventingService{} 3161 eventingSvc.On("GetForRuntime", txtest.CtxWithDBMatcher(), runtimeID).Return(modelRuntimeEventingCfg, nil).Once() 3162 3163 return eventingSvc 3164 }, 3165 SelfRegManagerFn: rtmtest.NoopSelfRegManager, 3166 ExpectedOutput: nil, 3167 ExpectedError: testErr, 3168 }, 3169 } 3170 3171 for _, testCase := range testCases { 3172 t.Run(testCase.Name, func(t *testing.T) { 3173 persist, transact := testCase.TransactionerFn() 3174 eventingSvc := testCase.EventingSvcFn() 3175 selfRegManager := testCase.SelfRegManagerFn() 3176 uuidSvc := &automock.UidService{} 3177 3178 resolver := runtime.NewResolver(transact, nil, nil, nil, nil, nil, nil, eventingSvc, nil, selfRegManager, uuidSvc, nil, nil, nil, nil, nil, nil, nil) 3179 3180 // WHEN 3181 result, err := resolver.EventingConfiguration(ctx, gqlRuntime) 3182 3183 // THEN 3184 if testCase.ExpectedError != nil { 3185 require.Error(t, err) 3186 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 3187 } else { 3188 assert.NoError(t, err) 3189 } 3190 assert.Equal(t, testCase.ExpectedOutput, result) 3191 3192 mock.AssertExpectationsForObjects(t, eventingSvc, transact, persist) 3193 }) 3194 } 3195 3196 t.Run("Error when parent object ID is not a valid UUID", func(t *testing.T) { 3197 // GIVEN 3198 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 3199 3200 // WHEN 3201 result, err := resolver.EventingConfiguration(ctx, &graphql.Runtime{ID: "abc"}) 3202 3203 // THEN 3204 require.Error(t, err) 3205 assert.Contains(t, err.Error(), "while parsing runtime ID as UUID") 3206 assert.Nil(t, result) 3207 }) 3208 3209 t.Run("Error when parent object is nil", func(t *testing.T) { 3210 // GIVEN 3211 resolver := runtime.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) 3212 3213 // WHEN 3214 result, err := resolver.EventingConfiguration(context.TODO(), nil) 3215 3216 // THEN 3217 require.Error(t, err) 3218 assert.Contains(t, err.Error(), "Runtime cannot be empty") 3219 assert.Nil(t, result) 3220 }) 3221 } 3222 3223 func fixOAuths() []pkgmodel.SystemAuth { 3224 return []pkgmodel.SystemAuth{ 3225 { 3226 ID: "foo", 3227 TenantID: str.Ptr("foo"), 3228 Value: &model.Auth{ 3229 Credential: model.CredentialData{ 3230 Basic: nil, 3231 Oauth: &model.OAuthCredentialData{ 3232 ClientID: "foo", 3233 ClientSecret: "foo", 3234 URL: "foo", 3235 }, 3236 }, 3237 }, 3238 }, 3239 { 3240 ID: "bar", 3241 TenantID: str.Ptr("bar"), 3242 Value: nil, 3243 }, 3244 { 3245 ID: "test", 3246 TenantID: str.Ptr("test"), 3247 Value: &model.Auth{ 3248 Credential: model.CredentialData{ 3249 Basic: &model.BasicCredentialData{ 3250 Username: "test", 3251 Password: "test", 3252 }, 3253 Oauth: nil, 3254 }, 3255 }, 3256 }, 3257 } 3258 } 3259 3260 func UnusedRuntimeConverter() *automock.RuntimeConverter { 3261 return &automock.RuntimeConverter{} 3262 } 3263 3264 func UnusedScenarioAssignmentService() *automock.ScenarioAssignmentService { 3265 return &automock.ScenarioAssignmentService{} 3266 } 3267 3268 func UnusedSysAuthService() *automock.SystemAuthService { 3269 return &automock.SystemAuthService{} 3270 } 3271 3272 func UnusedOauth20Service() *automock.OAuth20Service { 3273 return &automock.OAuth20Service{} 3274 } 3275 3276 func UnusedBundleInstanceAuthService() *automock.BundleInstanceAuthService { 3277 return &automock.BundleInstanceAuthService{} 3278 } 3279 3280 func UnusedFormationService() *automock.FormationService { 3281 return &automock.FormationService{} 3282 }