github.com/rothwerx/packer@v0.9.0/builder/amazon/ebs/tags_acc_test.go (about) 1 package ebs 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/ec2" 9 "github.com/mitchellh/packer/builder/amazon/common" 10 builderT "github.com/mitchellh/packer/helper/builder/testing" 11 "github.com/mitchellh/packer/packer" 12 ) 13 14 func TestBuilderTagsAcc_basic(t *testing.T) { 15 builderT.Test(t, builderT.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Builder: &Builder{}, 18 Template: testBuilderTagsAccBasic, 19 Check: checkTags(), 20 }) 21 } 22 23 func checkTags() builderT.TestCheckFunc { 24 return func(artifacts []packer.Artifact) error { 25 if len(artifacts) > 1 { 26 return fmt.Errorf("more than 1 artifact") 27 } 28 29 tags := make(map[string]string) 30 tags["OS_Version"] = "Ubuntu" 31 tags["Release"] = "Latest" 32 33 // Get the actual *Artifact pointer so we can access the AMIs directly 34 artifactRaw := artifacts[0] 35 artifact, ok := artifactRaw.(*common.Artifact) 36 if !ok { 37 return fmt.Errorf("unknown artifact: %#v", artifactRaw) 38 } 39 40 // describe the image, get block devices with a snapshot 41 ec2conn, _ := testEC2Conn() 42 imageResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ 43 ImageIds: []*string{aws.String(artifact.Amis["us-east-1"])}, 44 }) 45 46 if err != nil { 47 return fmt.Errorf("Error retrieving details for AMI Artifcat (%#v) in Tags Test: %s", artifact, err) 48 } 49 50 if len(imageResp.Images) == 0 { 51 return fmt.Errorf("No images found for AMI Artifcat (%#v) in Tags Test: %s", artifact, err) 52 } 53 54 image := imageResp.Images[0] 55 56 // Check only those with a Snapshot ID, i.e. not Ephemeral 57 var snapshots []*string 58 for _, device := range image.BlockDeviceMappings { 59 if device.Ebs != nil && device.Ebs.SnapshotId != nil { 60 snapshots = append(snapshots, device.Ebs.SnapshotId) 61 } 62 } 63 64 // grab matching snapshot info 65 resp, err := ec2conn.DescribeSnapshots(&ec2.DescribeSnapshotsInput{ 66 SnapshotIds: snapshots, 67 }) 68 69 if err != nil { 70 return fmt.Errorf("Error retreiving Snapshots for AMI Artifcat (%#v) in Tags Test: %s", artifact, err) 71 } 72 73 if len(resp.Snapshots) == 0 { 74 return fmt.Errorf("No Snapshots found for AMI Artifcat (%#v) in Tags Test", artifact) 75 } 76 77 // grab the snapshots, check the tags 78 for _, s := range resp.Snapshots { 79 expected := len(tags) 80 for _, t := range s.Tags { 81 for key, value := range tags { 82 if key == *t.Key && value == *t.Value { 83 expected-- 84 } 85 } 86 } 87 88 if expected > 0 { 89 return fmt.Errorf("Not all tags found") 90 } 91 } 92 93 return nil 94 } 95 } 96 97 const testBuilderTagsAccBasic = ` 98 { 99 "builders": [ 100 { 101 "type": "test", 102 "region": "us-east-1", 103 "source_ami": "ami-9eaa1cf6", 104 "instance_type": "t2.micro", 105 "ssh_username": "ubuntu", 106 "ami_name": "packer-tags-testing-{{timestamp}}", 107 "tags": { 108 "OS_Version": "Ubuntu", 109 "Release": "Latest" 110 } 111 } 112 ] 113 } 114 `