|
| 1 | +// +build acceptance |
| 2 | + |
| 3 | +package acceptance |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/hashicorp/go-hclog" |
| 14 | + "github.com/hashicorp/terraform-provider-kubernetes/manifest/provider" |
| 15 | + tfstatehelper "github.com/hashicorp/terraform-provider-kubernetes/manifest/test/helper/state" |
| 16 | +) |
| 17 | + |
| 18 | +func TestKubernetesManifest_CustomResource_OAPIv3_metadata(t *testing.T) { |
| 19 | + kind := strings.Title(randString(8)) |
| 20 | + plural := strings.ToLower(kind) + "s" |
| 21 | + group := "terraform.io" |
| 22 | + version := "v1" |
| 23 | + groupVersion := group + "/" + version |
| 24 | + crd := fmt.Sprintf("%s.%s", plural, group) |
| 25 | + |
| 26 | + name := strings.ToLower(randName()) |
| 27 | + namespace := "default" //randName() |
| 28 | + |
| 29 | + tfvars := TFVARS{ |
| 30 | + "name": name, |
| 31 | + "namespace": namespace, |
| 32 | + "kind": kind, |
| 33 | + "plural": plural, |
| 34 | + "group": group, |
| 35 | + "group_version": groupVersion, |
| 36 | + "cr_version": version, |
| 37 | + } |
| 38 | + |
| 39 | + step1 := tfhelper.RequireNewWorkingDir(t) |
| 40 | + step1.SetReattachInfo(reattachInfo) |
| 41 | + defer func() { |
| 42 | + step1.RequireDestroy(t) |
| 43 | + step1.Close() |
| 44 | + k8shelper.AssertResourceDoesNotExist(t, "apiextensions.k8s.io/v1", "customresourcedefinitions", crd) |
| 45 | + }() |
| 46 | + |
| 47 | + // Step 1: Create a structural CRD with a fairly complex schema |
| 48 | + // (inspired by the Prostgres Operator) |
| 49 | + tfconfig := loadTerraformConfig(t, "CustomResourceOAPI3-metadata/custom_resource_definition.tf", tfvars) |
| 50 | + step1.RequireSetConfig(t, string(tfconfig)) |
| 51 | + step1.RequireInit(t) |
| 52 | + step1.RequireApply(t) |
| 53 | + k8shelper.AssertResourceExists(t, "apiextensions.k8s.io/v1", "customresourcedefinitions", crd) |
| 54 | + |
| 55 | + // wait for API to finish ingesting the CRD |
| 56 | + time.Sleep(5 * time.Second) //lintignore:R018 |
| 57 | + |
| 58 | + reattachInfo2, err := provider.ServeTest(context.Background(), hclog.Default()) |
| 59 | + if err != nil { |
| 60 | + t.Errorf("Failed to create additional provider instance: %q", err) |
| 61 | + } |
| 62 | + step2 := tfhelper.RequireNewWorkingDir(t) |
| 63 | + step2.SetReattachInfo(reattachInfo2) |
| 64 | + defer func() { |
| 65 | + step2.RequireDestroy(t) |
| 66 | + step2.Close() |
| 67 | + k8shelper.AssertResourceDoesNotExist(t, groupVersion, kind, name) |
| 68 | + }() |
| 69 | + |
| 70 | + // Step 2: create a CR of the type defined by the CRD above |
| 71 | + tfconfig = loadTerraformConfig(t, "CustomResourceOAPI3-metadata/custom_resource.tf", tfvars) |
| 72 | + step2.RequireSetConfig(t, string(tfconfig)) |
| 73 | + step2.RequireInit(t) |
| 74 | + step2.RequireApply(t) |
| 75 | + |
| 76 | + tfstate := tfstatehelper.NewHelper(step2.RequireState(t)) |
| 77 | + tfstate.AssertAttributeValues(t, tfstatehelper.AttributeValues{ |
| 78 | + "kubernetes_manifest.test_cr.object.metadata.name": name, |
| 79 | + "kubernetes_manifest.test_cr.object.metadata.namespace": namespace, |
| 80 | + "kubernetes_manifest.test_cr.object.spec.teamId": "test", |
| 81 | + "kubernetes_manifest.test_cr.object.spec.volume.size": "1Gi", |
| 82 | + "kubernetes_manifest.test_cr.object.spec.users.foo_user": []interface{}{"superuser"}, |
| 83 | + "kubernetes_manifest.test_cr.object.spec.users.bar_user": []interface{}{}, |
| 84 | + "kubernetes_manifest.test_cr.object.spec.users.mike": []interface{}{"superuser", "createdb"}, |
| 85 | + "kubernetes_manifest.test_cr.object.spec.numberOfInstances": json.Number("2"), |
| 86 | + "kubernetes_manifest.test_cr.object.spec.databases.foo": "devdb", |
| 87 | + "kubernetes_manifest.test_cr.object.spec.postgresql.version": "12", |
| 88 | + }) |
| 89 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.additionalVolumes") |
| 90 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.allowedSourceRanges") |
| 91 | + |
| 92 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone") |
| 93 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.cluster") |
| 94 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.s3_access_key_id") |
| 95 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.s3_endpoint") |
| 96 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.s3_force_path_style") |
| 97 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.s3_secret_access_key") |
| 98 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.s3_wal_path") |
| 99 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.timestamp") |
| 100 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.clone.uid") |
| 101 | + |
| 102 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler") |
| 103 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.dockerImage") |
| 104 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.maxDBConnections") |
| 105 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.mode") |
| 106 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.numberOfInstances") |
| 107 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.schema") |
| 108 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.user") |
| 109 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.connectionPooler.resources") |
| 110 | + |
| 111 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.dockerImage") |
| 112 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.enableConnectionPooler") |
| 113 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.enableLogicalBackup") |
| 114 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.enableMasterLoadBalancer") |
| 115 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.enableReplicaConnectionPooler") |
| 116 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.enableReplicaLoadBalancer") |
| 117 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.enableShmVolume") |
| 118 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.initContainers") |
| 119 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.init_containers") |
| 120 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.logicalBackupSchedule") |
| 121 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.maintenanceWindows") |
| 122 | + |
| 123 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.nodeAffinity") |
| 124 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution") |
| 125 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution") |
| 126 | + |
| 127 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni") |
| 128 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.initdb") |
| 129 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.loop_wait") |
| 130 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.maximum_lag_on_failover") |
| 131 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.pg_hba") |
| 132 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.retry_timeout") |
| 133 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.slots") |
| 134 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.synchronous_mode") |
| 135 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.synchronous_mode_strict") |
| 136 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.patroni.ttl") |
| 137 | + |
| 138 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.podAnnotations") |
| 139 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.podPriorityClassName") |
| 140 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.pod_priority_class_name") |
| 141 | + |
| 142 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.postgresql") |
| 143 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.postgresql.parameters") |
| 144 | + |
| 145 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.preparedDatabases") |
| 146 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.replicaLoadBalancer") |
| 147 | + |
| 148 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources") |
| 149 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources.limits") |
| 150 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources.limits.cpu") |
| 151 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources.limits.memory") |
| 152 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources.requests") |
| 153 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources.requests.cpu") |
| 154 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.resources.requests.memory") |
| 155 | + |
| 156 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.schedulerName") |
| 157 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.serviceAnnotations") |
| 158 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.sidecars") |
| 159 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.spiloFSGroup") |
| 160 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.spiloRunAsGroup") |
| 161 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.spiloRunAsUser") |
| 162 | + |
| 163 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.standby") |
| 164 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.standby.s3_wal_path") |
| 165 | + |
| 166 | + tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test_cr.object.spec.tls") |
| 167 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.tls.caFile") |
| 168 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.tls.caSecretName") |
| 169 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.tls.certificateFile") |
| 170 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.tls.privateKeyFile") |
| 171 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.tls.secretName") |
| 172 | + |
| 173 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.tolerations") |
| 174 | + tfstate.AssertAttributeEmpty(t, "kubernetes_manifest.test_cr.object.spec.useLoadBalancer") |
| 175 | + |
| 176 | +} |
0 commit comments