Skip to content

Commit

Permalink
Add ability to pass envvar for acctest org (#37)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
twelsh-aw authored Aug 28, 2024
1 parent 41ff7c6 commit 7d4b443
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
```

Expand Down
25 changes: 25 additions & 0 deletions internal/envvar/envvar.go
Original file line number Diff line number Diff line change
@@ -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]
}
14 changes: 9 additions & 5 deletions internal/provider/data_source_org_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
`
26 changes: 17 additions & 9 deletions internal/provider/resource_org_member_test.go
Original file line number Diff line number Diff line change
@@ -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", "[email protected]"),
resource.TestCheckResourceAttr("docker_org_member.test", "role", "member"),
),
Expand All @@ -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 = "[email protected]"
role = "member"
}
`
}`, org, team)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/resource_org_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
41 changes: 25 additions & 16 deletions internal/provider/resource_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"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"),
Expand All @@ -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)
}

0 comments on commit 7d4b443

Please sign in to comment.