github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 TestAccGithubIssueLabel_importBasic(t *testing.T) {
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccGithubIssueLabelDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccGithubIssueLabelConfig,
    46  			},
    47  			resource.TestStep{
    48  				ResourceName:      "github_issue_label.test",
    49  				ImportState:       true,
    50  				ImportStateVerify: true,
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[n]
    59  		if !ok {
    60  			return fmt.Errorf("Not Found: %s", n)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No issue label ID is set")
    65  		}
    66  
    67  		conn := testAccProvider.Meta().(*Organization).client
    68  		o := testAccProvider.Meta().(*Organization).name
    69  		r, n := parseTwoPartID(rs.Primary.ID)
    70  
    71  		githubLabel, _, err := conn.Issues.GetLabel(o, r, n)
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		*label = *githubLabel
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccCheckGithubIssueLabelAttributes(label *github.Label, name, color string) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		if *label.Name != name {
    84  			return fmt.Errorf("Issue label name does not match: %s, %s", *label.Name, name)
    85  		}
    86  
    87  		if *label.Color != color {
    88  			return fmt.Errorf("Issue label color does not match: %s, %s", *label.Color, color)
    89  		}
    90  
    91  		return nil
    92  	}
    93  }
    94  
    95  func testAccGithubIssueLabelDestroy(s *terraform.State) error {
    96  	conn := testAccProvider.Meta().(*Organization).client
    97  
    98  	for _, rs := range s.RootModule().Resources {
    99  		if rs.Type != "github_issue_label" {
   100  			continue
   101  		}
   102  
   103  		o := testAccProvider.Meta().(*Organization).name
   104  		r, n := parseTwoPartID(rs.Primary.ID)
   105  		label, res, err := conn.Issues.GetLabel(o, r, n)
   106  
   107  		if err == nil {
   108  			if label != nil &&
   109  				buildTwoPartID(label.Name, label.Color) == rs.Primary.ID {
   110  				return fmt.Errorf("Issue label still exists")
   111  			}
   112  		}
   113  		if res.StatusCode != 404 {
   114  			return err
   115  		}
   116  		return nil
   117  	}
   118  	return nil
   119  }
   120  
   121  var testAccGithubIssueLabelConfig string = fmt.Sprintf(`
   122  resource "github_issue_label" "test" {
   123    repository = "%s"
   124    name       = "foo"
   125    color      = "000000"
   126  }
   127  `, testRepo)
   128  
   129  var testAccGithubIssueLabelUpdateConfig string = fmt.Sprintf(`
   130  resource "github_issue_label" "test" {
   131    repository = "%s"
   132    name       = "bar"
   133    color      = "FFFFFF"
   134  }
   135  `, testRepo)