github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/consul/resource_consul_prepared_query_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 TestAccConsulPreparedQuery_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckConsulPreparedQueryDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccConsulPreparedQueryConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckConsulPreparedQueryExists(),
    22  					testAccCheckConsulPreparedQueryAttrValue("name", "foo"),
    23  					testAccCheckConsulPreparedQueryAttrValue("stored_token", "pq-token"),
    24  					testAccCheckConsulPreparedQueryAttrValue("service", "redis"),
    25  					testAccCheckConsulPreparedQueryAttrValue("near", "_agent"),
    26  					testAccCheckConsulPreparedQueryAttrValue("tags.#", "1"),
    27  					testAccCheckConsulPreparedQueryAttrValue("only_passing", "true"),
    28  					testAccCheckConsulPreparedQueryAttrValue("failover.0.nearest_n", "3"),
    29  					testAccCheckConsulPreparedQueryAttrValue("failover.0.datacenters.#", "2"),
    30  					testAccCheckConsulPreparedQueryAttrValue("template.0.type", "name_prefix_match"),
    31  					testAccCheckConsulPreparedQueryAttrValue("template.0.regexp", "hello"),
    32  					testAccCheckConsulPreparedQueryAttrValue("dns.0.ttl", "8m"),
    33  				),
    34  			},
    35  			resource.TestStep{
    36  				Config: testAccConsulPreparedQueryConfigUpdate1,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckConsulPreparedQueryExists(),
    39  					testAccCheckConsulPreparedQueryAttrValue("name", "baz"),
    40  					testAccCheckConsulPreparedQueryAttrValue("stored_token", "pq-token-updated"),
    41  					testAccCheckConsulPreparedQueryAttrValue("service", "memcached"),
    42  					testAccCheckConsulPreparedQueryAttrValue("near", "node1"),
    43  					testAccCheckConsulPreparedQueryAttrValue("tags.#", "2"),
    44  					testAccCheckConsulPreparedQueryAttrValue("only_passing", "false"),
    45  					testAccCheckConsulPreparedQueryAttrValue("failover.0.nearest_n", "2"),
    46  					testAccCheckConsulPreparedQueryAttrValue("failover.0.datacenters.#", "1"),
    47  					testAccCheckConsulPreparedQueryAttrValue("template.0.regexp", "goodbye"),
    48  					testAccCheckConsulPreparedQueryAttrValue("dns.0.ttl", "16m"),
    49  				),
    50  			},
    51  			resource.TestStep{
    52  				Config: testAccConsulPreparedQueryConfigUpdate2,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckConsulPreparedQueryExists(),
    55  					testAccCheckConsulPreparedQueryAttrValue("stored_token", ""),
    56  					testAccCheckConsulPreparedQueryAttrValue("near", ""),
    57  					testAccCheckConsulPreparedQueryAttrValue("tags.#", "0"),
    58  					testAccCheckConsulPreparedQueryAttrValue("failover.#", "0"),
    59  					testAccCheckConsulPreparedQueryAttrValue("template.#", "0"),
    60  					testAccCheckConsulPreparedQueryAttrValue("dns.#", "0"),
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  func checkPreparedQueryExists(s *terraform.State) bool {
    68  	rn, ok := s.RootModule().Resources["consul_prepared_query.foo"]
    69  	if !ok {
    70  		return false
    71  	}
    72  	id := rn.Primary.ID
    73  
    74  	client := testAccProvider.Meta().(*consulapi.Client).PreparedQuery()
    75  	opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    76  	pq, _, err := client.Get(id, opts)
    77  	return err == nil && pq != nil
    78  }
    79  
    80  func testAccCheckConsulPreparedQueryDestroy(s *terraform.State) error {
    81  	if checkPreparedQueryExists(s) {
    82  		return fmt.Errorf("Prepared query 'foo' still exists")
    83  	}
    84  	return nil
    85  }
    86  
    87  func testAccCheckConsulPreparedQueryExists() resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		if !checkPreparedQueryExists(s) {
    90  			return fmt.Errorf("Prepared query 'foo' does not exist")
    91  		}
    92  		return nil
    93  	}
    94  }
    95  
    96  func testAccCheckConsulPreparedQueryAttrValue(attr, val string) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  		rn, ok := s.RootModule().Resources["consul_prepared_query.foo"]
    99  		if !ok {
   100  			return fmt.Errorf("Resource not found")
   101  		}
   102  		out, ok := rn.Primary.Attributes[attr]
   103  		if !ok {
   104  			return fmt.Errorf("Attribute '%s' not found: %#v", attr, rn.Primary.Attributes)
   105  		}
   106  		if out != val {
   107  			return fmt.Errorf("Attribute '%s' value '%s' != '%s'", attr, out, val)
   108  		}
   109  		return nil
   110  	}
   111  }
   112  
   113  const testAccConsulPreparedQueryConfig = `
   114  resource "consul_prepared_query" "foo" {
   115  	name = "foo"
   116  	token = "client-token"
   117  	stored_token = "pq-token"
   118  	service = "redis"
   119  	tags = ["prod"]
   120  	near = "_agent"
   121  	only_passing = true
   122  
   123  	failover {
   124  		nearest_n = 3
   125  		datacenters = ["dc1", "dc2"]
   126  	}
   127  
   128  	template {
   129  		type = "name_prefix_match"
   130  		regexp = "hello"
   131  	}
   132  
   133  	dns {
   134  		ttl = "8m"
   135  	}
   136  }
   137  `
   138  
   139  const testAccConsulPreparedQueryConfigUpdate1 = `
   140  resource "consul_prepared_query" "foo" {
   141  	name = "baz"
   142  	token = "client-token"
   143  	stored_token = "pq-token-updated"
   144  	service = "memcached"
   145  	tags = ["prod","sup"]
   146  	near = "node1"
   147  	only_passing = false
   148  
   149  	failover {
   150  		nearest_n = 2
   151  		datacenters = ["dc2"]
   152  	}
   153  
   154  	template {
   155  		type = "name_prefix_match"
   156  		regexp = "goodbye"
   157  	}
   158  
   159  	dns {
   160  		ttl = "16m"
   161  	}
   162  }
   163  `
   164  
   165  const testAccConsulPreparedQueryConfigUpdate2 = `
   166  resource "consul_prepared_query" "foo" {
   167  	name = "baz"
   168  	service = "memcached"
   169  	token = "client-token"
   170  }
   171  `