github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_withEmptyDescription(t *testing.T) { 109 ri := acctest.RandInt() 110 config := fmt.Sprintf(testAccOpsGenieTeam_withEmptyDescription, 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_withUser(t *testing.T) { 128 ri := acctest.RandInt() 129 config := fmt.Sprintf(testAccOpsGenieTeam_withUser, 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_withUserComplete(t *testing.T) { 147 ri := acctest.RandInt() 148 config := fmt.Sprintf(testAccOpsGenieTeam_withUserComplete, 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 TestAccOpsGenieTeam_withMultipleUsers(t *testing.T) { 166 ri := acctest.RandInt() 167 config := fmt.Sprintf(testAccOpsGenieTeam_withMultipleUsers, ri, ri, ri) 168 169 resource.Test(t, resource.TestCase{ 170 PreCheck: func() { testAccPreCheck(t) }, 171 Providers: testAccProviders, 172 CheckDestroy: testCheckOpsGenieTeamDestroy, 173 Steps: []resource.TestStep{ 174 { 175 Config: config, 176 Check: resource.ComposeTestCheckFunc( 177 testCheckOpsGenieTeamExists("opsgenie_team.test"), 178 ), 179 }, 180 }, 181 }) 182 } 183 184 func testCheckOpsGenieTeamDestroy(s *terraform.State) error { 185 client := testAccProvider.Meta().(*OpsGenieClient).teams 186 187 for _, rs := range s.RootModule().Resources { 188 if rs.Type != "opsgenie_team" { 189 continue 190 } 191 192 req := team.GetTeamRequest{ 193 Id: rs.Primary.Attributes["id"], 194 } 195 196 result, _ := client.Get(req) 197 if result != nil { 198 return fmt.Errorf("Team still exists:\n%#v", result) 199 } 200 } 201 202 return nil 203 } 204 205 func testCheckOpsGenieTeamExists(name string) resource.TestCheckFunc { 206 return func(s *terraform.State) error { 207 // Ensure we have enough information in state to look up in API 208 rs, ok := s.RootModule().Resources[name] 209 if !ok { 210 return fmt.Errorf("Not found: %s", name) 211 } 212 213 id := rs.Primary.Attributes["id"] 214 name := rs.Primary.Attributes["name"] 215 216 client := testAccProvider.Meta().(*OpsGenieClient).teams 217 218 req := team.GetTeamRequest{ 219 Id: rs.Primary.Attributes["id"], 220 } 221 222 result, _ := client.Get(req) 223 if result == nil { 224 return fmt.Errorf("Bad: Team %q (name: %q) does not exist", id, name) 225 } 226 227 return nil 228 } 229 } 230 231 var testAccOpsGenieTeam_basic = ` 232 resource "opsgenie_team" "test" { 233 name = "acctest%d" 234 } 235 ` 236 237 var testAccOpsGenieTeam_withEmptyDescription = ` 238 resource "opsgenie_team" "test" { 239 name = "acctest%d" 240 description = "" 241 } 242 ` 243 244 var testAccOpsGenieTeam_withUser = ` 245 resource "opsgenie_user" "test" { 246 username = "acctest-%d@example.tld" 247 full_name = "Acceptance Test User" 248 role = "User" 249 } 250 251 resource "opsgenie_team" "test" { 252 name = "acctest%d" 253 member { 254 username = "${opsgenie_user.test.username}" 255 } 256 } 257 ` 258 259 var testAccOpsGenieTeam_withUserComplete = ` 260 resource "opsgenie_user" "test" { 261 username = "acctest-%d@example.tld" 262 full_name = "Acceptance Test User" 263 role = "User" 264 } 265 266 resource "opsgenie_team" "test" { 267 name = "acctest%d" 268 description = "Some exmaple description" 269 member { 270 username = "${opsgenie_user.test.username}" 271 role = "user" 272 } 273 } 274 ` 275 276 var testAccOpsGenieTeam_withMultipleUsers = ` 277 resource "opsgenie_user" "first" { 278 username = "acctest-1-%d@example.tld" 279 full_name = "First Acceptance Test User" 280 role = "User" 281 } 282 resource "opsgenie_user" "second" { 283 username = "acctest-2-%d@example.tld" 284 full_name = "Second Acceptance Test User" 285 role = "User" 286 } 287 288 resource "opsgenie_team" "test" { 289 name = "acctest%d" 290 description = "Some exmaple description" 291 member { 292 username = "${opsgenie_user.first.username}" 293 } 294 member { 295 username = "${opsgenie_user.second.username}" 296 } 297 } 298 `