|
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"
|
@@ -373,6 +374,52 @@ resource "kustomization_resource" "ss" {
|
373 | 374 | `
|
374 | 375 | }
|
375 | 376 |
|
| 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 | + |
376 | 423 | //
|
377 | 424 | //
|
378 | 425 | // CRD Test
|
@@ -798,3 +845,33 @@ func testAccCheckManifestSelectorAbsent(n string, k string) resource.TestCheckFu
|
798 | 845 | return nil
|
799 | 846 | }
|
800 | 847 | }
|
| 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 | +} |
0 commit comments