github.phpd.cn/hashicorp/packer@v1.3.2/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) != 2 { 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) != 2 { 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_SourceAmiFilterOwnersBlank(t *testing.T) { 71 c := testConfigFilter() 72 filter_key := "name" 73 filter_value := "foo" 74 c.SourceAmiFilter = AmiFilterOptions{Filters: map[*string]*string{&filter_key: &filter_value}} 75 if err := c.Prepare(nil); len(err) != 1 { 76 t.Fatalf("Should error if Owners is not specified)") 77 } 78 } 79 80 func TestRunConfigPrepare_SourceAmiFilterGood(t *testing.T) { 81 c := testConfigFilter() 82 owner := "123" 83 filter_key := "name" 84 filter_value := "foo" 85 goodFilter := AmiFilterOptions{Owners: []*string{&owner}, Filters: map[*string]*string{&filter_key: &filter_value}} 86 c.SourceAmiFilter = goodFilter 87 if err := c.Prepare(nil); len(err) != 0 { 88 t.Fatalf("err: %s", err) 89 } 90 } 91 92 func TestRunConfigPrepare_EnableT2UnlimitedGood(t *testing.T) { 93 c := testConfig() 94 // Must have a T2 instance type if T2 Unlimited is enabled 95 c.InstanceType = "t2.micro" 96 c.EnableT2Unlimited = true 97 err := c.Prepare(nil) 98 if len(err) > 0 { 99 t.Fatalf("err: %s", err) 100 } 101 } 102 103 func TestRunConfigPrepare_EnableT2UnlimitedBadInstanceType(t *testing.T) { 104 c := testConfig() 105 // T2 Unlimited cannot be used with instance types other than T2 106 c.InstanceType = "m5.large" 107 c.EnableT2Unlimited = true 108 err := c.Prepare(nil) 109 if len(err) != 1 { 110 t.Fatalf("Should error if T2 Unlimited is enabled with non-T2 instance_type") 111 } 112 } 113 114 func TestRunConfigPrepare_EnableT2UnlimitedBadWithSpotInstanceRequest(t *testing.T) { 115 c := testConfig() 116 // T2 Unlimited cannot be used with Spot Instances 117 c.InstanceType = "t2.micro" 118 c.EnableT2Unlimited = true 119 c.SpotPrice = "auto" 120 c.SpotPriceAutoProduct = "Linux/UNIX" 121 err := c.Prepare(nil) 122 if len(err) != 1 { 123 t.Fatalf("Should error if T2 Unlimited has been used in conjuntion with a Spot Price request") 124 } 125 } 126 127 func TestRunConfigPrepare_SpotAuto(t *testing.T) { 128 c := testConfig() 129 c.SpotPrice = "auto" 130 if err := c.Prepare(nil); len(err) != 1 { 131 t.Fatalf("Should error if spot_price_auto_product is not set and spot_price is set to auto") 132 } 133 134 // Good - SpotPrice and SpotPriceAutoProduct are correctly set 135 c.SpotPriceAutoProduct = "foo" 136 if err := c.Prepare(nil); len(err) != 0 { 137 t.Fatalf("err: %s", err) 138 } 139 140 c.SpotPrice = "" 141 if err := c.Prepare(nil); len(err) != 1 { 142 t.Fatalf("Should error if spot_price is not set to auto and spot_price_auto_product is set") 143 } 144 } 145 146 func TestRunConfigPrepare_SSHPort(t *testing.T) { 147 c := testConfig() 148 c.Comm.SSHPort = 0 149 if err := c.Prepare(nil); len(err) != 0 { 150 t.Fatalf("err: %s", err) 151 } 152 153 if c.Comm.SSHPort != 22 { 154 t.Fatalf("invalid value: %d", c.Comm.SSHPort) 155 } 156 157 c.Comm.SSHPort = 44 158 if err := c.Prepare(nil); len(err) != 0 { 159 t.Fatalf("err: %s", err) 160 } 161 162 if c.Comm.SSHPort != 44 { 163 t.Fatalf("invalid value: %d", c.Comm.SSHPort) 164 } 165 } 166 167 func TestRunConfigPrepare_UserData(t *testing.T) { 168 c := testConfig() 169 tf, err := ioutil.TempFile("", "packer") 170 if err != nil { 171 t.Fatalf("err: %s", err) 172 } 173 defer os.Remove(tf.Name()) 174 defer tf.Close() 175 176 c.UserData = "foo" 177 c.UserDataFile = tf.Name() 178 if err := c.Prepare(nil); len(err) != 1 { 179 t.Fatalf("Should error if user_data string and user_data_file have both been specified") 180 } 181 } 182 183 func TestRunConfigPrepare_UserDataFile(t *testing.T) { 184 c := testConfig() 185 if err := c.Prepare(nil); len(err) != 0 { 186 t.Fatalf("err: %s", err) 187 } 188 189 c.UserDataFile = "idontexistidontthink" 190 if err := c.Prepare(nil); len(err) != 1 { 191 t.Fatalf("Should error if the file specified by user_data_file does not exist") 192 } 193 194 tf, err := ioutil.TempFile("", "packer") 195 if err != nil { 196 t.Fatalf("err: %s", err) 197 } 198 defer os.Remove(tf.Name()) 199 defer tf.Close() 200 201 c.UserDataFile = tf.Name() 202 if err := c.Prepare(nil); len(err) != 0 { 203 t.Fatalf("err: %s", err) 204 } 205 } 206 207 func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { 208 c := testConfig() 209 c.Comm.SSHTemporaryKeyPairName = "" 210 if err := c.Prepare(nil); len(err) != 0 { 211 t.Fatalf("err: %s", err) 212 } 213 214 if c.Comm.SSHTemporaryKeyPairName == "" { 215 t.Fatal("keypair name is empty") 216 } 217 218 // Match prefix and UUID, e.g. "packer_5790d491-a0b8-c84c-c9d2-2aea55086550". 219 r := regexp.MustCompile(`\Apacker_(?:(?i)[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}?)\z`) 220 if !r.MatchString(c.Comm.SSHTemporaryKeyPairName) { 221 t.Fatal("keypair name is not valid") 222 } 223 224 c.Comm.SSHTemporaryKeyPairName = "ssh-key-123" 225 if err := c.Prepare(nil); len(err) != 0 { 226 t.Fatalf("err: %s", err) 227 } 228 229 if c.Comm.SSHTemporaryKeyPairName != "ssh-key-123" { 230 t.Fatal("keypair name does not match") 231 } 232 }