@@ -490,8 +490,131 @@ resource "kustomization_resource" "scprov" {
490
490
`
491
491
}
492
492
493
- //
494
- //
493
+ func TestAccResourceKustomization_wait (t * testing.T ) {
494
+
495
+ resource .Test (t , resource.TestCase {
496
+ //PreCheck: func() { testAccPreCheck(t) },
497
+ Providers : testAccProviders ,
498
+ Steps : []resource.TestStep {
499
+ //
500
+ //
501
+ // Applying initial config with a svc and deployment in a namespace with wait
502
+ {
503
+ Config : testAccResourceKustomizationConfig_wait ("test_kustomizations/wait/initial" ),
504
+ Check : resource .ComposeAggregateTestCheckFunc (
505
+ testAccCheckManifestNestedString ("kustomization_resource.dep1" , "test" , "spec" , "selector" , "matchLabels" , "app" ),
506
+ testAccCheckDeploymentReady ("kustomization_resource.dep1" , "test-wait" , "test" ),
507
+ ),
508
+ },
509
+ //
510
+ //
511
+ // Applying modified config updating the deployment annotation with wait
512
+ {
513
+ Config : testAccResourceKustomizationConfig_wait ("test_kustomizations/wait/modified" ),
514
+ Check : resource .ComposeAggregateTestCheckFunc (
515
+ testAccCheckManifestNestedString ("kustomization_resource.dep1" , "this will cause a redeploy" , "spec" , "template" , "metadata" , "annotations" , "new" ),
516
+ testAccCheckDeploymentReady ("kustomization_resource.dep1" , "test-wait" , "test" ),
517
+ ),
518
+ },
519
+ },
520
+ })
521
+ }
522
+
523
+ func testAccResourceKustomizationConfig_wait (path string ) string {
524
+ return testAccDataSourceKustomizationConfig_basic (path ) + `
525
+ resource "kustomization_resource" "ns" {
526
+ manifest = data.kustomization_build.test.manifests["_/Namespace/_/test-wait"]
527
+ }
528
+ resource "kustomization_resource" "dep1" {
529
+ manifest = data.kustomization_build.test.manifests["apps/Deployment/test-wait/test"]
530
+ wait = true
531
+ timeouts {
532
+ create = "1m"
533
+ update = "1m"
534
+ }
535
+ }
536
+ `
537
+ }
538
+
539
+ func TestAccResourceKustomization_wait_failure (t * testing.T ) {
540
+
541
+ resource .Test (t , resource.TestCase {
542
+ //PreCheck: func() { testAccPreCheck(t) },
543
+ Providers : testAccProviders ,
544
+ Steps : []resource.TestStep {
545
+ //
546
+ //
547
+ // Applying initial config with a svc and a failing deployment in a namespace with wait
548
+ {
549
+ Config : testAccResourceKustomizationConfig_wait_failure ("test_kustomizations/wait-fail/initial" ),
550
+ Check : resource .ComposeAggregateTestCheckFunc (
551
+ testAccCheckDeploymentNotReady ("kustomization_resource.dep1" , "test-wait-fail" , "test" ),
552
+ ),
553
+ ExpectError : regexp .MustCompile ("timed out creating/updating Deployment test-wait-fail/test:" ),
554
+ },
555
+ },
556
+ })
557
+ }
558
+
559
+ func testAccResourceKustomizationConfig_wait_failure (path string ) string {
560
+ return testAccDataSourceKustomizationConfig_basic (path ) + `
561
+ resource "kustomization_resource" "ns" {
562
+ manifest = data.kustomization_build.test.manifests["_/Namespace/_/test-wait-fail"]
563
+ }
564
+ resource "kustomization_resource" "dep1" {
565
+ manifest = data.kustomization_build.test.manifests["apps/Deployment/test-wait-fail/test"]
566
+ wait = true
567
+ timeouts {
568
+ create = "1m"
569
+ }
570
+ }
571
+ `
572
+ }
573
+
574
+ func TestAccResourceKustomization_nowait (t * testing.T ) {
575
+
576
+ resource .Test (t , resource.TestCase {
577
+ //PreCheck: func() { testAccPreCheck(t) },
578
+ Providers : testAccProviders ,
579
+ Steps : []resource.TestStep {
580
+ //
581
+ //
582
+ // Applying initial config with a svc and deployment in a namespace without wait
583
+ // so shouldn't exist immediately after creation
584
+ {
585
+ Config : testAccResourceKustomizationConfig_nowait ("test_kustomizations/nowait/initial" ),
586
+ Check : resource .ComposeAggregateTestCheckFunc (
587
+ testAccCheckManifestNestedString ("kustomization_resource.dep1" , "test" , "spec" , "selector" , "matchLabels" , "app" ),
588
+ testAccCheckDeploymentNotReady ("kustomization_resource.dep1" , "test-nowait" , "test" ),
589
+ ),
590
+ },
591
+ //
592
+ //
593
+ // Applying modified config updating the deployment annotation without wait,
594
+ // so we don't immediately expect the annotation to be present
595
+ {
596
+ Config : testAccResourceKustomizationConfig_nowait ("test_kustomizations/nowait/modified" ),
597
+ Check : resource .ComposeAggregateTestCheckFunc (
598
+ testAccCheckManifestNestedString ("kustomization_resource.dep1" , "this will cause a redeploy" , "spec" , "template" , "metadata" , "annotations" , "new" ),
599
+ testAccCheckDeploymentNotReady ("kustomization_resource.dep1" , "test-nowait" , "test" ),
600
+ ),
601
+ },
602
+ },
603
+ })
604
+ }
605
+
606
+ func testAccResourceKustomizationConfig_nowait (path string ) string {
607
+ return testAccDataSourceKustomizationConfig_basic (path ) + `
608
+ resource "kustomization_resource" "ns" {
609
+ manifest = data.kustomization_build.test.manifests["_/Namespace/_/test-nowait"]
610
+ }
611
+
612
+ resource "kustomization_resource" "dep1" {
613
+ manifest = data.kustomization_build.test.manifests["apps/Deployment/test-nowait/test"]
614
+ }
615
+ `
616
+ }
617
+
495
618
// Upgrade_API_Version Test
496
619
func TestAccResourceKustomization_upgradeAPIVersion (t * testing.T ) {
497
620
@@ -922,6 +1045,50 @@ func testAccCheckDeploymentPurged(n string) resource.TestCheckFunc {
922
1045
}
923
1046
}
924
1047
1048
+ func testAccCheckDeploymentReady (n string , namespace string , name string ) resource.TestCheckFunc {
1049
+ return func (s * terraform.State ) error {
1050
+ u , err := getResourceFromTestState (s , n )
1051
+ if err != nil {
1052
+ return err
1053
+ }
1054
+
1055
+ resp , err := getResourceFromK8sAPI (u )
1056
+ if err != nil {
1057
+ return err
1058
+ }
1059
+ ready , err := deploymentReady (resp )
1060
+ if err != nil {
1061
+ return err
1062
+ }
1063
+ if ! ready {
1064
+ return fmt .Errorf ("deployment %s in %s not ready" , name , namespace )
1065
+ }
1066
+ return nil
1067
+ }
1068
+ }
1069
+
1070
+ func testAccCheckDeploymentNotReady (n string , namespace string , name string ) resource.TestCheckFunc {
1071
+ return func (s * terraform.State ) error {
1072
+ u , err := getResourceFromTestState (s , n )
1073
+ if err != nil {
1074
+ return err
1075
+ }
1076
+
1077
+ resp , err := getResourceFromK8sAPI (u )
1078
+ if err != nil {
1079
+ return err
1080
+ }
1081
+ ready , err := deploymentReady (resp )
1082
+ if err != nil {
1083
+ return err
1084
+ }
1085
+ if ready {
1086
+ return fmt .Errorf ("deployment %s in %s unexpectedly ready" , name , namespace )
1087
+ }
1088
+ return nil
1089
+ }
1090
+ }
1091
+
925
1092
func getResourceFromTestState (s * terraform.State , n string ) (ur * k8sunstructured.Unstructured , err error ) {
926
1093
rs , ok := s .RootModule ().Resources [n ]
927
1094
if ! ok {
0 commit comments