-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_helper.go
57 lines (48 loc) · 1.69 KB
/
test_helper.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
package taikun
import (
"errors"
"fmt"
//"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func checkDataSourceStateMatchesResourceState(dataSourceName, resourceName string) func(*terraform.State) error {
return checkDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourceName, map[string]struct{}{})
}
func checkDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourceName string, ignoreFields map[string]struct{}) func(*terraform.State) error {
return func(s *terraform.State) error {
ds, ok := s.RootModule().Resources[dataSourceName]
if !ok {
return fmt.Errorf("can't find %s in state", dataSourceName)
}
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("can't find %s in state", resourceName)
}
dsAttr := ds.Primary.Attributes
rsAttr := rs.Primary.Attributes
errMsg := ""
// Data sources are often derived from resources, so iterate over the resource fields to
// make sure all fields are accounted for in the data source.
// If a field exists in the data source but not in the resource, its expected value should
// be checked separately.
for k := range rsAttr {
if _, ok := ignoreFields[k]; ok {
continue
}
if k == "%" {
continue
}
if dsAttr[k] != rsAttr[k] {
// ignore data sources where an empty list is being compared against a null list.
if k[len(k)-1:] == "#" && (dsAttr[k] == "" || dsAttr[k] == "0") && (rsAttr[k] == "" || rsAttr[k] == "0") {
continue
}
errMsg += fmt.Sprintf("%s is %s; want %s\n", k, dsAttr[k], rsAttr[k])
}
}
if errMsg != "" {
return errors.New(errMsg)
}
return nil
}
}