github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_basic(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 TestAccBinding_propertiesKey(t *testing.T) {
    32  	var bindingInfo rabbithole.BindingInfo
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccBindingCheckDestroy(bindingInfo),
    37  		Steps: []resource.TestStep{
    38  			resource.TestStep{
    39  				Config: testAccBindingConfig_propertiesKey,
    40  				Check: testAccBindingCheck(
    41  					"rabbitmq_binding.test", &bindingInfo,
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccBindingCheck(rn string, bindingInfo *rabbithole.BindingInfo) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		rs, ok := s.RootModule().Resources[rn]
    51  		if !ok {
    52  			return fmt.Errorf("resource not found: %s", rn)
    53  		}
    54  
    55  		if rs.Primary.ID == "" {
    56  			return fmt.Errorf("binding id not set")
    57  		}
    58  
    59  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    60  		bindingParts := strings.Split(rs.Primary.ID, "/")
    61  
    62  		bindings, err := rmqc.ListBindingsIn(bindingParts[0])
    63  		if err != nil {
    64  			return fmt.Errorf("Error retrieving exchange: %s", err)
    65  		}
    66  
    67  		for _, binding := range bindings {
    68  			if binding.Source == bindingParts[1] && binding.Destination == bindingParts[2] && binding.DestinationType == bindingParts[3] && binding.PropertiesKey == bindingParts[4] {
    69  				bindingInfo = &binding
    70  				return nil
    71  			}
    72  		}
    73  
    74  		return fmt.Errorf("Unable to find binding %s", rn)
    75  	}
    76  }
    77  
    78  func testAccBindingCheckDestroy(bindingInfo rabbithole.BindingInfo) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    81  
    82  		bindings, err := rmqc.ListBindingsIn(bindingInfo.Vhost)
    83  		if err != nil {
    84  			return fmt.Errorf("Error retrieving exchange: %s", err)
    85  		}
    86  
    87  		for _, binding := range bindings {
    88  			if binding.Source == bindingInfo.Source && binding.Destination == bindingInfo.Destination && binding.DestinationType == bindingInfo.DestinationType && binding.PropertiesKey == bindingInfo.PropertiesKey {
    89  				return fmt.Errorf("Binding still exists")
    90  			}
    91  		}
    92  
    93  		return nil
    94  	}
    95  }
    96  
    97  const testAccBindingConfig_basic = `
    98  resource "rabbitmq_vhost" "test" {
    99      name = "test"
   100  }
   101  
   102  resource "rabbitmq_permissions" "guest" {
   103      user = "guest"
   104      vhost = "${rabbitmq_vhost.test.name}"
   105      permissions {
   106          configure = ".*"
   107          write = ".*"
   108          read = ".*"
   109      }
   110  }
   111  
   112  resource "rabbitmq_exchange" "test" {
   113      name = "test"
   114      vhost = "${rabbitmq_permissions.guest.vhost}"
   115      settings {
   116          type = "fanout"
   117          durable = false
   118          auto_delete = true
   119      }
   120  }
   121  
   122  resource "rabbitmq_queue" "test" {
   123      name = "test"
   124      vhost = "${rabbitmq_permissions.guest.vhost}"
   125      settings {
   126          durable = true
   127          auto_delete = false
   128      }
   129  }
   130  
   131  resource "rabbitmq_binding" "test" {
   132      source = "${rabbitmq_exchange.test.name}"
   133      vhost = "${rabbitmq_vhost.test.name}"
   134      destination = "${rabbitmq_queue.test.name}"
   135      destination_type = "queue"
   136      routing_key = "#"
   137      properties_key = "%23"
   138  }`
   139  
   140  const testAccBindingConfig_propertiesKey = `
   141  resource "rabbitmq_vhost" "test" {
   142      name = "test"
   143  }
   144  
   145  resource "rabbitmq_permissions" "guest" {
   146      user = "guest"
   147      vhost = "${rabbitmq_vhost.test.name}"
   148      permissions {
   149          configure = ".*"
   150          write = ".*"
   151          read = ".*"
   152      }
   153  }
   154  
   155  resource "rabbitmq_exchange" "test" {
   156      name = "Test"
   157      vhost = "${rabbitmq_permissions.guest.vhost}"
   158      settings {
   159          type = "topic"
   160          durable = true
   161          auto_delete = false
   162      }
   163  }
   164  
   165  resource "rabbitmq_queue" "test" {
   166      name = "Test.Queue"
   167      vhost = "${rabbitmq_permissions.guest.vhost}"
   168      settings {
   169          durable = true
   170          auto_delete = false
   171      }
   172  }
   173  
   174  resource "rabbitmq_binding" "test" {
   175      source = "${rabbitmq_exchange.test.name}"
   176      vhost = "${rabbitmq_vhost.test.name}"
   177      destination = "${rabbitmq_queue.test.name}"
   178      destination_type = "queue"
   179      routing_key = "ANYTHING.#"
   180      properties_key = "ANYTHING.%23"
   181  }
   182  `