github.com/hashicorp/packer@v1.14.3/fix/fixer_comm_config_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package fix 5 6 import ( 7 "reflect" 8 "testing" 9 ) 10 11 func TestFixerCommConfig_Impl(t *testing.T) { 12 var _ Fixer = new(FixerCommConfig) 13 } 14 15 func TestFixerCommConfig_Fix(t *testing.T) { 16 cases := []struct { 17 Input map[string]interface{} 18 Expected map[string]interface{} 19 }{ 20 // set host_port_min 21 { 22 Input: map[string]interface{}{ 23 "type": "virtualbox-iso", 24 "host_port_min": 2222, 25 }, 26 27 Expected: map[string]interface{}{ 28 "type": "virtualbox-iso", 29 "host_port_min": 2222, 30 }, 31 }, 32 33 // set ssh_host_port_min (old key) 34 { 35 Input: map[string]interface{}{ 36 "type": "virtualbox-ovf", 37 "ssh_host_port_min": 2222, 38 }, 39 40 Expected: map[string]interface{}{ 41 "type": "virtualbox-ovf", 42 "host_port_min": 2222, 43 }, 44 }, 45 46 // set ssh_host_port_min and host_port_min 47 // host_port_min takes precedence 48 { 49 Input: map[string]interface{}{ 50 "type": "virtualbox-vm", 51 "ssh_host_port_min": 1234, 52 "host_port_min": 4321, 53 }, 54 55 Expected: map[string]interface{}{ 56 "type": "virtualbox-vm", 57 "host_port_min": 4321, 58 }, 59 }, 60 61 // set host_port_max 62 { 63 Input: map[string]interface{}{ 64 "type": "virtualbox-iso", 65 "host_port_max": 4444, 66 }, 67 68 Expected: map[string]interface{}{ 69 "type": "virtualbox-iso", 70 "host_port_max": 4444, 71 }, 72 }, 73 74 // set ssh_host_port_max (old key) 75 { 76 Input: map[string]interface{}{ 77 "type": "virtualbox-iso", 78 "ssh_host_port_max": 4444, 79 }, 80 81 Expected: map[string]interface{}{ 82 "type": "virtualbox-iso", 83 "host_port_max": 4444, 84 }, 85 }, 86 87 // set ssh_host_port_max and host_port_max 88 // host_port_max takes precedence 89 { 90 Input: map[string]interface{}{ 91 "type": "virtualbox-vm", 92 "ssh_host_port_max": 1234, 93 "host_port_max": 4321, 94 }, 95 96 Expected: map[string]interface{}{ 97 "type": "virtualbox-vm", 98 "host_port_max": 4321, 99 }, 100 }, 101 102 // set skip_nat_mapping 103 { 104 Input: map[string]interface{}{ 105 "type": "virtualbox-vm", 106 "skip_nat_mapping": true, 107 }, 108 109 Expected: map[string]interface{}{ 110 "type": "virtualbox-vm", 111 "skip_nat_mapping": true, 112 }, 113 }, 114 115 // set ssh_skip_nat_mapping (old key) 116 { 117 Input: map[string]interface{}{ 118 "type": "virtualbox-vm", 119 "ssh_skip_nat_mapping": true, 120 }, 121 122 Expected: map[string]interface{}{ 123 "type": "virtualbox-vm", 124 "skip_nat_mapping": true, 125 }, 126 }, 127 128 // set ssh_skip_nat_mapping and skip_nat_mapping 129 // skip_nat_mapping takes precedence 130 { 131 Input: map[string]interface{}{ 132 "type": "virtualbox-iso", 133 "ssh_skip_nat_mapping": false, 134 "skip_nat_mapping": true, 135 }, 136 137 Expected: map[string]interface{}{ 138 "type": "virtualbox-iso", 139 "skip_nat_mapping": true, 140 }, 141 }, 142 } 143 144 for _, tc := range cases { 145 var f FixerCommConfig 146 147 input := map[string]interface{}{ 148 "builders": []interface{}{tc.Input}, 149 } 150 151 expected := map[string]interface{}{ 152 "builders": []interface{}{tc.Expected}, 153 } 154 155 output, err := f.Fix(input) 156 if err != nil { 157 t.Fatalf("err: %s", err) 158 } 159 160 if !reflect.DeepEqual(output, expected) { 161 t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) 162 } 163 } 164 }