github.com/mkuzmin/terraform@v0.3.7-0.20161118171027-ec4c00ff92a9/builtin/providers/github/resource_github_issue_label_test.go (about)

     1  package github
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/google/go-github/github"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccGithubIssueLabel_basic(t *testing.T) {
    13  	var label github.Label
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccGithubIssueLabelDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccGithubIssueLabelConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
    24  					testAccCheckGithubIssueLabelAttributes(&label, "foo", "000000"),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccGithubIssueLabelUpdateConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
    31  					testAccCheckGithubIssueLabelAttributes(&label, "bar", "FFFFFF"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		rs, ok := s.RootModule().Resources[n]
    41  		if !ok {
    42  			return fmt.Errorf("Not Found: %s", n)
    43  		}
    44  
    45  		if rs.Primary.ID == "" {
    46  			return fmt.Errorf("No issue label ID is set")
    47  		}
    48  
    49  		conn := testAccProvider.Meta().(*Organization).client
    50  		o := testAccProvider.Meta().(*Organization).name
    51  		r, n := parseTwoPartID(rs.Primary.ID)
    52  
    53  		githubLabel, _, err := conn.Issues.GetLabel(o, r, n)
    54  		if err != nil {
    55  			return err
    56  		}
    57  
    58  		*label = *githubLabel
    59  		return nil
    60  	}
    61  }
    62  
    63  func testAccCheckGithubIssueLabelAttributes(label *github.Label, name, color string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		if *label.Name != name {
    66  			return fmt.Errorf("Issue label name does not match: %s, %s", *label.Name, name)
    67  		}
    68  
    69  		if *label.Color != color {
    70  			return fmt.Errorf("Issue label color does not match: %s, %s", *label.Color, color)
    71  		}
    72  
    73  		return nil
    74  	}
    75  }
    76  
    77  func testAccGithubIssueLabelDestroy(s *terraform.State) error {
    78  	conn := testAccProvider.Meta().(*Organization).client
    79  
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "github_issue_label" {
    82  			continue
    83  		}
    84  
    85  		o := testAccProvider.Meta().(*Organization).name
    86  		r, n := parseTwoPartID(rs.Primary.ID)
    87  		label, res, err := conn.Issues.GetLabel(o, r, n)
    88  
    89  		if err == nil {
    90  			if label != nil &&
    91  				buildTwoPartID(label.Name, label.Color) == rs.Primary.ID {
    92  				return fmt.Errorf("Issue label still exists")
    93  			}
    94  		}
    95  		if res.StatusCode != 404 {
    96  			return err
    97  		}
    98  		return nil
    99  	}
   100  	return nil
   101  }
   102  
   103  var testAccGithubIssueLabelConfig string = fmt.Sprintf(`
   104  resource "github_repository" "foo" {
   105    name = "%s"
   106    description = "Terraform acceptance tests!"
   107    homepage_url = "http://example.com/"
   108  
   109    # So that acceptance tests can be run in a github organization
   110    # with no billing
   111    private = false
   112  
   113    has_issues = false
   114    has_wiki = false
   115    has_downloads = false
   116  }
   117  resource "github_issue_label" "test" {
   118    repository = "${github_repository.foo.name}"
   119    name       = "foo"
   120    color      = "000000"
   121  }
   122  `, testRepo)
   123  
   124  var testAccGithubIssueLabelUpdateConfig string = fmt.Sprintf(`
   125  resource "github_repository" "foo" {
   126    name = "%s"
   127    description = "Terraform acceptance tests!"
   128    homepage_url = "http://example.com/"
   129  
   130    # So that acceptance tests can be run in a github organization
   131    # with no billing
   132    private = false
   133  
   134    has_issues = false
   135    has_wiki = false
   136    has_downloads = false
   137  }
   138  resource "github_issue_label" "test" {
   139    repository = "${github_repository.foo.name}"
   140    name       = "bar"
   141    color      = "FFFFFF"
   142  }
   143  `, testRepo)