github.com/rothwerx/packer@v0.9.0/builder/googlecompute/config_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestConfigPrepare(t *testing.T) {
    10  	cases := []struct {
    11  		Key   string
    12  		Value interface{}
    13  		Err   bool
    14  	}{
    15  		{
    16  			"unknown_key",
    17  			"bad",
    18  			true,
    19  		},
    20  
    21  		{
    22  			"private_key_file",
    23  			"/tmp/i/should/not/exist",
    24  			true,
    25  		},
    26  
    27  		{
    28  			"project_id",
    29  			nil,
    30  			true,
    31  		},
    32  		{
    33  			"project_id",
    34  			"foo",
    35  			false,
    36  		},
    37  
    38  		{
    39  			"source_image",
    40  			nil,
    41  			true,
    42  		},
    43  		{
    44  			"source_image",
    45  			"foo",
    46  			false,
    47  		},
    48  
    49  		{
    50  			"zone",
    51  			nil,
    52  			true,
    53  		},
    54  		{
    55  			"zone",
    56  			"foo",
    57  			false,
    58  		},
    59  
    60  		{
    61  			"ssh_timeout",
    62  			"SO BAD",
    63  			true,
    64  		},
    65  		{
    66  			"ssh_timeout",
    67  			"5s",
    68  			false,
    69  		},
    70  
    71  		{
    72  			"state_timeout",
    73  			"SO BAD",
    74  			true,
    75  		},
    76  		{
    77  			"state_timeout",
    78  			"5s",
    79  			false,
    80  		},
    81  		{
    82  			"use_internal_ip",
    83  			nil,
    84  			false,
    85  		},
    86  		{
    87  			"use_internal_ip",
    88  			false,
    89  			false,
    90  		},
    91  		{
    92  			"use_internal_ip",
    93  			"SO VERY BAD",
    94  			true,
    95  		},
    96  		{
    97  			"preemptible",
    98  			nil,
    99  			false,
   100  		},
   101  		{
   102  			"preemptible",
   103  			false,
   104  			false,
   105  		},
   106  		{
   107  			"preemptible",
   108  			"SO VERY BAD",
   109  			true,
   110  		},
   111  	}
   112  
   113  	for _, tc := range cases {
   114  		raw := testConfig(t)
   115  
   116  		if tc.Value == nil {
   117  			delete(raw, tc.Key)
   118  		} else {
   119  			raw[tc.Key] = tc.Value
   120  		}
   121  
   122  		_, warns, errs := NewConfig(raw)
   123  
   124  		if tc.Err {
   125  			testConfigErr(t, warns, errs, tc.Key)
   126  		} else {
   127  			testConfigOk(t, warns, errs)
   128  		}
   129  	}
   130  }
   131  
   132  func TestConfigDefaults(t *testing.T) {
   133  	cases := []struct {
   134  		Read  func(c *Config) interface{}
   135  		Value interface{}
   136  	}{
   137  		{
   138  			func(c *Config) interface{} { return c.Comm.Type },
   139  			"ssh",
   140  		},
   141  
   142  		{
   143  			func(c *Config) interface{} { return c.Comm.SSHPort },
   144  			22,
   145  		},
   146  	}
   147  
   148  	for _, tc := range cases {
   149  		raw := testConfig(t)
   150  
   151  		c, warns, errs := NewConfig(raw)
   152  		testConfigOk(t, warns, errs)
   153  
   154  		actual := tc.Read(c)
   155  		if actual != tc.Value {
   156  			t.Fatalf("bad: %#v", actual)
   157  		}
   158  	}
   159  }
   160  
   161  func TestImageName(t *testing.T) {
   162  	c, _, _ := NewConfig(testConfig(t))
   163  	if !strings.HasPrefix(c.ImageName, "packer-") {
   164  		t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName)
   165  	}
   166  	if strings.Contains(c.ImageName, "{{timestamp}}") {
   167  		t.Errorf("ImageName should be interpolated; found %s", c.ImageName)
   168  	}
   169  }
   170  
   171  func TestRegion(t *testing.T) {
   172  	c, _, _ := NewConfig(testConfig(t))
   173  	if c.Region != "us-east1" {
   174  		t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region)
   175  	}
   176  }
   177  
   178  // Helper stuff below
   179  
   180  func testConfig(t *testing.T) map[string]interface{} {
   181  	return map[string]interface{}{
   182  		"account_file": testAccountFile(t),
   183  		"project_id":   "hashicorp",
   184  		"source_image": "foo",
   185  		"zone":         "us-east1-a",
   186  	}
   187  }
   188  
   189  func testConfigStruct(t *testing.T) *Config {
   190  	c, warns, errs := NewConfig(testConfig(t))
   191  	if len(warns) > 0 {
   192  		t.Fatalf("bad: %#v", len(warns))
   193  	}
   194  	if errs != nil {
   195  		t.Fatalf("bad: %#v", errs)
   196  	}
   197  
   198  	return c
   199  }
   200  
   201  func testConfigErr(t *testing.T, warns []string, err error, extra string) {
   202  	if len(warns) > 0 {
   203  		t.Fatalf("bad: %#v", warns)
   204  	}
   205  	if err == nil {
   206  		t.Fatalf("should error: %s", extra)
   207  	}
   208  }
   209  
   210  func testConfigOk(t *testing.T, warns []string, err error) {
   211  	if len(warns) > 0 {
   212  		t.Fatalf("bad: %#v", warns)
   213  	}
   214  	if err != nil {
   215  		t.Fatalf("bad: %s", err)
   216  	}
   217  }
   218  
   219  func testAccountFile(t *testing.T) string {
   220  	tf, err := ioutil.TempFile("", "packer")
   221  	if err != nil {
   222  		t.Fatalf("err: %s", err)
   223  	}
   224  	defer tf.Close()
   225  
   226  	if _, err := tf.Write([]byte(testAccountContent)); err != nil {
   227  		t.Fatalf("err: %s", err)
   228  	}
   229  
   230  	return tf.Name()
   231  }
   232  
   233  // This is just some dummy data that doesn't actually work (it was revoked
   234  // a long time ago).
   235  const testAccountContent = `{}`