github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/instance/builder_test.go (about) 1 package instance 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/packer/packer" 9 ) 10 11 func testConfig() (config map[string]interface{}, tf *os.File) { 12 tf, err := ioutil.TempFile("", "packer") 13 if err != nil { 14 panic(err) 15 } 16 17 config = map[string]interface{}{ 18 "account_id": "foo", 19 "ami_name": "foo", 20 "instance_type": "m1.small", 21 "region": "us-east-1", 22 "s3_bucket": "foo", 23 "source_ami": "foo", 24 "ssh_username": "bob", 25 "x509_cert_path": tf.Name(), 26 "x509_key_path": tf.Name(), 27 "x509_upload_path": "/foo", 28 } 29 30 return config, tf 31 } 32 33 func TestBuilder_ImplementsBuilder(t *testing.T) { 34 var raw interface{} 35 raw = &Builder{} 36 if _, ok := raw.(packer.Builder); !ok { 37 t.Fatalf("Builder should be a builder") 38 } 39 } 40 41 func TestBuilderPrepare_AccountId(t *testing.T) { 42 b := &Builder{} 43 config, tempfile := testConfig() 44 config["skip_region_validation"] = true 45 46 defer os.Remove(tempfile.Name()) 47 defer tempfile.Close() 48 49 config["account_id"] = "" 50 warnings, err := b.Prepare(config) 51 if len(warnings) > 0 { 52 t.Fatalf("bad: %#v", warnings) 53 } 54 if err == nil { 55 t.Fatal("should have error") 56 } 57 58 config["account_id"] = "foo" 59 warnings, err = b.Prepare(config) 60 if len(warnings) > 0 { 61 t.Fatalf("bad: %#v", warnings) 62 } 63 if err != nil { 64 t.Errorf("err: %s", err) 65 } 66 67 config["account_id"] = "0123-0456-7890" 68 warnings, err = b.Prepare(config) 69 if len(warnings) > 0 { 70 t.Fatalf("bad: %#v", warnings) 71 } 72 if err != nil { 73 t.Fatalf("err: %s", err) 74 } 75 76 if b.config.AccountId != "012304567890" { 77 t.Errorf("should strip hyphens: %s", b.config.AccountId) 78 } 79 } 80 81 func TestBuilderPrepare_AMIName(t *testing.T) { 82 var b Builder 83 config, tempfile := testConfig() 84 defer os.Remove(tempfile.Name()) 85 defer tempfile.Close() 86 87 // Test good 88 config["ami_name"] = "foo" 89 config["skip_region_validation"] = true 90 warnings, err := b.Prepare(config) 91 if len(warnings) > 0 { 92 t.Fatalf("bad: %#v", warnings) 93 } 94 if err != nil { 95 t.Fatalf("should not have error: %s", err) 96 } 97 98 // Test bad 99 config["ami_name"] = "foo {{" 100 b = Builder{} 101 warnings, err = b.Prepare(config) 102 if len(warnings) > 0 { 103 t.Fatalf("bad: %#v", warnings) 104 } 105 if err == nil { 106 t.Fatal("should have error") 107 } 108 109 // Test bad 110 delete(config, "ami_name") 111 b = Builder{} 112 warnings, err = b.Prepare(config) 113 if len(warnings) > 0 { 114 t.Fatalf("bad: %#v", warnings) 115 } 116 if err == nil { 117 t.Fatal("should have error") 118 } 119 } 120 121 func TestBuilderPrepare_BundleDestination(t *testing.T) { 122 b := &Builder{} 123 config, tempfile := testConfig() 124 config["skip_region_validation"] = true 125 defer os.Remove(tempfile.Name()) 126 defer tempfile.Close() 127 128 config["bundle_destination"] = "" 129 warnings, err := b.Prepare(config) 130 if len(warnings) > 0 { 131 t.Fatalf("bad: %#v", warnings) 132 } 133 if err != nil { 134 t.Fatalf("err: %s", err) 135 } 136 137 if b.config.BundleDestination != "/tmp" { 138 t.Fatalf("bad: %s", b.config.BundleDestination) 139 } 140 } 141 142 func TestBuilderPrepare_BundlePrefix(t *testing.T) { 143 b := &Builder{} 144 config, tempfile := testConfig() 145 config["skip_region_validation"] = true 146 defer os.Remove(tempfile.Name()) 147 defer tempfile.Close() 148 149 warnings, err := b.Prepare(config) 150 if len(warnings) > 0 { 151 t.Fatalf("bad: %#v", warnings) 152 } 153 if err != nil { 154 t.Fatalf("err: %s", err) 155 } 156 157 if b.config.BundlePrefix == "" { 158 t.Fatalf("bad: %s", b.config.BundlePrefix) 159 } 160 } 161 162 func TestBuilderPrepare_InvalidKey(t *testing.T) { 163 var b Builder 164 config, tempfile := testConfig() 165 defer os.Remove(tempfile.Name()) 166 defer tempfile.Close() 167 168 // Add a random key 169 config["i_should_not_be_valid"] = true 170 warnings, err := b.Prepare(config) 171 if len(warnings) > 0 { 172 t.Fatalf("bad: %#v", warnings) 173 } 174 if err == nil { 175 t.Fatal("should have error") 176 } 177 } 178 179 func TestBuilderPrepare_S3Bucket(t *testing.T) { 180 b := &Builder{} 181 config, tempfile := testConfig() 182 config["skip_region_validation"] = true 183 defer os.Remove(tempfile.Name()) 184 defer tempfile.Close() 185 186 config["s3_bucket"] = "" 187 warnings, err := b.Prepare(config) 188 if len(warnings) > 0 { 189 t.Fatalf("bad: %#v", warnings) 190 } 191 if err == nil { 192 t.Fatal("should have error") 193 } 194 195 config["s3_bucket"] = "foo" 196 warnings, err = b.Prepare(config) 197 if len(warnings) > 0 { 198 t.Fatalf("bad: %#v", warnings) 199 } 200 if err != nil { 201 t.Errorf("err: %s", err) 202 } 203 } 204 205 func TestBuilderPrepare_X509CertPath(t *testing.T) { 206 b := &Builder{} 207 config, tempfile := testConfig() 208 config["skip_region_validation"] = true 209 defer os.Remove(tempfile.Name()) 210 defer tempfile.Close() 211 212 config["x509_cert_path"] = "" 213 warnings, err := b.Prepare(config) 214 if len(warnings) > 0 { 215 t.Fatalf("bad: %#v", warnings) 216 } 217 if err == nil { 218 t.Fatal("should have error") 219 } 220 221 config["x509_cert_path"] = "i/am/a/file/that/doesnt/exist" 222 warnings, err = b.Prepare(config) 223 if len(warnings) > 0 { 224 t.Fatalf("bad: %#v", warnings) 225 } 226 if err == nil { 227 t.Error("should have error") 228 } 229 230 tf, err := ioutil.TempFile("", "packer") 231 if err != nil { 232 t.Fatalf("error tempfile: %s", err) 233 } 234 defer os.Remove(tf.Name()) 235 defer tf.Close() 236 237 config["x509_cert_path"] = tf.Name() 238 warnings, err = b.Prepare(config) 239 if len(warnings) > 0 { 240 t.Fatalf("bad: %#v", warnings) 241 } 242 if err != nil { 243 t.Fatalf("should not have error: %s", err) 244 } 245 } 246 247 func TestBuilderPrepare_X509KeyPath(t *testing.T) { 248 b := &Builder{} 249 config, tempfile := testConfig() 250 config["skip_region_validation"] = true 251 defer os.Remove(tempfile.Name()) 252 defer tempfile.Close() 253 254 config["x509_key_path"] = "" 255 warnings, err := b.Prepare(config) 256 if len(warnings) > 0 { 257 t.Fatalf("bad: %#v", warnings) 258 } 259 if err == nil { 260 t.Fatal("should have error") 261 } 262 263 config["x509_key_path"] = "i/am/a/file/that/doesnt/exist" 264 warnings, err = b.Prepare(config) 265 if len(warnings) > 0 { 266 t.Fatalf("bad: %#v", warnings) 267 } 268 if err == nil { 269 t.Error("should have error") 270 } 271 272 tf, err := ioutil.TempFile("", "packer") 273 if err != nil { 274 t.Fatalf("error tempfile: %s", err) 275 } 276 defer os.Remove(tf.Name()) 277 defer tf.Close() 278 279 config["x509_key_path"] = tf.Name() 280 warnings, err = b.Prepare(config) 281 if len(warnings) > 0 { 282 t.Fatalf("bad: %#v", warnings) 283 } 284 if err != nil { 285 t.Fatalf("should not have error: %s", err) 286 } 287 } 288 289 func TestBuilderPrepare_X509UploadPath(t *testing.T) { 290 b := &Builder{} 291 config, tempfile := testConfig() 292 config["skip_region_validation"] = true 293 defer os.Remove(tempfile.Name()) 294 defer tempfile.Close() 295 296 config["x509_upload_path"] = "" 297 warnings, err := b.Prepare(config) 298 if len(warnings) > 0 { 299 t.Fatalf("bad: %#v", warnings) 300 } 301 if err != nil { 302 t.Fatalf("should not have error: %s", err) 303 } 304 }