github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/resource_github_issue_label_test.go (about) 1 package github 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/google/go-github/github" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccGithubIssueLabel_basic(t *testing.T) { 14 var label github.Label 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccGithubIssueLabelDestroy, 20 Steps: []resource.TestStep{ 21 { 22 Config: testAccGithubIssueLabelConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckGithubIssueLabelExists("github_issue_label.test", &label), 25 testAccCheckGithubIssueLabelAttributes(&label, "foo", "000000"), 26 ), 27 }, 28 { 29 Config: testAccGithubIssueLabelUpdateConfig, 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckGithubIssueLabelExists("github_issue_label.test", &label), 32 testAccCheckGithubIssueLabelAttributes(&label, "bar", "FFFFFF"), 33 ), 34 }, 35 { 36 Config: testAccGitHubIssueLabelExistsConfig, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckGithubIssueLabelExists("github_issue_label.test", &label), 39 testAccCheckGithubIssueLabelAttributes(&label, "enhancement", "FF00FF"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func TestAccGithubIssueLabel_importBasic(t *testing.T) { 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testAccGithubIssueLabelDestroy, 51 Steps: []resource.TestStep{ 52 { 53 Config: testAccGithubIssueLabelConfig, 54 }, 55 { 56 ResourceName: "github_issue_label.test", 57 ImportState: true, 58 ImportStateVerify: true, 59 }, 60 }, 61 }) 62 } 63 64 func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 rs, ok := s.RootModule().Resources[n] 67 if !ok { 68 return fmt.Errorf("Not Found: %s", n) 69 } 70 71 if rs.Primary.ID == "" { 72 return fmt.Errorf("No issue label ID is set") 73 } 74 75 conn := testAccProvider.Meta().(*Organization).client 76 o := testAccProvider.Meta().(*Organization).name 77 r, n := parseTwoPartID(rs.Primary.ID) 78 79 githubLabel, _, err := conn.Issues.GetLabel(context.TODO(), o, r, n) 80 if err != nil { 81 return err 82 } 83 84 *label = *githubLabel 85 return nil 86 } 87 } 88 89 func testAccCheckGithubIssueLabelAttributes(label *github.Label, name, color string) resource.TestCheckFunc { 90 return func(s *terraform.State) error { 91 if *label.Name != name { 92 return fmt.Errorf("Issue label name does not match: %s, %s", *label.Name, name) 93 } 94 95 if *label.Color != color { 96 return fmt.Errorf("Issue label color does not match: %s, %s", *label.Color, color) 97 } 98 99 return nil 100 } 101 } 102 103 func testAccGithubIssueLabelDestroy(s *terraform.State) error { 104 conn := testAccProvider.Meta().(*Organization).client 105 106 for _, rs := range s.RootModule().Resources { 107 if rs.Type != "github_issue_label" { 108 continue 109 } 110 111 o := testAccProvider.Meta().(*Organization).name 112 r, n := parseTwoPartID(rs.Primary.ID) 113 label, res, err := conn.Issues.GetLabel(context.TODO(), o, r, n) 114 115 if err == nil { 116 if label != nil && 117 buildTwoPartID(label.Name, label.Color) == rs.Primary.ID { 118 return fmt.Errorf("Issue label still exists") 119 } 120 } 121 if res.StatusCode != 404 { 122 return err 123 } 124 return nil 125 } 126 return nil 127 } 128 129 var testAccGithubIssueLabelConfig string = fmt.Sprintf(` 130 resource "github_issue_label" "test" { 131 repository = "%s" 132 name = "foo" 133 color = "000000" 134 } 135 `, testRepo) 136 137 var testAccGithubIssueLabelUpdateConfig string = fmt.Sprintf(` 138 resource "github_issue_label" "test" { 139 repository = "%s" 140 name = "bar" 141 color = "FFFFFF" 142 } 143 `, testRepo) 144 145 var testAccGitHubIssueLabelExistsConfig string = fmt.Sprintf(` 146 // Create a repository which has the default labels 147 resource "github_repository" "test" { 148 name = "tf-acc-repo-label-abc1234" 149 } 150 151 resource "github_issue_label" "test" { 152 repository = "${github_repository.test.name}" 153 name = "enhancement" // Important! This is a pre-created label 154 color = "FF00FF" 155 } 156 `)