Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
more shuffling
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston committed May 6, 2020
1 parent 946bb1c commit 26080eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
3 changes: 2 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TEST?="./provider"
ACCTEST?="./acctest"
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
PKG_NAME=provider
Expand All @@ -17,7 +18,7 @@ test: fmtcheck
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
go test $(ACCTEST) -v $(TESTARGS) -timeout 120m

vet:
@echo "go vet ."
Expand Down
18 changes: 8 additions & 10 deletions provider/kubernetes_manifest_test.go → acctest/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestAccKubernetesManifest_ConfigMap(t *testing.T) {
func TestKubernetesManifest_ConfigMap(t *testing.T) {
wd := helper.RequireNewWorkingDir(t)
defer func() {
wd.RequireDestroy(t)
Expand All @@ -17,27 +17,25 @@ func TestAccKubernetesManifest_ConfigMap(t *testing.T) {
defer deleteKubernetesNamespace(t, namespace)

name := randName()
tfconfig := testAccKubernetesManifestConfig_ConfigMap(namespace, name)
tfconfig := testKubernetesManifestConfig_ConfigMap(namespace, name)
wd.RequireSetConfig(t, tfconfig)

wd.RequireInit(t)
wd.RequireApply(t)

assertKubernetesNamespacedResourceExists(t, "v1", "configmaps", namespace, name)

state := wd.RequireState(t)
object := getObjectFromResourceState(t, state, "kubernetes_manifest.test")
object := getObjectAttributeFromResourceState(t, state, "kubernetes_manifest.test")
assertObjectFieldEqual(t, object, "metadata.namespace", namespace)
assertObjectFieldEqual(t, object, "metadata.name", name)
assertObjectFieldEqual(t, object, "data.foo", "bar")

tfconfigModified := testAccKubernetesManifestConfig_ConfigMapModified(namespace, name)
tfconfigModified := testKubernetesManifestConfig_ConfigMapModified(namespace, name)
wd.RequireSetConfig(t, tfconfigModified)

wd.RequireApply(t)
state = wd.RequireState(t)
object = getObjectFromResourceState(t, state, "kubernetes_manifest.test")

state = wd.RequireState(t)
object = getObjectAttributeFromResourceState(t, state, "kubernetes_manifest.test")
assertObjectFieldEqual(t, object, "metadata.namespace", namespace)
assertObjectFieldEqual(t, object, "metadata.name", name)
assertObjectFieldEqual(t, object, "metadata.annotations.test", "1")
Expand All @@ -46,7 +44,7 @@ func TestAccKubernetesManifest_ConfigMap(t *testing.T) {
assertObjectFieldEqual(t, object, "data.fizz", "buzz")
}

func testAccKubernetesManifestConfig_ConfigMap(namespace, name string) string {
func testKubernetesManifestConfig_ConfigMap(namespace, name string) string {
return fmt.Sprintf(`
resource "kubernetes_manifest" "test" {
provider = kubernetes-alpha
Expand All @@ -65,7 +63,7 @@ resource "kubernetes_manifest" "test" {
}`, name, namespace)
}

func testAccKubernetesManifestConfig_ConfigMapModified(namespace, name string) string {
func testKubernetesManifestConfig_ConfigMapModified(namespace, name string) string {
return fmt.Sprintf(`
resource "kubernetes_manifest" "test" {
provider = kubernetes-alpha
Expand Down
78 changes: 43 additions & 35 deletions provider/provider_test.go → acctest/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"

provider "github.com/hashicorp/terraform-provider-kubernetes-alpha/provider"

tfjson "github.com/hashicorp/terraform-json"
tftest "github.com/hashicorp/terraform-plugin-test"

Expand All @@ -29,7 +32,7 @@ var kubernetesClient dynamic.Interface

func TestMain(m *testing.M) {
if tftest.RunningAsPlugin() {
Serve()
provider.Serve()
os.Exit(0)
return
}
Expand Down Expand Up @@ -73,21 +76,8 @@ func configureKubernetesClient() (dynamic.Interface, error) {
}

func createGroupVersionResource(gv, resource string) schema.GroupVersionResource {
var group, groupVersion string
gvparts := strings.Split(gv, "/")
if len(gvparts) < 2 {
group = ""
groupVersion = gv
} else {
group = gvparts[0]
groupVersion = gvparts[1]
}

return schema.GroupVersionResource{
Group: group,
Version: groupVersion,
Resource: resource,
}
gvr, _ := schema.ParseGroupVersion(gv)
return gvr.WithResource(resource)
}

func assertKubernetesNamespacedResourceExists(t *testing.T, gv, resource, namespace, name string) {
Expand Down Expand Up @@ -153,14 +143,14 @@ func randName() string {
return fmt.Sprintf("tf-acc-test-%s", string(b))
}

// getObjectFromResourceState will pull out the value of the `object` attribute of the resource and return it
// getAttributeFromResourceState will pull out the value of the specified attribute of the resource and return it
// as a map[string]interface{}.
func getObjectFromResourceState(t *testing.T, state *tfjson.State, resourceAddr string) map[string]interface{} {
func getAttributeFromResourceState(t *testing.T, state *tfjson.State, resourceAddr string, attributeName string) interface{} {
for _, r := range state.Values.RootModule.Resources {
if r.Address == resourceAddr {
value, ok := r.AttributeValues["object"].(map[string]interface{})
value, ok := r.AttributeValues[attributeName]
if !ok {
t.Fatalf("Could not find get `object` attribute from %q", resourceAddr)
t.Fatalf("Could not find get %q attribute from %q", attributeName, resourceAddr)
}
return value
}
Expand All @@ -170,28 +160,46 @@ func getObjectFromResourceState(t *testing.T, state *tfjson.State, resourceAddr
return nil
}

func getObjectAttributeFromResourceState(t *testing.T, state *tfjson.State, resourceAddr string) map[string]interface{} {
value := getAttributeFromResourceState(t, state, resourceAddr, "object")
obj, ok := value.(map[string]interface{})
if !ok {
t.Fatalf(`"object" doesn't seem to be a map: %v`, value)
}
return obj
}

var errFieldNotFound = fmt.Errorf("Field not found")

// findFieldValue will return the value of a field in the object using dot notation
func findFieldValue(object map[string]interface{}, fieldPath string) (interface{}, error) {
pathKeys := strings.Split(fieldPath, ".")
fieldKey := pathKeys[len(pathKeys)-1]
parentFieldPathKeys := pathKeys[:len(pathKeys)-1]

m := object
for _, key := range parentFieldPathKeys {
// FIXME need to handle arrays like: spec.containers.0.name
v, ok := m[key].(map[string]interface{})
func findFieldValue(object interface{}, fieldPath string) (interface{}, error) {
keys := strings.Split(fieldPath, ".")
key := keys[0]

var value interface{}
if index, err := strconv.Atoi(key); err == nil {
s, ok := object.([]interface{})
if !ok || index >= len(s) {
return nil, errFieldNotFound
}
value = s[index]
} else {
m, ok := object.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Field %q does not exist", fieldPath)
return nil, errFieldNotFound
}
m = v
v, ok := m[key]
if !ok {
return nil, errFieldNotFound
}
value = v
}

value, ok := m[fieldKey].(interface{})
if !ok {
return nil, fmt.Errorf("Field %q does not exist", fieldPath)
if len(keys) == 1 {
return value, nil
}

return value, nil
return findFieldValue(value, strings.Join(keys[1:], "."))
}

func assertObjectFieldEqual(t *testing.T, object map[string]interface{}, fieldPath string, expectedValue interface{}) {
Expand Down

0 comments on commit 26080eb

Please sign in to comment.