|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "regexp"
|
| 7 | + "strings" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
|
@@ -370,6 +371,52 @@ resource "kustomization_resource" "ss" {
|
370 | 371 | `
|
371 | 372 | }
|
372 | 373 |
|
| 374 | +// |
| 375 | +// |
| 376 | +// Upgrade_API_Version Test |
| 377 | +func TestAccResourceKustomization_upgradeAPIVersion(t *testing.T) { |
| 378 | + |
| 379 | + resource.Test(t, resource.TestCase{ |
| 380 | + Providers: testAccProviders, |
| 381 | + Steps: []resource.TestStep{ |
| 382 | + // |
| 383 | + // |
| 384 | + // Applying initial networking.k8s.io/v1beta1 ingress |
| 385 | + { |
| 386 | + Config: testAccResourceKustomizationConfig_upgradeAPIVersion("test_kustomizations/upgrade_api_version/initial"), |
| 387 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 388 | + resource.TestCheckResourceAttrSet("kustomization_resource.ns", "id"), |
| 389 | + resource.TestCheckResourceAttrSet("kustomization_resource.ing", "id"), |
| 390 | + testAccCheckManifestNestedString("kustomization_resource.ing", "networking.k8s.io/v1beta1", "apiVersion"), |
| 391 | + ), |
| 392 | + }, |
| 393 | + // |
| 394 | + // |
| 395 | + // Update ingress to networking.k8s.io/v1 |
| 396 | + { |
| 397 | + Config: testAccResourceKustomizationConfig_upgradeAPIVersion("test_kustomizations/upgrade_api_version/modified"), |
| 398 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 399 | + resource.TestCheckResourceAttrSet("kustomization_resource.ns", "id"), |
| 400 | + resource.TestCheckResourceAttrSet("kustomization_resource.ing", "id"), |
| 401 | + testAccCheckManifestNestedString("kustomization_resource.ing", "networking.k8s.io/v1", "apiVersion"), |
| 402 | + ), |
| 403 | + }, |
| 404 | + }, |
| 405 | + }) |
| 406 | +} |
| 407 | + |
| 408 | +func testAccResourceKustomizationConfig_upgradeAPIVersion(path string) string { |
| 409 | + return testAccDataSourceKustomizationConfig_basic(path) + ` |
| 410 | +resource "kustomization_resource" "ns" { |
| 411 | + manifest = data.kustomization_build.test.manifests["~G_~V_Namespace|~X|test-upgrade-api-version"] |
| 412 | +} |
| 413 | +
|
| 414 | +resource "kustomization_resource" "ing" { |
| 415 | + manifest = data.kustomization_build.test.manifests["networking.k8s.io_~V_Ingress|test-upgrade-api-version|test-upgrade-api-version"] |
| 416 | +} |
| 417 | +` |
| 418 | +} |
| 419 | + |
373 | 420 | //
|
374 | 421 | //
|
375 | 422 | // CRD Test
|
@@ -795,3 +842,33 @@ func testAccCheckManifestSelectorAbsent(n string, k string) resource.TestCheckFu
|
795 | 842 | return nil
|
796 | 843 | }
|
797 | 844 | }
|
| 845 | + |
| 846 | +func testAccCheckManifestNestedString(n string, expected string, k ...string) resource.TestCheckFunc { |
| 847 | + return func(s *terraform.State) error { |
| 848 | + u, err := getResourceFromTestState(s, n) |
| 849 | + if err != nil { |
| 850 | + return err |
| 851 | + } |
| 852 | + |
| 853 | + resp, err := getResourceFromK8sAPI(u) |
| 854 | + if err != nil { |
| 855 | + return err |
| 856 | + } |
| 857 | + |
| 858 | + k8spath := strings.Join(k, ".") |
| 859 | + |
| 860 | + actual, ok, err := k8sunstructured.NestedString(resp.Object, k...) |
| 861 | + if !ok { |
| 862 | + return fmt.Errorf("%s missing from resource %s", k8spath, n) |
| 863 | + } |
| 864 | + if err != nil { |
| 865 | + return err |
| 866 | + } |
| 867 | + |
| 868 | + if actual != expected { |
| 869 | + return fmt.Errorf("value %s of %s does not match expected %s", actual, k8spath, expected) |
| 870 | + } |
| 871 | + |
| 872 | + return nil |
| 873 | + } |
| 874 | +} |
0 commit comments