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

     1  package google
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/terraform"
     7  )
     8  
     9  func TestComputeInstanceGroupMigrateState(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  		"change instances from list to set": {
    17  			StateVersion: 0,
    18  			Attributes: map[string]string{
    19  				"instances.#": "1",
    20  				"instances.0": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-1",
    21  				"instances.1": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-0",
    22  			},
    23  			Expected: map[string]string{
    24  				"instances.#":          "1",
    25  				"instances.764135222":  "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-1",
    26  				"instances.1519187872": "https://www.googleapis.com/compute/v1/projects/project_name/zones/zone_name/instances/instancegroup-test-0",
    27  			},
    28  			Meta: &Config{},
    29  		},
    30  	}
    31  
    32  	for tn, tc := range cases {
    33  		is := &terraform.InstanceState{
    34  			ID:         "i-abc123",
    35  			Attributes: tc.Attributes,
    36  		}
    37  		is, err := resourceComputeInstanceGroupMigrateState(
    38  			tc.StateVersion, is, tc.Meta)
    39  
    40  		if err != nil {
    41  			t.Fatalf("bad: %s, err: %#v", tn, err)
    42  		}
    43  
    44  		for k, v := range tc.Expected {
    45  			if is.Attributes[k] != v {
    46  				t.Fatalf(
    47  					"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
    48  					tn, k, v, k, is.Attributes[k], is.Attributes)
    49  			}
    50  		}
    51  	}
    52  }
    53  
    54  func TestComputeInstanceGroupMigrateState_empty(t *testing.T) {
    55  	var is *terraform.InstanceState
    56  	var meta *Config
    57  
    58  	// should handle nil
    59  	is, err := resourceComputeInstanceGroupMigrateState(0, is, meta)
    60  
    61  	if err != nil {
    62  		t.Fatalf("err: %#v", err)
    63  	}
    64  	if is != nil {
    65  		t.Fatalf("expected nil instancestate, got: %#v", is)
    66  	}
    67  
    68  	// should handle non-nil but empty
    69  	is = &terraform.InstanceState{}
    70  	is, err = resourceComputeInstanceGroupMigrateState(0, is, meta)
    71  
    72  	if err != nil {
    73  		t.Fatalf("err: %#v", err)
    74  	}
    75  }