Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUP-1392: Random test suite names in tests/t.Run() conversion #376

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## Unreleased

- SUP-1392: Random test suite names in tests/t.Run() conversion [[PR #376](https://github.com/buildkite/terraform-provider-buildkite/pull/376)] @james2791

## [v0.26.0](https://github.com/buildkite/terraform-provider-buildkite/compare/v0.25.0...v0.26.0)

- SUP-1375 Use context everywhere [[PR #362](https://github.com/buildkite/terraform-provider-buildkite/pull/362)] @jradtilbrook
Expand Down
322 changes: 149 additions & 173 deletions buildkite/resource_test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,191 +7,167 @@ import (
"reflect"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAcc_testSuiteAddRemove(t *testing.T) {
t.Parallel()
var suite getTestSuiteSuite
func TestAccBuildkiteTestSuite(t *testing.T) {
basicTestSuite := func(name string) string {
return fmt.Sprintf(`
resource "buildkite_team" "team" {
name = "test suite team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite %s"
default_branch = "main"
team_owner_id = resource.buildkite_team.team.id
}
`, name)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testTestSuiteDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "buildkite_team" "team" {
name = "test suite team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite"
default_branch = "main"
team_owner_id = resource.buildkite_team.team.id
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", "test suite"),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", "test suite"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
),
},
},
})
}
testSuiteWithTwoTeams := func(name string) string {
return fmt.Sprintf(`
resource "buildkite_team" "ateam" {
name = "a team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_team" "bteam" {
name = "b team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite update %s"
default_branch = "main"
team_owner_id = resource.buildkite_team.bteam.id
}
`, name)
}

func TestAcc_testSuiteUpdate(t *testing.T) {
t.Parallel()
var suite getTestSuiteSuite
testSuiteTeamAddition := func(name string) string {
return fmt.Sprintf(`
resource "buildkite_team" "ateam" {
name = "a team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_team" "bteam" {
name = "b team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite update %s"
default_branch = "main"
team_owner_id = resource.buildkite_team.bteam.id
}
resource "buildkite_test_suite_team" "team-a" {
test_suite_id = buildkite_test_suite.suite.id
team_id = buildkite_team.ateam.id
access_level = "MANAGE_AND_READ"
}
`, name)
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testTestSuiteDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "buildkite_team" "team" {
name = "test suite team update"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite update"
default_branch = "main"
team_owner_id = resource.buildkite_team.team.id
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", "test suite update"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", "test suite update"),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
),
t.Run("Creates a Test Suite", func(t *testing.T) {
var suite getTestSuiteSuite
randName := acctest.RandString(10)
check := resource.ComposeAggregateTestCheckFunc(
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", fmt.Sprintf("test suite %s", randName)),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", fmt.Sprintf("test suite %s", randName)),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testTestSuiteDestroy,
Steps: []resource.TestStep{
{
Config: basicTestSuite(randName),
Check: check,
},
},
{
Config: `
resource "buildkite_team" "team" {
name = "test suite team update"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite update"
default_branch = "main"
team_owner_id = resource.buildkite_team.team.id
}
`,
Taint: []string{"buildkite_team.team"},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", "test suite update"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", "test suite update"),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
),
},
},
})
})
}

func TestAcc_testSuiteTeamOwnerResolution(t *testing.T) {
t.Parallel()
var suite getTestSuiteSuite

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testTestSuiteDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "buildkite_team" "ateam" {
name = "a team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_team" "bteam" {
name = "b team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite update"
default_branch = "main"
team_owner_id = resource.buildkite_team.bteam.id
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", "test suite update"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
resource.TestCheckResourceAttrPair("buildkite_test_suite.suite", "team_owner_id", "buildkite_team.bteam", "id"),
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", "test suite update"),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
),
t.Run("Updates a Test Suite", func(t *testing.T) {
var suite getTestSuiteSuite
randName := acctest.RandString(10)
check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", fmt.Sprintf("test suite %s", randName)),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", fmt.Sprintf("test suite %s", randName)),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testTestSuiteDestroy,
Steps: []resource.TestStep{
{
Config: basicTestSuite(randName),
Check: check,
},
{
Config: basicTestSuite(randName),
Taint: []string{"buildkite_team.team"},
Check: check,
},
},
{
Config: `
resource "buildkite_team" "ateam" {
name = "a team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_team" "bteam" {
name = "b team"
default_team = false
privacy = "VISIBLE"
default_member_role = "MAINTAINER"
}
resource "buildkite_test_suite" "suite" {
name = "test suite update"
default_branch = "main"
team_owner_id = resource.buildkite_team.bteam.id
}
resource "buildkite_test_suite_team" "team-a" {
test_suite_id = buildkite_test_suite.suite.id
team_id = buildkite_team.ateam.id
access_level = "MANAGE_AND_READ"
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", "test suite update"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
resource.TestCheckResourceAttrPair("buildkite_test_suite.suite", "team_owner_id", "buildkite_team.bteam", "id"),
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", "test suite update"),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
),
})
})

t.Run("Creates and handles Test Suite Team owner resolution", func(t *testing.T) {
var suite getTestSuiteSuite
randName := acctest.RandString(10)

check := resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "id"),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "api_token"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "default_branch", "main"),
resource.TestCheckResourceAttr("buildkite_test_suite.suite", "name", fmt.Sprintf("test suite update %s", randName)),
resource.TestCheckResourceAttrSet("buildkite_test_suite.suite", "team_owner_id"),
resource.TestCheckResourceAttrPair("buildkite_test_suite.suite", "team_owner_id", "buildkite_team.bteam", "id"),
checkTestSuiteExists("buildkite_test_suite.suite", &suite),
checkTestSuiteRemoteValue(&suite, "Name", fmt.Sprintf("test suite update %s", randName)),
checkTestSuiteRemoteValue(&suite, "DefaultBranch", "main"),
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testTestSuiteDestroy,
Steps: []resource.TestStep{
{
Config: testSuiteWithTwoTeams(randName),
Check: check,
},
{
Config: testSuiteTeamAddition(randName),
Check: check,
},
},
},
})
})
}

Expand Down