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

     1  package consul
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/terraform"
     7  )
     8  
     9  func TestConsulKeysMigrateState(t *testing.T) {
    10  	cases := map[string]struct {
    11  		StateVersion int
    12  		Attributes   map[string]string
    13  		Expected     map[string]string
    14  		Meta         interface{}
    15  	}{
    16  		"v0.6.9 and earlier, with old values hash function": {
    17  			StateVersion: 0,
    18  			Attributes: map[string]string{
    19  				"key.#":             "2",
    20  				"key.12345.name":    "hello",
    21  				"key.12345.path":    "foo/bar",
    22  				"key.12345.value":   "world",
    23  				"key.12345.default": "",
    24  				"key.12345.delete":  "false",
    25  				"key.6789.name":     "temp",
    26  				"key.6789.path":     "foo/foo",
    27  				"key.6789.value":    "",
    28  				"key.6789.default":  "",
    29  				"key.6789.delete":   "true",
    30  			},
    31  			Expected: map[string]string{
    32  				"key.#":                  "2",
    33  				"key.2401383718.default": "",
    34  				"key.2401383718.delete":  "true",
    35  				"key.2401383718.name":    "temp",
    36  				"key.2401383718.path":    "foo/foo",
    37  				"key.2401383718.value":   "",
    38  				"key.3116955509.path":    "foo/bar",
    39  				"key.3116955509.default": "",
    40  				"key.3116955509.delete":  "false",
    41  				"key.3116955509.name":    "hello",
    42  				"key.3116955509.value":   "world",
    43  			},
    44  		},
    45  	}
    46  
    47  	for tn, tc := range cases {
    48  		is := &terraform.InstanceState{
    49  			ID:         "consul",
    50  			Attributes: tc.Attributes,
    51  		}
    52  		is, err := resourceConsulKeys().MigrateState(
    53  			tc.StateVersion, is, tc.Meta)
    54  
    55  		if err != nil {
    56  			t.Fatalf("bad: %s, err: %#v", tn, err)
    57  		}
    58  
    59  		for k, v := range tc.Expected {
    60  			if is.Attributes[k] != v {
    61  				t.Fatalf(
    62  					"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
    63  					tn, k, v, k, is.Attributes[k], is.Attributes)
    64  			}
    65  		}
    66  	}
    67  }
    68  
    69  func TestConsulKeysMigrateState_empty(t *testing.T) {
    70  	var is *terraform.InstanceState
    71  	var meta interface{}
    72  
    73  	// should handle nil
    74  	is, err := resourceConsulKeys().MigrateState(0, is, meta)
    75  
    76  	if err != nil {
    77  		t.Fatalf("err: %#v", err)
    78  	}
    79  	if is != nil {
    80  		t.Fatalf("expected nil instancestate, got: %#v", is)
    81  	}
    82  
    83  	// should handle non-nil but empty
    84  	is = &terraform.InstanceState{}
    85  	is, err = resourceConsulKeys().MigrateState(0, is, meta)
    86  
    87  	if err != nil {
    88  		t.Fatalf("err: %#v", err)
    89  	}
    90  }