github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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 defer os.Remove(tempfile.Name()) 45 defer tempfile.Close() 46 47 config["account_id"] = "" 48 warnings, err := b.Prepare(config) 49 if len(warnings) > 0 { 50 t.Fatalf("bad: %#v", warnings) 51 } 52 if err == nil { 53 t.Fatal("should have error") 54 } 55 56 config["account_id"] = "foo" 57 warnings, err = b.Prepare(config) 58 if len(warnings) > 0 { 59 t.Fatalf("bad: %#v", warnings) 60 } 61 if err != nil { 62 t.Errorf("err: %s", err) 63 } 64 65 config["account_id"] = "0123-0456-7890" 66 warnings, err = b.Prepare(config) 67 if len(warnings) > 0 { 68 t.Fatalf("bad: %#v", warnings) 69 } 70 if err != nil { 71 t.Fatalf("err: %s", err) 72 } 73 74 if b.config.AccountId != "012304567890" { 75 t.Errorf("should strip hyphens: %s", b.config.AccountId) 76 } 77 } 78 79 func TestBuilderPrepare_AMIName(t *testing.T) { 80 var b Builder 81 config, tempfile := testConfig() 82 defer os.Remove(tempfile.Name()) 83 defer tempfile.Close() 84 85 // Test good 86 config["ami_name"] = "foo" 87 warnings, err := b.Prepare(config) 88 if len(warnings) > 0 { 89 t.Fatalf("bad: %#v", warnings) 90 } 91 if err != nil { 92 t.Fatalf("should not have error: %s", err) 93 } 94 95 // Test bad 96 config["ami_name"] = "foo {{" 97 b = Builder{} 98 warnings, err = b.Prepare(config) 99 if len(warnings) > 0 { 100 t.Fatalf("bad: %#v", warnings) 101 } 102 if err == nil { 103 t.Fatal("should have error") 104 } 105 106 // Test bad 107 delete(config, "ami_name") 108 b = Builder{} 109 warnings, err = b.Prepare(config) 110 if len(warnings) > 0 { 111 t.Fatalf("bad: %#v", warnings) 112 } 113 if err == nil { 114 t.Fatal("should have error") 115 } 116 } 117 118 func TestBuilderPrepare_BundleDestination(t *testing.T) { 119 b := &Builder{} 120 config, tempfile := testConfig() 121 defer os.Remove(tempfile.Name()) 122 defer tempfile.Close() 123 124 config["bundle_destination"] = "" 125 warnings, err := b.Prepare(config) 126 if len(warnings) > 0 { 127 t.Fatalf("bad: %#v", warnings) 128 } 129 if err != nil { 130 t.Fatalf("err: %s", err) 131 } 132 133 if b.config.BundleDestination != "/tmp" { 134 t.Fatalf("bad: %s", b.config.BundleDestination) 135 } 136 } 137 138 func TestBuilderPrepare_BundlePrefix(t *testing.T) { 139 b := &Builder{} 140 config, tempfile := testConfig() 141 defer os.Remove(tempfile.Name()) 142 defer tempfile.Close() 143 144 warnings, err := b.Prepare(config) 145 if len(warnings) > 0 { 146 t.Fatalf("bad: %#v", warnings) 147 } 148 if err != nil { 149 t.Fatalf("err: %s", err) 150 } 151 152 if b.config.BundlePrefix == "" { 153 t.Fatalf("bad: %s", b.config.BundlePrefix) 154 } 155 } 156 157 func TestBuilderPrepare_InvalidKey(t *testing.T) { 158 var b Builder 159 config, tempfile := testConfig() 160 defer os.Remove(tempfile.Name()) 161 defer tempfile.Close() 162 163 // Add a random key 164 config["i_should_not_be_valid"] = true 165 warnings, err := b.Prepare(config) 166 if len(warnings) > 0 { 167 t.Fatalf("bad: %#v", warnings) 168 } 169 if err == nil { 170 t.Fatal("should have error") 171 } 172 } 173 174 func TestBuilderPrepare_S3Bucket(t *testing.T) { 175 b := &Builder{} 176 config, tempfile := testConfig() 177 defer os.Remove(tempfile.Name()) 178 defer tempfile.Close() 179 180 config["s3_bucket"] = "" 181 warnings, err := b.Prepare(config) 182 if len(warnings) > 0 { 183 t.Fatalf("bad: %#v", warnings) 184 } 185 if err == nil { 186 t.Fatal("should have error") 187 } 188 189 config["s3_bucket"] = "foo" 190 warnings, err = b.Prepare(config) 191 if len(warnings) > 0 { 192 t.Fatalf("bad: %#v", warnings) 193 } 194 if err != nil { 195 t.Errorf("err: %s", err) 196 } 197 } 198 199 func TestBuilderPrepare_X509CertPath(t *testing.T) { 200 b := &Builder{} 201 config, tempfile := testConfig() 202 defer os.Remove(tempfile.Name()) 203 defer tempfile.Close() 204 205 config["x509_cert_path"] = "" 206 warnings, err := b.Prepare(config) 207 if len(warnings) > 0 { 208 t.Fatalf("bad: %#v", warnings) 209 } 210 if err == nil { 211 t.Fatal("should have error") 212 } 213 214 config["x509_cert_path"] = "i/am/a/file/that/doesnt/exist" 215 warnings, err = b.Prepare(config) 216 if len(warnings) > 0 { 217 t.Fatalf("bad: %#v", warnings) 218 } 219 if err == nil { 220 t.Error("should have error") 221 } 222 223 tf, err := ioutil.TempFile("", "packer") 224 if err != nil { 225 t.Fatalf("error tempfile: %s", err) 226 } 227 defer os.Remove(tf.Name()) 228 defer tf.Close() 229 230 config["x509_cert_path"] = tf.Name() 231 warnings, err = b.Prepare(config) 232 if len(warnings) > 0 { 233 t.Fatalf("bad: %#v", warnings) 234 } 235 if err != nil { 236 t.Fatalf("should not have error: %s", err) 237 } 238 } 239 240 func TestBuilderPrepare_X509KeyPath(t *testing.T) { 241 b := &Builder{} 242 config, tempfile := testConfig() 243 defer os.Remove(tempfile.Name()) 244 defer tempfile.Close() 245 246 config["x509_key_path"] = "" 247 warnings, err := b.Prepare(config) 248 if len(warnings) > 0 { 249 t.Fatalf("bad: %#v", warnings) 250 } 251 if err == nil { 252 t.Fatal("should have error") 253 } 254 255 config["x509_key_path"] = "i/am/a/file/that/doesnt/exist" 256 warnings, err = b.Prepare(config) 257 if len(warnings) > 0 { 258 t.Fatalf("bad: %#v", warnings) 259 } 260 if err == nil { 261 t.Error("should have error") 262 } 263 264 tf, err := ioutil.TempFile("", "packer") 265 if err != nil { 266 t.Fatalf("error tempfile: %s", err) 267 } 268 defer os.Remove(tf.Name()) 269 defer tf.Close() 270 271 config["x509_key_path"] = tf.Name() 272 warnings, err = b.Prepare(config) 273 if len(warnings) > 0 { 274 t.Fatalf("bad: %#v", warnings) 275 } 276 if err != nil { 277 t.Fatalf("should not have error: %s", err) 278 } 279 } 280 281 func TestBuilderPrepare_X509UploadPath(t *testing.T) { 282 b := &Builder{} 283 config, tempfile := testConfig() 284 defer os.Remove(tempfile.Name()) 285 defer tempfile.Close() 286 287 config["x509_upload_path"] = "" 288 warnings, err := b.Prepare(config) 289 if len(warnings) > 0 { 290 t.Fatalf("bad: %#v", warnings) 291 } 292 if err != nil { 293 t.Fatalf("should not have error: %s", err) 294 } 295 }