From 4fa027153cf647b2679040b6c4653ef24e34f816 Mon Sep 17 00:00:00 2001 From: Alex Somesan Date: Sat, 11 May 2019 00:15:05 +0200 Subject: [PATCH] Add more acceptance tests with managed internal annotations. --- kubernetes/provider_test.go | 20 ++- .../resource_kubernetes_ingress_test.go | 81 +++++---- .../resource_kubernetes_service_test.go | 158 ++++++++++++++++++ 3 files changed, 215 insertions(+), 44 deletions(-) diff --git a/kubernetes/provider_test.go b/kubernetes/provider_test.go index af93a1ffbc..17d022097c 100644 --- a/kubernetes/provider_test.go +++ b/kubernetes/provider_test.go @@ -204,12 +204,15 @@ func skipIfNoAwsSettingsFound(t *testing.T) { } func skipIfNoLoadBalancersAvailable(t *testing.T) { - // TODO: Support AWS ELBs isInGke, err := isRunningInGke() if err != nil { t.Fatal(err) } - if !isInGke { + isInEks, err := isRunningInEks() + if err != nil { + t.Fatal(err) + } + if !isInGke && !isInEks { t.Skip("The Kubernetes endpoint must come from an environment which supports " + "load balancer provisioning for this test to run - skipping") } @@ -273,6 +276,19 @@ func isRunningInGke() (bool, error) { return false, nil } +func isRunningInEks() (bool, error) { + node, err := getFirstNode() + if err != nil { + return false, err + } + + labels := node.GetLabels() + if _, ok := labels["failure-domain.beta.kubernetes.io/region"]; ok { + return true, nil + } + return false, nil +} + func getFirstNode() (api.Node, error) { meta := testAccProvider.Meta() if meta == nil { diff --git a/kubernetes/resource_kubernetes_ingress_test.go b/kubernetes/resource_kubernetes_ingress_test.go index ed8f68bc7c..cd3faa43d0 100644 --- a/kubernetes/resource_kubernetes_ingress_test.go +++ b/kubernetes/resource_kubernetes_ingress_test.go @@ -107,49 +107,46 @@ func TestAccKubernetesIngress_TLS(t *testing.T) { }, }) } +func TestAccKubernetesIngress_InternalKey(t *testing.T) { + var conf api.Ingress + name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) -// Test disabled until internal annotations issue is resolved. -// -// func TestAccKubernetesIngress_InternalKey(t *testing.T) { -// var conf api.Ingress -// name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - -// resource.Test(t, resource.TestCase{ -// PreCheck: func() { testAccPreCheck(t) }, -// IDRefreshName: "kubernetes_ingress.test", -// Providers: testAccProviders, -// CheckDestroy: testAccCheckKubernetesIngressDestroy, -// Steps: []resource.TestStep{ -// { -// Config: testAccKubernetesIngressConfig_internalKey(name), -// Check: resource.ComposeAggregateTestCheckFunc( -// testAccCheckKubernetesIngressExists("kubernetes_ingress.test", &conf), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.name", name), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.annotations.kubernetes.io/ingress-anno", "one"), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.labels.kubernetes.io/ingress-label", "one"), -// ), -// }, -// { -// Config: testAccKubernetesIngressConfig_internalKey_removed(name), -// Check: resource.ComposeAggregateTestCheckFunc( -// testAccCheckKubernetesIngressExists("kubernetes_ingress.test", &conf), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.name", name), -// resource.TestCheckNoResourceAttr("kubernetes_ingress.test", "metadata.0.annotations.kubernetes.io/ingress-anno"), -// resource.TestCheckNoResourceAttr("kubernetes_ingress.test", "metadata.0.labels.kubernetes.io/ingress-label"), -// ), -// }, -// { -// Config: testAccKubernetesIngressConfig_internalKey(name), -// Check: resource.ComposeAggregateTestCheckFunc( -// testAccCheckKubernetesIngressExists("kubernetes_ingress.test", &conf), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.name", name), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.annotations.kubernetes.io/ingress-anno", "one"), -// resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.labels.kubernetes.io/ingress-label", "one"), -// ), -// }, -// }, -// }) -// } + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "kubernetes_ingress.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckKubernetesIngressDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesIngressConfig_internalKey(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesIngressExists("kubernetes_ingress.test", &conf), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.name", name), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.annotations.kubernetes.io/ingress-anno", "one"), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.labels.kubernetes.io/ingress-label", "one"), + ), + }, + { + Config: testAccKubernetesIngressConfig_internalKey_removed(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesIngressExists("kubernetes_ingress.test", &conf), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.name", name), + resource.TestCheckNoResourceAttr("kubernetes_ingress.test", "metadata.0.annotations.kubernetes.io/ingress-anno"), + resource.TestCheckNoResourceAttr("kubernetes_ingress.test", "metadata.0.labels.kubernetes.io/ingress-label"), + ), + }, + { + Config: testAccKubernetesIngressConfig_internalKey(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesIngressExists("kubernetes_ingress.test", &conf), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.name", name), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.annotations.kubernetes.io/ingress-anno", "one"), + resource.TestCheckResourceAttr("kubernetes_ingress.test", "metadata.0.labels.kubernetes.io/ingress-label", "one"), + ), + }, + }, + }) +} func testAccCheckKubernetesIngressDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*kubernetes.Clientset) diff --git a/kubernetes/resource_kubernetes_service_test.go b/kubernetes/resource_kubernetes_service_test.go index 80fe678e0e..a51ada9c30 100644 --- a/kubernetes/resource_kubernetes_service_test.go +++ b/kubernetes/resource_kubernetes_service_test.go @@ -196,6 +196,94 @@ func TestAccKubernetesService_loadBalancer(t *testing.T) { }) } +func TestAccKubernetesService_loadBalancer_annotations_aws(t *testing.T) { + var conf api.Service + name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); skipIfNoLoadBalancersAvailable(t) }, + IDRefreshName: "kubernetes_service.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckKubernetesServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesServiceConfig_loadBalancer_annotations_aws(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesServiceExists("kubernetes_service.test", &conf), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.name", name), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.%", "4"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-backend-protocol", "http"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-access-log-enabled", "true"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout", "300"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-ssl-ports", "*"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.#", "1"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.#", "1"), + resource.TestCheckResourceAttrSet("kubernetes_service.test", "spec.0.port.0.node_port"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.port", "8888"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.protocol", "TCP"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.target_port", "80"), + resource.TestCheckResourceAttrSet("kubernetes_service.test", "spec.0.cluster_ip"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.#", "2"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.1452553500", "10.0.0.4"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.3371212991", "10.0.0.3"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_name", "ext-name-"+name), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.#", "2"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.138364083", "10.0.0.5/32"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.445311837", "10.0.0.6/32"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.selector.%", "1"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.selector.App", "MyApp"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.session_affinity", "ClientIP"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.type", "LoadBalancer"), + testAccCheckServicePorts(&conf, []api.ServicePort{ + { + Port: int32(8888), + Protocol: api.ProtocolTCP, + TargetPort: intstr.FromInt(80), + }, + }), + ), + }, + { + Config: testAccKubernetesServiceConfig_loadBalancer_annotations_aws_modified(name), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesServiceExists("kubernetes_service.test", &conf), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.name", name), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.%", "3"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-access-log-enabled", "false"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-type", "nlb"), + resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.annotations.service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout", "100"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.#", "1"), + resource.TestCheckResourceAttrSet("kubernetes_service.test", "spec.0.cluster_ip"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.#", "2"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.1452553500", "10.0.0.4"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_ips.563283338", "10.0.0.5"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.external_name", "ext-name-modified-"+name), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.#", "2"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.2271073252", "10.0.0.1/32"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.load_balancer_source_ranges.2515041290", "10.0.0.2/32"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.#", "1"), + resource.TestCheckResourceAttrSet("kubernetes_service.test", "spec.0.port.0.node_port"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.port", "9999"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.protocol", "TCP"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.target_port", "81"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.selector.%", "2"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.selector.App", "MyModifiedApp"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.selector.NewSelector", "NewValue"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.session_affinity", "ClientIP"), + resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.type", "LoadBalancer"), + testAccCheckServicePorts(&conf, []api.ServicePort{ + { + Port: int32(9999), + Protocol: api.ProtocolTCP, + TargetPort: intstr.FromInt(81), + }, + }), + ), + }, + }, + }) +} + func TestAccKubernetesService_nodePort(t *testing.T) { var conf api.Service name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) @@ -626,6 +714,76 @@ resource "kubernetes_service" "test" { `, name, name) } +func testAccKubernetesServiceConfig_loadBalancer_annotations_aws(name string) string { + return fmt.Sprintf(` +resource "kubernetes_service" "test" { + metadata { + name = "%s" + annotations = { + "service.beta.kubernetes.io/aws-load-balancer-backend-protocol" = "http" + "service.beta.kubernetes.io/aws-load-balancer-access-log-enabled" = "true" + "service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout" = "300" + "service.beta.kubernetes.io/aws-load-balancer-ssl-ports" = "*" + } + } + + spec { + external_name = "ext-name-%s" + external_ips = ["10.0.0.3", "10.0.0.4"] + load_balancer_source_ranges = ["10.0.0.5/32", "10.0.0.6/32"] + + selector = { + App = "MyApp" + } + + session_affinity = "ClientIP" + + port { + port = 8888 + target_port = 80 + } + + type = "LoadBalancer" + } +} +`, name, name) +} + +func testAccKubernetesServiceConfig_loadBalancer_annotations_aws_modified(name string) string { + return fmt.Sprintf(` +resource "kubernetes_service" "test" { + metadata { + name = "%s" + annotations = { + "service.beta.kubernetes.io/aws-load-balancer-access-log-enabled" = "false" + "service.beta.kubernetes.io/aws-load-balancer-type" = "nlb" + "service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout" = "100" + } + } + + spec { + external_name = "ext-name-modified-%s" + external_ips = ["10.0.0.4", "10.0.0.5"] + load_balancer_source_ranges = ["10.0.0.1/32", "10.0.0.2/32"] + + selector = { + App = "MyModifiedApp" + NewSelector = "NewValue" + } + + session_affinity = "ClientIP" + + port { + port = 9999 + target_port = 81 + } + + type = "LoadBalancer" + } +} +`, name, name) +} + func testAccKubernetesServiceConfig_nodePort(name string) string { return fmt.Sprintf(` resource "kubernetes_service" "test" {