github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_instance_migrate_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/terraform"
     7  )
     8  
     9  func TestAWSInstanceMigrateState(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.3.6 and earlier": {
    17  			StateVersion: 0,
    18  			Attributes: map[string]string{
    19  				// EBS
    20  				"block_device.#":                                "2",
    21  				"block_device.3851383343.delete_on_termination": "true",
    22  				"block_device.3851383343.device_name":           "/dev/sdx",
    23  				"block_device.3851383343.encrypted":             "false",
    24  				"block_device.3851383343.snapshot_id":           "",
    25  				"block_device.3851383343.virtual_name":          "",
    26  				"block_device.3851383343.volume_size":           "5",
    27  				"block_device.3851383343.volume_type":           "standard",
    28  				// Ephemeral
    29  				"block_device.3101711606.delete_on_termination": "false",
    30  				"block_device.3101711606.device_name":           "/dev/sdy",
    31  				"block_device.3101711606.encrypted":             "false",
    32  				"block_device.3101711606.snapshot_id":           "",
    33  				"block_device.3101711606.virtual_name":          "ephemeral0",
    34  				"block_device.3101711606.volume_size":           "",
    35  				"block_device.3101711606.volume_type":           "",
    36  				// Root
    37  				"block_device.56575650.delete_on_termination": "true",
    38  				"block_device.56575650.device_name":           "/dev/sda1",
    39  				"block_device.56575650.encrypted":             "false",
    40  				"block_device.56575650.snapshot_id":           "",
    41  				"block_device.56575650.volume_size":           "10",
    42  				"block_device.56575650.volume_type":           "standard",
    43  			},
    44  			Expected: map[string]string{
    45  				"ebs_block_device.#":                                 "1",
    46  				"ebs_block_device.3851383343.delete_on_termination":  "true",
    47  				"ebs_block_device.3851383343.device_name":            "/dev/sdx",
    48  				"ebs_block_device.3851383343.encrypted":              "false",
    49  				"ebs_block_device.3851383343.snapshot_id":            "",
    50  				"ebs_block_device.3851383343.volume_size":            "5",
    51  				"ebs_block_device.3851383343.volume_type":            "standard",
    52  				"ephemeral_block_device.#":                           "1",
    53  				"ephemeral_block_device.2458403513.device_name":      "/dev/sdy",
    54  				"ephemeral_block_device.2458403513.virtual_name":     "ephemeral0",
    55  				"root_block_device.#":                                "1",
    56  				"root_block_device.3018388612.delete_on_termination": "true",
    57  				"root_block_device.3018388612.device_name":           "/dev/sda1",
    58  				"root_block_device.3018388612.snapshot_id":           "",
    59  				"root_block_device.3018388612.volume_size":           "10",
    60  				"root_block_device.3018388612.volume_type":           "standard",
    61  			},
    62  		},
    63  		"v0.3.7": {
    64  			StateVersion: 0,
    65  			Attributes: map[string]string{
    66  				// EBS
    67  				"block_device.#":                                "2",
    68  				"block_device.3851383343.delete_on_termination": "true",
    69  				"block_device.3851383343.device_name":           "/dev/sdx",
    70  				"block_device.3851383343.encrypted":             "false",
    71  				"block_device.3851383343.snapshot_id":           "",
    72  				"block_device.3851383343.virtual_name":          "",
    73  				"block_device.3851383343.volume_size":           "5",
    74  				"block_device.3851383343.volume_type":           "standard",
    75  				"block_device.3851383343.iops":                  "",
    76  				// Ephemeral
    77  				"block_device.3101711606.delete_on_termination": "false",
    78  				"block_device.3101711606.device_name":           "/dev/sdy",
    79  				"block_device.3101711606.encrypted":             "false",
    80  				"block_device.3101711606.snapshot_id":           "",
    81  				"block_device.3101711606.virtual_name":          "ephemeral0",
    82  				"block_device.3101711606.volume_size":           "",
    83  				"block_device.3101711606.volume_type":           "",
    84  				"block_device.3101711606.iops":                  "",
    85  				// Root
    86  				"root_block_device.#":                                "1",
    87  				"root_block_device.3018388612.delete_on_termination": "true",
    88  				"root_block_device.3018388612.device_name":           "/dev/sda1",
    89  				"root_block_device.3018388612.snapshot_id":           "",
    90  				"root_block_device.3018388612.volume_size":           "10",
    91  				"root_block_device.3018388612.volume_type":           "io1",
    92  				"root_block_device.3018388612.iops":                  "1000",
    93  			},
    94  			Expected: map[string]string{
    95  				"ebs_block_device.#":                                 "1",
    96  				"ebs_block_device.3851383343.delete_on_termination":  "true",
    97  				"ebs_block_device.3851383343.device_name":            "/dev/sdx",
    98  				"ebs_block_device.3851383343.encrypted":              "false",
    99  				"ebs_block_device.3851383343.snapshot_id":            "",
   100  				"ebs_block_device.3851383343.volume_size":            "5",
   101  				"ebs_block_device.3851383343.volume_type":            "standard",
   102  				"ephemeral_block_device.#":                           "1",
   103  				"ephemeral_block_device.2458403513.device_name":      "/dev/sdy",
   104  				"ephemeral_block_device.2458403513.virtual_name":     "ephemeral0",
   105  				"root_block_device.#":                                "1",
   106  				"root_block_device.3018388612.delete_on_termination": "true",
   107  				"root_block_device.3018388612.device_name":           "/dev/sda1",
   108  				"root_block_device.3018388612.snapshot_id":           "",
   109  				"root_block_device.3018388612.volume_size":           "10",
   110  				"root_block_device.3018388612.volume_type":           "io1",
   111  				"root_block_device.3018388612.iops":                  "1000",
   112  			},
   113  		},
   114  	}
   115  
   116  	for tn, tc := range cases {
   117  		is := &terraform.InstanceState{
   118  			ID:         "i-abc123",
   119  			Attributes: tc.Attributes,
   120  		}
   121  		is, err := resourceAwsInstanceMigrateState(
   122  			tc.StateVersion, is, tc.Meta)
   123  
   124  		if err != nil {
   125  			t.Fatalf("bad: %s, err: %#v", tn, err)
   126  		}
   127  
   128  		for k, v := range tc.Expected {
   129  			if is.Attributes[k] != v {
   130  				t.Fatalf(
   131  					"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
   132  					tn, k, v, k, is.Attributes[k], is.Attributes)
   133  			}
   134  		}
   135  	}
   136  }
   137  
   138  func TestAWSInstanceMigrateState_empty(t *testing.T) {
   139  	var is *terraform.InstanceState
   140  	var meta interface{}
   141  
   142  	// should handle nil
   143  	is, err := resourceAwsInstanceMigrateState(0, is, meta)
   144  
   145  	if err != nil {
   146  		t.Fatalf("err: %#v", err)
   147  	}
   148  	if is != nil {
   149  		t.Fatalf("expected nil instancestate, got: %#v", is)
   150  	}
   151  
   152  	// should handle non-nil but empty
   153  	is = &terraform.InstanceState{}
   154  	is, err = resourceAwsInstanceMigrateState(0, is, meta)
   155  
   156  	if err != nil {
   157  		t.Fatalf("err: %#v", err)
   158  	}
   159  }