github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/consul/resource_consul_catalog_entry_test.go (about)

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	consulapi "github.com/hashicorp/consul/api"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccConsulCatalogEntry_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() {},
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckConsulCatalogEntryDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccConsulCatalogEntryConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckConsulCatalogEntryExists(),
    22  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "address", "127.0.0.1"),
    23  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "node", "bastion"),
    24  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.#", "1"),
    25  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.address", "www.google.com"),
    26  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.id", "google1"),
    27  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.name", "google"),
    28  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.port", "80"),
    29  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.#", "2"),
    30  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.2154398732", "tag0"),
    31  					testAccCheckConsulCatalogEntryValue("consul_catalog_entry.app", "service.3112399829.tags.4151227546", "tag1"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckConsulCatalogEntryDestroy(s *terraform.State) error {
    39  	catalog := testAccProvider.Meta().(*consulapi.Client).Catalog()
    40  	qOpts := consulapi.QueryOptions{}
    41  	services, _, err := catalog.Services(&qOpts)
    42  	if err != nil {
    43  		return fmt.Errorf("Could not retrieve services: %#v", err)
    44  	}
    45  	_, ok := services["google"]
    46  	if ok {
    47  		return fmt.Errorf("Service still exists: %#v", "google")
    48  	}
    49  	return nil
    50  }
    51  
    52  func testAccCheckConsulCatalogEntryExists() resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		catalog := testAccProvider.Meta().(*consulapi.Client).Catalog()
    55  		qOpts := consulapi.QueryOptions{}
    56  		services, _, err := catalog.Services(&qOpts)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		_, ok := services["google"]
    61  		if !ok {
    62  			return fmt.Errorf("Service does not exist: %#v", "google")
    63  		}
    64  		return nil
    65  	}
    66  }
    67  
    68  func testAccCheckConsulCatalogEntryValue(n, attr, val string) resource.TestCheckFunc {
    69  	return func(s *terraform.State) error {
    70  		rn, ok := s.RootModule().Resources[n]
    71  		if !ok {
    72  			return fmt.Errorf("Resource not found")
    73  		}
    74  		out, ok := rn.Primary.Attributes[attr]
    75  		if !ok {
    76  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
    77  		}
    78  		if val != "<any>" && out != val {
    79  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
    80  		}
    81  		if val == "<any>" && out == "" {
    82  			return fmt.Errorf("Attribute '%s' value '%s'", attr, out)
    83  		}
    84  		return nil
    85  	}
    86  }
    87  
    88  const testAccConsulCatalogEntryConfig = `
    89  resource "consul_catalog_entry" "app" {
    90  	address = "127.0.0.1"
    91  	node = "bastion"
    92  	service = {
    93  		address = "www.google.com"
    94  		id = "google1"
    95  		name = "google"
    96  		port = 80
    97  		tags = ["tag0", "tag1"]
    98  	}
    99  }
   100  `