github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/rabbitmq/resource_queue_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 TestAccQueue(t *testing.T) {
    15  	var queueInfo rabbithole.QueueInfo
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccQueueCheckDestroy(&queueInfo),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccQueueConfig_basic,
    23  				Check: testAccQueueCheck(
    24  					"rabbitmq_queue.test", &queueInfo,
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccQueueCheck(rn string, queueInfo *rabbithole.QueueInfo) 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("queue id not set")
    40  		}
    41  
    42  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    43  		queueParts := strings.Split(rs.Primary.ID, "@")
    44  
    45  		queues, err := rmqc.ListQueuesIn(queueParts[1])
    46  		if err != nil {
    47  			return fmt.Errorf("Error retrieving queue: %s", err)
    48  		}
    49  
    50  		for _, queue := range queues {
    51  			if queue.Name == queueParts[0] && queue.Vhost == queueParts[1] {
    52  				queueInfo = &queue
    53  				return nil
    54  			}
    55  		}
    56  
    57  		return fmt.Errorf("Unable to find queue %s", rn)
    58  	}
    59  }
    60  
    61  func testAccQueueCheckDestroy(queueInfo *rabbithole.QueueInfo) resource.TestCheckFunc {
    62  	return func(s *terraform.State) error {
    63  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    64  
    65  		queues, err := rmqc.ListQueuesIn(queueInfo.Vhost)
    66  		if err != nil {
    67  			return fmt.Errorf("Error retrieving queue: %s", err)
    68  		}
    69  
    70  		for _, queue := range queues {
    71  			if queue.Name == queueInfo.Name && queue.Vhost == queueInfo.Vhost {
    72  				return fmt.Errorf("Queue %s@%s still exist", queueInfo.Name, queueInfo.Vhost)
    73  			}
    74  		}
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  const testAccQueueConfig_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_queue" "test" {
    96      name = "test"
    97      vhost = "${rabbitmq_permissions.guest.vhost}"
    98      settings {
    99          durable = false
   100          auto_delete = true
   101      }
   102  }`