github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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  		"add new create_timeout attribute": {
    52  			StateVersion: 2,
    53  			Attributes:   map[string]string{},
    54  			Expected: map[string]string{
    55  				"create_timeout": "4",
    56  			},
    57  		},
    58  	}
    59  
    60  	for tn, tc := range cases {
    61  		is := &terraform.InstanceState{
    62  			ID:         "i-abc123",
    63  			Attributes: tc.Attributes,
    64  		}
    65  		is, err := resourceComputeInstanceMigrateState(
    66  			tc.StateVersion, is, tc.Meta)
    67  
    68  		if err != nil {
    69  			t.Fatalf("bad: %s, err: %#v", tn, err)
    70  		}
    71  
    72  		for k, v := range tc.Expected {
    73  			if is.Attributes[k] != v {
    74  				t.Fatalf(
    75  					"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
    76  					tn, k, v, k, is.Attributes[k], is.Attributes)
    77  			}
    78  		}
    79  	}
    80  }
    81  
    82  func TestComputeInstanceMigrateState_empty(t *testing.T) {
    83  	var is *terraform.InstanceState
    84  	var meta interface{}
    85  
    86  	// should handle nil
    87  	is, err := resourceComputeInstanceMigrateState(0, is, meta)
    88  
    89  	if err != nil {
    90  		t.Fatalf("err: %#v", err)
    91  	}
    92  	if is != nil {
    93  		t.Fatalf("expected nil instancestate, got: %#v", is)
    94  	}
    95  
    96  	// should handle non-nil but empty
    97  	is = &terraform.InstanceState{}
    98  	is, err = resourceComputeInstanceMigrateState(0, is, meta)
    99  
   100  	if err != nil {
   101  		t.Fatalf("err: %#v", err)
   102  	}
   103  }