Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.log
**/terraform-provider-kubernetes*
/terraform-provider-kubernetes*
/test/acceptance/testdata/*/*.tfvars
76 changes: 76 additions & 0 deletions test/acceptance/service_cluster_ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// +build acceptance

package acceptance

import (
"encoding/json"
"testing"

tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes-alpha/test/helper/state"
)

// This test case tests a Service but also is a demonstration of some the assert functions
// available in the test helper
func TestKubernetesManifest_Service_ClusterIP(t *testing.T) {
name := randName()
namespace := randName()

tf := tfhelper.RequireNewWorkingDir(t)
tf.SetReattachInfo(reattachInfo)
defer func() {
tf.RequireDestroy(t)
tf.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "services", namespace, name)
}()

k8shelper.CreateNamespace(t, namespace)
defer k8shelper.DeleteNamespace(t, namespace)

tfvars := TFVARS{
"namespace": namespace,
"name": name,
}
tfconfig := loadTerraformConfig(t, "Service_ClusterIP/service.tf", tfvars)
tf.RequireSetConfig(t, tfconfig)
tf.RequireInit(t)
tf.RequireApply(t)

k8shelper.AssertNamespacedResourceExists(t, "v1", "services", namespace, name)

tfstate := tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.spec.ports.0.name": "http",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("80"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8080"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "ClusterIP",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a service of type "ClusterIP" is created, the resulting resource gets two attributes added by the controller manager which hold the actual cluster IP address(es) allocated to the service. These were causing inconsistent state issues in the pre-0.3.0 provider and are a simple demonstration of why we need the OpenAPI type system.

It would be useful to add assertions for the presence of these two attributes which demonstrate the resource is correctly typed and Terraform saves these computed values as well.

Here an example of how they show up in state:

resource "kubernetes_manifest" "test" {
    manifest = {
        apiVersion = "v1"
        kind       = "Service"
        metadata   = {
            name      = "alextest"
            namespace = "default"
        }
        spec       = {
            ports    = [
                {
                    name       = "http"
                    port       = 80
                    protocol   = "TCP"
                    targetPort = 8080
                },
            ]
            selector = {
                app = "test"
            }
            type     = "ClusterIP"
        }
    }
    object   = {
        apiVersion = "v1"
        kind       = "Service"
        metadata   = {
            annotations                = null
            clusterName                = null
            creationTimestamp          = null
            deletionGracePeriodSeconds = null
            deletionTimestamp          = null
            finalizers                 = null
            generateName               = null
            generation                 = null
            labels                     = null
            managedFields              = null
            name                       = "alextest"
            namespace                  = "default"
            ownerReferences            = null
            resourceVersion            = null
            selfLink                   = null
            uid                        = null
        }
        spec       = {
            allocateLoadBalancerNodePorts = null
            clusterIP                     = "10.104.36.74"
            clusterIPs                    = [
                "10.104.36.74",
            ]
            externalIPs                   = null
            externalName                  = null
            externalTrafficPolicy         = null
            healthCheckNodePort           = null
            ipFamilies                    = null
            ipFamilyPolicy                = null
            loadBalancerIP                = null
            loadBalancerSourceRanges      = null
            ports                         = [
                {
                    appProtocol = null
                    name        = "http"
                    nodePort    = null
                    port        = 80
                    protocol    = "TCP"
                    targetPort  = 8080
                },
            ]
            publishNotReadyAddresses      = null
            selector                      = {
                "app" = "test"
            }
            sessionAffinity               = "None"
            sessionAffinityConfig         = {
                clientIP = {
                    timeoutSeconds = null
                }
            }
            topologyKeys                  = null
            type                          = "ClusterIP"
        }
    }
}

})

tfconfigModified := loadTerraformConfig(t, "Service_ClusterIP/service_modified.tf", tfvars)
tf.RequireSetConfig(t, tfconfigModified)
tf.RequireApply(t)

tfstate = tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.annotations.test": "1",
"kubernetes_manifest.test.object.metadata.labels.test": "2",
"kubernetes_manifest.test.object.spec.ports.0.name": "https",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("443"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8443"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "ClusterIP",
})

tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.labels", 1)
tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.annotations", 1)

tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.metadata.labels.test")

tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.spec")
}
72 changes: 72 additions & 0 deletions test/acceptance/service_external_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// +build acceptance

package acceptance

import (
"testing"

tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes-alpha/test/helper/state"
)

// This test case tests a Service but also is a demonstration of some the assert functions
// available in the test helper
func TestKubernetesManifest_Service_ExternalName(t *testing.T) {
name := randName()
namespace := randName()

tf := tfhelper.RequireNewWorkingDir(t)
tf.SetReattachInfo(reattachInfo)
defer func() {
tf.RequireDestroy(t)
tf.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "services", namespace, name)
}()

k8shelper.CreateNamespace(t, namespace)
defer k8shelper.DeleteNamespace(t, namespace)

tfvars := TFVARS{
"namespace": namespace,
"name": name,
}
tfconfig := loadTerraformConfig(t, "Service_ExternalName/service.tf", tfvars)
tf.RequireSetConfig(t, tfconfig)
tf.RequireInit(t)
tf.RequireApply(t)

k8shelper.AssertNamespacedResourceExists(t, "v1", "services", namespace, name)

tfstate := tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "ExternalName",
"kubernetes_manifest.test.object.spec.externalName": "terraform.kubernetes.test.com",
})

tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.object.spec.ports.0")

tfconfigModified := loadTerraformConfig(t, "Service_ExternalName/service_modified.tf", tfvars)
tf.RequireSetConfig(t, tfconfigModified)
tf.RequireApply(t)

tfstate = tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.annotations.test": "1",
"kubernetes_manifest.test.object.metadata.labels.test": "2",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "ExternalName",
"kubernetes_manifest.test.object.spec.externalName": "kubernetes-alpha.terraform.test.com",
})

tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.labels", 1)
tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.annotations", 1)

tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.metadata.labels.test")

tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.spec")

}
76 changes: 76 additions & 0 deletions test/acceptance/service_load_balancer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// +build acceptance

package acceptance

import (
"encoding/json"
"testing"

tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes-alpha/test/helper/state"
)

// This test case tests a Service but also is a demonstration of some the assert functions
// available in the test helper
func TestKubernetesManifest_Service_LoadBalancer(t *testing.T) {
name := randName()
namespace := randName()

tf := tfhelper.RequireNewWorkingDir(t)
tf.SetReattachInfo(reattachInfo)
defer func() {
tf.RequireDestroy(t)
tf.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "services", namespace, name)
}()

k8shelper.CreateNamespace(t, namespace)
defer k8shelper.DeleteNamespace(t, namespace)

tfvars := TFVARS{
"namespace": namespace,
"name": name,
}
tfconfig := loadTerraformConfig(t, "Service_LoadBalancer/service.tf", tfvars)
tf.RequireSetConfig(t, tfconfig)
tf.RequireInit(t)
tf.RequireApply(t)

k8shelper.AssertNamespacedResourceExists(t, "v1", "services", namespace, name)

tfstate := tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.spec.ports.0.name": "http",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("80"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8080"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "LoadBalancer",
})

tfconfigModified := loadTerraformConfig(t, "Service_LoadBalancer/service_modified.tf", tfvars)
tf.RequireSetConfig(t, tfconfigModified)
tf.RequireApply(t)

tfstate = tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.annotations.test": "1",
"kubernetes_manifest.test.object.metadata.labels.test": "2",
"kubernetes_manifest.test.object.spec.ports.0.name": "https",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("443"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8443"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "LoadBalancer",
})

tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.labels", 1)
tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.annotations", 1)

tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.metadata.labels.test")

tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.spec")
}
79 changes: 79 additions & 0 deletions test/acceptance/service_node_port_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// +build acceptance

package acceptance

import (
"encoding/json"
"testing"

tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes-alpha/test/helper/state"
)

// This test case tests a Service but also is a demonstration of some the assert functions
// available in the test helper
func TestKubernetesManifest_Service_NodePort(t *testing.T) {
name := randName()
namespace := randName()

tf := tfhelper.RequireNewWorkingDir(t)
tf.SetReattachInfo(reattachInfo)
defer func() {
tf.RequireDestroy(t)
tf.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "services", namespace, name)
}()

k8shelper.CreateNamespace(t, namespace)
defer k8shelper.DeleteNamespace(t, namespace)

tfvars := TFVARS{
"namespace": namespace,
"name": name,
}
tfconfig := loadTerraformConfig(t, "Service_NodePort/service.tf", tfvars)
tf.RequireSetConfig(t, tfconfig)
tf.RequireInit(t)
tf.RequireApply(t)

k8shelper.AssertNamespacedResourceExists(t, "v1", "services", namespace, name)

tfstate := tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.spec.ports.0.name": "http",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("80"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8080"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "NodePort",
})

tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.spec.ports.0.nodePort")

tfconfigModified := loadTerraformConfig(t, "Service_NodePort/service_modified.tf", tfvars)
tf.RequireSetConfig(t, tfconfigModified)
tf.RequireApply(t)

tfstate = tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.annotations.test": "1",
"kubernetes_manifest.test.object.metadata.labels.test": "2",
"kubernetes_manifest.test.object.spec.ports.0.name": "https",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("443"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8443"),
"kubernetes_manifest.test.object.spec.ports.0.nodePort": json.Number("32767"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "NodePort",
})

tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.labels", 1)
tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.annotations", 1)

tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.metadata.labels.test")

tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.spec")
}
76 changes: 76 additions & 0 deletions test/acceptance/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// +build acceptance

package acceptance

import (
"encoding/json"
"testing"

tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes-alpha/test/helper/state"
)

// This test case tests a Service but also is a demonstration of some the assert functions
// available in the test helper
func TestKubernetesManifest_Service(t *testing.T) {
name := randName()
namespace := randName()

tf := tfhelper.RequireNewWorkingDir(t)
tf.SetReattachInfo(reattachInfo)
defer func() {
tf.RequireDestroy(t)
tf.Close()
k8shelper.AssertNamespacedResourceDoesNotExist(t, "v1", "services", namespace, name)
}()

k8shelper.CreateNamespace(t, namespace)
defer k8shelper.DeleteNamespace(t, namespace)

tfvars := TFVARS{
"namespace": namespace,
"name": name,
}
tfconfig := loadTerraformConfig(t, "Service/service.tf", tfvars)
tf.RequireSetConfig(t, tfconfig)
tf.RequireInit(t)
tf.RequireApply(t)

k8shelper.AssertNamespacedResourceExists(t, "v1", "services", namespace, name)

tfstate := tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.spec.ports.0.name": "http",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("80"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8080"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "ClusterIP",
})

tfconfigModified := loadTerraformConfig(t, "Service/service_modified.tf", tfvars)
tf.RequireSetConfig(t, tfconfigModified)
tf.RequireApply(t)

tfstate = tfstatehelper.NewHelper(tf.RequireState(t))
tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{
"kubernetes_manifest.test.object.metadata.namespace": namespace,
"kubernetes_manifest.test.object.metadata.name": name,
"kubernetes_manifest.test.object.metadata.annotations.test": "1",
"kubernetes_manifest.test.object.metadata.labels.test": "2",
"kubernetes_manifest.test.object.spec.ports.0.name": "https",
"kubernetes_manifest.test.object.spec.ports.0.port": json.Number("443"),
"kubernetes_manifest.test.object.spec.ports.0.targetPort": json.Number("8443"),
"kubernetes_manifest.test.object.spec.ports.0.protocol": "TCP",
"kubernetes_manifest.test.object.spec.selector.app": "test",
"kubernetes_manifest.test.object.spec.type": "ClusterIP",
})

tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.labels", 1)
tfstate.AssertAttributeLen(t, "kubernetes_manifest.test.object.metadata.annotations", 1)

tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.metadata.labels.test")

tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.spec")
}
Loading