package github import ( "fmt" "testing" "github.com/google/go-github/github" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccGithubIssueLabel_basic(t *testing.T) { var label github.Label resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccGithubIssueLabelDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccGithubIssueLabelConfig, Check: resource.ComposeTestCheckFunc( testAccCheckGithubIssueLabelExists("github_issue_label.test", &label), testAccCheckGithubIssueLabelAttributes(&label, "foo", "000000"), ), }, resource.TestStep{ Config: testAccGithubIssueLabelUpdateConfig, Check: resource.ComposeTestCheckFunc( testAccCheckGithubIssueLabelExists("github_issue_label.test", &label), testAccCheckGithubIssueLabelAttributes(&label, "bar", "FFFFFF"), ), }, }, }) } func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not Found: %s", n) } if rs.Primary.ID == "" { return fmt.Errorf("No issue label ID is set") } conn := testAccProvider.Meta().(*Organization).client o := testAccProvider.Meta().(*Organization).name r, n := parseTwoPartID(rs.Primary.ID) githubLabel, _, err := conn.Issues.GetLabel(o, r, n) if err != nil { return err } *label = *githubLabel return nil } } func testAccCheckGithubIssueLabelAttributes(label *github.Label, name, color string) resource.TestCheckFunc { return func(s *terraform.State) error { if *label.Name != name { return fmt.Errorf("Issue label name does not match: %s, %s", *label.Name, name) } if *label.Color != color { return fmt.Errorf("Issue label color does not match: %s, %s", *label.Color, color) } return nil } } func testAccGithubIssueLabelDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*Organization).client for _, rs := range s.RootModule().Resources { if rs.Type != "github_issue_label" { continue } o := testAccProvider.Meta().(*Organization).name r, n := parseTwoPartID(rs.Primary.ID) label, res, err := conn.Issues.GetLabel(o, r, n) if err == nil { if label != nil && buildTwoPartID(label.Name, label.Color) == rs.Primary.ID { return fmt.Errorf("Issue label still exists") } } if res.StatusCode != 404 { return err } return nil } return nil } var testAccGithubIssueLabelConfig string = fmt.Sprintf(` resource "github_repository" "foo" { name = "%s" description = "Terraform acceptance tests!" homepage_url = "http://example.com/" # So that acceptance tests can be run in a github organization # with no billing private = false has_issues = false has_wiki = false has_downloads = false } resource "github_issue_label" "test" { repository = "${github_repository.foo.name}" name = "foo" color = "000000" } `, testRepo) var testAccGithubIssueLabelUpdateConfig string = fmt.Sprintf(` resource "github_repository" "foo" { name = "%s" description = "Terraform acceptance tests!" homepage_url = "http://example.com/" # So that acceptance tests can be run in a github organization # with no billing private = false has_issues = false has_wiki = false has_downloads = false } resource "github_issue_label" "test" { repository = "${github_repository.foo.name}" name = "bar" color = "FFFFFF" } `, testRepo)