Skip to content

Commit

Permalink
Skip acceptance tests for secrets in version less then needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
anvial committed Apr 17, 2024
1 parent 48b94fb commit 37624a6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ For example, here they are set using the currently active controller:
```shell
export CONTROLLER=$(juju whoami | yq .Controller)
export JUJU_AGENT_VERSION="$(juju show-controller | yq .[$CONTROLLER].details.\"agent-version\"|tr -d '"')"
export JUJU_CONTROLLER_ADDRESSES="$(juju show-controller | yq '.[$CONTROLLER]'.details.\"api-endpoints\" | tr -d "[]' "|tr -d '"'|tr -d '\n')"
export JUJU_USERNAME="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.user|tr -d '"')"
export JUJU_PASSWORD="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.password|tr -d '"')"
Expand Down
1 change: 1 addition & 0 deletions internal/juju/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func populateControllerConfig() {
}

localProviderConfig = map[string]string{}
localProviderConfig["JUJU_AGENT_VERSION"] = controllerConfig.ProviderDetails.AgentVersion
localProviderConfig["JUJU_CONTROLLER_ADDRESSES"] = strings.Join(controllerConfig.ProviderDetails.ApiEndpoints, ",")
localProviderConfig["JUJU_CA_CERT"] = controllerConfig.ProviderDetails.CACert
localProviderConfig["JUJU_USERNAME"] = controllerConfig.Account.User
Expand Down
16 changes: 16 additions & 0 deletions internal/provider/resource_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -12,6 +13,11 @@ import (
)

func TestAcc_ResourceSecret_CreateWithInfo(t *testing.T) {
// Check env var JUJU_AGENT_VERSION is above 3.1.0
if os.Getenv("JUJU_AGENT_VERSION") == "" || internaltesting.CompareVersions(os.Getenv("JUJU_AGENT_VERSION"), "3.3.0") < 0 {
t.Skip("JUJU_AGENT_VERSION is not set or is below 3.3.0")
}

modelName := acctest.RandomWithPrefix("tf-test-model")
secretName := "tf-test-secret"
secretInfo := "test-info"
Expand Down Expand Up @@ -39,6 +45,11 @@ func TestAcc_ResourceSecret_CreateWithInfo(t *testing.T) {
}

func TestAcc_ResourceSecret_CreateWithNoInfo(t *testing.T) {
// Check env var JUJU_AGENT_VERSION is above 3.1.0
if os.Getenv("JUJU_AGENT_VERSION") == "" || internaltesting.CompareVersions(os.Getenv("JUJU_AGENT_VERSION"), "3.3.0") < 0 {
t.Skip("JUJU_AGENT_VERSION is not set or is below 3.3.0")
}

modelName := acctest.RandomWithPrefix("tf-test-model")
secretName := "tf-test-secret"
secretValue := map[string]string{
Expand All @@ -64,6 +75,11 @@ func TestAcc_ResourceSecret_CreateWithNoInfo(t *testing.T) {
}

func TestAcc_ResourceSecret_Update(t *testing.T) {
// Check env var JUJU_AGENT_VERSION is above 3.1.0
if os.Getenv("JUJU_AGENT_VERSION") == "" || internaltesting.CompareVersions(os.Getenv("JUJU_AGENT_VERSION"), "3.3.0") < 0 {
t.Skip("JUJU_AGENT_VERSION is not set or is below 3.3.0")
}

modelName := acctest.RandomWithPrefix("tf-test-model")
secretName := "tf-test-secret"
secretInfo := "test-info"
Expand Down
37 changes: 37 additions & 0 deletions internal/testing/versioncomparer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the Apache License, Version 2.0, see LICENCE file for details.

package testsing

import (
"strconv"
"strings"
)

// CompareVersions compares two versions in the format "X.Y.Z" and returns:
// -1 if version1 is less than version2
// 0 if version1 is equal to version2
// 1 if version1 is greater than version2
func CompareVersions(version1, version2 string) int {
v1Parts := strings.Split(version1, ".")
v2Parts := strings.Split(version2, ".")

for i := 0; i < 3; i++ {
v1, err := strconv.Atoi(v1Parts[i])
if err != nil {
panic(err)
}
v2, err := strconv.Atoi(v2Parts[i])
if err != nil {
panic(err)
}

if v1 < v2 {
return -1
} else if v1 > v2 {
return 1
}
}

return 0
}

0 comments on commit 37624a6

Please sign in to comment.