github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rabbitmq/resource_user_test.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/michaelklishin/rabbit-hole"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccUser(t *testing.T) {
    14  	var user string
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccUserCheckDestroy(user),
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccUserConfig_basic,
    22  				Check: testAccUserCheck(
    23  					"rabbitmq_user.test", &user,
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: testAccUserConfig_update,
    28  				Check: testAccUserCheck(
    29  					"rabbitmq_user.test", &user,
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccUserCheck(rn string, name *string) resource.TestCheckFunc {
    37  	return func(s *terraform.State) error {
    38  		rs, ok := s.RootModule().Resources[rn]
    39  		if !ok {
    40  			return fmt.Errorf("resource not found: %s", rn)
    41  		}
    42  
    43  		if rs.Primary.ID == "" {
    44  			return fmt.Errorf("user id not set")
    45  		}
    46  
    47  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    48  		users, err := rmqc.ListUsers()
    49  		if err != nil {
    50  			return fmt.Errorf("Error retrieving users: %s", err)
    51  		}
    52  
    53  		for _, user := range users {
    54  			if user.Name == rs.Primary.ID {
    55  				*name = rs.Primary.ID
    56  				return nil
    57  			}
    58  		}
    59  
    60  		return fmt.Errorf("Unable to find user %s", rn)
    61  	}
    62  }
    63  
    64  func testAccUserCheckDestroy(name string) resource.TestCheckFunc {
    65  	return func(s *terraform.State) error {
    66  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    67  		users, err := rmqc.ListUsers()
    68  		if err != nil {
    69  			return fmt.Errorf("Error retrieving users: %s", err)
    70  		}
    71  
    72  		for _, user := range users {
    73  			if user.Name == name {
    74  				return fmt.Errorf("user still exists: %s", name)
    75  			}
    76  		}
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  const testAccUserConfig_basic = `
    83  resource "rabbitmq_user" "test" {
    84      name = "mctest"
    85      password = "foobar"
    86      tags = ["administrator", "management"]
    87  }`
    88  
    89  const testAccUserConfig_update = `
    90  resource "rabbitmq_user" "test" {
    91      name = "mctest"
    92      password = "foobarry"
    93      tags = ["management"]
    94  }`