github.com/hashicorp/packer@v1.14.3/fix/fixer_sshkeypath_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 TestFixerSSHKeyPath_Impl(t *testing.T) {
    12  	var _ Fixer = new(FixerSSHKeyPath)
    13  }
    14  
    15  func TestFixerSSHKeyPath_Fix(t *testing.T) {
    16  	cases := []struct {
    17  		Input    map[string]interface{}
    18  		Expected map[string]interface{}
    19  	}{
    20  		// No key_path field
    21  		{
    22  			Input: map[string]interface{}{
    23  				"type": "virtualbox",
    24  			},
    25  
    26  			Expected: map[string]interface{}{
    27  				"type": "virtualbox",
    28  			},
    29  		},
    30  
    31  		// private_key_file without key_path
    32  		{
    33  			Input: map[string]interface{}{
    34  				"ssh_private_key_file": "id_rsa",
    35  			},
    36  
    37  			Expected: map[string]interface{}{
    38  				"ssh_private_key_file": "id_rsa",
    39  			},
    40  		},
    41  
    42  		// key_path without private_key_file
    43  		{
    44  			Input: map[string]interface{}{
    45  				"ssh_key_path": "id_rsa",
    46  			},
    47  
    48  			Expected: map[string]interface{}{
    49  				"ssh_private_key_file": "id_rsa",
    50  			},
    51  		},
    52  
    53  		// key_path and private_key_file
    54  		{
    55  			Input: map[string]interface{}{
    56  				"ssh_key_path":         "key_id_rsa",
    57  				"ssh_private_key_file": "private_id_rsa",
    58  			},
    59  
    60  			Expected: map[string]interface{}{
    61  				"ssh_private_key_file": "private_id_rsa",
    62  			},
    63  		},
    64  	}
    65  
    66  	for _, tc := range cases {
    67  		var f FixerSSHKeyPath
    68  
    69  		input := map[string]interface{}{
    70  			"builders": []map[string]interface{}{tc.Input},
    71  		}
    72  
    73  		expected := map[string]interface{}{
    74  			"builders": []map[string]interface{}{tc.Expected},
    75  		}
    76  
    77  		output, err := f.Fix(input)
    78  		if err != nil {
    79  			t.Fatalf("err: %s", err)
    80  		}
    81  
    82  		if !reflect.DeepEqual(output, expected) {
    83  			t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
    84  		}
    85  	}
    86  }