github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/instance/builder_test.go (about) 1 package instance 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func testConfig() map[string]interface{} { 11 tf, err := ioutil.TempFile("", "packer") 12 if err != nil { 13 panic(err) 14 } 15 16 return map[string]interface{}{ 17 "account_id": "foo", 18 "ami_name": "foo", 19 "instance_type": "m1.small", 20 "region": "us-east-1", 21 "s3_bucket": "foo", 22 "source_ami": "foo", 23 "ssh_username": "bob", 24 "x509_cert_path": tf.Name(), 25 "x509_key_path": tf.Name(), 26 "x509_upload_path": "/foo", 27 } 28 } 29 30 func TestBuilder_ImplementsBuilder(t *testing.T) { 31 var raw interface{} 32 raw = &Builder{} 33 if _, ok := raw.(packer.Builder); !ok { 34 t.Fatalf("Builder should be a builder") 35 } 36 } 37 38 func TestBuilderPrepare_AccountId(t *testing.T) { 39 b := &Builder{} 40 config := testConfig() 41 42 config["account_id"] = "" 43 err := b.Prepare(config) 44 if err == nil { 45 t.Fatal("should have error") 46 } 47 48 config["account_id"] = "foo" 49 err = b.Prepare(config) 50 if err != nil { 51 t.Errorf("err: %s", err) 52 } 53 54 config["account_id"] = "0123-0456-7890" 55 err = b.Prepare(config) 56 if err != nil { 57 t.Fatalf("err: %s", err) 58 } 59 60 if b.config.AccountId != "012304567890" { 61 t.Errorf("should strip hyphens: %s", b.config.AccountId) 62 } 63 } 64 65 func TestBuilderPrepare_AMIName(t *testing.T) { 66 var b Builder 67 config := testConfig() 68 69 // Test good 70 config["ami_name"] = "foo" 71 err := b.Prepare(config) 72 if err != nil { 73 t.Fatalf("should not have error: %s", err) 74 } 75 76 // Test bad 77 config["ami_name"] = "foo {{" 78 b = Builder{} 79 err = b.Prepare(config) 80 if err == nil { 81 t.Fatal("should have error") 82 } 83 84 // Test bad 85 delete(config, "ami_name") 86 b = Builder{} 87 err = b.Prepare(config) 88 if err == nil { 89 t.Fatal("should have error") 90 } 91 } 92 93 func TestBuilderPrepare_BundleDestination(t *testing.T) { 94 b := &Builder{} 95 config := testConfig() 96 97 config["bundle_destination"] = "" 98 err := b.Prepare(config) 99 if err != nil { 100 t.Fatalf("err: %s", err) 101 } 102 103 if b.config.BundleDestination != "/tmp" { 104 t.Fatalf("bad: %s", b.config.BundleDestination) 105 } 106 } 107 108 func TestBuilderPrepare_BundlePrefix(t *testing.T) { 109 b := &Builder{} 110 config := testConfig() 111 112 config["bundle_prefix"] = "" 113 err := b.Prepare(config) 114 if err != nil { 115 t.Fatalf("err: %s", err) 116 } 117 118 if b.config.BundlePrefix == "" { 119 t.Fatalf("bad: %s", b.config.BundlePrefix) 120 } 121 } 122 123 func TestBuilderPrepare_InvalidKey(t *testing.T) { 124 var b Builder 125 config := testConfig() 126 127 // Add a random key 128 config["i_should_not_be_valid"] = true 129 err := b.Prepare(config) 130 if err == nil { 131 t.Fatal("should have error") 132 } 133 } 134 135 func TestBuilderPrepare_S3Bucket(t *testing.T) { 136 b := &Builder{} 137 config := testConfig() 138 139 config["s3_bucket"] = "" 140 err := b.Prepare(config) 141 if err == nil { 142 t.Fatal("should have error") 143 } 144 145 config["s3_bucket"] = "foo" 146 err = b.Prepare(config) 147 if err != nil { 148 t.Errorf("err: %s", err) 149 } 150 } 151 152 func TestBuilderPrepare_X509CertPath(t *testing.T) { 153 b := &Builder{} 154 config := testConfig() 155 156 config["x509_cert_path"] = "" 157 err := b.Prepare(config) 158 if err == nil { 159 t.Fatal("should have error") 160 } 161 162 config["x509_cert_path"] = "i/am/a/file/that/doesnt/exist" 163 err = b.Prepare(config) 164 if err == nil { 165 t.Error("should have error") 166 } 167 168 tf, err := ioutil.TempFile("", "packer") 169 if err != nil { 170 t.Fatalf("error tempfile: %s", err) 171 } 172 defer os.Remove(tf.Name()) 173 174 config["x509_cert_path"] = tf.Name() 175 err = b.Prepare(config) 176 if err != nil { 177 t.Fatalf("should not have error: %s", err) 178 } 179 } 180 181 func TestBuilderPrepare_X509KeyPath(t *testing.T) { 182 b := &Builder{} 183 config := testConfig() 184 185 config["x509_key_path"] = "" 186 err := b.Prepare(config) 187 if err == nil { 188 t.Fatal("should have error") 189 } 190 191 config["x509_key_path"] = "i/am/a/file/that/doesnt/exist" 192 err = b.Prepare(config) 193 if err == nil { 194 t.Error("should have error") 195 } 196 197 tf, err := ioutil.TempFile("", "packer") 198 if err != nil { 199 t.Fatalf("error tempfile: %s", err) 200 } 201 defer os.Remove(tf.Name()) 202 203 config["x509_key_path"] = tf.Name() 204 err = b.Prepare(config) 205 if err != nil { 206 t.Fatalf("should not have error: %s", err) 207 } 208 } 209 210 func TestBuilderPrepare_X509UploadPath(t *testing.T) { 211 b := &Builder{} 212 config := testConfig() 213 214 config["x509_upload_path"] = "" 215 err := b.Prepare(config) 216 if err != nil { 217 t.Fatalf("should not have error: %s", err) 218 } 219 }