-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdata_source_namespace_test.go
118 lines (94 loc) · 3.23 KB
/
data_source_namespace_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//go:build namespace
// +build namespace
/*
Copyright © 2021 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0
*/
package namespace
import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
testhelper "github.com/vmware/terraform-provider-tanzu-mission-control/internal/resources/testing"
)
const (
namespaceResource = "tanzu-mission-control_namespace"
namespaceResourceVar = "test_namespace"
namespaceDataSourceVar = "test_data_namespace"
clusterResource = "tanzu-mission-control_cluster"
clusterResourceVar = "tmc_cluster_test"
)
func TestAcceptanceForNamespaceDataSource(t *testing.T) {
var provider = initTestProvider(t)
namespaceName := acctest.RandomWithPrefix("tf-ns-test")
clusterName := acctest.RandomWithPrefix("tf-cluster")
dataSourceName := fmt.Sprintf("data.%s.%s", namespaceResource, namespaceDataSourceVar)
resourceName := fmt.Sprintf("%s.%s", namespaceResource, namespaceResourceVar)
resource.Test(t, resource.TestCase{
PreCheck: testhelper.TestPreCheck(t),
ProviderFactories: testhelper.GetTestProviderFactories(provider),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: getTestNamespaceDataSourceConfigValue(t, clusterName, namespaceName, testhelper.MetaTemplate),
Check: resource.ComposeTestCheckFunc(
checkDataSourceAttributes(dataSourceName, resourceName),
),
},
},
})
t.Log("namespace data source acceptance test complete!")
}
func getTestNamespaceDataSourceConfigValue(t *testing.T, clusterName, namespaceName, meta string) string {
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
t.Skipf("KUBECONFIG env var is not set: %s", kubeconfigPath)
}
return fmt.Sprintf(`
resource "%s" "%s" {
name = "%s"
attach_k8s_cluster {
kubeconfig_file = "%s"
}
%s
spec {
cluster_group = "default"
}
ready_wait_timeout = "3m"
}
resource "%s" "%s" {
name = "%s"
cluster_name = tanzu-mission-control_cluster.tmc_cluster_test.name
spec {
workspace_name = "default"
attach = false
}
}
data "%s" "%s" {
name = tanzu-mission-control_namespace.test_namespace.name
cluster_name = tanzu-mission-control_namespace.test_namespace.cluster_name
}
`, clusterResource, clusterResourceVar, clusterName, kubeconfigPath, meta, namespaceResource,
namespaceResourceVar, namespaceName, namespaceResource, namespaceDataSourceVar)
}
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
var check = []resource.TestCheckFunc{
verifyNamespaceDataSource(dataSourceName),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
}
check = append(check, testhelper.MetaDataSourceAttributeCheck(dataSourceName, resourceName)...)
return resource.ComposeTestCheckFunc(check...)
}
func verifyNamespaceDataSource(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("root module does not have namespace resource %s", name)
}
return nil
}
}