github.com/hashicorp/packer@v1.14.3/fix/fixer_amazon_private_ip_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 TestFixerAmazonPrivateIP_Impl(t *testing.T) { 12 var _ Fixer = new(FixerAmazonPrivateIP) 13 } 14 15 func TestFixerAmazonPrivateIP(t *testing.T) { 16 cases := []struct { 17 Input map[string]interface{} 18 Expected map[string]interface{} 19 }{ 20 // Attach field == false 21 { 22 Input: map[string]interface{}{ 23 "type": "amazon-ebs", 24 "ssh_private_ip": false, 25 }, 26 27 Expected: map[string]interface{}{ 28 "type": "amazon-ebs", 29 "ssh_interface": "public_ip", 30 }, 31 }, 32 33 // Attach field == true 34 { 35 Input: map[string]interface{}{ 36 "type": "amazon-ebs", 37 "ssh_private_ip": true, 38 }, 39 40 Expected: map[string]interface{}{ 41 "type": "amazon-ebs", 42 "ssh_interface": "private_ip", 43 }, 44 }, 45 46 // ssh_private_ip specified as string 47 { 48 Input: map[string]interface{}{ 49 "type": "amazon-ebs", 50 "ssh_private_ip": "true", 51 }, 52 53 Expected: map[string]interface{}{ 54 "type": "amazon-ebs", 55 "ssh_interface": "private_ip", 56 }, 57 }, 58 } 59 60 for _, tc := range cases { 61 var f FixerAmazonPrivateIP 62 63 input := map[string]interface{}{ 64 "builders": []map[string]interface{}{tc.Input}, 65 } 66 67 expected := map[string]interface{}{ 68 "builders": []map[string]interface{}{tc.Expected}, 69 } 70 71 output, err := f.Fix(input) 72 if err != nil { 73 t.Fatalf("err: %s", err) 74 } 75 76 if !reflect.DeepEqual(output, expected) { 77 t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected) 78 } 79 } 80 } 81 82 func TestFixerAmazonPrivateIPNonBoolean(t *testing.T) { 83 var f FixerAmazonPrivateIP 84 85 input := map[string]interface{}{ 86 "builders": []map[string]interface{}{{ 87 "type": "amazon-ebs", 88 "ssh_private_ip": "not-a-boolean-value", 89 }}, 90 } 91 92 _, err := f.Fix(input) 93 if err == nil { 94 t.Fatal("should have errored") 95 } 96 }