Skip to content

Commit a938e8d

Browse files
committed
Add test for API version upgrades
1 parent bd399e5 commit a938e8d

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

kustomize/resource_kustomization_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"regexp"
7+
"strings"
78
"testing"
89

910
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -373,6 +374,52 @@ resource "kustomization_resource" "ss" {
373374
`
374375
}
375376

377+
//
378+
//
379+
// Upgrade_API_Version Test
380+
func TestAccResourceKustomization_upgradeAPIVersion(t *testing.T) {
381+
382+
resource.Test(t, resource.TestCase{
383+
Providers: testAccProviders,
384+
Steps: []resource.TestStep{
385+
//
386+
//
387+
// Applying initial networking.k8s.io/v1beta1 ingress
388+
{
389+
Config: testAccResourceKustomizationConfig_upgradeAPIVersion("test_kustomizations/upgrade_api_version/initial"),
390+
Check: resource.ComposeAggregateTestCheckFunc(
391+
resource.TestCheckResourceAttrSet("kustomization_resource.ns", "id"),
392+
resource.TestCheckResourceAttrSet("kustomization_resource.ing", "id"),
393+
testAccCheckManifestNestedString("kustomization_resource.ing", "networking.k8s.io/v1beta1", "apiVersion"),
394+
),
395+
},
396+
//
397+
//
398+
// Update ingress to networking.k8s.io/v1
399+
{
400+
Config: testAccResourceKustomizationConfig_upgradeAPIVersion("test_kustomizations/upgrade_api_version/modified"),
401+
Check: resource.ComposeAggregateTestCheckFunc(
402+
resource.TestCheckResourceAttrSet("kustomization_resource.ns", "id"),
403+
resource.TestCheckResourceAttrSet("kustomization_resource.ing", "id"),
404+
testAccCheckManifestNestedString("kustomization_resource.ing", "networking.k8s.io/v1", "apiVersion"),
405+
),
406+
},
407+
},
408+
})
409+
}
410+
411+
func testAccResourceKustomizationConfig_upgradeAPIVersion(path string) string {
412+
return testAccDataSourceKustomizationConfig_basic(path) + `
413+
resource "kustomization_resource" "ns" {
414+
manifest = data.kustomization_build.test.manifests["~G_~V_Namespace|~X|test-upgrade-api-version"]
415+
}
416+
417+
resource "kustomization_resource" "ing" {
418+
manifest = data.kustomization_build.test.manifests["networking.k8s.io_~V_Ingress|test-upgrade-api-version|test-upgrade-api-version"]
419+
}
420+
`
421+
}
422+
376423
//
377424
//
378425
// CRD Test
@@ -798,3 +845,33 @@ func testAccCheckManifestSelectorAbsent(n string, k string) resource.TestCheckFu
798845
return nil
799846
}
800847
}
848+
849+
func testAccCheckManifestNestedString(n string, expected string, k ...string) resource.TestCheckFunc {
850+
return func(s *terraform.State) error {
851+
u, err := getResourceFromTestState(s, n)
852+
if err != nil {
853+
return err
854+
}
855+
856+
resp, err := getResourceFromK8sAPI(u)
857+
if err != nil {
858+
return err
859+
}
860+
861+
k8spath := strings.Join(k, ".")
862+
863+
actual, ok, err := k8sunstructured.NestedString(resp.Object, k...)
864+
if !ok {
865+
return fmt.Errorf("%s missing from resource %s", k8spath, n)
866+
}
867+
if err != nil {
868+
return err
869+
}
870+
871+
if actual != expected {
872+
return fmt.Errorf("value %s of %s does not match expected %s", actual, k8spath, expected)
873+
}
874+
875+
return nil
876+
}
877+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: networking.k8s.io/v1beta1
2+
kind: Ingress
3+
metadata:
4+
labels:
5+
app: test-upgrade-api-version
6+
name: test-upgrade-api-version
7+
namespace: test-upgrade-api-version
8+
spec:
9+
rules:
10+
- host: test-upgrade-api-version.example.com
11+
http:
12+
paths:
13+
- backend:
14+
serviceName: test-upgrade-api-version
15+
servicePort: 80
16+
path: /
17+
tls:
18+
- hosts:
19+
- test-upgrade-api-version.example.com
20+
secretName: test-upgrade-api-version-tls-internal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
namespace: test-upgrade-api-version
5+
6+
resources:
7+
- namespace.yaml
8+
- ingress.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: test-upgrade-api-version
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
labels:
5+
app: test-upgrade-api-version
6+
name: test-upgrade-api-version
7+
namespace: test-upgrade-api-version
8+
spec:
9+
rules:
10+
- host: test-upgrade-api-version.example.com
11+
http:
12+
paths:
13+
- backend:
14+
service:
15+
name: test-upgrade-api-version
16+
port:
17+
number: 80
18+
path: /
19+
pathType: ImplementationSpecific
20+
tls:
21+
- hosts:
22+
- test-upgrade-api-version.example.com
23+
secretName: test-upgrade-api-version-tls-internal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
namespace: test-upgrade-api-version
5+
6+
resources:
7+
- namespace.yaml
8+
- ingress.yaml

0 commit comments

Comments
 (0)