Skip to content

Commit f934115

Browse files
shobha2626Shreyas Sreenivas
authored and
Shreyas Sreenivas
committed
Acceptance test for workspace resource
1 parent 5fbe66f commit f934115

12 files changed

+373
-61
lines changed

internal/provider/provider.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func Provider() *schema.Provider {
2727
},
2828
DataSourcesMap: map[string]*schema.Resource{
2929
cluster.ResourceName: cluster.DataSourceTMCCluster(),
30-
workspace.ResourceName: workspace.DataSourceTMCWorkspace(),
30+
workspace.ResourceName: workspace.DataSourceWorkspace(),
3131
namespace.ResourceName: namespace.DataSourceTMCNamespace(),
32-
clustergroup.ResourceName: clustergroup.DataSourceTMCClusterGroup(),
32+
clustergroup.ResourceName: clustergroup.DataSourceClusterGroup(),
3333
},
3434
ConfigureContextFunc: authctx.ProviderConfigureContext,
3535
}

internal/resources/clustergroup/clustergroup_provider_test.go

+1-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ SPDX-License-Identifier: MPL-2.0
66
package clustergroup
77

88
import (
9-
"os"
109
"testing"
1110

1211
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -15,16 +14,14 @@ import (
1514
"gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/authctx"
1615
)
1716

18-
const providerName = "tmc"
19-
2017
func initTestProvider(t *testing.T) *schema.Provider {
2118
testAccProvider := &schema.Provider{
2219
Schema: authctx.ProviderAuthSchema(),
2320
ResourcesMap: map[string]*schema.Resource{
2421
ResourceName: ResourceClusterGroup(),
2522
},
2623
DataSourcesMap: map[string]*schema.Resource{
27-
ResourceName: DataSourceTMCClusterGroup(),
24+
ResourceName: DataSourceClusterGroup(),
2825
},
2926
ConfigureContextFunc: authctx.ProviderConfigureContext,
3027
}
@@ -34,18 +31,3 @@ func initTestProvider(t *testing.T) *schema.Provider {
3431

3532
return testAccProvider
3633
}
37-
38-
func testPreCheck(t *testing.T) func() {
39-
return func() {
40-
for _, env := range []string{authctx.ServerEndpointEnvVar, authctx.CSPTokenEnvVar, authctx.CSPEndpointEnvVar} {
41-
require.NotEmpty(t, os.Getenv(env))
42-
}
43-
}
44-
}
45-
46-
func getTestProviderFactories(provider *schema.Provider) map[string]func() (*schema.Provider, error) {
47-
//nolint:unparam
48-
return map[string]func() (*schema.Provider, error){
49-
providerName: func() (*schema.Provider, error) { return provider, nil },
50-
}
51-
}

internal/resources/clustergroup/data_source_cluster_group.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 DataSourceTMCClusterGroup() *schema.Resource {
20+
func DataSourceClusterGroup() *schema.Resource {
2121
return &schema.Resource{
2222
ReadContext: dataSourceClusterGroupRead,
2323
Schema: clusterGroupSchema,

internal/resources/clustergroup/data_source_cluster_group_test.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
15+
16+
testhelper "gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/testing"
1517
)
1618

1719
func TestAcceptanceForClusterGroupDataSource(t *testing.T) {
@@ -22,21 +24,14 @@ func TestAcceptanceForClusterGroupDataSource(t *testing.T) {
2224
resourceName := "tmc_cluster_group.test_cluster_group"
2325

2426
resource.Test(t, resource.TestCase{
25-
PreCheck: testPreCheck(t),
26-
ProviderFactories: getTestProviderFactories(provider),
27+
PreCheck: testhelper.TestPreCheck(t),
28+
ProviderFactories: testhelper.GetTestProviderFactories(provider),
2729
CheckDestroy: nil,
2830
Steps: []resource.TestStep{
2931
{
30-
Config: getTestDataSourceClusterGroupConfigValue(clusterGroup),
32+
Config: getTestDataSourceClusterGroupConfigValue(clusterGroup, testhelper.MetaTemplate),
3133
Check: resource.ComposeTestCheckFunc(
32-
verifyClusterGroupDataSource(dataSourceName),
33-
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
34-
resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.description", resourceName, "meta.0.description"),
35-
resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.labels.key1", resourceName, "meta.0.labels.key1"),
36-
resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.labels.key2", resourceName, "meta.0.labels.key2"),
37-
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
38-
resource.TestCheckResourceAttrSet(dataSourceName, "meta.0.annotations.authoritativeRID"),
39-
resource.TestCheckResourceAttrSet(dataSourceName, "meta.0.uid"),
34+
checkDataSourceAttributes(dataSourceName, resourceName),
4035
),
4136
},
4237
},
@@ -45,30 +40,36 @@ func TestAcceptanceForClusterGroupDataSource(t *testing.T) {
4540
t.Log("cluster group data source acceptance test complete!")
4641
}
4742

48-
func getTestDataSourceClusterGroupConfigValue(clusterGroupName string) string {
43+
func getTestDataSourceClusterGroupConfigValue(clusterGroupName, meta string) string {
4944
return fmt.Sprintf(`
5045
resource "tmc_cluster_group" "test_cluster_group" {
5146
name = "%s"
52-
meta {
53-
description = "cluster group with description"
54-
labels = {
55-
"key1" : "value1"
56-
"key2" : "value2"
57-
}
58-
}
47+
%s
5948
}
6049
6150
data "tmc_cluster_group" "test_data_cluster_group" {
6251
name = tmc_cluster_group.test_cluster_group.name
6352
}
64-
`, clusterGroupName)
53+
`, clusterGroupName, meta)
54+
}
55+
56+
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
57+
var check = []resource.TestCheckFunc{
58+
verifyClusterGroupDataSource(dataSourceName),
59+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
60+
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
61+
}
62+
63+
check = append(check, testhelper.MetaDataSourceAttributeCheck(dataSourceName, resourceName)...)
64+
65+
return resource.ComposeTestCheckFunc(check...)
6566
}
6667

6768
func verifyClusterGroupDataSource(name string) resource.TestCheckFunc {
6869
return func(s *terraform.State) error {
6970
_, ok := s.RootModule().Resources[name]
7071
if !ok {
71-
return fmt.Errorf("root module has cluster group resource %s", name)
72+
return fmt.Errorf("root module does not have cluster group resource %s", name)
7273
}
7374

7475
return nil

internal/resources/clustergroup/resource_cluster_group_test.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/authctx"
2020
clustergroupmodel "gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/models/clustergroup"
21+
testhelper "gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/testing"
2122
)
2223

2324
func TestAcceptanceForClusterGroupResource(t *testing.T) {
@@ -27,8 +28,8 @@ func TestAcceptanceForClusterGroupResource(t *testing.T) {
2728
clusterGroupName := acctest.RandomWithPrefix("tf-cg-test")
2829

2930
resource.Test(t, resource.TestCase{
30-
PreCheck: testPreCheck(t),
31-
ProviderFactories: getTestProviderFactories(provider),
31+
PreCheck: testhelper.TestPreCheck(t),
32+
ProviderFactories: testhelper.GetTestProviderFactories(provider),
3233
CheckDestroy: nil,
3334
Steps: []resource.TestStep{
3435
{
@@ -41,13 +42,7 @@ func TestAcceptanceForClusterGroupResource(t *testing.T) {
4142
{
4243
Config: getTestResourceClusterGroupConfigValue(clusterGroupName),
4344
Check: resource.ComposeTestCheckFunc(
44-
verifyClusterGroupResourceCreation(provider, resourceName, clusterGroupName),
45-
resource.TestCheckResourceAttr(resourceName, "name", clusterGroupName),
46-
resource.TestCheckResourceAttr(resourceName, "meta.#", "1"),
47-
resource.TestCheckResourceAttr(resourceName, "meta.0.labels.%", "2"),
48-
resource.TestCheckResourceAttr(resourceName, "meta.0.description", "cluster group with description"),
49-
resource.TestCheckResourceAttr(resourceName, "meta.0.labels.key1", "value1"),
50-
resource.TestCheckResourceAttr(resourceName, "meta.0.labels.key2", "value2"),
45+
checkResourceAttributes(provider, resourceName, clusterGroupName),
5146
),
5247
},
5348
},
@@ -68,15 +63,20 @@ func getTestResourceClusterGroupConfigValue(clusterGroupName string) string {
6863
return fmt.Sprintf(`
6964
resource "tmc_cluster_group" "test_cluster_group" {
7065
name = "%s"
71-
meta {
72-
description = "cluster group with description"
73-
labels = {
74-
"key1" : "value1"
75-
"key2" : "value2"
76-
}
77-
}
66+
%s
7867
}
79-
`, clusterGroupName)
68+
`, clusterGroupName, testhelper.MetaTemplate)
69+
}
70+
71+
func checkResourceAttributes(provider *schema.Provider, resourceName, clusterGroupName string) resource.TestCheckFunc {
72+
var check = []resource.TestCheckFunc{
73+
verifyClusterGroupResourceCreation(provider, resourceName, clusterGroupName),
74+
resource.TestCheckResourceAttr(resourceName, "name", clusterGroupName),
75+
}
76+
77+
check = append(check, testhelper.MetaResourceAttributeCheck(resourceName)...)
78+
79+
return resource.ComposeTestCheckFunc(check...)
8080
}
8181

8282
func verifyClusterGroupResourceCreation(
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Copyright © 2021 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
// testing is a helper package created for testing purpose.
7+
// go linker would not include this package in the binary, as it is not imported anywhere else other than for testing
8+
9+
package testing
10+
11+
const (
12+
providerName = "tmc"
13+
value1 = "value1"
14+
value2 = "value2"
15+
description = "resource with description"
16+
)

internal/resources/testing/meta.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © 2021 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package testing
7+
8+
import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
10+
const (
11+
MetaTemplate = `meta {
12+
description = "resource with description"
13+
labels = {
14+
"key1" : "value1"
15+
"key2" : "value2"
16+
}
17+
}`
18+
)
19+
20+
func MetaDataSourceAttributeCheck(dataSourceName, resourceName string) []resource.TestCheckFunc {
21+
return []resource.TestCheckFunc{
22+
resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.description", resourceName, "meta.0.description"),
23+
resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.labels.key1", resourceName, "meta.0.labels.key1"),
24+
resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.labels.key2", resourceName, "meta.0.labels.key2"),
25+
resource.TestCheckResourceAttrSet(dataSourceName, "meta.0.annotations.authoritativeRID"),
26+
resource.TestCheckResourceAttrSet(dataSourceName, "meta.0.uid"),
27+
}
28+
}
29+
30+
func MetaResourceAttributeCheck(resourceName string) []resource.TestCheckFunc {
31+
return []resource.TestCheckFunc{
32+
resource.TestCheckResourceAttr(resourceName, "meta.#", "1"),
33+
resource.TestCheckResourceAttr(resourceName, "meta.0.labels.%", "2"),
34+
resource.TestCheckResourceAttr(resourceName, "meta.0.description", description),
35+
resource.TestCheckResourceAttr(resourceName, "meta.0.labels.key1", value1),
36+
resource.TestCheckResourceAttr(resourceName, "meta.0.labels.key2", value2),
37+
resource.TestCheckResourceAttrSet(resourceName, "meta.0.uid"),
38+
resource.TestCheckResourceAttrSet(resourceName, "meta.0.annotations.authoritativeRID"),
39+
}
40+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright © 2021 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package testing
7+
8+
import (
9+
"os"
10+
"testing"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/pkg/errors"
14+
"github.com/stretchr/testify/require"
15+
16+
"gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/authctx"
17+
)
18+
19+
func TestPreCheck(t *testing.T) func() {
20+
return func() {
21+
for _, env := range []string{authctx.ServerEndpointEnvVar, authctx.CSPTokenEnvVar, authctx.CSPEndpointEnvVar} {
22+
require.NotEmpty(t, os.Getenv(env))
23+
}
24+
}
25+
}
26+
27+
func GetTestProviderFactories(provider *schema.Provider) map[string]func() (*schema.Provider, error) {
28+
return map[string]func() (*schema.Provider, error){
29+
providerName: func() (*schema.Provider, error) {
30+
if provider == nil {
31+
return provider, errors.New("provider cannot be nil")
32+
}
33+
34+
return provider, nil
35+
},
36+
}
37+
}

internal/resources/workspace/data_source_workspace.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 DataSourceTMCWorkspace() *schema.Resource {
20+
func DataSourceWorkspace() *schema.Resource {
2121
return &schema.Resource{
2222
ReadContext: dataSourceWorkspaceRead,
2323
Schema: workspaceSchema,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright © 2021 VMware, Inc. All Rights Reserved.
3+
SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
package workspace
7+
8+
import (
9+
"fmt"
10+
"testing"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
15+
16+
testhelper "gitlab.eng.vmware.com/olympus/terraform-provider-tanzu/internal/resources/testing"
17+
)
18+
19+
func TestAcceptanceForWorkspaceDataSource(t *testing.T) {
20+
var provider = initTestProvider(t)
21+
22+
workspace := acctest.RandomWithPrefix("tf-ws-test")
23+
dataSourceName := "data.tmc_workspace.test_data_workspace"
24+
resourceName := "tmc_workspace.test_workspace"
25+
26+
resource.Test(t, resource.TestCase{
27+
PreCheck: testhelper.TestPreCheck(t),
28+
ProviderFactories: testhelper.GetTestProviderFactories(provider),
29+
CheckDestroy: nil,
30+
Steps: []resource.TestStep{
31+
{
32+
Config: getTestWorkspaceDataSourceConfigValue(workspace),
33+
Check: resource.ComposeTestCheckFunc(
34+
checkDataSourceAttributes(dataSourceName, resourceName),
35+
),
36+
},
37+
},
38+
})
39+
t.Log("workspace data source acceptance test complete!")
40+
}
41+
42+
func getTestWorkspaceDataSourceConfigValue(workspaceName string) string {
43+
return fmt.Sprintf(`
44+
resource "tmc_workspace" "test_workspace" {
45+
name = "%s"
46+
%s
47+
}
48+
49+
data "tmc_workspace" "test_data_workspace" {
50+
name = tmc_workspace.test_workspace.name
51+
}
52+
`, workspaceName, testhelper.MetaTemplate)
53+
}
54+
55+
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
56+
var check = []resource.TestCheckFunc{
57+
verifyWorkspaceDataSource(dataSourceName),
58+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
59+
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
60+
}
61+
62+
check = append(check, testhelper.MetaDataSourceAttributeCheck(dataSourceName, resourceName)...)
63+
64+
return resource.ComposeTestCheckFunc(check...)
65+
}
66+
67+
func verifyWorkspaceDataSource(name string) resource.TestCheckFunc {
68+
return func(s *terraform.State) error {
69+
_, ok := s.RootModule().Resources[name]
70+
if !ok {
71+
return fmt.Errorf("root module does not have workspace resource %s", name)
72+
}
73+
74+
return nil
75+
}
76+
}

0 commit comments

Comments
 (0)