github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/rabbitmq/resource_exchange_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 TestAccExchange(t *testing.T) {
    15  	var exchangeInfo rabbithole.ExchangeInfo
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccExchangeCheckDestroy(&exchangeInfo),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccExchangeConfig_basic,
    23  				Check: testAccExchangeCheck(
    24  					"rabbitmq_exchange.test", &exchangeInfo,
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccExchangeCheck(rn string, exchangeInfo *rabbithole.ExchangeInfo) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  		rs, ok := s.RootModule().Resources[rn]
    34  		if !ok {
    35  			return fmt.Errorf("resource not found: %s", rn)
    36  		}
    37  
    38  		if rs.Primary.ID == "" {
    39  			return fmt.Errorf("exchange id not set")
    40  		}
    41  
    42  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    43  		exchParts := strings.Split(rs.Primary.ID, "@")
    44  
    45  		exchanges, err := rmqc.ListExchangesIn(exchParts[1])
    46  		if err != nil {
    47  			return fmt.Errorf("Error retrieving exchange: %s", err)
    48  		}
    49  
    50  		for _, exchange := range exchanges {
    51  			if exchange.Name == exchParts[0] && exchange.Vhost == exchParts[1] {
    52  				exchangeInfo = &exchange
    53  				return nil
    54  			}
    55  		}
    56  
    57  		return fmt.Errorf("Unable to find exchange %s", rn)
    58  	}
    59  }
    60  
    61  func testAccExchangeCheckDestroy(exchangeInfo *rabbithole.ExchangeInfo) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    64  
    65  		exchanges, err := rmqc.ListExchangesIn(exchangeInfo.Vhost)
    66  		if err != nil {
    67  			return fmt.Errorf("Error retrieving exchange: %s", err)
    68  		}
    69  
    70  		for _, exchange := range exchanges {
    71  			if exchange.Name == exchangeInfo.Name && exchange.Vhost == exchangeInfo.Vhost {
    72  				return fmt.Errorf("Exchange %s@%s still exist", exchangeInfo.Name, exchangeInfo.Vhost)
    73  			}
    74  		}
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  const testAccExchangeConfig_basic = `
    81  resource "rabbitmq_vhost" "test" {
    82      name = "test"
    83  }
    84  
    85  resource "rabbitmq_permissions" "guest" {
    86      user = "guest"
    87      vhost = "${rabbitmq_vhost.test.name}"
    88      permissions {
    89          configure = ".*"
    90          write = ".*"
    91          read = ".*"
    92      }
    93  }
    94  
    95  resource "rabbitmq_exchange" "test" {
    96      name = "test"
    97      vhost = "${rabbitmq_permissions.guest.vhost}"
    98      settings {
    99          type = "fanout"
   100          durable = false
   101          auto_delete = true
   102      }
   103  }`