Skip to content

Commit 43be239

Browse files
authored
feat(builder): shared tests (#7)
we have similar setup for 3 providers, might make sense to have common tests for them --------- Signed-off-by: slonka <slonka@users.noreply.github.com>
1 parent 0876b14 commit 43be239

File tree

4 files changed

+147
-4
lines changed

4 files changed

+147
-4
lines changed

tfbuilder/assertions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/hashicorp/terraform-plugin-testing/plancheck"
66
)
77

8-
func CheckReapplyPlanEmpty(builder *Builder) resource.TestStep {
8+
func CheckReapplyPlanEmpty(builder OnlyBuild) resource.TestStep {
99
return resource.TestStep{
1010
// Re-apply the same config and ensure no changes occur
1111
Config: builder.Build(),

tfbuilder/builder.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ type Builder struct {
2626
policies map[string]*PolicyBuilder
2727
}
2828

29+
type ModifyMeshBuilder interface {
30+
OnlyBuild
31+
AddressableBuilder
32+
AddMesh(mesh *MeshBuilder) *Builder
33+
RemoveMesh(name string) *Builder
34+
}
35+
36+
type ModifyPolicyBuilder interface {
37+
OnlyBuild
38+
AddressableBuilder
39+
AddPolicy(builder *PolicyBuilder) *Builder
40+
RemovePolicy(name string) *Builder
41+
}
42+
43+
type AddressableBuilder interface {
44+
ResourceAddress(s string, resource string) string
45+
}
46+
47+
type OnlyBuild interface {
48+
Build() string
49+
}
50+
2951
func NewBuilder(provider ProviderType, scheme, host string, port int) *Builder {
3052
return &Builder{
3153
provider: provider,

tfbuilder/cases.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package tfbuilder
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
5+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
6+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
7+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
8+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
9+
)
10+
11+
func CreateMeshAndModifyFieldsOnIt(
12+
providerFactory map[string]func() (tfprotov6.ProviderServer, error),
13+
builder ModifyMeshBuilder,
14+
mesh *MeshBuilder,
15+
) resource.TestCase {
16+
return resource.TestCase{
17+
ProtoV6ProviderFactories: providerFactory,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: builder.AddMesh(mesh).Build(),
21+
ConfigPlanChecks: resource.ConfigPlanChecks{
22+
PreApply: []plancheck.PlanCheck{
23+
plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionCreate),
24+
},
25+
},
26+
},
27+
CheckReapplyPlanEmpty(builder),
28+
{
29+
Config: builder.AddMesh(mesh.AddToSpec("at the end", `
30+
constraints = {
31+
dataplane_proxy = {
32+
requirements = [ { tags = { key = "a" } } ]
33+
restrictions = []
34+
}
35+
}
36+
routing = {
37+
default_forbid_mesh_external_service_access = true
38+
}
39+
`)).Build(),
40+
ConfigPlanChecks: resource.ConfigPlanChecks{
41+
PreApply: []plancheck.PlanCheck{
42+
plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionUpdate),
43+
plancheck.ExpectKnownValue(builder.ResourceAddress("mesh", mesh.ResourceName), tfjsonpath.New("routing").AtMapKey("default_forbid_mesh_external_service_access"), knownvalue.Bool(true)),
44+
},
45+
},
46+
},
47+
{
48+
Config: builder.AddMesh(mesh.RemoveFromSpec(`default_forbid_mesh_external_service_access = true`)).Build(),
49+
ConfigPlanChecks: resource.ConfigPlanChecks{
50+
PreApply: []plancheck.PlanCheck{
51+
plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionUpdate),
52+
plancheck.ExpectKnownValue(builder.ResourceAddress("mesh", mesh.ResourceName), tfjsonpath.New("routing").AtMapKey("default_forbid_mesh_external_service_access"), knownvalue.Null()),
53+
},
54+
},
55+
},
56+
CheckReapplyPlanEmpty(builder),
57+
{
58+
Config: builder.AddMesh(mesh.UpdateSpec(`requirements = [ { tags = { key = "a" } } ]`, `requirements = []`)).Build(),
59+
ConfigPlanChecks: resource.ConfigPlanChecks{
60+
PreApply: []plancheck.PlanCheck{
61+
plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionUpdate),
62+
plancheck.ExpectKnownValue(builder.ResourceAddress("mesh", mesh.ResourceName), tfjsonpath.New("constraints").AtMapKey("dataplane_proxy").AtMapKey("requirements"), knownvalue.ListExact([]knownvalue.Check{})),
63+
},
64+
},
65+
},
66+
CheckReapplyPlanEmpty(builder),
67+
{
68+
Config: builder.RemoveMesh(mesh.MeshName).Build(),
69+
ConfigPlanChecks: resource.ConfigPlanChecks{
70+
PreApply: []plancheck.PlanCheck{
71+
plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionDestroy),
72+
},
73+
},
74+
},
75+
CheckReapplyPlanEmpty(builder),
76+
},
77+
}
78+
}
79+
80+
func CreatePolicyAndModifyFieldsOnIt(
81+
providerFactory map[string]func() (tfprotov6.ProviderServer, error),
82+
builder ModifyPolicyBuilder,
83+
mtp *PolicyBuilder,
84+
) resource.TestCase {
85+
mtp.WithSpec(AllowAllTrafficPermissionSpec)
86+
87+
return resource.TestCase{
88+
ProtoV6ProviderFactories: providerFactory,
89+
Steps: []resource.TestStep{
90+
{
91+
Config: builder.AddPolicy(mtp).Build(),
92+
ConfigPlanChecks: resource.ConfigPlanChecks{
93+
PreApply: []plancheck.PlanCheck{
94+
plancheck.ExpectResourceAction(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), plancheck.ResourceActionCreate),
95+
},
96+
},
97+
},
98+
CheckReapplyPlanEmpty(builder),
99+
{
100+
Config: builder.AddPolicy(mtp.AddToSpec(`kind = "Mesh"`, `proxy_types = ["Sidecar"]`)).Build(),
101+
ConfigPlanChecks: resource.ConfigPlanChecks{
102+
PreApply: []plancheck.PlanCheck{
103+
plancheck.ExpectResourceAction(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), plancheck.ResourceActionUpdate),
104+
plancheck.ExpectKnownValue(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), tfjsonpath.New("spec").AtMapKey("from").AtSliceIndex(0).AtMapKey("target_ref").AtMapKey("proxy_types"), knownvalue.ListExact([]knownvalue.Check{knownvalue.StringExact("Sidecar")})),
105+
},
106+
},
107+
},
108+
CheckReapplyPlanEmpty(builder),
109+
{
110+
Config: builder.AddPolicy(mtp.UpdateSpec(`proxy_types = ["Sidecar"]`, `proxy_types = []`)).Build(),
111+
ConfigPlanChecks: resource.ConfigPlanChecks{
112+
PreApply: []plancheck.PlanCheck{
113+
plancheck.ExpectResourceAction(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), plancheck.ResourceActionUpdate),
114+
plancheck.ExpectKnownValue(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), tfjsonpath.New("spec").AtMapKey("from").AtSliceIndex(0).AtMapKey("target_ref").AtMapKey("proxy_types"), knownvalue.ListExact([]knownvalue.Check{})),
115+
},
116+
},
117+
},
118+
CheckReapplyPlanEmpty(builder),
119+
},
120+
}
121+
}

tfbuilder/spec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package tfbuilder
22

33
import (
4-
"log"
54
"strings"
65
)
76

8-
func addToSpec(spec string, after, newLine string) string {
7+
func addToSpec(spec, after, newLine string) string {
98
lines := strings.Split(spec, "\n")
109
var result []string
1110
inserted := false
@@ -21,8 +20,9 @@ func addToSpec(spec string, after, newLine string) string {
2120
}
2221
}
2322

23+
// If "after" not found, just append at end
2424
if !inserted {
25-
log.Fatalf("AddToSpec failed: could not find line containing: %q", after)
25+
result = append(result, newLine)
2626
}
2727

2828
return strings.Join(result, "\n")

0 commit comments

Comments
 (0)