Skip to content

Commit

Permalink
provider: Add default remote test
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Oct 10, 2024
1 parent ae10a78 commit 4492a61
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4492a61

Please sign in to comment.