github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/consul/resource_consul_key_prefix_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 TestAccConsulKeyPrefix_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:  func() { testAccPreCheck(t) },
    15  		Providers: testAccProviders,
    16  		CheckDestroy: resource.ComposeTestCheckFunc(
    17  			testAccCheckConsulKeyPrefixKeyAbsent("species"),
    18  			testAccCheckConsulKeyPrefixKeyAbsent("meat"),
    19  			testAccCheckConsulKeyPrefixKeyAbsent("cheese"),
    20  			testAccCheckConsulKeyPrefixKeyAbsent("bread"),
    21  		),
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccConsulKeyPrefixConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckConsulKeyPrefixKeyValue("cheese", "chevre"),
    27  					testAccCheckConsulKeyPrefixKeyValue("bread", "baguette"),
    28  					testAccCheckConsulKeyPrefixKeyAbsent("species"),
    29  					testAccCheckConsulKeyPrefixKeyAbsent("meat"),
    30  				),
    31  			},
    32  			resource.TestStep{
    33  				Config:             testAccConsulKeyPrefixConfig,
    34  				ExpectNonEmptyPlan: true,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					// This will add a rogue key that Terraform isn't
    37  					// expecting, causing a non-empty plan that wants
    38  					// to remove it.
    39  					testAccAddConsulKeyPrefixRogue("species", "gorilla"),
    40  				),
    41  			},
    42  			resource.TestStep{
    43  				Config: testAccConsulKeyPrefixConfig_Update,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckConsulKeyPrefixKeyValue("meat", "ham"),
    46  					testAccCheckConsulKeyPrefixKeyValue("bread", "batard"),
    47  					testAccCheckConsulKeyPrefixKeyAbsent("cheese"),
    48  					testAccCheckConsulKeyPrefixKeyAbsent("species"),
    49  				),
    50  			},
    51  			resource.TestStep{
    52  				Config:             testAccConsulKeyPrefixConfig_Update,
    53  				ExpectNonEmptyPlan: true,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccAddConsulKeyPrefixRogue("species", "gorilla"),
    56  				),
    57  			},
    58  		},
    59  	})
    60  }
    61  
    62  func testAccCheckConsulKeyPrefixDestroy(s *terraform.State) error {
    63  	kv := testAccProvider.Meta().(*consulapi.Client).KV()
    64  	opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    65  	pair, _, err := kv.Get("test/set", opts)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if pair != nil {
    70  		return fmt.Errorf("Key still exists: %#v", pair)
    71  	}
    72  	return nil
    73  }
    74  
    75  func testAccCheckConsulKeyPrefixKeyAbsent(name string) resource.TestCheckFunc {
    76  	fullName := "prefix_test/" + name
    77  	return func(s *terraform.State) error {
    78  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
    79  		opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    80  		pair, _, err := kv.Get(fullName, opts)
    81  		if err != nil {
    82  			return err
    83  		}
    84  		if pair != nil {
    85  			return fmt.Errorf("key '%s' exists, but shouldn't", fullName)
    86  		}
    87  		return nil
    88  	}
    89  }
    90  
    91  // This one is actually not a check, but rather a mutation step. It writes
    92  // a value directly into Consul, bypassing our Terraform resource.
    93  func testAccAddConsulKeyPrefixRogue(name, value string) resource.TestCheckFunc {
    94  	fullName := "prefix_test/" + name
    95  	return func(s *terraform.State) error {
    96  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
    97  		opts := &consulapi.WriteOptions{Datacenter: "dc1"}
    98  		pair := &consulapi.KVPair{
    99  			Key:   fullName,
   100  			Value: []byte(value),
   101  		}
   102  		_, err := kv.Put(pair, opts)
   103  		return err
   104  	}
   105  }
   106  
   107  func testAccCheckConsulKeyPrefixKeyValue(name, value string) resource.TestCheckFunc {
   108  	fullName := "prefix_test/" + name
   109  	return func(s *terraform.State) error {
   110  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
   111  		opts := &consulapi.QueryOptions{Datacenter: "dc1"}
   112  		pair, _, err := kv.Get(fullName, opts)
   113  		if err != nil {
   114  			return err
   115  		}
   116  		if pair == nil {
   117  			return fmt.Errorf("key %v doesn't exist, but should", fullName)
   118  		}
   119  		if string(pair.Value) != value {
   120  			return fmt.Errorf("key %v has value %v; want %v", fullName, pair.Value, value)
   121  		}
   122  		return nil
   123  	}
   124  }
   125  
   126  const testAccConsulKeyPrefixConfig = `
   127  resource "consul_key_prefix" "app" {
   128  	datacenter = "dc1"
   129  
   130      path_prefix = "prefix_test/"
   131  
   132      subkeys = {
   133          cheese = "chevre"
   134          bread = "baguette"
   135      }
   136  }
   137  `
   138  
   139  const testAccConsulKeyPrefixConfig_Update = `
   140  resource "consul_key_prefix" "app" {
   141  	datacenter = "dc1"
   142  
   143      path_prefix = "prefix_test/"
   144  
   145      subkeys = {
   146          bread = "batard"
   147          meat = "ham"
   148      }
   149  }
   150  `