github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/builder/amazon/ebs/builder_acc_test.go (about) 1 package ebs 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/mitchellh/packer/builder/amazon/common" 11 builderT "github.com/mitchellh/packer/helper/builder/testing" 12 "github.com/mitchellh/packer/packer" 13 ) 14 15 func TestBuilderAcc_basic(t *testing.T) { 16 builderT.Test(t, builderT.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Builder: &Builder{}, 19 Template: testBuilderAccBasic, 20 }) 21 } 22 23 func TestBuilderAcc_regionCopy(t *testing.T) { 24 builderT.Test(t, builderT.TestCase{ 25 PreCheck: func() { testAccPreCheck(t) }, 26 Builder: &Builder{}, 27 Template: testBuilderAccRegionCopy, 28 Check: checkRegionCopy([]string{"us-east-1", "us-west-2"}), 29 }) 30 } 31 32 func TestBuilderAcc_forceDeregister(t *testing.T) { 33 // Build the same AMI name twice, with force_deregister on the second run 34 builderT.Test(t, builderT.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Builder: &Builder{}, 37 Template: buildForceDeregisterConfig("false", "dereg"), 38 SkipArtifactTeardown: true, 39 }) 40 41 builderT.Test(t, builderT.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Builder: &Builder{}, 44 Template: buildForceDeregisterConfig("true", "dereg"), 45 }) 46 } 47 48 func TestBuilderAcc_amiSharing(t *testing.T) { 49 builderT.Test(t, builderT.TestCase{ 50 PreCheck: func() { testAccPreCheck(t) }, 51 Builder: &Builder{}, 52 Template: testBuilderAccSharing, 53 Check: checkAMISharing(1, "932021504756"), 54 }) 55 } 56 57 func checkAMISharing(count int, uid string) builderT.TestCheckFunc { 58 return func(artifacts []packer.Artifact) error { 59 if len(artifacts) > 1 { 60 return fmt.Errorf("more than 1 artifact") 61 } 62 63 // Get the actual *Artifact pointer so we can access the AMIs directly 64 artifactRaw := artifacts[0] 65 artifact, ok := artifactRaw.(*common.Artifact) 66 if !ok { 67 return fmt.Errorf("unknown artifact: %#v", artifactRaw) 68 } 69 70 // describe the image, get block devices with a snapshot 71 ec2conn, _ := testEC2Conn() 72 imageResp, err := ec2conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{ 73 Attribute: aws.String("launchPermission"), 74 ImageID: aws.String(artifact.Amis["us-east-1"]), 75 }) 76 77 if err != nil { 78 return fmt.Errorf("Error retrieving Image Attributes for AMI Artifact (%#v) in AMI Sharing Test: %s", artifact, err) 79 } 80 81 // Launch Permissions are in addition to the userid that created it, so if 82 // you add 3 additional ami_users, you expect 2 Launch Permissions here 83 if len(imageResp.LaunchPermissions) != count { 84 return fmt.Errorf("Error in Image Attributes, expected (%d) Launch Permissions, got (%d)", count, len(imageResp.LaunchPermissions)) 85 } 86 87 found := false 88 for _, lp := range imageResp.LaunchPermissions { 89 if uid == *lp.UserID { 90 found = true 91 } 92 } 93 94 if !found { 95 return fmt.Errorf("Error in Image Attributes, expected User ID (%s) to have Launch Permissions, but was not found", uid) 96 } 97 98 return nil 99 } 100 } 101 102 func checkRegionCopy(regions []string) builderT.TestCheckFunc { 103 return func(artifacts []packer.Artifact) error { 104 if len(artifacts) > 1 { 105 return fmt.Errorf("more than 1 artifact") 106 } 107 108 // Get the actual *Artifact pointer so we can access the AMIs directly 109 artifactRaw := artifacts[0] 110 artifact, ok := artifactRaw.(*common.Artifact) 111 if !ok { 112 return fmt.Errorf("unknown artifact: %#v", artifactRaw) 113 } 114 115 // Verify that we copied to only the regions given 116 regionSet := make(map[string]struct{}) 117 for _, r := range regions { 118 regionSet[r] = struct{}{} 119 } 120 for r, _ := range artifact.Amis { 121 if _, ok := regionSet[r]; !ok { 122 return fmt.Errorf("unknown region: %s", r) 123 } 124 125 delete(regionSet, r) 126 } 127 if len(regionSet) > 0 { 128 return fmt.Errorf("didn't copy to: %#v", regionSet) 129 } 130 131 return nil 132 } 133 } 134 135 func testAccPreCheck(t *testing.T) { 136 if v := os.Getenv("AWS_ACCESS_KEY_ID"); v == "" { 137 t.Fatal("AWS_ACCESS_KEY_ID must be set for acceptance tests") 138 } 139 140 if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v == "" { 141 t.Fatal("AWS_SECRET_ACCESS_KEY must be set for acceptance tests") 142 } 143 } 144 145 func testEC2Conn() (*ec2.EC2, error) { 146 access := &common.AccessConfig{RawRegion: "us-east-1"} 147 config, err := access.Config() 148 if err != nil { 149 return nil, err 150 } 151 152 return ec2.New(config), nil 153 } 154 155 const testBuilderAccBasic = ` 156 { 157 "builders": [{ 158 "type": "test", 159 "region": "us-east-1", 160 "instance_type": "m3.medium", 161 "source_ami": "ami-76b2a71e", 162 "ssh_username": "ubuntu", 163 "ami_name": "packer-test {{timestamp}}" 164 }] 165 } 166 ` 167 168 const testBuilderAccRegionCopy = ` 169 { 170 "builders": [{ 171 "type": "test", 172 "region": "us-east-1", 173 "instance_type": "m3.medium", 174 "source_ami": "ami-76b2a71e", 175 "ssh_username": "ubuntu", 176 "ami_name": "packer-test {{timestamp}}", 177 "ami_regions": ["us-east-1", "us-west-2"] 178 }] 179 } 180 ` 181 182 const testBuilderAccForceDeregister = ` 183 { 184 "builders": [{ 185 "type": "test", 186 "region": "us-east-1", 187 "instance_type": "m3.medium", 188 "source_ami": "ami-76b2a71e", 189 "ssh_username": "ubuntu", 190 "force_deregister": "%s", 191 "ami_name": "packer-test-%s" 192 }] 193 } 194 ` 195 196 // share with catsby 197 const testBuilderAccSharing = ` 198 { 199 "builders": [{ 200 "type": "test", 201 "region": "us-east-1", 202 "instance_type": "m3.medium", 203 "source_ami": "ami-76b2a71e", 204 "ssh_username": "ubuntu", 205 "ami_name": "packer-test {{timestamp}}", 206 "ami_users":["932021504756"] 207 }] 208 } 209 ` 210 211 func buildForceDeregisterConfig(name, flag string) string { 212 return fmt.Sprintf(testBuilderAccForceDeregister, name, flag) 213 }