Skip to content

Commit

Permalink
Merge pull request #534 from MusicDin/fix/def-remote
Browse files Browse the repository at this point in the history
provider: Fix incorrect check for configured default remote
  • Loading branch information
simondeziel authored Oct 10, 2024
2 parents f8538e7 + 9a8357a commit 7b6dae6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/provider-config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func NewLxdProviderConfig(version string, remotes map[string]LxdRemote, options
}

// Ensure only one remote is configured as default.
if len(defaultRemotes) > 0 {
if len(defaultRemotes) > 1 {
return nil, fmt.Errorf("Multiple remotes are configured as default: [%v]", strings.Join(defaultRemotes, ", "))
}

Expand Down
52 changes: 52 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,31 @@ func TestAccProvider_acceptRemoteCertificate(t *testing.T) {
})
}

func TestAccProvider_defaultRemote(t *testing.T) {
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 remotes are configured.
Config: testAccProvider_defaultRemote(configDir, 2),
ExpectError: regexp.MustCompile(`Multiple remotes are configured as default`),
PlanOnly: true, // This error should be thrown during the plan.
},
{
// Ensure a default remote can be configured.
Config: testAccProvider_defaultRemote(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 +281,32 @@ resource "lxd_noop" "noop" {
`, configDir)
}

func testAccProvider_defaultRemote(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 7b6dae6

Please sign in to comment.