-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add acceptance tests for teams #103
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package buildkite | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// Confirm that we can create a team, and then delete it without error | ||
func TestAccTeam_add_remove(t *testing.T) { | ||
var resourceTeam TeamNode | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckTeamResourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTeamConfigBasic("developers"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Confirm the team exists in the buildkite API | ||
testAccCheckTeamExists("buildkite_team.test", &resourceTeam), | ||
// Confirm the team has the correct values in Buildkite's system | ||
testAccCheckTeamRemoteValues(&resourceTeam, "developers"), | ||
// Confirm the team has the correct values in terraform state | ||
resource.TestCheckResourceAttr("buildkite_team.test", "name", "developers"), | ||
resource.TestCheckResourceAttr("buildkite_team.test", "privacy", "VISIBLE"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccTeam_update(t *testing.T) { | ||
var resourceTeam TeamNode | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckTeamResourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTeamConfigBasic("developers"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Confirm the team exists in the buildkite API | ||
testAccCheckTeamExists("buildkite_team.test", &resourceTeam), | ||
// Quick check to confirm the local state is correct before we update it | ||
resource.TestCheckResourceAttr("buildkite_team.test", "name", "developers"), | ||
), | ||
}, | ||
{ | ||
Config: testAccTeamConfigBasic("wombats"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Confirm the team exists in the buildkite API | ||
testAccCheckTeamExists("buildkite_team.test", &resourceTeam), | ||
// Confirm the team has the updated values in Buildkite's system | ||
testAccCheckTeamRemoteValues(&resourceTeam, "wombats"), | ||
// Confirm the team has the updated values in terraform state | ||
resource.TestCheckResourceAttr("buildkite_team.test", "name", "wombats"), | ||
resource.TestCheckResourceAttr("buildkite_team.test", "name", "wombats"), | ||
), | ||
}, | ||
{ | ||
Config: testAccTeamConfigSecret("wombats"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Confirm the team exists in the buildkite API | ||
testAccCheckTeamExists("buildkite_team.test", &resourceTeam), | ||
// Confirm the team has the updated values in Buildkite's system | ||
testAccCheckTeamRemoteValues(&resourceTeam, "wombats"), | ||
// Confirm the team has the updated values in terraform state | ||
resource.TestCheckResourceAttr("buildkite_team.test", "name", "wombats"), | ||
resource.TestCheckResourceAttr("buildkite_team.test", "description", "a secret team of wombats"), | ||
resource.TestCheckResourceAttr("buildkite_team.test", "privacy", "SECRET"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// Confirm that this resource can be imported | ||
func TestAccTeam_import(t *testing.T) { | ||
var resourceTeam TeamNode | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckExampleResourceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTeamConfigBasic("important"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// Confirm the team exists in the buildkite API | ||
testAccCheckTeamExists("buildkite_team.test", &resourceTeam), | ||
// Quick check to confirm the local state is correct before we re-import it | ||
resource.TestCheckResourceAttr("buildkite_team.test", "name", "important"), | ||
), | ||
}, | ||
{ | ||
// re-import the resource (using the graphql token of the existing resource) and confirm they match | ||
ResourceName: "buildkite_team.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckTeamExists(resourceName string, resourceTeam *TeamNode) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
resourceState, ok := s.RootModule().Resources[resourceName] | ||
|
||
if !ok { | ||
return fmt.Errorf("Not found in state: %s", resourceName) | ||
} | ||
|
||
if resourceState.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set in state") | ||
} | ||
|
||
provider := testAccProvider.Meta().(*Client) | ||
var query struct { | ||
Node struct { | ||
Team TeamNode `graphql:"... on Team"` | ||
} `graphql:"node(id: $id)"` | ||
} | ||
|
||
vars := map[string]interface{}{ | ||
"id": resourceState.Primary.ID, | ||
} | ||
|
||
err := provider.graphql.Query(context.Background(), &query, vars) | ||
if err != nil { | ||
return fmt.Errorf("Error fetching team from graphql API: %v", err) | ||
} | ||
|
||
if string(query.Node.Team.ID) == "" { | ||
return fmt.Errorf("No team found with graphql id: %s", resourceState.Primary.ID) | ||
} | ||
|
||
if string(query.Node.Team.Name) != resourceState.Primary.Attributes["name"] { | ||
return fmt.Errorf("Team name in state doesn't match remote name") | ||
} | ||
|
||
*resourceTeam = query.Node.Team | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckTeamRemoteValues(resourceTeam *TeamNode, name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
||
if string(resourceTeam.Name) != name { | ||
return fmt.Errorf("remote team name (%s) doesn't match expected value (%s)", resourceTeam.Name, name) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func testAccTeamConfigBasic(name string) string { | ||
config := ` | ||
resource "buildkite_team" "test" { | ||
name = "%s" | ||
description = "a cool team of %s" | ||
privacy = "VISIBLE" | ||
default_team = true | ||
default_member_role = "MEMBER" | ||
} | ||
` | ||
return fmt.Sprintf(config, name, name) | ||
} | ||
|
||
func testAccTeamConfigSecret(name string) string { | ||
config := ` | ||
resource "buildkite_team" "test" { | ||
name = "%s" | ||
description = "a secret team of %s" | ||
privacy = "SECRET" | ||
default_team = true | ||
default_member_role = "MEMBER" | ||
} | ||
` | ||
return fmt.Sprintf(config, name, name) | ||
} | ||
|
||
// verifies the team has been destroyed | ||
func testAccCheckTeamResourceDestroy(s *terraform.State) error { | ||
// TODO manually check that all resources created during acceptance tests have been cleaned up | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our other resource tests I think we only checked computer properties in this function, and I also note that name is checked in
testAccCheckTeamRemoteValues
. Do we definitely want to check it here too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably excessive, I'll remove it!