github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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 "image_family", 113 nil, 114 false, 115 }, 116 { 117 "image_family", 118 "", 119 false, 120 }, 121 { 122 "image_family", 123 "foo-bar", 124 false, 125 }, 126 { 127 "image_family", 128 "foo bar", 129 true, 130 }, 131 } 132 133 for _, tc := range cases { 134 raw := testConfig(t) 135 136 if tc.Value == nil { 137 delete(raw, tc.Key) 138 } else { 139 raw[tc.Key] = tc.Value 140 } 141 142 _, warns, errs := NewConfig(raw) 143 144 if tc.Err { 145 testConfigErr(t, warns, errs, tc.Key) 146 } else { 147 testConfigOk(t, warns, errs) 148 } 149 } 150 } 151 152 func TestConfigDefaults(t *testing.T) { 153 cases := []struct { 154 Read func(c *Config) interface{} 155 Value interface{} 156 }{ 157 { 158 func(c *Config) interface{} { return c.Comm.Type }, 159 "ssh", 160 }, 161 162 { 163 func(c *Config) interface{} { return c.Comm.SSHPort }, 164 22, 165 }, 166 } 167 168 for _, tc := range cases { 169 raw := testConfig(t) 170 171 c, warns, errs := NewConfig(raw) 172 testConfigOk(t, warns, errs) 173 174 actual := tc.Read(c) 175 if actual != tc.Value { 176 t.Fatalf("bad: %#v", actual) 177 } 178 } 179 } 180 181 func TestImageName(t *testing.T) { 182 c, _, _ := NewConfig(testConfig(t)) 183 if !strings.HasPrefix(c.ImageName, "packer-") { 184 t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName) 185 } 186 if strings.Contains(c.ImageName, "{{timestamp}}") { 187 t.Errorf("ImageName should be interpolated; found %s", c.ImageName) 188 } 189 } 190 191 func TestRegion(t *testing.T) { 192 c, _, _ := NewConfig(testConfig(t)) 193 if c.Region != "us-east1" { 194 t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region) 195 } 196 } 197 198 // Helper stuff below 199 200 func testConfig(t *testing.T) map[string]interface{} { 201 return map[string]interface{}{ 202 "account_file": testAccountFile(t), 203 "project_id": "hashicorp", 204 "source_image": "foo", 205 "image_family": "bar", 206 "zone": "us-east1-a", 207 } 208 } 209 210 func testConfigStruct(t *testing.T) *Config { 211 c, warns, errs := NewConfig(testConfig(t)) 212 if len(warns) > 0 { 213 t.Fatalf("bad: %#v", len(warns)) 214 } 215 if errs != nil { 216 t.Fatalf("bad: %#v", errs) 217 } 218 219 return c 220 } 221 222 func testConfigErr(t *testing.T, warns []string, err error, extra string) { 223 if len(warns) > 0 { 224 t.Fatalf("bad: %#v", warns) 225 } 226 if err == nil { 227 t.Fatalf("should error: %s", extra) 228 } 229 } 230 231 func testConfigOk(t *testing.T, warns []string, err error) { 232 if len(warns) > 0 { 233 t.Fatalf("bad: %#v", warns) 234 } 235 if err != nil { 236 t.Fatalf("bad: %s", err) 237 } 238 } 239 240 func testAccountFile(t *testing.T) string { 241 tf, err := ioutil.TempFile("", "packer") 242 if err != nil { 243 t.Fatalf("err: %s", err) 244 } 245 defer tf.Close() 246 247 if _, err := tf.Write([]byte(testAccountContent)); err != nil { 248 t.Fatalf("err: %s", err) 249 } 250 251 return tf.Name() 252 } 253 254 // This is just some dummy data that doesn't actually work (it was revoked 255 // a long time ago). 256 const testAccountContent = `{}`