-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathcomputed_test.go
More file actions
168 lines (146 loc) · 3.8 KB
/
computed_test.go
File metadata and controls
168 lines (146 loc) · 3.8 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright IBM Corp. 2019, 2026
// SPDX-License-Identifier: MPL-2.0
package customdiff
import (
"context"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func TestComputedIf(t *testing.T) {
t.Parallel()
t.Run("true", func(t *testing.T) {
var condCalls int
var gotOld, gotNew string
provider := testProvider(
map[string]*schema.Schema{
"foo": {
Type: schema.TypeString,
Optional: true,
},
"comp": {
Type: schema.TypeString,
Computed: true,
},
},
ComputedIf("comp", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
// When we set "ForceNew", our CustomizeDiff function is actually
// called a second time to construct the "create" portion of
// the replace diff. On the second call, the old value is masked
// as "" to suggest that the object is being created rather than
// updated.
condCalls++
oldValue, newValue := d.GetChange("foo")
gotOld = oldValue.(string)
gotNew = newValue.(string)
return true
}),
)
diff, err := testDiff(
provider,
map[string]string{
"foo": "bar",
"comp": "old",
},
map[string]string{
"foo": "baz",
},
)
if err != nil {
t.Fatalf("Diff failed with error: %s", err)
}
if condCalls != 1 {
t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 1)
} else {
if got, want := gotOld, "bar"; got != want {
t.Errorf("wrong old value %q on first call; want %q", got, want)
}
if got, want := gotNew, "baz"; got != want {
t.Errorf("wrong new value %q on first call; want %q", got, want)
}
}
if !diff.Attributes["comp"].NewComputed {
t.Error("Attribute 'comp' is not marked as NewComputed")
}
})
t.Run("true-non-existent-attribute", func(t *testing.T) {
provider := testProvider(
map[string]*schema.Schema{},
ComputedIf("non-existent", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
return true
}),
)
_, err := testDiff(
provider,
map[string]string{},
map[string]string{},
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
})
t.Run("false", func(t *testing.T) {
var condCalls int
var gotOld, gotNew string
provider := testProvider(
map[string]*schema.Schema{
"foo": {
Type: schema.TypeString,
Optional: true,
},
"comp": {
Type: schema.TypeString,
Computed: true,
},
},
ComputedIf("comp", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
condCalls++
oldValue, newValue := d.GetChange("foo")
gotOld = oldValue.(string)
gotNew = newValue.(string)
return false
}),
)
diff, err := testDiff(
provider,
map[string]string{
"foo": "bar",
"comp": "old",
},
map[string]string{
"foo": "baz",
},
)
if err != nil {
t.Fatalf("Diff failed with error: %s", err)
}
if condCalls != 1 {
t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 1)
} else {
if got, want := gotOld, "bar"; got != want {
t.Errorf("wrong old value %q on first call; want %q", got, want)
}
if got, want := gotNew, "baz"; got != want {
t.Errorf("wrong new value %q on first call; want %q", got, want)
}
}
if diff.Attributes["comp"] != nil && diff.Attributes["comp"].NewComputed {
t.Error("Attribute 'foo' is marked as NewComputed, but should not be")
}
})
t.Run("false-non-existent-attribute", func(t *testing.T) {
provider := testProvider(
map[string]*schema.Schema{},
ComputedIf("non-existent", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
return false
}),
)
_, err := testDiff(
provider,
map[string]string{},
map[string]string{},
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
})
}