github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/rabbitmq/resource_permissions_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 TestAccPermissions(t *testing.T) {
    15  	var permissionInfo rabbithole.PermissionInfo
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccPermissionsCheckDestroy(&permissionInfo),
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccPermissionsConfig_basic,
    23  				Check: testAccPermissionsCheck(
    24  					"rabbitmq_permissions.test", &permissionInfo,
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccPermissionsConfig_update,
    29  				Check: testAccPermissionsCheck(
    30  					"rabbitmq_permissions.test", &permissionInfo,
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccPermissionsCheck(rn string, permissionInfo *rabbithole.PermissionInfo) 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("permission id not set")
    46  		}
    47  
    48  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    49  		perms, err := rmqc.ListPermissions()
    50  		if err != nil {
    51  			return fmt.Errorf("Error retrieving permissions: %s", err)
    52  		}
    53  
    54  		userParts := strings.Split(rs.Primary.ID, "@")
    55  		for _, perm := range perms {
    56  			if perm.User == userParts[0] && perm.Vhost == userParts[1] {
    57  				permissionInfo = &perm
    58  				return nil
    59  			}
    60  		}
    61  
    62  		return fmt.Errorf("Unable to find permissions for user %s", rn)
    63  	}
    64  }
    65  
    66  func testAccPermissionsCheckDestroy(permissionInfo *rabbithole.PermissionInfo) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    69  		perms, err := rmqc.ListPermissions()
    70  		if err != nil {
    71  			return fmt.Errorf("Error retrieving permissions: %s", err)
    72  		}
    73  
    74  		for _, perm := range perms {
    75  			if perm.User == permissionInfo.User && perm.Vhost == permissionInfo.Vhost {
    76  				return fmt.Errorf("Permissions still exist for user %s@%s", permissionInfo.User, permissionInfo.Vhost)
    77  			}
    78  		}
    79  
    80  		return nil
    81  	}
    82  }
    83  
    84  const testAccPermissionsConfig_basic = `
    85  resource "rabbitmq_vhost" "test" {
    86      name = "test"
    87  }
    88  
    89  resource "rabbitmq_user" "test" {
    90      name = "mctest"
    91      password = "foobar"
    92      tags = ["administrator"]
    93  }
    94  
    95  resource "rabbitmq_permissions" "test" {
    96      user = "${rabbitmq_user.test.name}"
    97      vhost = "${rabbitmq_vhost.test.name}"
    98      permissions {
    99          configure = ".*"
   100          write = ".*"
   101          read = ".*"
   102      }
   103  }`
   104  
   105  const testAccPermissionsConfig_update = `
   106  resource "rabbitmq_vhost" "test" {
   107      name = "test"
   108  }
   109  
   110  resource "rabbitmq_user" "test" {
   111      name = "mctest"
   112      password = "foobar"
   113      tags = ["administrator"]
   114  }
   115  
   116  resource "rabbitmq_permissions" "test" {
   117      user = "${rabbitmq_user.test.name}"
   118      vhost = "${rabbitmq_vhost.test.name}"
   119      permissions {
   120          configure = ".*"
   121          write = ".*"
   122          read = ""
   123      }
   124  }`