-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathprovisioner_data_source_test.go
95 lines (78 loc) · 3.12 KB
/
provisioner_data_source_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
//go:build provisioner
// +build provisioner
/*
Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0
*/
package provisioner
import (
"fmt"
"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"
)
func TestAcceptanceForProvisionerDataSource(t *testing.T) {
var provider = initTestProvider(t)
provisionerResourceName := fmt.Sprintf("%s.%s", ResourceName, resourceVar)
provisionerName := acctest.RandomWithPrefix("tf-prv-test")
provisionerDataSource := fmt.Sprintf("data.%s.%s", ResourceName, dataSourceVar)
resource.Test(t, resource.TestCase{
PreCheck: testhelper.TestPreCheck(t),
ProviderFactories: testhelper.GetTestProviderFactories(provider),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: getTestProvisionerWithDataSourceConfigValue(provisionerName),
Check: resource.ComposeTestCheckFunc(
checkDataSourceAttributes(provisionerDataSource, provisionerResourceName),
),
},
},
})
t.Log("provisioner datasource acceptance test complete!")
}
func getTestProvisionerWithDataSourceConfigValue(prvName string) string {
return fmt.Sprintf(`
resource "%s" "%s" {
name = "%s"
management_cluster = "%s"
%s
}
data "%s" "%s" {
provisioners {
name = tanzu-mission-control_provisioner.provisioner_resource.name
management_cluster = tanzu-mission-control_provisioner.provisioner_resource.management_cluster
}
}
`, ResourceName, resourceVar, prvName, eksManagementCluster, testhelper.MetaTemplate, ResourceName, dataSourceVar)
}
func checkDataSourceAttributes(dataSourceName, resourceName string) resource.TestCheckFunc {
var check = []resource.TestCheckFunc{
verifyProvisionerDataSource(dataSourceName),
resource.TestCheckResourceAttrPair(dataSourceName, "provisioners.0.name", resourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
}
// TODO: Add the meta check after TMC-54016 fix.
// check = append(check, metaDataSourceAttributeCheck(dataSourceName, resourceName)...)
return resource.ComposeTestCheckFunc(check...)
}
func verifyProvisionerDataSource(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 provisioner resource %s", name)
}
return nil
}
}
// TODO: Add the meta check after TMC-54016 fix.
// func metaDataSourceAttributeCheck(dataSourceName, resourceName string) []resource.TestCheckFunc {
// return []resource.TestCheckFunc{
// resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.description", resourceName, "meta.0.description"),
// resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.labels.key1", resourceName, "meta.0.labels.key1"),
// resource.TestCheckResourceAttrPair(dataSourceName, "meta.0.labels.key2", resourceName, "meta.0.labels.key2"),
// resource.TestCheckResourceAttrSet(dataSourceName, "meta.0.uid"),
// }
//}