github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/common/dump_config_test.go (about)

     1  package common
     2  
     3  import "testing"
     4  
     5  func TestShouldDumpPublicValues(t *testing.T) {
     6  	type S struct {
     7  		MyString string
     8  		myString string
     9  	}
    10  
    11  	data := &S{
    12  		MyString: "value1",
    13  	}
    14  
    15  	dumps := make([]string, 0, 2)
    16  	DumpConfig(data, func(s string) { dumps = append(dumps, s) })
    17  
    18  	if len(dumps) != 1 {
    19  		t.Fatalf("Expected len(dumps) to be 1, but got %d", len(dumps))
    20  
    21  	}
    22  	if dumps[0] != "MyString=value1" {
    23  		t.Errorf("Expected dumps[0] to be 'MyString=value1', but got %s", dumps[0])
    24  	}
    25  }
    26  
    27  func TestShouldOnlyDumpStrings(t *testing.T) {
    28  	type S struct {
    29  		MyString        string
    30  		MyInt           int
    31  		MyFloat32       float32
    32  		MyStringPointer *string
    33  	}
    34  
    35  	s := "value1"
    36  	data := &S{
    37  		MyString:        s,
    38  		MyInt:           1,
    39  		MyFloat32:       2.0,
    40  		MyStringPointer: &s,
    41  	}
    42  
    43  	dumps := make([]string, 0, 4)
    44  	DumpConfig(data, func(s string) { dumps = append(dumps, s) })
    45  
    46  	if len(dumps) != 1 {
    47  		t.Fatalf("Expected len(dumps) to be 1, but got %d", len(dumps))
    48  
    49  	}
    50  	if dumps[0] != "MyString=value1" {
    51  		t.Errorf("Expected dumps[0] to be 'MyString=value1', but got %s", dumps[0])
    52  	}
    53  }
    54  
    55  func TestDumpShouldMaskSensitiveFieldValues(t *testing.T) {
    56  	type S struct {
    57  		MyString        string
    58  		MySecret        string
    59  		MySecretValue   string
    60  		MyPassword      string
    61  		MyPasswordValue string
    62  	}
    63  
    64  	data := &S{
    65  		MyString:        "my-string",
    66  		MySecret:        "s3cr3t",
    67  		MySecretValue:   "s3cr3t-value",
    68  		MyPassword:      "p@ssw0rd",
    69  		MyPasswordValue: "p@ssw0rd-value",
    70  	}
    71  
    72  	dumps := make([]string, 0, 5)
    73  	DumpConfig(data, func(s string) { dumps = append(dumps, s) })
    74  
    75  	if len(dumps) != 5 {
    76  		t.Fatalf("Expected len(dumps) to be 5, but got %d", len(dumps))
    77  
    78  	}
    79  	if dumps[0] != "MyString=my-string" {
    80  		t.Errorf("Expected dumps[0] to be 'MyString=my-string', but got %s", dumps[0])
    81  	}
    82  	if dumps[1] != "MySecret=******" {
    83  		t.Errorf("Expected dumps[1] to be 'MySecret=******', but got %s", dumps[1])
    84  	}
    85  	if dumps[2] != "MySecretValue=************" {
    86  		t.Errorf("Expected dumps[2] to be 'MySecret=************', but got %s", dumps[2])
    87  	}
    88  	if dumps[3] != "MyPassword=********" {
    89  		t.Errorf("Expected dumps[3] to be 'MyPassword=******** but got %s", dumps[3])
    90  	}
    91  	if dumps[4] != "MyPasswordValue=**************" {
    92  		t.Errorf("Expected dumps[4] to be 'MyPasswordValue=**************' but got %s", dumps[4])
    93  	}
    94  }
    95  
    96  func TestDumpConfigShouldDumpTopLevelValuesOnly(t *testing.T) {
    97  	type N struct {
    98  		NestedString string
    99  	}
   100  
   101  	type S struct {
   102  		Nested1  N
   103  		MyString string
   104  		Nested2  N
   105  	}
   106  
   107  	data := &S{
   108  		Nested1: N{
   109  			NestedString: "nested-string1",
   110  		},
   111  		MyString: "my-string",
   112  		Nested2: N{
   113  			NestedString: "nested-string2",
   114  		},
   115  	}
   116  
   117  	dumps := make([]string, 0, 1)
   118  	DumpConfig(data, func(s string) { dumps = append(dumps, s) })
   119  
   120  	if len(dumps) != 1 {
   121  		t.Fatalf("Expected len(dumps) to be 1, but got %d", len(dumps))
   122  
   123  	}
   124  	if dumps[0] != "MyString=my-string" {
   125  		t.Errorf("Expected dumps[0] to be 'MyString=my-string', but got %s", dumps[0])
   126  	}
   127  }