Skip to content

Commit

Permalink
Add more acceptance tests with managed internal annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsomesan committed May 10, 2019
1 parent 287e158 commit 4fa0271
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 44 deletions.
20 changes: 18 additions & 2 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
81 changes: 39 additions & 42 deletions kubernetes/resource_kubernetes_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
158 changes: 158 additions & 0 deletions kubernetes/resource_kubernetes_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 4fa0271

Please sign in to comment.