github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/rabbitmq/resource_user_test.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/michaelklishin/rabbit-hole"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccUser_basic(t *testing.T) {
    15  	var user string
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccUserCheckDestroy(user),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccUserConfig_basic,
    23  				Check: testAccUserCheck(
    24  					"rabbitmq_user.test", &user,
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccUserConfig_update,
    29  				Check: testAccUserCheck(
    30  					"rabbitmq_user.test", &user,
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccUser_emptyTag(t *testing.T) {
    38  	var user string
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccUserCheckDestroy(user),
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccUserConfig_emptyTag_1,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccUserCheck("rabbitmq_user.test", &user),
    48  					testAccUserCheckTagCount(&user, 0),
    49  				),
    50  			},
    51  			resource.TestStep{
    52  				Config: testAccUserConfig_emptyTag_2,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccUserCheck("rabbitmq_user.test", &user),
    55  					testAccUserCheckTagCount(&user, 1),
    56  				),
    57  			},
    58  			resource.TestStep{
    59  				Config: testAccUserConfig_emptyTag_1,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccUserCheck("rabbitmq_user.test", &user),
    62  					testAccUserCheckTagCount(&user, 0),
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func TestAccUser_noTags(t *testing.T) {
    70  	var user string
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccUserCheckDestroy(user),
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccUserConfig_noTags_1,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccUserCheck("rabbitmq_user.test", &user),
    80  					testAccUserCheckTagCount(&user, 0),
    81  				),
    82  			},
    83  			resource.TestStep{
    84  				Config: testAccUserConfig_noTags_2,
    85  				Check: resource.ComposeTestCheckFunc(
    86  					testAccUserCheck("rabbitmq_user.test", &user),
    87  					testAccUserCheckTagCount(&user, 1),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testAccUserCheck(rn string, name *string) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  		rs, ok := s.RootModule().Resources[rn]
    97  		if !ok {
    98  			return fmt.Errorf("resource not found: %s", rn)
    99  		}
   100  
   101  		if rs.Primary.ID == "" {
   102  			return fmt.Errorf("user id not set")
   103  		}
   104  
   105  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
   106  		users, err := rmqc.ListUsers()
   107  		if err != nil {
   108  			return fmt.Errorf("Error retrieving users: %s", err)
   109  		}
   110  
   111  		for _, user := range users {
   112  			if user.Name == rs.Primary.ID {
   113  				*name = rs.Primary.ID
   114  				return nil
   115  			}
   116  		}
   117  
   118  		return fmt.Errorf("Unable to find user %s", rn)
   119  	}
   120  }
   121  
   122  func testAccUserCheckTagCount(name *string, tagCount int) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
   125  		user, err := rmqc.GetUser(*name)
   126  		if err != nil {
   127  			return fmt.Errorf("Error retrieving user: %s", err)
   128  		}
   129  
   130  		var tagList []string
   131  		for _, v := range strings.Split(user.Tags, ",") {
   132  			if v != "" {
   133  				tagList = append(tagList, v)
   134  			}
   135  		}
   136  
   137  		if len(tagList) != tagCount {
   138  			return fmt.Errorf("Expected %d tags, user has %d", tagCount, len(tagList))
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  func testAccUserCheckDestroy(name string) resource.TestCheckFunc {
   146  	return func(s *terraform.State) error {
   147  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
   148  		users, err := rmqc.ListUsers()
   149  		if err != nil {
   150  			return fmt.Errorf("Error retrieving users: %s", err)
   151  		}
   152  
   153  		for _, user := range users {
   154  			if user.Name == name {
   155  				return fmt.Errorf("user still exists: %s", name)
   156  			}
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  const testAccUserConfig_basic = `
   164  resource "rabbitmq_user" "test" {
   165      name = "mctest"
   166      password = "foobar"
   167      tags = ["administrator", "management"]
   168  }`
   169  
   170  const testAccUserConfig_update = `
   171  resource "rabbitmq_user" "test" {
   172      name = "mctest"
   173      password = "foobarry"
   174      tags = ["management"]
   175  }`
   176  
   177  const testAccUserConfig_emptyTag_1 = `
   178  resource "rabbitmq_user" "test" {
   179      name = "mctest"
   180      password = "foobar"
   181      tags = [""]
   182  }`
   183  
   184  const testAccUserConfig_emptyTag_2 = `
   185  resource "rabbitmq_user" "test" {
   186      name = "mctest"
   187      password = "foobar"
   188      tags = ["administrator"]
   189  }`
   190  
   191  const testAccUserConfig_noTags_1 = `
   192  resource "rabbitmq_user" "test" {
   193      name = "mctest"
   194      password = "foobar"
   195  }`
   196  
   197  const testAccUserConfig_noTags_2 = `
   198  resource "rabbitmq_user" "test" {
   199      name = "mctest"
   200      password = "foobar"
   201      tags = ["administrator"]
   202  }`