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