github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/rabbitmq/resource_vhost_test.go (about)

     1  package rabbitmq
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/michaelklishin/rabbit-hole"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccVhost(t *testing.T) {
    14  	var vhost string
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccVhostCheckDestroy(vhost),
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccVhostConfig_basic,
    22  				Check: testAccVhostCheck(
    23  					"rabbitmq_vhost.test", &vhost,
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccVhostCheck(rn string, name *string) resource.TestCheckFunc {
    31  	return func(s *terraform.State) error {
    32  		rs, ok := s.RootModule().Resources[rn]
    33  		if !ok {
    34  			return fmt.Errorf("resource not found: %s", rn)
    35  		}
    36  
    37  		if rs.Primary.ID == "" {
    38  			return fmt.Errorf("vhost id not set")
    39  		}
    40  
    41  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    42  		vhosts, err := rmqc.ListVhosts()
    43  		if err != nil {
    44  			return fmt.Errorf("Error retrieving vhosts: %s", err)
    45  		}
    46  
    47  		for _, vhost := range vhosts {
    48  			if vhost.Name == rs.Primary.ID {
    49  				*name = rs.Primary.ID
    50  				return nil
    51  			}
    52  		}
    53  
    54  		return fmt.Errorf("Unable to find vhost %s", rn)
    55  	}
    56  }
    57  
    58  func testAccVhostCheckDestroy(name string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rmqc := testAccProvider.Meta().(*rabbithole.Client)
    61  		vhosts, err := rmqc.ListVhosts()
    62  		if err != nil {
    63  			return fmt.Errorf("Error retrieving vhosts: %s", err)
    64  		}
    65  
    66  		for _, vhost := range vhosts {
    67  			if vhost.Name == name {
    68  				return fmt.Errorf("vhost still exists: %v", vhost)
    69  			}
    70  		}
    71  
    72  		return nil
    73  	}
    74  }
    75  
    76  const testAccVhostConfig_basic = `
    77  resource "rabbitmq_vhost" "test" {
    78      name = "test"
    79  }`