github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/google/resource_compute_firewall_migrate_test.go (about)

     1  package google
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/terraform"
     7  )
     8  
     9  func TestComputeFirewallMigrateState(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 scope from list to set": {
    17  			StateVersion: 0,
    18  			Attributes: map[string]string{
    19  				"allow.#":                  "1",
    20  				"allow.0.protocol":         "udp",
    21  				"allow.0.ports.#":          "4",
    22  				"allow.0.ports.1693978638": "8080",
    23  				"allow.0.ports.172152165":  "8081",
    24  				"allow.0.ports.299962681":  "7072",
    25  				"allow.0.ports.3435931483": "4044",
    26  			},
    27  			Expected: map[string]string{
    28  				"allow.#":          "1",
    29  				"allow.0.protocol": "udp",
    30  				"allow.0.ports.#":  "4",
    31  				"allow.0.ports.0":  "8080",
    32  				"allow.0.ports.1":  "8081",
    33  				"allow.0.ports.2":  "7072",
    34  				"allow.0.ports.3":  "4044",
    35  			},
    36  		},
    37  	}
    38  	for tn, tc := range cases {
    39  		is := &terraform.InstanceState{
    40  			ID:         "i-abc123",
    41  			Attributes: tc.Attributes,
    42  		}
    43  		is, err := resourceComputeFirewallMigrateState(
    44  			tc.StateVersion, is, tc.Meta)
    45  
    46  		if err != nil {
    47  			t.Fatalf("bad: %s, err: %#v", tn, err)
    48  		}
    49  
    50  		for k, v := range tc.Expected {
    51  			if is.Attributes[k] != v {
    52  				t.Fatalf(
    53  					"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
    54  					tn, k, v, k, is.Attributes[k], is.Attributes)
    55  			}
    56  		}
    57  	}
    58  }
    59  
    60  func TestComputeFirewallMigrateState_empty(t *testing.T) {
    61  	var is *terraform.InstanceState
    62  	var meta interface{}
    63  
    64  	// should handle nil
    65  	is, err := resourceComputeFirewallMigrateState(0, is, meta)
    66  
    67  	if err != nil {
    68  		t.Fatalf("err: %#v", err)
    69  	}
    70  	if is != nil {
    71  		t.Fatalf("expected nil instancestate, got: %#v", is)
    72  	}
    73  
    74  	// should handle non-nil but empty
    75  	is = &terraform.InstanceState{}
    76  	is, err = resourceComputeFirewallMigrateState(0, is, meta)
    77  
    78  	if err != nil {
    79  		t.Fatalf("err: %#v", err)
    80  	}
    81  }