diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 56b42193..1d922842 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "regexp" + "strings" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -188,6 +189,33 @@ func TestAccProvider_acceptRemoteCertificate(t *testing.T) { }) } +func TestAccProvider_defaultProvider(t *testing.T) { + defer resetLXDRemoteEnvVars() + + configDir := t.TempDir() + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + // Ensure an error is returned if multiple default providers are configured. + Config: testAccProvider_defaultProvider(configDir, 2), + ExpectError: regexp.MustCompile(`Multiple remotes are configured as default: \[remote-0, remote-1\]`), + PlanOnly: true, // This error should already be thrown during the plan. + }, + { + // Ensure a default provider can be configured. + Config: testAccProvider_defaultProvider(configDir, 1), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("lxd_noop.noop", "remote", "remote-0"), + resource.TestCheckResourceAttr("lxd_noop.noop", "project", "default"), + resource.TestCheckResourceAttrSet("lxd_noop.noop", "server_version"), + ), + }, + }, + }) +} + func testAccProvider_configDir(configDir string) string { return fmt.Sprintf(` provider "lxd" { @@ -255,6 +283,32 @@ resource "lxd_noop" "noop" { `, configDir) } +func testAccProvider_defaultProvider(configDir string, remoteCount int) string { + var remotes string + + for i := 0; i < remoteCount; i++ { + remotes += fmt.Sprintf(` +remote { + name = "remote-%d" + default = true +} +`, i) + } + + return fmt.Sprintf(` +provider "lxd" { + generate_client_certificates = true + accept_remote_certificate = true + config_dir = %q + + # Remotes. + %s +} + +resource "lxd_noop" "noop" {} + `, configDir, strings.Join([]string{remotes}, "\n")) +} + // testCheckClientCert checks that the client certificate was generated. func testCheckClientCert(configDir string, shouldExist bool) resource.TestCheckFunc { return func(_ *terraform.State) error {