github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rabbitmq/resource_policy_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 TestAccPolicy(t *testing.T) {
    15  	var policy rabbithole.Policy
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccPolicyCheckDestroy(&policy),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccPolicyConfig_basic,
    23  				Check: testAccPolicyCheck(
    24  					"rabbitmq_policy.test", &policy,
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccPolicyConfig_update,
    29  				Check: testAccPolicyCheck(
    30  					"rabbitmq_policy.test", &policy,
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccPolicyCheck(rn string, policy *rabbithole.Policy) resource.TestCheckFunc {
    38  	return func(s *terraform.State) error {
    39  		rs, ok := s.RootModule().Resources[rn]
    40  		if !ok {
    41  			return fmt.Errorf("resource not found: %s", rn)
    42  		}
    43  
    44  		if rs.Primary.ID == "" {
    45  			return fmt.Errorf("policy id not set")
    46  		}
    47  
    48  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    49  		policyParts := strings.Split(rs.Primary.ID, "@")
    50  
    51  		policies, err := rmqc.ListPolicies()
    52  		if err != nil {
    53  			return fmt.Errorf("Error retrieving policies: %s", err)
    54  		}
    55  
    56  		for _, p := range policies {
    57  			if p.Name == policyParts[0] && p.Vhost == policyParts[1] {
    58  				policy = &p
    59  				return nil
    60  			}
    61  		}
    62  
    63  		return fmt.Errorf("Unable to find policy %s", rn)
    64  	}
    65  }
    66  
    67  func testAccPolicyCheckDestroy(policy *rabbithole.Policy) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    70  
    71  		policies, err := rmqc.ListPolicies()
    72  		if err != nil {
    73  			return fmt.Errorf("Error retrieving policies: %s", err)
    74  		}
    75  
    76  		for _, p := range policies {
    77  			if p.Name == policy.Name && p.Vhost == policy.Vhost {
    78  				return fmt.Errorf("Policy %s@%s still exist", policy.Name, policy.Vhost)
    79  			}
    80  		}
    81  
    82  		return nil
    83  	}
    84  }
    85  
    86  const testAccPolicyConfig_basic = `
    87  resource "rabbitmq_vhost" "test" {
    88      name = "test"
    89  }
    90  
    91  resource "rabbitmq_permissions" "guest" {
    92      user = "guest"
    93      vhost = "${rabbitmq_vhost.test.name}"
    94      permissions {
    95          configure = ".*"
    96          write = ".*"
    97          read = ".*"
    98      }
    99  }
   100  
   101  resource "rabbitmq_policy" "test" {
   102      name = "test"
   103      vhost = "${rabbitmq_permissions.guest.vhost}"
   104      policy {
   105          pattern = ".*"
   106          priority = 0
   107          apply_to = "all"
   108          definition {
   109              ha-mode = "nodes"
   110              ha-params = "a,b,c"
   111          }
   112      }
   113  }`
   114  
   115  const testAccPolicyConfig_update = `
   116  resource "rabbitmq_vhost" "test" {
   117      name = "test"
   118  }
   119  
   120  resource "rabbitmq_permissions" "guest" {
   121      user = "guest"
   122      vhost = "${rabbitmq_vhost.test.name}"
   123      permissions {
   124          configure = ".*"
   125          write = ".*"
   126          read = ".*"
   127      }
   128  }
   129  
   130  resource "rabbitmq_policy" "test" {
   131      name = "test"
   132      vhost = "${rabbitmq_permissions.guest.vhost}"
   133      policy {
   134          pattern = ".*"
   135          priority = 0
   136          apply_to = "all"
   137          definition {
   138              ha-mode = "all"
   139          }
   140      }
   141  }`