github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opsgenie/resource_opsgenie_user_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/user"
    11  )
    12  
    13  func TestAccOpsGenieUserUsername_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "hello",
    20  			ErrCount: 0,
    21  		},
    22  		{
    23  			Value:    acctest.RandString(99),
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    acctest.RandString(100),
    28  			ErrCount: 1,
    29  		},
    30  	}
    31  
    32  	for _, tc := range cases {
    33  		_, errors := validateOpsGenieUserUsername(tc.Value, "opsgenie_team")
    34  
    35  		if len(errors) != tc.ErrCount {
    36  			t.Fatalf("Expected the OpsGenie User Username Validation to trigger a validation error: %v", errors)
    37  		}
    38  	}
    39  }
    40  
    41  func TestAccOpsGenieUserFullName_validation(t *testing.T) {
    42  	cases := []struct {
    43  		Value    string
    44  		ErrCount int
    45  	}{
    46  		{
    47  			Value:    "hello",
    48  			ErrCount: 0,
    49  		},
    50  		{
    51  			Value:    acctest.RandString(100),
    52  			ErrCount: 0,
    53  		},
    54  		{
    55  			Value:    acctest.RandString(511),
    56  			ErrCount: 0,
    57  		},
    58  		{
    59  			Value:    acctest.RandString(512),
    60  			ErrCount: 1,
    61  		},
    62  	}
    63  
    64  	for _, tc := range cases {
    65  		_, errors := validateOpsGenieUserFullName(tc.Value, "opsgenie_team")
    66  
    67  		if len(errors) != tc.ErrCount {
    68  			t.Fatalf("Expected the OpsGenie User Full Name Validation to trigger a validation error: %v", errors)
    69  		}
    70  	}
    71  }
    72  
    73  func TestAccOpsGenieUserRole_validation(t *testing.T) {
    74  	cases := []struct {
    75  		Value    string
    76  		ErrCount int
    77  	}{
    78  		{
    79  			Value:    "hello",
    80  			ErrCount: 0,
    81  		},
    82  		{
    83  			Value:    acctest.RandString(100),
    84  			ErrCount: 0,
    85  		},
    86  		{
    87  			Value:    acctest.RandString(511),
    88  			ErrCount: 0,
    89  		},
    90  		{
    91  			Value:    acctest.RandString(512),
    92  			ErrCount: 1,
    93  		},
    94  	}
    95  
    96  	for _, tc := range cases {
    97  		_, errors := validateOpsGenieUserRole(tc.Value, "opsgenie_team")
    98  
    99  		if len(errors) != tc.ErrCount {
   100  			t.Fatalf("Expected the OpsGenie User Role Validation to trigger a validation error: %v", errors)
   101  		}
   102  	}
   103  }
   104  
   105  func TestAccOpsGenieUser_basic(t *testing.T) {
   106  	ri := acctest.RandInt()
   107  	config := fmt.Sprintf(testAccOpsGenieUser_basic, ri)
   108  
   109  	resource.Test(t, resource.TestCase{
   110  		PreCheck:     func() { testAccPreCheck(t) },
   111  		Providers:    testAccProviders,
   112  		CheckDestroy: testCheckOpsGenieUserDestroy,
   113  		Steps: []resource.TestStep{
   114  			{
   115  				Config: config,
   116  				Check: resource.ComposeTestCheckFunc(
   117  					testCheckOpsGenieUserExists("opsgenie_user.test"),
   118  				),
   119  			},
   120  		},
   121  	})
   122  }
   123  
   124  func TestAccOpsGenieUser_complete(t *testing.T) {
   125  	ri := acctest.RandInt()
   126  	config := fmt.Sprintf(testAccOpsGenieUser_complete, ri)
   127  
   128  	resource.Test(t, resource.TestCase{
   129  		PreCheck:     func() { testAccPreCheck(t) },
   130  		Providers:    testAccProviders,
   131  		CheckDestroy: testCheckOpsGenieUserDestroy,
   132  		Steps: []resource.TestStep{
   133  			{
   134  				Config: config,
   135  				Check: resource.ComposeTestCheckFunc(
   136  					testCheckOpsGenieUserExists("opsgenie_user.test"),
   137  				),
   138  			},
   139  		},
   140  	})
   141  }
   142  
   143  func testCheckOpsGenieUserDestroy(s *terraform.State) error {
   144  	client := testAccProvider.Meta().(*OpsGenieClient).users
   145  
   146  	for _, rs := range s.RootModule().Resources {
   147  		if rs.Type != "opsgenie_user" {
   148  			continue
   149  		}
   150  
   151  		req := user.GetUserRequest{
   152  			Id: rs.Primary.Attributes["id"],
   153  		}
   154  
   155  		result, _ := client.Get(req)
   156  		if result != nil {
   157  			return fmt.Errorf("User still exists:\n%#v", result)
   158  		}
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  func testCheckOpsGenieUserExists(name string) resource.TestCheckFunc {
   165  	return func(s *terraform.State) error {
   166  		// Ensure we have enough information in state to look up in API
   167  		rs, ok := s.RootModule().Resources[name]
   168  		if !ok {
   169  			return fmt.Errorf("Not found: %s", name)
   170  		}
   171  
   172  		id := rs.Primary.Attributes["id"]
   173  		username := rs.Primary.Attributes["username"]
   174  
   175  		client := testAccProvider.Meta().(*OpsGenieClient).users
   176  
   177  		req := user.GetUserRequest{
   178  			Id: rs.Primary.Attributes["id"],
   179  		}
   180  
   181  		result, _ := client.Get(req)
   182  		if result == nil {
   183  			return fmt.Errorf("Bad: User %q (username: %q) does not exist", id, username)
   184  		}
   185  
   186  		return nil
   187  	}
   188  }
   189  
   190  var testAccOpsGenieUser_basic = `
   191  resource "opsgenie_user" "test" {
   192    username  = "acctest-%d@example.tld"
   193    full_name = "Acceptance Test User"
   194    role      = "User"
   195  }
   196  `
   197  
   198  var testAccOpsGenieUser_complete = `
   199  resource "opsgenie_user" "test" {
   200    username  = "acctest-%d@example.tld"
   201    full_name = "Acceptance Test User"
   202    role      = "User"
   203    locale    = "en_GB"
   204    timezone  = "Etc/GMT"
   205  }
   206  `