github.com/sneal/packer@v0.5.2/builder/googlecompute/config_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func testConfig(t *testing.T) map[string]interface{} {
     8  	return map[string]interface{}{
     9  		"bucket_name":         "foo",
    10  		"client_secrets_file": testClientSecretsFile(t),
    11  		"private_key_file":    testPrivateKeyFile(t),
    12  		"project_id":          "hashicorp",
    13  		"source_image":        "foo",
    14  		"zone":                "us-east-1a",
    15  	}
    16  }
    17  
    18  func testConfigStruct(t *testing.T) *Config {
    19  	c, warns, errs := NewConfig(testConfig(t))
    20  	if len(warns) > 0 {
    21  		t.Fatalf("bad: %#v", len(warns))
    22  	}
    23  	if errs != nil {
    24  		t.Fatalf("bad: %#v", errs)
    25  	}
    26  
    27  	return c
    28  }
    29  
    30  func testConfigErr(t *testing.T, warns []string, err error, extra string) {
    31  	if len(warns) > 0 {
    32  		t.Fatalf("bad: %#v", warns)
    33  	}
    34  	if err == nil {
    35  		t.Fatalf("should error: %s", extra)
    36  	}
    37  }
    38  
    39  func testConfigOk(t *testing.T, warns []string, err error) {
    40  	if len(warns) > 0 {
    41  		t.Fatalf("bad: %#v", warns)
    42  	}
    43  	if err != nil {
    44  		t.Fatalf("bad: %s", err)
    45  	}
    46  }
    47  
    48  func TestConfigPrepare(t *testing.T) {
    49  	cases := []struct {
    50  		Key   string
    51  		Value interface{}
    52  		Err   bool
    53  	}{
    54  		{
    55  			"unknown_key",
    56  			"bad",
    57  			true,
    58  		},
    59  
    60  		{
    61  			"bucket_name",
    62  			nil,
    63  			true,
    64  		},
    65  		{
    66  			"bucket_name",
    67  			"good",
    68  			false,
    69  		},
    70  
    71  		{
    72  			"client_secrets_file",
    73  			nil,
    74  			true,
    75  		},
    76  		{
    77  			"client_secrets_file",
    78  			testClientSecretsFile(t),
    79  			false,
    80  		},
    81  		{
    82  			"client_secrets_file",
    83  			"/tmp/i/should/not/exist",
    84  			true,
    85  		},
    86  
    87  		{
    88  			"private_key_file",
    89  			nil,
    90  			true,
    91  		},
    92  		{
    93  			"private_key_file",
    94  			testPrivateKeyFile(t),
    95  			false,
    96  		},
    97  		{
    98  			"private_key_file",
    99  			"/tmp/i/should/not/exist",
   100  			true,
   101  		},
   102  
   103  		{
   104  			"project_id",
   105  			nil,
   106  			true,
   107  		},
   108  		{
   109  			"project_id",
   110  			"foo",
   111  			false,
   112  		},
   113  
   114  		{
   115  			"source_image",
   116  			nil,
   117  			true,
   118  		},
   119  		{
   120  			"source_image",
   121  			"foo",
   122  			false,
   123  		},
   124  
   125  		{
   126  			"zone",
   127  			nil,
   128  			true,
   129  		},
   130  		{
   131  			"zone",
   132  			"foo",
   133  			false,
   134  		},
   135  
   136  		{
   137  			"ssh_timeout",
   138  			"SO BAD",
   139  			true,
   140  		},
   141  		{
   142  			"ssh_timeout",
   143  			"5s",
   144  			false,
   145  		},
   146  
   147  		{
   148  			"state_timeout",
   149  			"SO BAD",
   150  			true,
   151  		},
   152  		{
   153  			"state_timeout",
   154  			"5s",
   155  			false,
   156  		},
   157  	}
   158  
   159  	for _, tc := range cases {
   160  		raw := testConfig(t)
   161  
   162  		if tc.Value == nil {
   163  			delete(raw, tc.Key)
   164  		} else {
   165  			raw[tc.Key] = tc.Value
   166  		}
   167  
   168  		_, warns, errs := NewConfig(raw)
   169  
   170  		if tc.Err {
   171  			testConfigErr(t, warns, errs, tc.Key)
   172  		} else {
   173  			testConfigOk(t, warns, errs)
   174  		}
   175  	}
   176  }