github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/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/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccGithubIssueLabel_basic(t *testing.T) {
    15  	var label github.Label
    16  
    17  	rString := acctest.RandString(5)
    18  	repoName := fmt.Sprintf("tf-acc-test-branch-issue-label-%s", rString)
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccGithubIssueLabelDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccGithubIssueLabelConfig(repoName),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
    29  					testAccCheckGithubIssueLabelAttributes(&label, "foo", "000000"),
    30  				),
    31  			},
    32  			{
    33  				Config: testAccGithubIssueLabelUpdateConfig(repoName),
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
    36  					testAccCheckGithubIssueLabelAttributes(&label, "bar", "FFFFFF"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccGithubIssueLabel_existingLabel(t *testing.T) {
    44  	var label github.Label
    45  
    46  	rString := acctest.RandString(5)
    47  	repoName := fmt.Sprintf("tf-acc-test-branch-issue-label-%s", rString)
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccGithubIssueLabelDestroy,
    53  		Steps: []resource.TestStep{
    54  			{
    55  				Config: testAccGitHubIssueLabelExistsConfig(repoName),
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
    58  					testAccCheckGithubIssueLabelAttributes(&label, "enhancement", "FF00FF"),
    59  				),
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func TestAccGithubIssueLabel_importBasic(t *testing.T) {
    66  	rString := acctest.RandString(5)
    67  	repoName := fmt.Sprintf("tf-acc-test-branch-issue-label-%s", rString)
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testAccGithubIssueLabelDestroy,
    73  		Steps: []resource.TestStep{
    74  			{
    75  				Config: testAccGithubIssueLabelConfig(repoName),
    76  			},
    77  			{
    78  				ResourceName:      "github_issue_label.test",
    79  				ImportState:       true,
    80  				ImportStateVerify: true,
    81  			},
    82  		},
    83  	})
    84  }
    85  
    86  func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		rs, ok := s.RootModule().Resources[n]
    89  		if !ok {
    90  			return fmt.Errorf("Not Found: %s", n)
    91  		}
    92  
    93  		if rs.Primary.ID == "" {
    94  			return fmt.Errorf("No issue label ID is set")
    95  		}
    96  
    97  		conn := testAccProvider.Meta().(*Organization).client
    98  		o := testAccProvider.Meta().(*Organization).name
    99  		r, n := parseTwoPartID(rs.Primary.ID)
   100  
   101  		githubLabel, _, err := conn.Issues.GetLabel(context.TODO(), o, r, n)
   102  		if err != nil {
   103  			return err
   104  		}
   105  
   106  		*label = *githubLabel
   107  		return nil
   108  	}
   109  }
   110  
   111  func testAccCheckGithubIssueLabelAttributes(label *github.Label, name, color string) resource.TestCheckFunc {
   112  	return func(s *terraform.State) error {
   113  		if *label.Name != name {
   114  			return fmt.Errorf("Issue label name does not match: %s, %s", *label.Name, name)
   115  		}
   116  
   117  		if *label.Color != color {
   118  			return fmt.Errorf("Issue label color does not match: %s, %s", *label.Color, color)
   119  		}
   120  
   121  		return nil
   122  	}
   123  }
   124  
   125  func testAccGithubIssueLabelDestroy(s *terraform.State) error {
   126  	conn := testAccProvider.Meta().(*Organization).client
   127  
   128  	for _, rs := range s.RootModule().Resources {
   129  		if rs.Type != "github_issue_label" {
   130  			continue
   131  		}
   132  
   133  		o := testAccProvider.Meta().(*Organization).name
   134  		r, n := parseTwoPartID(rs.Primary.ID)
   135  		label, res, err := conn.Issues.GetLabel(context.TODO(), o, r, n)
   136  
   137  		if err == nil {
   138  			if label != nil &&
   139  				buildTwoPartID(label.Name, label.Color) == rs.Primary.ID {
   140  				return fmt.Errorf("Issue label still exists")
   141  			}
   142  		}
   143  		if res.StatusCode != 404 {
   144  			return err
   145  		}
   146  		return nil
   147  	}
   148  	return nil
   149  }
   150  
   151  func testAccGithubIssueLabelConfig(repoName string) string {
   152  	return fmt.Sprintf(`
   153  resource "github_repository" "test" {
   154    name = "%s"
   155  }
   156  
   157  resource "github_issue_label" "test" {
   158    repository = "${github_repository.test.name}"
   159    name       = "foo"
   160    color      = "000000"
   161  }
   162  `, repoName)
   163  }
   164  
   165  func testAccGithubIssueLabelUpdateConfig(repoName string) string {
   166  	return fmt.Sprintf(`
   167  resource "github_repository" "test" {
   168    name = "%s"
   169  }
   170  
   171  resource "github_issue_label" "test" {
   172    repository = "${github_repository.test.name}"
   173    name       = "bar"
   174    color      = "FFFFFF"
   175  }
   176  `, repoName)
   177  }
   178  
   179  func testAccGitHubIssueLabelExistsConfig(repoName string) string {
   180  	return fmt.Sprintf(`
   181  // Create a repository which has the default labels
   182  resource "github_repository" "test" {
   183    name = "%s"
   184  }
   185  
   186  resource "github_issue_label" "test" {
   187    repository = "${github_repository.test.name}"
   188    name       = "enhancement" // Important! This is a pre-created label
   189    color      = "FF00FF"
   190  }
   191  `, repoName)
   192  }