|
6 | 6 | "fmt" |
7 | 7 | "os" |
8 | 8 | "path/filepath" |
| 9 | + "regexp" |
9 | 10 | "strings" |
10 | 11 | "testing" |
11 | 12 |
|
@@ -166,6 +167,9 @@ func getEnv() *currentEnv { |
166 | 167 | return e |
167 | 168 | } |
168 | 169 |
|
| 170 | +// testAccPreCheck verifies and sets required provider testing configuration |
| 171 | +// This PreCheck function should be present in every acceptance test. It allows |
| 172 | +// test configurations to omit a provider configuration |
169 | 173 | func testAccPreCheck(t *testing.T) { |
170 | 174 | ctx := context.TODO() |
171 | 175 | hasFileCfg := (os.Getenv("KUBE_CTX_AUTH_INFO") != "" && os.Getenv("KUBE_CTX_CLUSTER") != "") || |
@@ -436,3 +440,75 @@ func requiredProviders() string { |
436 | 440 | } |
437 | 441 | `) |
438 | 442 | } |
| 443 | + |
| 444 | +// testAccProviderFactoriesInternal is a factory used for provider configuration testing. |
| 445 | +// This should only be used for TestAccKubernetesProvider_ tests which need to |
| 446 | +// reference the provider instance itself. Other testing should use testAccProviderFactories. |
| 447 | +var testAccProviderFactoriesInternal = map[string]func() (*schema.Provider, error){ |
| 448 | + "kubernetes": func() (*schema.Provider, error) { |
| 449 | + return Provider(), nil |
| 450 | + }, |
| 451 | +} |
| 452 | + |
| 453 | +func TestAccAKubernetesProvider_config_path(t *testing.T) { |
| 454 | + |
| 455 | + resource.ParallelTest(t, resource.TestCase{ |
| 456 | + PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInMinikube(t) }, |
| 457 | + ProviderFactories: testAccProviderFactoriesInternal, |
| 458 | + Steps: []resource.TestStep{ |
| 459 | + { |
| 460 | + Config: testAccKubernetesProvider_config_path(), |
| 461 | + Check: resource.ComposeTestCheckFunc( |
| 462 | + resource.TestCheckResourceAttr("data.kubernetes_namespace.test", "metadata.0.name", "default"), |
| 463 | + ), |
| 464 | + }, |
| 465 | + }, |
| 466 | + }) |
| 467 | +} |
| 468 | + |
| 469 | +func TestAccAKubernetesProvider_config_paths(t *testing.T) { |
| 470 | + wantError := `"config_path": conflicts with config_paths` |
| 471 | + |
| 472 | + resource.ParallelTest(t, resource.TestCase{ |
| 473 | + PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInMinikube(t) }, |
| 474 | + ProviderFactories: testAccProviderFactoriesInternal, |
| 475 | + Steps: []resource.TestStep{ |
| 476 | + { |
| 477 | + Config: testAccKubernetesProvider_config_paths(), |
| 478 | + ExpectError: regexp.MustCompile(wantError), |
| 479 | + }, |
| 480 | + }, |
| 481 | + }) |
| 482 | +} |
| 483 | + |
| 484 | +func testAccKubernetesProvider_config_path() string { |
| 485 | + return fmt.Sprintf(`provider "kubernetes" { |
| 486 | + config_path = "~/.kube/config" |
| 487 | +} |
| 488 | +
|
| 489 | +# Required to initialize the provider. |
| 490 | +data kubernetes_namespace "test" { |
| 491 | + metadata { |
| 492 | + name = "default" |
| 493 | + } |
| 494 | +} |
| 495 | +`) |
| 496 | +} |
| 497 | + |
| 498 | +func testAccKubernetesProvider_config_paths() string { |
| 499 | + return fmt.Sprintf(`provider "kubernetes" { |
| 500 | + config_path = "~/.kube/config" |
| 501 | + config_paths = ["~/.kube/config"] |
| 502 | +# host = "https://192.168.39.129:8443" |
| 503 | +# cluster_ca_certificate = file("~/.minikube/profiles/minikube/apiserver.crt") |
| 504 | +# token = base64decode(file("~/.minikube/profiles/minikube/token")) |
| 505 | +} |
| 506 | +
|
| 507 | +# Required to initialize the provider. |
| 508 | +data kubernetes_namespace "test" { |
| 509 | + metadata { |
| 510 | + name = "default" |
| 511 | + } |
| 512 | +} |
| 513 | +`) |
| 514 | +} |
0 commit comments