From 5f714b6ea2704b890f289c4b8614c6a2010ba6ff Mon Sep 17 00:00:00 2001 From: Alexander Chernov Date: Fri, 25 Oct 2024 15:46:07 +0100 Subject: [PATCH] cleanup and improvements for tests. --- ...data_source_kubectl_file_documents_test.go | 6 +- kubernetes/resource_kubectl_manifest_test.go | 354 +++++++++--------- 2 files changed, 184 insertions(+), 176 deletions(-) diff --git a/kubernetes/data_source_kubectl_file_documents_test.go b/kubernetes/data_source_kubectl_file_documents_test.go index 95a45b48..9aeedf65 100644 --- a/kubernetes/data_source_kubectl_file_documents_test.go +++ b/kubernetes/data_source_kubectl_file_documents_test.go @@ -7,7 +7,7 @@ import ( ) func TestAccKubectlDataSourceFileDocuments_single(t *testing.T) { - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() {}, Providers: testAccProviders, Steps: []resource.TestStep{ @@ -25,7 +25,7 @@ func TestAccKubectlDataSourceFileDocuments_single(t *testing.T) { } func TestAccKubectlDataSourceFileDocuments_basic(t *testing.T) { - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() {}, Providers: testAccProviders, Steps: []resource.TestStep{ @@ -45,7 +45,7 @@ func TestAccKubectlDataSourceFileDocuments_basic(t *testing.T) { } func TestAccKubectlDataSourceFileDocuments_basicMultipleEmpty(t *testing.T) { - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() {}, Providers: testAccProviders, Steps: []resource.TestStep{ diff --git a/kubernetes/resource_kubectl_manifest_test.go b/kubernetes/resource_kubectl_manifest_test.go index 4afb06d3..3b78877c 100644 --- a/kubernetes/resource_kubectl_manifest_test.go +++ b/kubernetes/resource_kubectl_manifest_test.go @@ -28,7 +28,7 @@ YAML ` expectedError, _ := regexp.Compile(".*failed to create kubernetes.*") - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ Providers: testAccProviders, Steps: []resource.TestStep{ { @@ -39,7 +39,7 @@ YAML }) } -func TestAccKubectl(t *testing.T) { +func TestAccKubectl_SinglePod(t *testing.T) { //language=hcl config := ` resource "kubectl_manifest" "test" { @@ -48,11 +48,11 @@ resource "kubectl_manifest" "test" { apiVersion: v1 kind: Pod metadata: - name: nginx + name: caddy-single-pod spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine readinessProbe: httpGet: path: "/" @@ -62,8 +62,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -84,22 +83,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -111,8 +110,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -134,22 +132,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait-foreground labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -161,8 +159,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -184,22 +181,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait-background labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -211,8 +208,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -233,22 +229,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx + name: caddy-wait-for-rollout-dp labels: - app: nginx + app: caddy spec: replicas: 3 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -260,8 +256,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -282,23 +277,23 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: DaemonSet metadata: - name: nginx + name: caddy-wait-for-rollout-ds labels: - app: nginx + app: caddy spec: updateStrategy: type: RollingUpdate selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -310,8 +305,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -332,37 +326,37 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: StatefulSet metadata: - name: nginx + name: caddy-wait-for-rollout-ss labels: - app: nginx + app: caddy spec: updateStrategy: type: RollingUpdate replicas: 3 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: httpGet: path: "/" port: 80 - initialDelaySeconds: 10 + initialDelaySeconds: 5 YAML } ` //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -383,11 +377,11 @@ resource "kubectl_manifest" "test" { apiVersion: v1 kind: Pod metadata: - name: nginx + name: caddy spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine readinessProbe: httpGet: path: "/" @@ -397,9 +391,8 @@ YAML } ` - //start := time.Now() expectedError, _ := regexp.Compile(".*at least one of `field` or `condition` must be provided in `wait_for` block.*") - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -423,7 +416,7 @@ resource "kubectl_manifest" "test_wait_for" { apiVersion: v1 kind: Namespace metadata: - name: test-wait-for + name: test-wait-for-negative EOF wait_for { @@ -432,11 +425,11 @@ EOF value = "Activez" } } -}` //start := time.Now() +}` // atm the actual error is being hidden by the wait context being deleted. Fix this at some point //errorRegex, _ := regexp.Compile(".*failed to wait for resource*") errorRegex, _ := regexp.Compile(".*Wait returned an error*") - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -447,7 +440,6 @@ EOF }, }, }) - log.Println(config) } func TestAccKubectl_WaitForNegativeCondition(t *testing.T) { @@ -468,18 +460,18 @@ resource "kubectl_manifest" "test" { apiVersion: v1 kind: Pod metadata: - name: busybox-sleep + name: busybox-sleep-wait-for-negative spec: containers: - name: busybox image: busybox command: ["sleep", "30"] YAML -}` //start := time.Now() +}` // atm the actual error is being hidden by the wait context being deleted. Fix this at some point //errorRegex, _ := regexp.Compile(".*failed to wait for resource*") errorRegex, _ := regexp.Compile(".*Wait returned an error*") - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -514,7 +506,7 @@ EOF } } }` //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -551,11 +543,11 @@ resource "kubectl_manifest" "test" { apiVersion: v1 kind: Pod metadata: - name: nginx + name: caddy-wait-for-field spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine readinessProbe: httpGet: path: "/" @@ -565,8 +557,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -597,11 +588,11 @@ resource "kubectl_manifest" "test" { apiVersion: v1 kind: Pod metadata: - name: nginx + name: caddy-wait-for-conditions spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine readinessProbe: httpGet: path: "/" @@ -611,8 +602,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -656,11 +646,11 @@ resource "kubectl_manifest" "test" { apiVersion: v1 kind: Pod metadata: - name: nginx + name: caddy-wait-for-field-and-conditions spec: containers: - - name: nginx - image: nginx:1.14.2 + - name: caddy + image: caddy:2.8.4-alpine readinessProbe: httpGet: path: "/" @@ -671,7 +661,7 @@ YAML ` //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -693,22 +683,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait-for-condition-update labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.27.0 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -720,6 +710,7 @@ YAML } ` + //language=hcl updateConfig := ` resource "kubectl_manifest" "test" { wait_for_rollout = false @@ -733,22 +724,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait-for-condition-update labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.27.2 + - name: caddy + image: caddy:2.8.1-alpine ports: - containerPort: 80 readinessProbe: @@ -760,8 +751,7 @@ YAML } ` - //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -785,22 +775,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait-for-field labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.27.0 + - name: caddy + image: caddy:2.8.4-alpine ports: - containerPort: 80 readinessProbe: @@ -812,6 +802,7 @@ YAML } ` + // language=hcl updateConfig := ` resource "kubectl_manifest" "test" { wait_for_rollout = false @@ -825,22 +816,22 @@ resource "kubectl_manifest" "test" { apiVersion: apps/v1 kind: Deployment metadata: - name: nginx-deployment + name: caddy-wait-for-field labels: - app: nginx + app: caddy spec: replicas: 1 selector: matchLabels: - app: nginx + app: caddy template: metadata: labels: - app: nginx + app: caddy spec: containers: - - name: nginx - image: nginx:1.27.2 + - name: caddy + image: caddy:2.8.1-alpine ports: - containerPort: 80 readinessProbe: @@ -853,7 +844,7 @@ YAML ` //start := time.Now() - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckkubectlDestroy, @@ -884,7 +875,7 @@ YAML //` // // //start := time.Now() -// resource.Test(t, resource.TestCase{ +// resource.ParallelTest(t, resource.TestCase{ // PreCheck: func() { testAccPreCheck(t) }, // Providers: testAccProviders, // CheckDestroy: testAccCheckkubectlDestroy, @@ -897,21 +888,23 @@ YAML // }) //} -func TestAccInconsistentPlanning(t *testing.T) { +func TestAccKubectl_InconsistentPlanning(t *testing.T) { + //See https://github.com/alekc/terraform-provider-kubectl/pull/46 + // language=hcl config := ` resource "kubectl_manifest" "secret" { yaml_body = <