From 7d4b4431228b2af55e0128c82eae50c5eed044af Mon Sep 17 00:00:00 2001 From: twelsh-aw <84401379+twelsh-aw@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:06:19 -0400 Subject: [PATCH] Add ability to pass envvar for acctest org (#37) * Add ability to pass envvar for acctest org This fixes failing test (by updating default) but also gives a way to test against other orgs more easily. * Fix typo --- CONTRIBUTING.md | 2 + internal/envvar/envvar.go | 25 +++++++++++ internal/provider/data_source_org_test.go | 14 ++++--- internal/provider/resource_org_member_test.go | 26 ++++++++---- ...rg_setting_image_access_management_test.go | 3 +- ...setting_registry_access_management_test.go | 3 +- internal/provider/resource_org_team_test.go | 3 +- ...esource_repository_team_permission_test.go | 3 +- internal/provider/resource_repository_test.go | 41 +++++++++++-------- 9 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 internal/envvar/envvar.go diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5c65edc..c1447bc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -85,6 +85,8 @@ where `TestFuncName` is the testing function within the `_test.go` file. # enable debug logging export TF_LOG=DEBUG export TF_LOG_PATH="/PATH/TO/YOUR/LOG_FILE.log" +# acceptance testing overrides +export ACCTEST_DOCKER_ORG=myorgname ... ``` diff --git a/internal/envvar/envvar.go b/internal/envvar/envvar.go new file mode 100644 index 0000000..d57fb5c --- /dev/null +++ b/internal/envvar/envvar.go @@ -0,0 +1,25 @@ +// Package envvar contains constants and defaults for various environment +// variables for the provider. +package envvar + +import "os" + +// Acceptance test related environment variables +const ( + AccTestOrganization = "ACCTEST_DOCKER_ORG" +) + +// defaults is a map of pre-configured defaults for each envvar +var defaults = map[string]string{ + AccTestOrganization: "dockerterraform", +} + +// GetWithDefault returns the value of the environment variable or the +// pre-configured default if empty. +func GetWithDefault(key string) string { + ret := os.Getenv(key) + if len(ret) > 0 { + return ret + } + return defaults[key] +} diff --git a/internal/provider/data_source_org_test.go b/internal/provider/data_source_org_test.go index 88e931b..743ff33 100644 --- a/internal/provider/data_source_org_test.go +++ b/internal/provider/data_source_org_test.go @@ -1,32 +1,36 @@ package provider import ( + "fmt" "testing" + "github.com/docker/terraform-provider-docker/internal/envvar" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestOrgDataSource(t *testing.T) { + org := envvar.GetWithDefault(envvar.AccTestOrganization) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ // Read testing { - Config: testOrgExampleDataSourceConfig, + Config: testOrgExampleDataSourceConfig(org), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.docker_org.test", "id", "dockerhackathon"), + resource.TestCheckResourceAttr("data.docker_org.test", "id", org), ), }, }, }) } -const testOrgExampleDataSourceConfig = ` +func testOrgExampleDataSourceConfig(org string) string { + return fmt.Sprintf(` provider "docker" { host = "hub-stage.docker.com" } data "docker_org" "test" { - org_name = "dockerhackathon" + org_name = "%[1]s" +}`, org) } -` diff --git a/internal/provider/resource_org_member_test.go b/internal/provider/resource_org_member_test.go index 270869f..fc039f6 100644 --- a/internal/provider/resource_org_member_test.go +++ b/internal/provider/resource_org_member_test.go @@ -1,22 +1,26 @@ package provider import ( + "fmt" "testing" + "github.com/docker/terraform-provider-docker/internal/envvar" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccOrgMemberResource(t *testing.T) { + org := envvar.GetWithDefault(envvar.AccTestOrganization) + teamName := "test" + randString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { - Config: testOrgMemberResourceConfig(), + Config: testOrgMemberResourceConfig(org, teamName), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet("docker_org_member.test", "invite_id"), - resource.TestCheckResourceAttr("docker_org_member.test", "org_name", "dockerhackathon"), - resource.TestCheckResourceAttr("docker_org_member.test", "team_name", "test"), + resource.TestCheckResourceAttr("docker_org_member.test", "org_name", org), + resource.TestCheckResourceAttr("docker_org_member.test", "team_name", teamName), resource.TestCheckResourceAttr("docker_org_member.test", "user_name", "newtest@example.com"), resource.TestCheckResourceAttr("docker_org_member.test", "role", "member"), ), @@ -31,17 +35,21 @@ func TestAccOrgMemberResource(t *testing.T) { }) } -func testOrgMemberResourceConfig() string { - return ` +func testOrgMemberResourceConfig(org string, team string) string { + return fmt.Sprintf(` provider "docker" { host = "hub-stage.docker.com" } +resource "docker_org_team" "testing" { + org_name = "%[1]s" + team_name = "%[2]s" +} + resource "docker_org_member" "test" { - org_name = "dockerhackathon" - team_name = "test" + org_name = docker_org_team.testing.org_name + team_name = docker_org_team.testing.team_name user_name = "newtest@example.com" role = "member" -} -` +}`, org, team) } diff --git a/internal/provider/resource_org_setting_image_access_management_test.go b/internal/provider/resource_org_setting_image_access_management_test.go index 944e79a..ea30d3a 100644 --- a/internal/provider/resource_org_setting_image_access_management_test.go +++ b/internal/provider/resource_org_setting_image_access_management_test.go @@ -4,11 +4,12 @@ import ( "fmt" "testing" + "github.com/docker/terraform-provider-docker/internal/envvar" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccOrgSettingImageAccessManagement(t *testing.T) { - orgName := "dockerterraform" + orgName := envvar.GetWithDefault(envvar.AccTestOrganization) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, diff --git a/internal/provider/resource_org_setting_registry_access_management_test.go b/internal/provider/resource_org_setting_registry_access_management_test.go index 57425e5..5bf3af8 100644 --- a/internal/provider/resource_org_setting_registry_access_management_test.go +++ b/internal/provider/resource_org_setting_registry_access_management_test.go @@ -4,12 +4,13 @@ import ( "fmt" "testing" + "github.com/docker/terraform-provider-docker/internal/envvar" "github.com/docker/terraform-provider-docker/internal/pkg/hubclient" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccOrgSettingRegistryAccessManagement(t *testing.T) { - orgName := "dockerterraform" + orgName := envvar.GetWithDefault(envvar.AccTestOrganization) customRegistry := hubclient.RegistryAccessManagementCustomRegistry{ Allowed: true, FriendlyName: "My personal registry", diff --git a/internal/provider/resource_org_team_test.go b/internal/provider/resource_org_team_test.go index 6b45355..e7fbc97 100644 --- a/internal/provider/resource_org_team_test.go +++ b/internal/provider/resource_org_team_test.go @@ -5,11 +5,12 @@ import ( "math/rand" "testing" + "github.com/docker/terraform-provider-docker/internal/envvar" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccOrgTeamResource(t *testing.T) { - orgName := "dockerterraform" + orgName := envvar.GetWithDefault(envvar.AccTestOrganization) teamName := "test" + randString(10) updatedName := "test" + randString(10) resource.Test(t, resource.TestCase{ diff --git a/internal/provider/resource_repository_team_permission_test.go b/internal/provider/resource_repository_team_permission_test.go index 5178a24..c5a9309 100644 --- a/internal/provider/resource_repository_team_permission_test.go +++ b/internal/provider/resource_repository_team_permission_test.go @@ -4,13 +4,14 @@ import ( "fmt" "testing" + "github.com/docker/terraform-provider-docker/internal/envvar" "github.com/docker/terraform-provider-docker/internal/pkg/hubclient" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" ) func TestAccRepositoryTeamPermission(t *testing.T) { - orgName := "dockerterraform" + orgName := envvar.GetWithDefault(envvar.AccTestOrganization) teamName := "test" + randString(10) repoName := "test" + randString(10) resource.Test(t, resource.TestCase{ diff --git a/internal/provider/resource_repository_test.go b/internal/provider/resource_repository_test.go index 23aae5e..14865e9 100644 --- a/internal/provider/resource_repository_test.go +++ b/internal/provider/resource_repository_test.go @@ -1,6 +1,7 @@ package provider import ( + "fmt" "os" "testing" @@ -8,24 +9,26 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) -func TestRepositoryResource(t *testing.T) { +func TestAccRepositoryResource(t *testing.T) { + namespace := os.Getenv("DOCKER_USERNAME") + name := "example-repo" + randString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, Steps: []resource.TestStep{ { - Config: testRepositoryResourceConfig(), + Config: testRepositoryResourceConfig(namespace, name), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet("docker_hub_repository.test", "id"), - resource.TestCheckResourceAttr("docker_hub_repository.test", "name", "example-repo"), - resource.TestCheckResourceAttr("docker_hub_repository.test", "namespace", os.Getenv("DOCKER_USERNAME")), + resource.TestCheckResourceAttr("docker_hub_repository.test", "name", name), + resource.TestCheckResourceAttr("docker_hub_repository.test", "namespace", namespace), resource.TestCheckResourceAttr("docker_hub_repository.test", "description", "Example repository"), resource.TestCheckNoResourceAttr("docker_hub_repository.test", "full_description"), resource.TestCheckResourceAttr("docker_hub_repository.test", "private", "false"), ), }, { - Config: testRepositoryResourceConfigUpdated(), + Config: testRepositoryResourceConfigUpdated(namespace, name), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("docker_hub_repository.test", "description", "Updated example repository"), resource.TestCheckResourceAttr("docker_hub_repository.test", "full_description", "Full description update"), @@ -45,25 +48,31 @@ func TestRepositoryResource(t *testing.T) { }) } -func testRepositoryResourceConfig() string { - return ` +func testRepositoryResourceConfig(namespace, name string) string { + return fmt.Sprintf(` +provider "docker" { + host = "hub-stage.docker.com" +} + resource "docker_hub_repository" "test" { - name = "example-repo" - namespace = "` + os.Getenv("DOCKER_USERNAME") + `" + name = "%[2]s" + namespace = "%[1]s" description = "Example repository" private = false +}`, namespace, name) } -` + +func testRepositoryResourceConfigUpdated(namespace, name string) string { + return fmt.Sprintf(` +provider "docker" { + host = "hub-stage.docker.com" } -func testRepositoryResourceConfigUpdated() string { - return ` resource "docker_hub_repository" "test" { - name = "example-repo" - namespace = "` + os.Getenv("DOCKER_USERNAME") + `" + name = "%[2]s" + namespace = "%[1]s" description = "Updated example repository" full_description = "Full description update" private = true -} -` +}`, namespace, name) }