github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/amazon/common/run_config_test.go (about) 1 package common 2 3 import ( 4 "io/ioutil" 5 "os" 6 "regexp" 7 "testing" 8 9 "github.com/hashicorp/packer/helper/communicator" 10 ) 11 12 func init() { 13 // Clear out the AWS access key env vars so they don't 14 // affect our tests. 15 os.Setenv("AWS_ACCESS_KEY_ID", "") 16 os.Setenv("AWS_ACCESS_KEY", "") 17 os.Setenv("AWS_SECRET_ACCESS_KEY", "") 18 os.Setenv("AWS_SECRET_KEY", "") 19 } 20 21 func testConfig() *RunConfig { 22 return &RunConfig{ 23 SourceAmi: "abcd", 24 InstanceType: "m1.small", 25 26 Comm: communicator.Config{ 27 SSHUsername: "foo", 28 }, 29 } 30 } 31 32 func testConfigFilter() *RunConfig { 33 config := testConfig() 34 config.SourceAmi = "" 35 config.SourceAmiFilter = AmiFilterOptions{} 36 return config 37 } 38 39 func TestRunConfigPrepare(t *testing.T) { 40 c := testConfig() 41 err := c.Prepare(nil) 42 if len(err) > 0 { 43 t.Fatalf("err: %s", err) 44 } 45 } 46 47 func TestRunConfigPrepare_InstanceType(t *testing.T) { 48 c := testConfig() 49 c.InstanceType = "" 50 if err := c.Prepare(nil); len(err) != 1 { 51 t.Fatalf("Should error if an instance_type is not specified") 52 } 53 } 54 55 func TestRunConfigPrepare_SourceAmi(t *testing.T) { 56 c := testConfig() 57 c.SourceAmi = "" 58 if err := c.Prepare(nil); len(err) != 1 { 59 t.Fatalf("Should error if a source_ami (or source_ami_filter) is not specified") 60 } 61 } 62 63 func TestRunConfigPrepare_SourceAmiFilterBlank(t *testing.T) { 64 c := testConfigFilter() 65 if err := c.Prepare(nil); len(err) != 1 { 66 t.Fatalf("Should error if source_ami_filter is empty or not specified (and source_ami is not specified)") 67 } 68 } 69 70 func TestRunConfigPrepare_SourceAmiFilterGood(t *testing.T) { 71 c := testConfigFilter() 72 owner := "123" 73 filter_key := "name" 74 filter_value := "foo" 75 goodFilter := AmiFilterOptions{Owners: []*string{&owner}, Filters: map[*string]*string{&filter_key: &filter_value}} 76 c.SourceAmiFilter = goodFilter 77 if err := c.Prepare(nil); len(err) != 0 { 78 t.Fatalf("err: %s", err) 79 } 80 } 81 82 func TestRunConfigPrepare_EnableT2UnlimitedGood(t *testing.T) { 83 c := testConfig() 84 // Must have a T2 instance type if T2 Unlimited is enabled 85 c.InstanceType = "t2.micro" 86 c.EnableT2Unlimited = true 87 err := c.Prepare(nil) 88 if len(err) > 0 { 89 t.Fatalf("err: %s", err) 90 } 91 } 92 93 func TestRunConfigPrepare_EnableT2UnlimitedBadInstanceType(t *testing.T) { 94 c := testConfig() 95 // T2 Unlimited cannot be used with instance types other than T2 96 c.InstanceType = "m5.large" 97 c.EnableT2Unlimited = true 98 err := c.Prepare(nil) 99 if len(err) != 1 { 100 t.Fatalf("Should error if T2 Unlimited is enabled with non-T2 instance_type") 101 } 102 } 103 104 func TestRunConfigPrepare_EnableT2UnlimitedBadWithSpotInstanceRequest(t *testing.T) { 105 c := testConfig() 106 // T2 Unlimited cannot be used with Spot Instances 107 c.InstanceType = "t2.micro" 108 c.EnableT2Unlimited = true 109 c.SpotPrice = "auto" 110 c.SpotPriceAutoProduct = "Linux/UNIX" 111 err := c.Prepare(nil) 112 if len(err) != 1 { 113 t.Fatalf("Should error if T2 Unlimited has been used in conjuntion with a Spot Price request") 114 } 115 } 116 117 func TestRunConfigPrepare_SpotAuto(t *testing.T) { 118 c := testConfig() 119 c.SpotPrice = "auto" 120 if err := c.Prepare(nil); len(err) != 1 { 121 t.Fatalf("Should error if spot_price_auto_product is not set and spot_price is set to auto") 122 } 123 124 // Good - SpotPrice and SpotPriceAutoProduct are correctly set 125 c.SpotPriceAutoProduct = "foo" 126 if err := c.Prepare(nil); len(err) != 0 { 127 t.Fatalf("err: %s", err) 128 } 129 130 c.SpotPrice = "" 131 if err := c.Prepare(nil); len(err) != 1 { 132 t.Fatalf("Should error if spot_price is not set to auto and spot_price_auto_product is set") 133 } 134 } 135 136 func TestRunConfigPrepare_SSHPort(t *testing.T) { 137 c := testConfig() 138 c.Comm.SSHPort = 0 139 if err := c.Prepare(nil); len(err) != 0 { 140 t.Fatalf("err: %s", err) 141 } 142 143 if c.Comm.SSHPort != 22 { 144 t.Fatalf("invalid value: %d", c.Comm.SSHPort) 145 } 146 147 c.Comm.SSHPort = 44 148 if err := c.Prepare(nil); len(err) != 0 { 149 t.Fatalf("err: %s", err) 150 } 151 152 if c.Comm.SSHPort != 44 { 153 t.Fatalf("invalid value: %d", c.Comm.SSHPort) 154 } 155 } 156 157 func TestRunConfigPrepare_UserData(t *testing.T) { 158 c := testConfig() 159 tf, err := ioutil.TempFile("", "packer") 160 if err != nil { 161 t.Fatalf("err: %s", err) 162 } 163 defer os.Remove(tf.Name()) 164 defer tf.Close() 165 166 c.UserData = "foo" 167 c.UserDataFile = tf.Name() 168 if err := c.Prepare(nil); len(err) != 1 { 169 t.Fatalf("Should error if user_data string and user_data_file have both been specified") 170 } 171 } 172 173 func TestRunConfigPrepare_UserDataFile(t *testing.T) { 174 c := testConfig() 175 if err := c.Prepare(nil); len(err) != 0 { 176 t.Fatalf("err: %s", err) 177 } 178 179 c.UserDataFile = "idontexistidontthink" 180 if err := c.Prepare(nil); len(err) != 1 { 181 t.Fatalf("Should error if the file specified by user_data_file does not exist") 182 } 183 184 tf, err := ioutil.TempFile("", "packer") 185 if err != nil { 186 t.Fatalf("err: %s", err) 187 } 188 defer os.Remove(tf.Name()) 189 defer tf.Close() 190 191 c.UserDataFile = tf.Name() 192 if err := c.Prepare(nil); len(err) != 0 { 193 t.Fatalf("err: %s", err) 194 } 195 } 196 197 func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { 198 c := testConfig() 199 c.TemporaryKeyPairName = "" 200 if err := c.Prepare(nil); len(err) != 0 { 201 t.Fatalf("err: %s", err) 202 } 203 204 if c.TemporaryKeyPairName == "" { 205 t.Fatal("keypair name is empty") 206 } 207 208 // Match prefix and UUID, e.g. "packer_5790d491-a0b8-c84c-c9d2-2aea55086550". 209 r := regexp.MustCompile(`\Apacker_(?:(?i)[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}?)\z`) 210 if !r.MatchString(c.TemporaryKeyPairName) { 211 t.Fatal("keypair name is not valid") 212 } 213 214 c.TemporaryKeyPairName = "ssh-key-123" 215 if err := c.Prepare(nil); len(err) != 0 { 216 t.Fatalf("err: %s", err) 217 } 218 219 if c.TemporaryKeyPairName != "ssh-key-123" { 220 t.Fatal("keypair name does not match") 221 } 222 }