github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rabbitmq/resource_binding_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 TestAccBinding(t *testing.T) {
    15  	var bindingInfo rabbithole.BindingInfo
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccBindingCheckDestroy(bindingInfo),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccBindingConfig_basic,
    23  				Check: testAccBindingCheck(
    24  					"rabbitmq_binding.test", &bindingInfo,
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccBindingCheck(rn string, bindingInfo *rabbithole.BindingInfo) 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("binding id not set")
    40  		}
    41  
    42  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    43  		bindingParts := strings.Split(rs.Primary.ID, "/")
    44  
    45  		bindings, err := rmqc.ListBindingsIn(bindingParts[0])
    46  		if err != nil {
    47  			return fmt.Errorf("Error retrieving exchange: %s", err)
    48  		}
    49  
    50  		for _, binding := range bindings {
    51  			if binding.Source == bindingParts[1] && binding.Destination == bindingParts[2] && binding.DestinationType == bindingParts[3] && binding.PropertiesKey == bindingParts[4] {
    52  				bindingInfo = &binding
    53  				return nil
    54  			}
    55  		}
    56  
    57  		return fmt.Errorf("Unable to find binding %s", rn)
    58  	}
    59  }
    60  
    61  func testAccBindingCheckDestroy(bindingInfo rabbithole.BindingInfo) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    64  
    65  		bindings, err := rmqc.ListBindingsIn(bindingInfo.Vhost)
    66  		if err != nil {
    67  			return fmt.Errorf("Error retrieving exchange: %s", err)
    68  		}
    69  
    70  		for _, binding := range bindings {
    71  			if binding.Source == bindingInfo.Source && binding.Destination == bindingInfo.Destination && binding.DestinationType == bindingInfo.DestinationType && binding.PropertiesKey == bindingInfo.PropertiesKey {
    72  				return fmt.Errorf("Binding still exists")
    73  			}
    74  		}
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  const testAccBindingConfig_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  }
   104  
   105  resource "rabbitmq_queue" "test" {
   106      name = "test"
   107      vhost = "${rabbitmq_permissions.guest.vhost}"
   108      settings {
   109          durable = true
   110          auto_delete = false
   111      }
   112  }
   113  
   114  resource "rabbitmq_binding" "test" {
   115      source = "${rabbitmq_exchange.test.name}"
   116      vhost = "${rabbitmq_vhost.test.name}"
   117      destination = "${rabbitmq_queue.test.name}"
   118      destination_type = "queue"
   119      routing_key = "#"
   120      properties_key = "%23"
   121  }`