Skip to content

Commit 0dd56ef

Browse files
committed
Add an acceptance test to cover the fix
1 parent 257173d commit 0dd56ef

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
//go:build acceptance
5+
// +build acceptance
6+
7+
package acceptance
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"strings"
13+
"testing"
14+
"time"
15+
16+
"github.com/hashicorp/go-hclog"
17+
"github.com/hashicorp/terraform-provider-kubernetes/manifest/provider"
18+
)
19+
20+
func TestKubernetesManifest_StrategicPatch(t *testing.T) {
21+
ctx := context.Background()
22+
23+
reattachInfo, err := provider.ServeTest(ctx, hclog.Default(), t)
24+
if err != nil {
25+
t.Errorf("Failed to create provider instance: %q", err)
26+
}
27+
28+
kind := "Strategic"
29+
plural := "strategics"
30+
group := "terraform.io"
31+
version := "v1"
32+
groupVersion := group + "/" + version
33+
crd := fmt.Sprintf("%s.%s", plural, group)
34+
35+
name := strings.ToLower(randName())
36+
namespace := "default"
37+
38+
tfvars := TFVARS{
39+
"name": name,
40+
"namespace": namespace,
41+
"kind": kind,
42+
"plural": plural,
43+
"group": group,
44+
"group_version": groupVersion,
45+
"cr_version": version,
46+
}
47+
48+
step1 := tfhelper.RequireNewWorkingDir(ctx, t)
49+
step1.SetReattachInfo(ctx, reattachInfo)
50+
defer func() {
51+
step1.Destroy(ctx)
52+
step1.Close()
53+
k8shelper.AssertResourceDoesNotExist(t, "apiextensions.k8s.io/v1", "customresourcedefinitions", crd)
54+
}()
55+
56+
crdTfConfig := loadTerraformConfig(t, "StrategicPatch/crd.tf", tfvars)
57+
step1.SetConfig(ctx, string(crdTfConfig))
58+
step1.Init(ctx)
59+
step1.Apply(ctx)
60+
k8shelper.AssertResourceExists(t, "apiextensions.k8s.io/v1", "customresourcedefinitions", crd)
61+
62+
// wait for API to finish ingesting the CRD
63+
time.Sleep(5 * time.Second) //lintignore:R018
64+
65+
reattachInfo2, err := provider.ServeTest(ctx, hclog.Default(), t)
66+
if err != nil {
67+
t.Errorf("Failed to create additional provider instance: %q", err)
68+
}
69+
step2 := tfhelper.RequireNewWorkingDir(ctx, t)
70+
step2.SetReattachInfo(ctx, reattachInfo2)
71+
defer func() {
72+
step2.Destroy(ctx)
73+
step2.Close()
74+
k8shelper.AssertResourceDoesNotExist(t, groupVersion, kind, name)
75+
}()
76+
77+
tfconfig := loadTerraformConfig(t, "StrategicPatch/strategic_patch.tf", tfvars)
78+
step2.SetConfig(ctx, string(tfconfig))
79+
step2.Init(ctx)
80+
step2.Apply(ctx)
81+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
resource "kubernetes_manifest" "crd" {
2+
manifest = {
3+
apiVersion = "apiextensions.k8s.io/v1"
4+
kind = "CustomResourceDefinition"
5+
metadata = {
6+
name = "${var.plural}.${var.group}"
7+
}
8+
spec = {
9+
group = "${var.group}"
10+
names = {
11+
kind = "${var.kind}"
12+
plural = "${var.plural}"
13+
}
14+
scope = "Namespaced"
15+
versions = [
16+
{
17+
name = "${var.cr_version}"
18+
served = true
19+
storage = true
20+
schema = {
21+
openAPIV3Schema = {
22+
type = "object"
23+
properties = {
24+
spec = {
25+
type = "object"
26+
properties = {
27+
patchStrategicMerge = {
28+
type = "object"
29+
properties = {
30+
containers = {
31+
type = "array"
32+
"x-kubernetes-list-type" = "map"
33+
"x-kubernetes-list-map-keys" = ["name"]
34+
items = {
35+
type = "object"
36+
required = ["name"]
37+
properties = {
38+
name = {
39+
type = "string"
40+
}
41+
image = {
42+
type = "string"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
},
55+
]
56+
}
57+
}
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "kubernetes_manifest" "test" {
2+
manifest = {
3+
apiVersion = "${var.group_version}"
4+
kind = "${var.kind}"
5+
metadata = {
6+
name = "${var.name}"
7+
namespace = "${var.namespace}"
8+
}
9+
spec = {
10+
patchStrategicMerge = {
11+
containers = [
12+
{
13+
name = "nginx"
14+
image = "nginx:latest"
15+
},
16+
]
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)