Skip to content

Commit

Permalink
Add acceptance test
Browse files Browse the repository at this point in the history
  • Loading branch information
anvial committed Apr 18, 2024
1 parent e0f6483 commit c043874
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ grep "@module=juju.datasource" ./terraform.log
To find logs specific to the juju client talking to juju itself:
```shell
grep "@module=juju.client" ./terraform.log
```
```
1 change: 1 addition & 0 deletions internal/provider/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const TestCloudEnvKey string = "TEST_CLOUD"
const TestMachineIPEnvKey string = "TEST_ADD_MACHINE_IP"
const TestSSHPublicKeyFileEnvKey string = "TEST_SSH_PUB_KEY_PATH"
const TestSSHPrivateKeyFileEnvKey string = "TEST_SSH_PRIV_KEY_PATH"
const TestJujuAgentVersion = "JUJU_AGENT_VERSION"

// CloudTesting is a value indicating the current cloud
// available for testing
Expand Down
112 changes: 112 additions & 0 deletions internal/provider/resource_secret_access_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,113 @@
// Copyright 2024 Canonical Ltd.
// Licensed under the Apache License, Version 2.0, see LICENCE file for details.

package provider

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
internaltesting "github.com/juju/terraform-provider-juju/internal/testing"
)

func TestAcc_ResourceSecretAccess_GrantRevoke(t *testing.T) {
if os.Getenv(TestJujuAgentVersion) == "" {
t.Errorf("%s is not set", TestJujuAgentVersion)
} else if internaltesting.CompareVersions(os.Getenv(TestJujuAgentVersion), "3.3.0") < 0 {
t.Skipf("%s is not set or is below 3.3.0", TestJujuAgentVersion)
}

modelName := acctest.RandomWithPrefix("tf-test-model")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: frameworkProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccResourceSecretWithAccess(modelName, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "model", modelName),
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "applications.0", "jul"),
),
},
{
Config: testAccResourceSecretWithAccess(modelName, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "model", modelName),
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "applications.0", "jul"),
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "applications.1", "jul2"),
),
},
{
Config: testAccResourceSecretWithAccess(modelName, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "model", modelName),
resource.TestCheckResourceAttr("juju_secret_access.test_secret_access", "applications.0", "jul"),
),
},
},
})
}

func testAccResourceSecretWithAccess(modelName string, allApplicationAccess bool) string {
return internaltesting.GetStringFromTemplateWithData(
"testAccResourceSecret",
`
resource "juju_model" "{{.ModelName}}" {
name = "{{.ModelName}}"
}
resource "juju_application" "jul" {
name = "jul"
model = juju_model.{{.ModelName}}.name
charm {
name = "jameinel-ubuntu-lite"
channel = "latest/stable"
}
units = 1
}
resource "juju_application" "jul2" {
name = "jul2"
model = juju_model.{{.ModelName}}.name
charm {
name = "jameinel-ubuntu-lite"
channel = "latest/stable"
}
units = 1
}
resource "juju_secret" "test_secret" {
model = juju_model.{{.ModelName}}.name
name = "test_secret_name"
value = {
key1 = "value1"
key2 = "value2"
}
info = "This is my secret"
}
resource "juju_secret_access" "test_secret_access" {
model = juju_model.{{.ModelName}}.name
{{- if .AllApplicationAccess }}
applications = [
juju_application.jul.name, juju_application.jul2.name
]
{{- else }}
applications = [
juju_application.jul.name
]
{{- end }}
secret_id = juju_secret.test_secret.secret_id
}
`, internaltesting.TemplateData{
"ModelName": modelName,
"AllApplicationAccess": allApplicationAccess,
})
}
24 changes: 16 additions & 8 deletions internal/provider/resource_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

func TestAcc_ResourceSecret_CreateWithoutName(t *testing.T) {
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")
if os.Getenv(TestJujuAgentVersion) == "" {
t.Errorf("%s is not set", TestJujuAgentVersion)
} else if internaltesting.CompareVersions(os.Getenv(TestJujuAgentVersion), "3.3.0") < 0 {
t.Skipf("%s is not set or is below 3.3.0", TestJujuAgentVersion)
}

modelName := acctest.RandomWithPrefix("tf-test-model")
Expand Down Expand Up @@ -42,8 +44,10 @@ func TestAcc_ResourceSecret_CreateWithoutName(t *testing.T) {
}

func TestAcc_ResourceSecret_CreateWithInfo(t *testing.T) {
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")
if os.Getenv(TestJujuAgentVersion) == "" {
t.Errorf("%s is not set", TestJujuAgentVersion)
} else if internaltesting.CompareVersions(os.Getenv(TestJujuAgentVersion), "3.3.0") < 0 {
t.Skipf("%s is not set or is below 3.3.0", TestJujuAgentVersion)
}

modelName := acctest.RandomWithPrefix("tf-test-model")
Expand Down Expand Up @@ -73,8 +77,10 @@ func TestAcc_ResourceSecret_CreateWithInfo(t *testing.T) {
}

func TestAcc_ResourceSecret_CreateWithNoInfo(t *testing.T) {
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")
if os.Getenv(TestJujuAgentVersion) == "" {
t.Errorf("%s is not set", TestJujuAgentVersion)
} else if internaltesting.CompareVersions(os.Getenv(TestJujuAgentVersion), "3.3.0") < 0 {
t.Skipf("%s is not set or is below 3.3.0", TestJujuAgentVersion)
}

modelName := acctest.RandomWithPrefix("tf-test-model")
Expand Down Expand Up @@ -102,8 +108,10 @@ func TestAcc_ResourceSecret_CreateWithNoInfo(t *testing.T) {
}

func TestAcc_ResourceSecret_Update(t *testing.T) {
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")
if os.Getenv(TestJujuAgentVersion) == "" {
t.Errorf("%s is not set", TestJujuAgentVersion)
} else if internaltesting.CompareVersions(os.Getenv(TestJujuAgentVersion), "3.3.0") < 0 {
t.Skipf("%s is not set or is below 3.3.0", TestJujuAgentVersion)
}

modelName := acctest.RandomWithPrefix("tf-test-model")
Expand Down

0 comments on commit c043874

Please sign in to comment.