Skip to content

Commit

Permalink
Merge pull request #10 from lawrencegripper/lg/issue/3
Browse files Browse the repository at this point in the history
Run integration tests over all examples in the repository using MiniKube and Travis
  • Loading branch information
lawrencegripper authored Jan 20, 2019
2 parents 3605102 + 3995a11 commit dff6dbd
Show file tree
Hide file tree
Showing 26 changed files with 104 additions and 995 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ website/vendor
!command/test-fixtures/**/.terraform/
**/terraform-provider-kubernetes
**/terraform-provider-kubernetes-yaml
_examples/yaml/terraform-provider-k8sraw
**/terraform-provider-k8sraw
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
services:
- docker
language: go
sudo: required
go:
- "1.10.x"
dist: xenial
Expand All @@ -15,6 +15,8 @@ install:
- go get github.com/kardianos/govendor

script:
- bash scripts/startminikube_ci.sh
- make testacc
- make build-binaries

matrix:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
provider "k8sraw" {}

resource "k8sraw_yaml" "test" {
yaml_body = <<YAML
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
name: __NAME_HERE__
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
azure/frontdoor: enabled
Expand All @@ -12,4 +16,6 @@ spec:
- path: /testpath
backend:
serviceName: test
servicePort: 80
servicePort: 80
YAML
}
19 changes: 1 addition & 18 deletions _examples/yaml/main.tf → _examples/service/basic_service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resource "k8sraw_yaml" "test" {
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-ingress
name: __NAME_HERE__
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
azure/frontdoor: enabled
Expand All @@ -20,20 +20,3 @@ spec:
YAML
}


resource "k8sraw_yaml" "test-service" {
yaml_body = <<YAML
apiVersion: v1
kind: Service
metadata:
name: terraform-nginx-example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
App: TerraformNginxExample
type: LoadBalancer
YAML
}
262 changes: 25 additions & 237 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package kubernetes

import (
"errors"
"os"
"strings"
"fmt"
"testing"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-aws/aws"
"github.com/terraform-providers/terraform-provider-google/google"
api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var testAccProviders map[string]terraform.ResourceProvider
Expand All @@ -21,9 +14,7 @@ var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"kubernetes": testAccProvider,
"google": google.Provider(),
"aws": aws.Provider(),
"k8sraw": testAccProvider,
}
}

