Skip to content

Commit e7ade6c

Browse files
shobha2626Shreyas Sreenivas
authored and
Shreyas Sreenivas
committed
Acceptance test for namespace
1 parent f934115 commit e7ade6c

14 files changed

+358
-28
lines changed

internal/provider/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Provider() *schema.Provider {
2828
DataSourcesMap: map[string]*schema.Resource{
2929
cluster.ResourceName: cluster.DataSourceTMCCluster(),
3030
workspace.ResourceName: workspace.DataSourceWorkspace(),
31-
namespace.ResourceName: namespace.DataSourceTMCNamespace(),
31+
namespace.ResourceName: namespace.DataSourceNamespace(),
3232
clustergroup.ResourceName: clustergroup.DataSourceClusterGroup(),
3333
},
3434
ConfigureContextFunc: authctx.ProviderConfigureContext,

internal/resources/cluster/cluster_flatten_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ func TestFlattenSpec(t *testing.T) {
3030
name: "normal scenario with cluster group and proxy",
3131
input: &clustermodel.VmwareTanzuManageV1alpha1ClusterSpec{
3232
ClusterGroupName: "default",
33-
ProxyName: "test",
3433
},
3534
expected: []interface{}{
3635
map[string]interface{}{
3736
clusterGroupKey: "default",
38-
proxyKey: "test",
3937
},
4038
},
4139
},

internal/resources/cluster/constants.go

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const (
99
managementClusterNameKey = "management_cluster_name"
1010
provisionerNameKey = "provisioner_name"
1111
clusterGroupKey = "cluster_group"
12-
proxyKey = "proxy"
1312
clusterNameKey = "name"
1413
specKey = "spec"
1514
statusKey = "status"

internal/resources/cluster/resource_cluster.go

-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ func flattenSpec(spec *clustermodel.VmwareTanzuManageV1alpha1ClusterSpec) (data
147147
flattenSpecData := make(map[string]interface{})
148148

149149
flattenSpecData[clusterGroupKey] = spec.ClusterGroupName
150-
flattenSpecData[proxyKey] = spec.ProxyName
151150

152151
return []interface{}{flattenSpecData}
153152
}

internal/resources/clustergroup/data_source_cluster_group_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ import (
1616
testhelper "gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/testing"
1717
)
1818

19+
const (
20+
clusterGroupResource = "tmc_cluster_group"
21+
clusterGroupResourceVar = "test_cluster_group"
22+
clusterGroupDataSourceVar = "test_data_cluster_group"
23+
)
24+
1925
func TestAcceptanceForClusterGroupDataSource(t *testing.T) {
2026
var provider = initTestProvider(t)
2127

2228
clusterGroup := acctest.RandomWithPrefix("tf-cg-test")
23-
dataSourceName := "data.tmc_cluster_group.test_data_cluster_group"
24-
resourceName := "tmc_cluster_group.test_cluster_group"
29+
dataSourceName := fmt.Sprintf("data.%s.%s", clusterGroupResource, clusterGroupDataSourceVar)
30+
resourceName := fmt.Sprintf("%s.%s", clusterGroupResource, clusterGroupResourceVar)
2531

2632
resource.Test(t, resource.TestCase{
2733
PreCheck: testhelper.TestPreCheck(t),
@@ -42,15 +48,15 @@ func TestAcceptanceForClusterGroupDataSource(t *testing.T) {
4248

4349
func getTestDataSourceClusterGroupConfigValue(clusterGroupName, meta string) string {
4450
return fmt.Sprintf(`
45-
resource "tmc_cluster_group" "test_cluster_group" {
51+
resource "%s" "%s" {
4652
name = "%s"
4753
%s
4854
}
4955
50-
data "tmc_cluster_group" "test_data_cluster_group" {
56+
data "%s" "%s" {
5157
name = tmc_cluster_group.test_cluster_group.name
5258
}
53-
`, clusterGroupName, meta)
59+
`, clusterGroupResource, clusterGroupResourceVar, clusterGroupName, meta, clusterGroupResource, clusterGroupDataSourceVar)
5460
}
5561

5662
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {

internal/resources/clustergroup/resource_cluster_group_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
func TestAcceptanceForClusterGroupResource(t *testing.T) {
2525
var provider = initTestProvider(t)
2626

27-
resourceName := "tmc_cluster_group.test_cluster_group"
27+
resourceName := fmt.Sprintf("%s.%s", clusterGroupResource, clusterGroupResourceVar)
2828
clusterGroupName := acctest.RandomWithPrefix("tf-cg-test")
2929

3030
resource.Test(t, resource.TestCase{
@@ -53,19 +53,19 @@ func TestAcceptanceForClusterGroupResource(t *testing.T) {
5353

5454
func getTestResourceClusterGroupBasicConfigValue(clusterGroupName string) string {
5555
return fmt.Sprintf(`
56-
resource "tmc_cluster_group" "test_cluster_group" {
56+
resource "%s" "%s" {
5757
name = "%s"
5858
}
59-
`, clusterGroupName)
59+
`, clusterGroupResource, clusterGroupResourceVar, clusterGroupName)
6060
}
6161

6262
func getTestResourceClusterGroupConfigValue(clusterGroupName string) string {
6363
return fmt.Sprintf(`
64-
resource "tmc_cluster_group" "test_cluster_group" {
64+
resource "%s" "%s" {
6565
name = "%s"
6666
%s
6767
}
68-
`, clusterGroupName, testhelper.MetaTemplate)
68+
`, clusterGroupResource, clusterGroupResourceVar, clusterGroupName, testhelper.MetaTemplate)
6969
}
7070

7171
func checkResourceAttributes(provider *schema.Provider, resourceName, clusterGroupName string) resource.TestCheckFunc {

internal/resources/common/objectmeta_schema.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package common
77

88
import (
99
"fmt"
10+
"strings"
1011

1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213

@@ -38,8 +39,13 @@ var Meta = &schema.Schema{
3839
LabelsKey: {
3940
Type: schema.TypeMap,
4041
Optional: true,
41-
Computed: true,
4242
Elem: &schema.Schema{Type: schema.TypeString},
43+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
44+
if strings.Contains(k, "tmc.cloud.vmware.com/creator") || strings.Contains(k, "tmc.cloud.vmware.com/managed") || strings.Contains(k, "tmc.cloud.vmware.com/workspace") {
45+
return true
46+
}
47+
return false
48+
},
4349
},
4450
DescriptionKey: {
4551
Type: schema.TypeString,

internal/resources/namespace/data_source_namespace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/common"
1818
)
1919

20-
func DataSourceTMCNamespace() *schema.Resource {
20+
func DataSourceNamespace() *schema.Resource {
2121
return &schema.Resource{
2222
ReadContext: dataSourceNamespaceRead,
2323
Schema: namespaceSchema,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Copyright © 2021 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package namespace
7+
8+
import (
9+
"fmt"
10+
"os"
11+
"testing"
12+
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
16+
17+
testhelper "gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/testing"
18+
)
19+
20+
const (
21+
namespaceResource = "tmc_namespace"
22+
namespaceResourceVar = "test_namespace"
23+
namespaceDataSourceVar = "test_data_namespace"
24+
25+
clusterResource = "tmc_cluster"
26+
clusterResourceVar = "tmc_cluster_test"
27+
)
28+
29+
func TestAcceptanceForNamespaceDataSource(t *testing.T) {
30+
var provider = initTestProvider(t)
31+
32+
namespaceName := acctest.RandomWithPrefix("tf-ns-test")
33+
clusterName := acctest.RandomWithPrefix("tf-cluster")
34+
dataSourceName := fmt.Sprintf("data.%s.%s", namespaceResource, namespaceDataSourceVar)
35+
resourceName := fmt.Sprintf("%s.%s", namespaceResource, namespaceResourceVar)
36+
37+
resource.Test(t, resource.TestCase{
38+
PreCheck: testPreCheck(t),
39+
ProviderFactories: getTestProviderFactories(provider),
40+
CheckDestroy: nil,
41+
Steps: []resource.TestStep{
42+
{
43+
Config: getTestNamespaceDataSourceConfigValue(t, clusterName, namespaceName, testhelper.MetaTemplate),
44+
Check: resource.ComposeTestCheckFunc(
45+
checkDataSourceAttributes(dataSourceName, resourceName),
46+
),
47+
},
48+
},
49+
})
50+
t.Log("namespace data source acceptance test complete!")
51+
}
52+
53+
func getTestNamespaceDataSourceConfigValue(t *testing.T, clusterName, namespaceName, meta string) string {
54+
kubeconfigPath := os.Getenv("KUBECONFIG")
55+
if kubeconfigPath == "" {
56+
t.Skipf("KUBECONFIG env var is not set: %s", kubeconfigPath)
57+
}
58+
59+
return fmt.Sprintf(`
60+
resource "%s" "%s" {
61+
name = "%s"
62+
63+
attach_k8s_cluster {
64+
kubeconfig_file = "%s"
65+
}
66+
67+
%s
68+
69+
spec {
70+
cluster_group = "default"
71+
}
72+
73+
wait_until_ready = true
74+
}
75+
76+
resource "%s" "%s" {
77+
name = "%s"
78+
cluster_name = tmc_cluster.tmc_cluster_test.name
79+
80+
spec {
81+
workspace_name = "default"
82+
attach = false
83+
}
84+
}
85+
86+
data "%s" "%s" {
87+
name = tmc_namespace.test_namespace.name
88+
cluster_name = tmc_namespace.test_namespace.cluster_name
89+
}
90+
`, clusterResource, clusterResourceVar, clusterName, kubeconfigPath, meta, namespaceResource,
91+
namespaceResourceVar, namespaceName, namespaceResource, namespaceDataSourceVar)
92+
}
93+
94+
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
95+
var check = []resource.TestCheckFunc{
96+
verifyNamespaceDataSource(dataSourceName),
97+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
98+
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
99+
}
100+
101+
check = append(check, testhelper.MetaDataSourceAttributeCheck(dataSourceName, resourceName)...)
102+
103+
return resource.ComposeTestCheckFunc(check...)
104+
}
105+
106+
func verifyNamespaceDataSource(name string) resource.TestCheckFunc {
107+
return func(s *terraform.State) error {
108+
_, ok := s.RootModule().Resources[name]
109+
if !ok {
110+
return fmt.Errorf("root module does not have namespace resource %s", name)
111+
}
112+
113+
return nil
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright © 2021 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package namespace
7+
8+
import (
9+
"os"
10+
"testing"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/stretchr/testify/require"
14+
15+
"gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/authctx"
16+
"gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/cluster"
17+
)
18+
19+
const providerName = "tmc"
20+
21+
func initTestProvider(t *testing.T) *schema.Provider {
22+
testProvider := &schema.Provider{
23+
Schema: authctx.ProviderAuthSchema(),
24+
ResourcesMap: map[string]*schema.Resource{
25+
ResourceName: ResourceNamespace(),
26+
cluster.ResourceName: cluster.ResourceTMCCluster(),
27+
},
28+
DataSourcesMap: map[string]*schema.Resource{
29+
ResourceName: DataSourceNamespace(),
30+
cluster.ResourceName: cluster.DataSourceTMCCluster(),
31+
},
32+
ConfigureContextFunc: authctx.ProviderConfigureContext,
33+
}
34+
if err := testProvider.InternalValidate(); err != nil {
35+
require.NoError(t, err)
36+
}
37+
38+
return testProvider
39+
}
40+
41+
func testPreCheck(t *testing.T) func() {
42+
return func() {
43+
for _, env := range []string{authctx.ServerEndpointEnvVar, authctx.CSPTokenEnvVar, authctx.CSPEndpointEnvVar} {
44+
require.NotEmpty(t, os.Getenv(env))
45+
}
46+
}
47+
}
48+
49+
func getTestProviderFactories(provider *schema.Provider) map[string]func() (*schema.Provider, error) {
50+
//nolint:unparam
51+
return map[string]func() (*schema.Provider, error){
52+
providerName: func() (*schema.Provider, error) { return provider, nil },
53+
}
54+
}

0 commit comments

Comments
 (0)