github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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 "source_image_family", 51 nil, 52 false, 53 }, 54 { 55 "source_image_family", 56 "foo", 57 false, 58 }, 59 60 { 61 "zone", 62 nil, 63 true, 64 }, 65 { 66 "zone", 67 "foo", 68 false, 69 }, 70 71 { 72 "ssh_timeout", 73 "SO BAD", 74 true, 75 }, 76 { 77 "ssh_timeout", 78 "5s", 79 false, 80 }, 81 82 { 83 "state_timeout", 84 "SO BAD", 85 true, 86 }, 87 { 88 "state_timeout", 89 "5s", 90 false, 91 }, 92 { 93 "use_internal_ip", 94 nil, 95 false, 96 }, 97 { 98 "use_internal_ip", 99 false, 100 false, 101 }, 102 { 103 "use_internal_ip", 104 "SO VERY BAD", 105 true, 106 }, 107 { 108 "preemptible", 109 nil, 110 false, 111 }, 112 { 113 "preemptible", 114 false, 115 false, 116 }, 117 { 118 "preemptible", 119 "SO VERY BAD", 120 true, 121 }, 122 { 123 "image_family", 124 nil, 125 false, 126 }, 127 { 128 "image_family", 129 "", 130 false, 131 }, 132 { 133 "image_family", 134 "foo-bar", 135 false, 136 }, 137 { 138 "image_family", 139 "foo bar", 140 true, 141 }, 142 { 143 "scopes", 144 []string{}, 145 false, 146 }, 147 { 148 "scopes", 149 []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control", "https://www.googleapis.com/auth/sqlservice.admin"}, 150 false, 151 }, 152 { 153 "scopes", 154 []string{"https://www.googleapis.com/auth/cloud-platform"}, 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 } 177 178 func TestConfigDefaults(t *testing.T) { 179 cases := []struct { 180 Read func(c *Config) interface{} 181 Value interface{} 182 }{ 183 { 184 func(c *Config) interface{} { return c.Comm.Type }, 185 "ssh", 186 }, 187 188 { 189 func(c *Config) interface{} { return c.Comm.SSHPort }, 190 22, 191 }, 192 } 193 194 for _, tc := range cases { 195 raw := testConfig(t) 196 197 c, warns, errs := NewConfig(raw) 198 testConfigOk(t, warns, errs) 199 200 actual := tc.Read(c) 201 if actual != tc.Value { 202 t.Fatalf("bad: %#v", actual) 203 } 204 } 205 } 206 207 func TestImageName(t *testing.T) { 208 c, _, _ := NewConfig(testConfig(t)) 209 if !strings.HasPrefix(c.ImageName, "packer-") { 210 t.Fatalf("ImageName should have 'packer-' prefix, found %s", c.ImageName) 211 } 212 if strings.Contains(c.ImageName, "{{timestamp}}") { 213 t.Errorf("ImageName should be interpolated; found %s", c.ImageName) 214 } 215 } 216 217 func TestRegion(t *testing.T) { 218 c, _, _ := NewConfig(testConfig(t)) 219 if c.Region != "us-east1" { 220 t.Fatalf("Region should be 'us-east1' given Zone of 'us-east1-a', but is %s", c.Region) 221 } 222 } 223 224 // Helper stuff below 225 226 func testConfig(t *testing.T) map[string]interface{} { 227 return map[string]interface{}{ 228 "account_file": testAccountFile(t), 229 "project_id": "hashicorp", 230 "source_image": "foo", 231 "ssh_username": "root", 232 "image_family": "bar", 233 "zone": "us-east1-a", 234 } 235 } 236 237 func testConfigStruct(t *testing.T) *Config { 238 c, warns, errs := NewConfig(testConfig(t)) 239 if len(warns) > 0 { 240 t.Fatalf("bad: %#v", len(warns)) 241 } 242 if errs != nil { 243 t.Fatalf("bad: %#v", errs) 244 } 245 246 return c 247 } 248 249 func testConfigErr(t *testing.T, warns []string, err error, extra string) { 250 if len(warns) > 0 { 251 t.Fatalf("bad: %#v", warns) 252 } 253 if err == nil { 254 t.Fatalf("should error: %s", extra) 255 } 256 } 257 258 func testConfigOk(t *testing.T, warns []string, err error) { 259 if len(warns) > 0 { 260 t.Fatalf("bad: %#v", warns) 261 } 262 if err != nil { 263 t.Fatalf("bad: %s", err) 264 } 265 } 266 267 func testAccountFile(t *testing.T) string { 268 tf, err := ioutil.TempFile("", "packer") 269 if err != nil { 270 t.Fatalf("err: %s", err) 271 } 272 defer tf.Close() 273 274 if _, err := tf.Write([]byte(testAccountContent)); err != nil { 275 t.Fatalf("err: %s", err) 276 } 277 278 return tf.Name() 279 } 280 281 // This is just some dummy data that doesn't actually work (it was revoked 282 // a long time ago). 283 const testAccountContent = `{}`