Skip to content

Commit abb046d

Browse files
committed
Add acceptance tests for kubernetes_manifest identity fields
1 parent 9e12d9b commit abb046d

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

manifest/test/acceptance/configmap_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ func TestKubernetesManifest_ConfigMap(t *testing.T) {
8787
tfstate.AssertAttributeNotEmpty(t, "kubernetes_manifest.test.object.metadata.labels.test")
8888

8989
tfstate.AssertAttributeDoesNotExist(t, "kubernetes_manifest.test.spec")
90+
91+
tfstate.AssertIdentityValueEqual(t, "kubernetes_manifest.test", "api_version", "v1")
92+
tfstate.AssertIdentityValueEqual(t, "kubernetes_manifest.test", "kind", "ConfigMap")
93+
tfstate.AssertIdentityValueEqual(t, "kubernetes_manifest.test", "name", name)
94+
tfstate.AssertIdentityValueEqual(t, "kubernetes_manifest.test", "namespace", namespace)
9095
}

manifest/test/helper/state/state_helper.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Helper struct {
2626
func NewHelper(tfstate *tfjson.State) *Helper {
2727
return &Helper{tfstate}
2828
}
29+
2930
func (s *Helper) ResourceExists(t *testing.T, resourceAddress string) bool {
3031
t.Helper()
3132
_, err := getAttributesValuesFromResource(s, resourceAddress)
@@ -42,6 +43,16 @@ func getAttributesValuesFromResource(state *Helper, address string) (interface{}
4243
return nil, fmt.Errorf("Could not find resource %q in state", address)
4344
}
4445

46+
// getIdentityValues pulls out the getIdentityValues field from the resource at the given address
47+
func getIdentityValuesFromResource(state *Helper, address string) (map[string]any, error) {
48+
for _, r := range state.Values.RootModule.Resources {
49+
if r.Address == address {
50+
return r.IdentityValues, nil
51+
}
52+
}
53+
return nil, fmt.Errorf("Could not find resource %q in state", address)
54+
}
55+
4556
// getOutputValues gets the given output name value from the state
4657
func getOutputValues(state *Helper, name string) (interface{}, error) {
4758
for n, v := range state.Values.Outputs {
@@ -95,10 +106,10 @@ func parseStateAddress(address string) (string, string) {
95106
switch parts[0] {
96107
case "data":
97108
resourceAddress = strings.Join(parts[0:3], ".")
98-
attributeAddress = strings.Join(parts[3:len(parts)], ".")
109+
attributeAddress = strings.Join(parts[3:], ".")
99110
default:
100111
resourceAddress = strings.Join(parts[0:2], ".")
101-
attributeAddress = strings.Join(parts[2:len(parts)], ".")
112+
attributeAddress = strings.Join(parts[2:], ".")
102113
}
103114

104115
return resourceAddress, attributeAddress
@@ -123,6 +134,22 @@ func (s *Helper) GetAttributeValue(t *testing.T, address string) interface{} {
123134
return value
124135
}
125136

137+
// GetIdentityValue will get the identity value at the given resource address from the state
138+
func (s *Helper) GetIdentityValue(t *testing.T, address string, identitykey string) interface{} {
139+
t.Helper()
140+
141+
identityvals, err := getIdentityValuesFromResource(s, address)
142+
if err != nil {
143+
t.Fatal(err)
144+
}
145+
146+
val, ok := identityvals[identitykey]
147+
if !ok {
148+
t.Fatalf("resource identity value %q does note exist for %q", identitykey, address)
149+
}
150+
return val
151+
}
152+
126153
// GetOutputValue gets the given output name value from the state
127154
func (s *Helper) GetOutputValue(t *testing.T, name string) interface{} {
128155
t.Helper()
@@ -157,6 +184,14 @@ func (s *Helper) AssertAttributeEqual(t *testing.T, address string, expectedValu
157184
fmt.Sprintf("Address: %q", address))
158185
}
159186

187+
// AssertIdentityValueEqual will fail the test if the identity value does not equal expectedValue
188+
func (s *Helper) AssertIdentityValueEqual(t *testing.T, address string, identitykey string, expectedValue interface{}) {
189+
t.Helper()
190+
191+
assert.EqualValues(t, expectedValue, s.GetIdentityValue(t, address, identitykey),
192+
fmt.Sprintf("Resource: %q, Identity key: %q", address, identitykey))
193+
}
194+
160195
// AssertAttributeNotEqual will fail the test if the attribute is equal to expectedValue
161196
func (s *Helper) AssertAttributeNotEqual(t *testing.T, address string, expectedValue interface{}) {
162197
t.Helper()

0 commit comments

Comments
 (0)