|
| 1 | +//go:build acceptance |
| 2 | +// +build acceptance |
| 3 | + |
| 4 | +package acceptance |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/hashicorp/go-hclog" |
| 15 | + "github.com/hashicorp/terraform-provider-kubernetes/manifest/provider" |
| 16 | + tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/state" |
| 17 | +) |
| 18 | + |
| 19 | +func TestKubernetesManifest_CustomResource_x_preserve_unknown_fields(t *testing.T) { |
| 20 | + ctx := context.Background() |
| 21 | + |
| 22 | + reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t) |
| 23 | + if err != nil { |
| 24 | + t.Errorf("Failed to create provider instance: %q", err) |
| 25 | + } |
| 26 | + |
| 27 | + kind := strings.Title(randString(8)) |
| 28 | + plural := strings.ToLower(kind) + "s" |
| 29 | + group := "terraform.io" |
| 30 | + version := "v1" |
| 31 | + groupVersion := group + "/" + version |
| 32 | + crd := fmt.Sprintf("%s.%s", plural, group) |
| 33 | + |
| 34 | + name := strings.ToLower(randName()) |
| 35 | + namespace := "default" //randName() |
| 36 | + |
| 37 | + tfvars := TFVARS{ |
| 38 | + "name": name, |
| 39 | + "namespace": namespace, |
| 40 | + "kind": kind, |
| 41 | + "plural": plural, |
| 42 | + "group": group, |
| 43 | + "group_version": groupVersion, |
| 44 | + "cr_version": version, |
| 45 | + } |
| 46 | + |
| 47 | + crdStep := tfhelper.RequireNewWorkingDir(ctx, t) |
| 48 | + crdStep.SetReattachInfo(ctx, reattachInfo) |
| 49 | + defer func() { |
| 50 | + crdStep.Destroy(ctx) |
| 51 | + crdStep.Close() |
| 52 | + k8shelper.AssertResourceDoesNotExist(t, "apiextensions.k8s.io/v1", "customresourcedefinitions", crd) |
| 53 | + }() |
| 54 | + |
| 55 | + tfconfig := loadTerraformConfig(t, "x-kubernetes-preserve-unknown-fields/crd/test.tf", tfvars) |
| 56 | + crdStep.SetConfig(ctx, string(tfconfig)) |
| 57 | + crdStep.Init(ctx) |
| 58 | + crdStep.Apply(ctx) |
| 59 | + k8shelper.AssertResourceExists(t, "apiextensions.k8s.io/v1", "customresourcedefinitions", crd) |
| 60 | + |
| 61 | + // wait for API to finish ingesting the CRD |
| 62 | + time.Sleep(5 * time.Second) //lintignore:R018 |
| 63 | + |
| 64 | + reattachInfo2, err := provider.ServeTest(ctx, hclog.Default(), t) |
| 65 | + if err != nil { |
| 66 | + t.Errorf("Failed to create additional provider instance: %q", err) |
| 67 | + } |
| 68 | + |
| 69 | + step1 := tfhelper.RequireNewWorkingDir(ctx, t) |
| 70 | + step1.SetReattachInfo(ctx, reattachInfo2) |
| 71 | + defer func() { |
| 72 | + step1.Destroy(ctx) |
| 73 | + step1.Close() |
| 74 | + k8shelper.AssertResourceDoesNotExist(t, groupVersion, kind, name) |
| 75 | + }() |
| 76 | + |
| 77 | + tfconfig = loadTerraformConfig(t, "x-kubernetes-preserve-unknown-fields/test-cr-1.tf", tfvars) |
| 78 | + step1.SetConfig(ctx, string(tfconfig)) |
| 79 | + step1.Init(ctx) |
| 80 | + step1.Apply(ctx) |
| 81 | + |
| 82 | + s1, err := step1.State(ctx) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Failed to retrieve terraform state: %q", err) |
| 85 | + } |
| 86 | + tfstate := tfstatehelper.NewHelper(s1) |
| 87 | + tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{ |
| 88 | + "kubernetes_manifest.test.object.metadata.name": name, |
| 89 | + "kubernetes_manifest.test.object.metadata.namespace": namespace, |
| 90 | + "kubernetes_manifest.test.object.spec.count": json.Number("100"), |
| 91 | + "kubernetes_manifest.test.object.spec.resources": map[string]interface{}{ |
| 92 | + "foo": interface{}("bar"), |
| 93 | + }, |
| 94 | + }) |
| 95 | + |
| 96 | + tfconfig = loadTerraformConfig(t, "x-kubernetes-preserve-unknown-fields/test-cr-2.tf", tfvars) |
| 97 | + step1.SetConfig(ctx, string(tfconfig)) |
| 98 | + step1.Apply(ctx) |
| 99 | + |
| 100 | + s2, err := step1.State(ctx) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("Failed to retrieve terraform state: %q", err) |
| 103 | + } |
| 104 | + tfstate2 := tfstatehelper.NewHelper(s2) |
| 105 | + tfstate2.AssertAttributeValues(t, tfstatehelper.AttributeValues{ |
| 106 | + "kubernetes_manifest.test.object.metadata.name": name, |
| 107 | + "kubernetes_manifest.test.object.metadata.namespace": namespace, |
| 108 | + "kubernetes_manifest.test.object.spec.count": json.Number("100"), |
| 109 | + "kubernetes_manifest.test.object.spec.resources": map[string]interface{}{ |
| 110 | + "foo": interface{}("bar"), |
| 111 | + "baz": interface{}("42"), |
| 112 | + }, |
| 113 | + }) |
| 114 | +} |
0 commit comments