From 9203ad72a577ec793bc8b5f85978910860f054b8 Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Wed, 26 Dec 2018 14:59:46 -0700 Subject: [PATCH] rnd-ify service_account name used by TestAccKubernetesPod_config_with_automount_service_account_token --- kubernetes/resource_kubernetes_pod_test.go | 32 ++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/kubernetes/resource_kubernetes_pod_test.go b/kubernetes/resource_kubernetes_pod_test.go index e515cc92d5..fa2250771c 100644 --- a/kubernetes/resource_kubernetes_pod_test.go +++ b/kubernetes/resource_kubernetes_pod_test.go @@ -1,9 +1,11 @@ package kubernetes import ( + "bytes" "fmt" "os" "testing" + "text/template" api "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -595,6 +597,7 @@ func TestAccKubernetesPod_config_with_automount_service_account_token(t *testing podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) imageName := "nginx:1.7.9" + serviceAccountName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -602,7 +605,7 @@ func TestAccKubernetesPod_config_with_automount_service_account_token(t *testing CheckDestroy: testAccCheckKubernetesPodDestroy, Steps: []resource.TestStep{ { - Config: testAccKubernetesPodConfigWithAutomountServiceAccountToken(podName, imageName), + Config: testAccKubernetesPodConfigWithAutomountServiceAccountToken(podName, imageName, serviceAccountName), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckKubernetesPodExists("kubernetes_pod.test", &confPod), testAccCheckKubernetesServiceAccountExists("kubernetes_service_account.test", &confSA), @@ -1324,11 +1327,18 @@ resource "kubernetes_pod" "test" { `, podName, imageName, val) } -func testAccKubernetesPodConfigWithAutomountServiceAccountToken(podName, imageName string) string { - return fmt.Sprintf(` +func testAccKubernetesPodConfigWithAutomountServiceAccountToken(podName string, imageName string, serviceAccountName string) string { + type PodTmplArgs struct { + PodName string + ImageName string + ServiceAccountName string + } + args := PodTmplArgs{podName, imageName, serviceAccountName} + + podTmpl := ` resource "kubernetes_service_account" "test" { metadata { - name = "foo" + name = "{{.ServiceAccountName}}" } } @@ -1338,20 +1348,26 @@ resource "kubernetes_pod" "test" { app = "pod_label" } - name = "%s" + name = "{{.PodName}}" } spec { - service_account_name = "foo" + service_account_name = "{{.ServiceAccountName}}" automount_service_account_token = true container { - image = "%s" + image = "{{.ImageName}}" name = "containername" } } depends_on = [ "kubernetes_service_account.test" ] } -`, podName, imageName) +` + t := template.Must(template.New("pod").Parse(podTmpl)) + buf := &bytes.Buffer{} + if err := t.Execute(buf, args); err != nil { + panic(err) + } + return buf.String() }