From c043874c34e87c7af7e4b45b1d8d37eea8db1299 Mon Sep 17 00:00:00 2001 From: Vitaly Antonenko Date: Thu, 18 Apr 2024 22:26:33 +0300 Subject: [PATCH] Add acceptance test --- README.md | 2 +- internal/provider/main_test.go | 1 + .../provider/resource_secret_access_test.go | 112 ++++++++++++++++++ internal/provider/resource_secret_test.go | 24 ++-- 4 files changed, 130 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9ecffd2c..2a065c5c 100644 --- a/README.md +++ b/README.md @@ -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 -``` +``` \ No newline at end of file diff --git a/internal/provider/main_test.go b/internal/provider/main_test.go index 5fe7ace1..f0410cd4 100644 --- a/internal/provider/main_test.go +++ b/internal/provider/main_test.go @@ -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 diff --git a/internal/provider/resource_secret_access_test.go b/internal/provider/resource_secret_access_test.go index 4f504f66..20cef32e 100644 --- a/internal/provider/resource_secret_access_test.go +++ b/internal/provider/resource_secret_access_test.go @@ -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, + }) +} diff --git a/internal/provider/resource_secret_test.go b/internal/provider/resource_secret_test.go index 6b670dd0..04dce744 100644 --- a/internal/provider/resource_secret_test.go +++ b/internal/provider/resource_secret_test.go @@ -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") @@ -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") @@ -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") @@ -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")