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

Add acceptance tests for teams #103

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 buildkite/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TeamNode struct {
Description graphql.String
ID graphql.String
IsDefaultTeam graphql.Boolean
DefaultMemberRole graphql.String
Name graphql.String
MembersCanCreatePipelines graphql.Boolean
Privacy TeamPrivacy
Expand Down Expand Up @@ -208,6 +209,7 @@ func updateTeam(d *schema.ResourceData, t *TeamNode) {
d.SetId(string(t.ID))
d.Set("default_team", bool(t.IsDefaultTeam))
d.Set("description", string(t.Description))
d.Set("default_member_role", string(t.DefaultMemberRole))
d.Set("members_can_create_pipelines", bool(t.MembersCanCreatePipelines))
d.Set("name", string(t.Name))
d.Set("privacy", string(t.Privacy))
Expand Down
193 changes: 193 additions & 0 deletions buildkite/resource_team_test.go
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")
}
Copy link
Contributor

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?

Copy link
Contributor Author

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!


*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
}