Skip to content

Commit 615ca56

Browse files
committed
wip
1 parent 578daf7 commit 615ca56

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

kubernetes/provider_test.go

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ func testAccPreCheck(t *testing.T) {
206206
return
207207
}
208208

209+
// testAccPreCheckInternal configures the provider for internal tests.
210+
// This is the equivalent of running `terraform init`, but with a bare
211+
// minimum configuration, to create a fully separate environment where
212+
// all configuration options (including environment variables) can be
213+
// tested separately from the user's environment. It is used exclusively
214+
// in functions labelled testAccKubernetesProvider_*.
215+
func testAccPreCheckInternal(t *testing.T) {
216+
ctx := context.TODO()
217+
unsetEnv(t)
218+
diags := testAccProvider.Configure(ctx, terraform.NewResourceConfigRaw(nil))
219+
if diags.HasError() {
220+
t.Fatal(diags[0].Summary)
221+
}
222+
return
223+
}
224+
209225
func getClusterVersion() (*gversion.Version, error) {
210226
meta := testAccProvider.Meta()
211227

@@ -453,12 +469,12 @@ var testAccProviderFactoriesInternal = map[string]func() (*schema.Provider, erro
453469
func TestAccKubernetesProvider_config_path(t *testing.T) {
454470

455471
resource.Test(t, resource.TestCase{
456-
PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInMinikube(t); unsetEnv(t) },
472+
PreCheck: func() { testAccPreCheckInternal(t) },
457473
ProviderFactories: testAccProviderFactoriesInternal,
458474
Steps: []resource.TestStep{
459475
{
460476
Config: testAccKubernetesProviderConfig(
461-
providerConfig_config_path("~/.kube/config"),
477+
providerConfig_config_path("./testdata/kubeconfig"),
462478
),
463479
Check: resource.TestCheckResourceAttr("data.kubernetes_namespace.test", "metadata.0.name", "default"),
464480
},
@@ -470,26 +486,67 @@ func TestAccKubernetesProvider_config_paths(t *testing.T) {
470486
wantError := `"config_path": conflicts with config_paths`
471487

472488
resource.Test(t, resource.TestCase{
473-
PreCheck: func() { skipIfNotRunningInMinikube(t); unsetEnv(t) },
489+
// Unset env vars to prevent them from being used in the test.
490+
PreCheck: func() { testAccPreCheckInternal(t) },
474491
ProviderFactories: testAccProviderFactoriesInternal,
475492
Steps: []resource.TestStep{
476493
{
477494
Config: testAccKubernetesProviderConfig(
478-
providerConfig_config_paths([]string{"~/.kube/config", "~/.kube/config2"}),
495+
providerConfig_config_paths(`["./testdata/kubeconfig", "./testdata/kubeconfig"]`),
479496
),
480497
Check: resource.TestCheckResourceAttr("data.kubernetes_namespace.test", "metadata.0.name", "default"),
481498
},
482499
{
483500
Config: testAccKubernetesProviderConfig(
484-
providerConfig_config_path("~/.kube/config") +
485-
providerConfig_config_paths([]string{"~/.kube/config", "~/.kube/config2"}),
501+
providerConfig_config_path("./internal/testdata/kubeconfig") +
502+
providerConfig_config_paths(`["./testdata/kubeconfig", "./testdata/kubeconfig"]`),
486503
),
487504
ExpectError: regexp.MustCompile(wantError),
488505
},
489506
},
490507
})
491508
}
492509

510+
func TestAccKubernetesProvider_config_paths_env(t *testing.T) {
511+
os.Setenv("KUBE_CONFIG_PATHS", strings.Join([]string{
512+
"./testdata/kubeconfig",
513+
"./testdata/kubeconfig",
514+
}, string(os.PathListSeparator)))
515+
516+
resource.Test(t, resource.TestCase{
517+
PreCheck: func() { testAccPreCheckInternal(t) },
518+
ProviderFactories: testAccProviderFactoriesInternal,
519+
Steps: []resource.TestStep{
520+
{
521+
Config: testAccKubernetesProviderConfig("# empty"),
522+
Check: resource.TestCheckResourceAttr("data.kubernetes_namespace.test", "metadata.0.name", "default"),
523+
},
524+
},
525+
})
526+
}
527+
528+
func TestAccKubernetesProvider_config_paths_env_wantError(t *testing.T) {
529+
wantError := `"config_path": conflicts with config_paths`
530+
531+
unsetEnv(t)
532+
os.Setenv("KUBE_CONFIG_PATHS", strings.Join([]string{
533+
"testdata/kubeconfig",
534+
"testdata/kubeconfig",
535+
}, string(os.PathListSeparator)))
536+
537+
os.Setenv("KUBE_CONFIG_PATH", "testdata/kubeconfig")
538+
resource.Test(t, resource.TestCase{
539+
PreCheck: func() { testAccPreCheckInternal(t) },
540+
ProviderFactories: testAccProviderFactoriesInternal,
541+
Steps: []resource.TestStep{
542+
{
543+
Config: testAccKubernetesProviderConfig("# empty"),
544+
ExpectError: regexp.MustCompile(wantError),
545+
},
546+
},
547+
})
548+
}
549+
493550
// testAccKubernetesProviderConfig is used together with the providerConfig_* functions
494551
// to assemble a Kubernetes provider configuration with interchangeable options.
495552
func testAccKubernetesProviderConfig(providerConfig string) string {
@@ -511,10 +568,10 @@ func providerConfig_config_path(path string) string {
511568
`, path)
512569
}
513570

514-
func providerConfig_config_paths(paths []string) string {
571+
func providerConfig_config_paths(paths string) string {
515572
return fmt.Sprintf(`
516-
config_paths = [%q]
517-
`, strings.Join(paths, ", "))
573+
config_paths = %s
574+
`, paths)
518575
}
519576

520577
func testAccKubernetesProviderConfig_token(token string) string {

0 commit comments

Comments
 (0)