Expand All @@ -33,241 +24,38 @@ func TestProvider(t *testing.T) {
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
func testAccCheckK8srawDestroy(s *terraform.State) error {
return testAccCheckK8srawStatus(s, 404)
}

func TestProvider_configure(t *testing.T) {
resetEnv := unsetEnv(t)
defer resetEnv()

os.Setenv("KUBECONFIG", "test-fixtures/kube-config.yaml")
os.Setenv("KUBE_CTX", "gcp")

c, err := config.NewRawConfig(map[string]interface{}{})
if err != nil {
t.Fatal(err)
}
rc := terraform.NewResourceConfig(c)
p := Provider()
err = p.Configure(rc)
if err != nil {
t.Fatal(err)
}
func testAccCheckK8srawExists(s *terraform.State) error {
return testAccCheckK8srawStatus(s, 200)
}

func unsetEnv(t *testing.T) func() {
e := getEnv()
func testAccCheckK8srawStatus(s *terraform.State, expectedCode int) error {
conn, _ := testAccProvider.Meta().(KubeProvider)()

if err := os.Unsetenv("KUBECONFIG"); err != nil {
t.Fatalf("Error unsetting env var KUBECONFIG: %s", err)
}
if err := os.Unsetenv("KUBE_CONFIG"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CONFIG: %s", err)
}
if err := os.Unsetenv("KUBE_CTX"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CTX: %s", err)
}
if err := os.Unsetenv("KUBE_CTX_AUTH_INFO"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CTX_AUTH_INFO: %s", err)
}
if err := os.Unsetenv("KUBE_CTX_CLUSTER"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CTX_CLUSTER: %s", err)
}
if err := os.Unsetenv("KUBE_HOST"); err != nil {
t.Fatalf("Error unsetting env var KUBE_HOST: %s", err)
}
if err := os.Unsetenv("KUBE_USER"); err != nil {
t.Fatalf("Error unsetting env var KUBE_USER: %s", err)
}
if err := os.Unsetenv("KUBE_PASSWORD"); err != nil {
t.Fatalf("Error unsetting env var KUBE_PASSWORD: %s", err)
}
if err := os.Unsetenv("KUBE_CLIENT_CERT_DATA"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CLIENT_CERT_DATA: %s", err)
}
if err := os.Unsetenv("KUBE_CLIENT_KEY_DATA"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CLIENT_KEY_DATA: %s", err)
}
if err := os.Unsetenv("KUBE_CLUSTER_CA_CERT_DATA"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CLUSTER_CA_CERT_DATA: %s", err)
}

return func() {
if err := os.Setenv("KUBE_CONFIG", e.Config); err != nil {
t.Fatalf("Error resetting env var KUBE_CONFIG: %s", err)
}
if err := os.Setenv("KUBECONFIG", e.Config); err != nil {
t.Fatalf("Error resetting env var KUBECONFIG: %s", err)
for _, rs := range s.RootModule().Resources {
if rs.Type != "k8sraw_yaml" {
continue
}
if err := os.Setenv("KUBE_CTX", e.Config); err != nil {
t.Fatalf("Error resetting env var KUBE_CTX: %s", err)
}
if err := os.Setenv("KUBE_CTX_AUTH_INFO", e.CtxAuthInfo); err != nil {
t.Fatalf("Error resetting env var KUBE_CTX_AUTH_INFO: %s", err)
}
if err := os.Setenv("KUBE_CTX_CLUSTER", e.CtxCluster); err != nil {
t.Fatalf("Error resetting env var KUBE_CTX_CLUSTER: %s", err)
}
if err := os.Setenv("KUBE_HOST", e.Host); err != nil {
t.Fatalf("Error resetting env var KUBE_HOST: %s", err)
}
if err := os.Setenv("KUBE_USER", e.User); err != nil {
t.Fatalf("Error resetting env var KUBE_USER: %s", err)
}
if err := os.Setenv("KUBE_PASSWORD", e.Password); err != nil {
t.Fatalf("Error resetting env var KUBE_PASSWORD: %s", err)
}
if err := os.Setenv("KUBE_CLIENT_CERT_DATA", e.ClientCertData); err != nil {
t.Fatalf("Error resetting env var KUBE_CLIENT_CERT_DATA: %s", err)
}
if err := os.Setenv("KUBE_CLIENT_KEY_DATA", e.ClientKeyData); err != nil {
t.Fatalf("Error resetting env var KUBE_CLIENT_KEY_DATA: %s", err)
}
if err := os.Setenv("KUBE_CLUSTER_CA_CERT_DATA", e.ClusterCACertData); err != nil {
t.Fatalf("Error resetting env var KUBE_CLUSTER_CA_CERT_DATA: %s", err)
}
}
}

func getEnv() *currentEnv {
e := &currentEnv{
Ctx: os.Getenv("KUBE_CTX_CLUSTER"),
CtxAuthInfo: os.Getenv("KUBE_CTX_AUTH_INFO"),
CtxCluster: os.Getenv("KUBE_CTX_CLUSTER"),
Host: os.Getenv("KUBE_HOST"),
User: os.Getenv("KUBE_USER"),
Password: os.Getenv("KUBE_PASSWORD"),
ClientCertData: os.Getenv("KUBE_CLIENT_CERT_DATA"),
ClientKeyData: os.Getenv("KUBE_CLIENT_KEY_DATA"),
ClusterCACertData: os.Getenv("KUBE_CLUSTER_CA_CERT_DATA"),
}
if cfg := os.Getenv("KUBE_CONFIG"); cfg != "" {
e.Config = cfg
}
if cfg := os.Getenv("KUBECONFIG"); cfg != "" {
e.Config = cfg
}
return e
}

func testAccPreCheck(t *testing.T) {
hasFileCfg := (os.Getenv("KUBE_CTX_AUTH_INFO") != "" && os.Getenv("KUBE_CTX_CLUSTER") != "")
hasStaticCfg := (os.Getenv("KUBE_HOST") != "" &&
os.Getenv("KUBE_USER") != "" &&
os.Getenv("KUBE_PASSWORD") != "" &&
os.Getenv("KUBE_CLIENT_CERT_DATA") != "" &&
os.Getenv("KUBE_CLIENT_KEY_DATA") != "" &&
os.Getenv("KUBE_CLUSTER_CA_CERT_DATA") != "")

if !hasFileCfg && !hasStaticCfg {
t.Fatalf("File config (KUBE_CTX_AUTH_INFO and KUBE_CTX_CLUSTER) or static configuration"+
" (%s) must be set for acceptance tests",
strings.Join([]string{
"KUBE_HOST",
"KUBE_USER",
"KUBE_PASSWORD",
"KUBE_CLIENT_CERT_DATA",
"KUBE_CLIENT_KEY_DATA",
"KUBE_CLUSTER_CA_CERT_DATA",
}, ", "))
}

err := testAccProvider.Configure(terraform.NewResourceConfig(nil))
if err != nil {
t.Fatal(err)
}
}

func skipIfNoGoogleCloudSettingsFound(t *testing.T) {
if os.Getenv("GOOGLE_PROJECT") == "" || os.Getenv("GOOGLE_REGION") == "" || os.Getenv("GOOGLE_ZONE") == "" {
t.Skip("The environment variables GOOGLE_PROJECT, GOOGLE_REGION and GOOGLE_ZONE" +
" must be set to run Google Cloud tests - skipping")
}
}

func skipIfNoAwsSettingsFound(t *testing.T) {
if os.Getenv("AWS_DEFAULT_REGION") == "" || os.Getenv("AWS_ZONE") == "" || os.Getenv("AWS_ACCESS_KEY_ID") == "" || os.Getenv("AWS_SECRET_ACCESS_KEY") == "" {
t.Skip("The environment variables AWS_DEFAULT_REGION, AWS_ZONE, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY" +
" must be set to run AWS tests - skipping")
}
}

func skipIfNoLoadBalancersAvailable(t *testing.T) {
// TODO: Support AWS ELBs
isInGke, err := isRunningInGke()
if err != nil {
t.Fatal(err)
}
if !isInGke {
t.Skip("The Kubernetes endpoint must come from an environment which supports " +
"load balancer provisioning for this test to run - skipping")
}
}

func skipIfNotRunningInGke(t *testing.T) {
isInGke, err := isRunningInGke()
if err != nil {
t.Fatal(err)
}
if !isInGke {
t.Skip("The Kubernetes endpoint must come from GKE for this test to run - skipping")
}
}

func isRunningInMinikube() (bool, error) {
node, err := getFirstNode()
if err != nil {
return false, err
}

labels := node.GetLabels()
if v, ok := labels["kubernetes.io/hostname"]; ok && v == "minikube" {
return true, nil
}
return false, nil
}

func isRunningInGke() (bool, error) {
node, err := getFirstNode()
if err != nil {
return false, err
}

labels := node.GetLabels()
if _, ok := labels["cloud.google.com/gke-nodepool"]; ok {
return true, nil
}
return false, nil
}
result := conn.RESTClient().Get().AbsPath(rs.Primary.ID).Do()
var statusCode int
result.StatusCode(&statusCode)
// Did we get the status code we expected?
if statusCode == expectedCode {
continue
}

func getFirstNode() (api.Node, error) {
meta := testAccProvider.Meta()
if meta == nil {
return api.Node{}, errors.New("Provider not initialized, unable to get cluster node")
}
conn, _ := meta.(KubeProvider)()
resp, err := conn.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return api.Node{}, err
}
// Another error occured
response, err := result.Get()
if err != nil {
return fmt.Errorf("Failed to get 404 for resource, likely a failure to delete occured: %+v", err)
}
return fmt.Errorf("Response: %+v", response)

if len(resp.Items) < 1 {
return api.Node{}, errors.New("Expected at least 1 node, none found")
}

return resp.Items[0], nil
}

type currentEnv struct {
Config string
Ctx string
CtxAuthInfo string
CtxCluster string
Host string
User string
Password string
ClientCertData string
ClientKeyData string
ClusterCACertData string
return nil
}
Loading

0 comments on commit dff6dbd

Please sign in to comment.