github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/opsgenie/resource_opsgenie_team_test.go (about)

     1  package opsgenie
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/opsgenie/opsgenie-go-sdk/team"
    11  )
    12  
    13  func TestAccOpsGenieTeamName_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "hello-world",
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    "hello_world",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "helloWorld",
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    "helloworld12",
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    "hello@world",
    36  			ErrCount: 1,
    37  		},
    38  		{
    39  			Value:    "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd3324120",
    40  			ErrCount: 0,
    41  		},
    42  		{
    43  			Value:    "qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd33241202qfvbdsbvipqdbwsbddbdcwqffewsqwcdw21ddwqwd33241202",
    44  			ErrCount: 0,
    45  		},
    46  		{
    47  			Value:    "qfvbdsbvipqdbwsbddbdcwqfjjfewsqwcdw21ddwqwd3324120qfvbdsbvipqdbwsbddbdcwqfjjfewsqwcdw21ddwqwd3324120",
    48  			ErrCount: 1,
    49  		},
    50  	}
    51  
    52  	for _, tc := range cases {
    53  		_, errors := validateOpsGenieTeamName(tc.Value, "opsgenie_team")
    54  
    55  		if len(errors) != tc.ErrCount {
    56  			t.Fatalf("Expected the OpsGenie Team Name to trigger a validation error: %v", errors)
    57  		}
    58  	}
    59  }
    60  
    61  func TestAccOpsGenieTeamRole_validation(t *testing.T) {
    62  	cases := []struct {
    63  		Value    string
    64  		ErrCount int
    65  	}{
    66  		{
    67  			Value:    "admin",
    68  			ErrCount: 0,
    69  		},
    70  		{
    71  			Value:    "user",
    72  			ErrCount: 0,
    73  		},
    74  		{
    75  			Value:    "custom",
    76  			ErrCount: 1,
    77  		},
    78  	}
    79  
    80  	for _, tc := range cases {
    81  		_, errors := validateOpsGenieTeamRole(tc.Value, "opsgenie_team")
    82  
    83  		if len(errors) != tc.ErrCount {
    84  			t.Fatalf("Expected the OpsGenie Team Role to trigger a validation error")
    85  		}
    86  	}
    87  }
    88  
    89  func TestAccOpsGenieTeam_basic(t *testing.T) {
    90  	ri := acctest.RandInt()
    91  	config := fmt.Sprintf(testAccOpsGenieTeam_basic, ri)
    92  
    93  	resource.Test(t, resource.TestCase{
    94  		PreCheck:     func() { testAccPreCheck(t) },
    95  		Providers:    testAccProviders,
    96  		CheckDestroy: testCheckOpsGenieTeamDestroy,
    97  		Steps: []resource.TestStep{
    98  			{
    99  				Config: config,
   100  				Check: resource.ComposeTestCheckFunc(
   101  					testCheckOpsGenieTeamExists("opsgenie_team.test"),
   102  				),
   103  			},
   104  		},
   105  	})
   106  }
   107  
   108  func TestAccOpsGenieTeam_withUser(t *testing.T) {
   109  	ri := acctest.RandInt()
   110  	config := fmt.Sprintf(testAccOpsGenieTeam_withUser, ri, ri)
   111  
   112  	resource.Test(t, resource.TestCase{
   113  		PreCheck:     func() { testAccPreCheck(t) },
   114  		Providers:    testAccProviders,
   115  		CheckDestroy: testCheckOpsGenieTeamDestroy,
   116  		Steps: []resource.TestStep{
   117  			{
   118  				Config: config,
   119  				Check: resource.ComposeTestCheckFunc(
   120  					testCheckOpsGenieTeamExists("opsgenie_team.test"),
   121  				),
   122  			},
   123  		},
   124  	})
   125  }
   126  
   127  func TestAccOpsGenieTeam_withUserComplete(t *testing.T) {
   128  	ri := acctest.RandInt()
   129  	config := fmt.Sprintf(testAccOpsGenieTeam_withUserComplete, ri, ri)
   130  
   131  	resource.Test(t, resource.TestCase{
   132  		PreCheck:     func() { testAccPreCheck(t) },
   133  		Providers:    testAccProviders,
   134  		CheckDestroy: testCheckOpsGenieTeamDestroy,
   135  		Steps: []resource.TestStep{
   136  			{
   137  				Config: config,
   138  				Check: resource.ComposeTestCheckFunc(
   139  					testCheckOpsGenieTeamExists("opsgenie_team.test"),
   140  				),
   141  			},
   142  		},
   143  	})
   144  }
   145  
   146  func TestAccOpsGenieTeam_withMultipleUsers(t *testing.T) {
   147  	ri := acctest.RandInt()
   148  	config := fmt.Sprintf(testAccOpsGenieTeam_withMultipleUsers, ri, ri, ri)
   149  
   150  	resource.Test(t, resource.TestCase{
   151  		PreCheck:     func() { testAccPreCheck(t) },
   152  		Providers:    testAccProviders,
   153  		CheckDestroy: testCheckOpsGenieTeamDestroy,
   154  		Steps: []resource.TestStep{
   155  			{
   156  				Config: config,
   157  				Check: resource.ComposeTestCheckFunc(
   158  					testCheckOpsGenieTeamExists("opsgenie_team.test"),
   159  				),
   160  			},
   161  		},
   162  	})
   163  }
   164  
   165  func testCheckOpsGenieTeamDestroy(s *terraform.State) error {
   166  	client := testAccProvider.Meta().(*OpsGenieClient).teams
   167  
   168  	for _, rs := range s.RootModule().Resources {
   169  		if rs.Type != "opsgenie_team" {
   170  			continue
   171  		}
   172  
   173  		req := team.GetTeamRequest{
   174  			Id: rs.Primary.Attributes["id"],
   175  		}
   176  
   177  		result, _ := client.Get(req)
   178  		if result != nil {
   179  			return fmt.Errorf("Team still exists:\n%#v", result)
   180  		}
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func testCheckOpsGenieTeamExists(name string) resource.TestCheckFunc {
   187  	return func(s *terraform.State) error {
   188  		// Ensure we have enough information in state to look up in API
   189  		rs, ok := s.RootModule().Resources[name]
   190  		if !ok {
   191  			return fmt.Errorf("Not found: %s", name)
   192  		}
   193  
   194  		id := rs.Primary.Attributes["id"]
   195  		name := rs.Primary.Attributes["name"]
   196  
   197  		client := testAccProvider.Meta().(*OpsGenieClient).teams
   198  
   199  		req := team.GetTeamRequest{
   200  			Id: rs.Primary.Attributes["id"],
   201  		}
   202  
   203  		result, _ := client.Get(req)
   204  		if result == nil {
   205  			return fmt.Errorf("Bad: Team %q (name: %q) does not exist", id, name)
   206  		}
   207  
   208  		return nil
   209  	}
   210  }
   211  
   212  var testAccOpsGenieTeam_basic = `
   213  resource "opsgenie_team" "test" {
   214    name = "acctest%d"
   215  }
   216  `
   217  
   218  var testAccOpsGenieTeam_withUser = `
   219  resource "opsgenie_user" "test" {
   220    username  = "acctest-%d@example.tld"
   221    full_name = "Acceptance Test User"
   222    role      = "User"
   223  }
   224  
   225  resource "opsgenie_team" "test" {
   226    name  = "acctest%d"
   227    member {
   228      username = "${opsgenie_user.test.username}"
   229    }
   230  }
   231  `
   232  
   233  var testAccOpsGenieTeam_withUserComplete = `
   234  resource "opsgenie_user" "test" {
   235    username  = "acctest-%d@example.tld"
   236    full_name = "Acceptance Test User"
   237    role      = "User"
   238  }
   239  
   240  resource "opsgenie_team" "test" {
   241    name  = "acctest%d"
   242    member {
   243      username = "${opsgenie_user.test.username}"
   244      role     = "user"
   245    }
   246  }
   247  `
   248  
   249  var testAccOpsGenieTeam_withMultipleUsers = `
   250  resource "opsgenie_user" "first" {
   251    username  = "acctest-1-%d@example.tld"
   252    full_name = "First Acceptance Test User"
   253    role      = "User"
   254  }
   255  resource "opsgenie_user" "second" {
   256    username  = "acctest-2-%d@example.tld"
   257    full_name = "Second Acceptance Test User"
   258    role      = "User"
   259  }
   260  
   261  resource "opsgenie_team" "test" {
   262    name  = "acctest%d"
   263    member {
   264      username = "${opsgenie_user.first.username}"
   265    }
   266    member {
   267      username = "${opsgenie_user.second.username}"
   268    }
   269  }
   270  `