github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/consul/resource_consul_keys_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 TestAccConsulKeys_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckConsulKeysDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccConsulKeysConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckConsulKeysExists(),
    22  					testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
    23  					testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptance"),
    24  					testAccCheckConsulKeysValue("consul_keys.app", "remove_one", "hello"),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccConsulKeysConfig_Update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckConsulKeysExists(),
    31  					testAccCheckConsulKeysValue("consul_keys.app", "enabled", "true"),
    32  					testAccCheckConsulKeysValue("consul_keys.app", "set", "acceptanceUpdated"),
    33  					testAccCheckConsulKeysRemoved("consul_keys.app", "remove_one"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func testAccCheckConsulKeysDestroy(s *terraform.State) error {
    41  	kv := testAccProvider.Meta().(*consulapi.Client).KV()
    42  	opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    43  	pair, _, err := kv.Get("test/set", opts)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	if pair != nil {
    48  		return fmt.Errorf("Key still exists: %#v", pair)
    49  	}
    50  	return nil
    51  }
    52  
    53  func testAccCheckConsulKeysExists() resource.TestCheckFunc {
    54  	return func(s *terraform.State) error {
    55  		kv := testAccProvider.Meta().(*consulapi.Client).KV()
    56  		opts := &consulapi.QueryOptions{Datacenter: "dc1"}
    57  		pair, _, err := kv.Get("test/set", opts)
    58  		if err != nil {
    59  			return err
    60  		}
    61  		if pair == nil {
    62  			return fmt.Errorf("Key 'test/set' does not exist")
    63  		}
    64  		return nil
    65  	}
    66  }
    67  
    68  func testAccCheckConsulKeysValue(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["var."+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  func testAccCheckConsulKeysRemoved(n, attr string) resource.TestCheckFunc {
    89  	return func(s *terraform.State) error {
    90  		rn, ok := s.RootModule().Resources[n]
    91  		if !ok {
    92  			return fmt.Errorf("Resource not found")
    93  		}
    94  		_, ok = rn.Primary.Attributes["var."+attr]
    95  		if ok {
    96  			return fmt.Errorf("Attribute '%s' still present: %#v", attr, rn.Primary.Attributes)
    97  		}
    98  		return nil
    99  	}
   100  }
   101  
   102  const testAccConsulKeysConfig = `
   103  resource "consul_keys" "app" {
   104  	datacenter = "dc1"
   105  	key {
   106  		name = "enabled"
   107  		path = "test/enabled"
   108  		default = "true"
   109  	}
   110  	key {
   111  		name = "set"
   112  		path = "test/set"
   113  		value = "acceptance"
   114  		delete = true
   115  	}
   116  	key {
   117  		name = "remove_one"
   118  		path = "test/remove_one"
   119  		value = "hello"
   120  		delete = true
   121  	}
   122  }
   123  `
   124  
   125  const testAccConsulKeysConfig_Update = `
   126  resource "consul_keys" "app" {
   127  	datacenter = "dc1"
   128  	key {
   129  		name = "enabled"
   130  		path = "test/enabled"
   131  		default = "true"
   132  	}
   133  	key {
   134  		name = "set"
   135  		path = "test/set"
   136  		value = "acceptanceUpdated"
   137  		delete = true
   138  	}
   139  }
   140  `