github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_instance_migrate_test.go (about)

     1  package google
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/terraform"
     7  )
     8  
     9  func TestComputeInstanceMigrateState(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.4.2 and earlier": {
    17  			StateVersion: 0,
    18  			Attributes: map[string]string{
    19  				"metadata.#":           "2",
    20  				"metadata.0.foo":       "bar",
    21  				"metadata.1.baz":       "qux",
    22  				"metadata.2.with.dots": "should.work",
    23  			},
    24  			Expected: map[string]string{
    25  				"metadata.foo":       "bar",
    26  				"metadata.baz":       "qux",
    27  				"metadata.with.dots": "should.work",
    28  			},
    29  		},
    30  		"change scope from list to set": {
    31  			StateVersion: 1,
    32  			Attributes: map[string]string{
    33  				"service_account.#":          "1",
    34  				"service_account.0.email":    "xxxxxx-compute@developer.gserviceaccount.com",
    35  				"service_account.0.scopes.#": "4",
    36  				"service_account.0.scopes.0": "https://www.googleapis.com/auth/compute",
    37  				"service_account.0.scopes.1": "https://www.googleapis.com/auth/datastore",
    38  				"service_account.0.scopes.2": "https://www.googleapis.com/auth/devstorage.full_control",
    39  				"service_account.0.scopes.3": "https://www.googleapis.com/auth/logging.write",
    40  			},
    41  			Expected: map[string]string{
    42  				"service_account.#":                   "1",
    43  				"service_account.0.email":             "xxxxxx-compute@developer.gserviceaccount.com",
    44  				"service_account.0.scopes.#":          "4",
    45  				"service_account.0.scopes.1693978638": "https://www.googleapis.com/auth/devstorage.full_control",
    46  				"service_account.0.scopes.172152165":  "https://www.googleapis.com/auth/logging.write",
    47  				"service_account.0.scopes.299962681":  "https://www.googleapis.com/auth/compute",
    48  				"service_account.0.scopes.3435931483": "https://www.googleapis.com/auth/datastore",
    49  			},
    50  		},
    51  	}
    52  
    53  	for tn, tc := range cases {
    54  		is := &terraform.InstanceState{
    55  			ID:         "i-abc123",
    56  			Attributes: tc.Attributes,
    57  		}
    58  		is, err := resourceComputeInstanceMigrateState(
    59  			tc.StateVersion, is, tc.Meta)
    60  
    61  		if err != nil {
    62  			t.Fatalf("bad: %s, err: %#v", tn, err)
    63  		}
    64  
    65  		for k, v := range tc.Expected {
    66  			if is.Attributes[k] != v {
    67  				t.Fatalf(
    68  					"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
    69  					tn, k, v, k, is.Attributes[k], is.Attributes)
    70  			}
    71  		}
    72  	}
    73  }
    74  
    75  func TestComputeInstanceMigrateState_empty(t *testing.T) {
    76  	var is *terraform.InstanceState
    77  	var meta interface{}
    78  
    79  	// should handle nil
    80  	is, err := resourceComputeInstanceMigrateState(0, is, meta)
    81  
    82  	if err != nil {
    83  		t.Fatalf("err: %#v", err)
    84  	}
    85  	if is != nil {
    86  		t.Fatalf("expected nil instancestate, got: %#v", is)
    87  	}
    88  
    89  	// should handle non-nil but empty
    90  	is = &terraform.InstanceState{}
    91  	is, err = resourceComputeInstanceMigrateState(0, is, meta)
    92  
    93  	if err != nil {
    94  		t.Fatalf("err: %#v", err)
    95  	}
    96  }