2018-01-08 14:54:33 -05:00
|
|
|
/*
|
|
|
|
Deregister the test image with
|
|
|
|
aws ec2 deregister-image --image-id $(aws ec2 describe-images --output text --filters "Name=name,Values=packer-test-packer-test-dereg" --query 'Images[*].{ID:ImageId}')
|
|
|
|
*/
|
2015-05-26 16:47:24 -04:00
|
|
|
package ebs
|
|
|
|
|
|
|
|
import (
|
2015-06-08 12:16:01 -04:00
|
|
|
"fmt"
|
2020-09-02 11:44:38 -04:00
|
|
|
"os"
|
2015-05-26 16:47:24 -04:00
|
|
|
"testing"
|
|
|
|
|
2015-06-24 12:41:55 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-08 12:16:01 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/amazon/common"
|
|
|
|
builderT "github.com/hashicorp/packer/helper/builder/testing"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-05-26 16:47:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilderAcc_basic(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: testBuilderAccBasic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-08 12:16:01 -04:00
|
|
|
func TestBuilderAcc_regionCopy(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: testBuilderAccRegionCopy,
|
|
|
|
Check: checkRegionCopy([]string{"us-east-1", "us-west-2"}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-12 14:05:15 -04:00
|
|
|
func TestBuilderAcc_forceDeregister(t *testing.T) {
|
|
|
|
// Build the same AMI name twice, with force_deregister on the second run
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: buildForceDeregisterConfig("false", "dereg"),
|
|
|
|
SkipArtifactTeardown: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: buildForceDeregisterConfig("true", "dereg"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:28:34 -05:00
|
|
|
func TestBuilderAcc_forceDeleteSnapshot(t *testing.T) {
|
2016-11-30 22:58:19 -05:00
|
|
|
amiName := "packer-test-dereg"
|
|
|
|
|
2016-11-30 16:28:34 -05:00
|
|
|
// Build the same AMI name twice, with force_delete_snapshot on the second run
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
2016-11-30 22:58:19 -05:00
|
|
|
Template: buildForceDeleteSnapshotConfig("false", amiName),
|
2016-11-30 16:28:34 -05:00
|
|
|
SkipArtifactTeardown: true,
|
|
|
|
})
|
|
|
|
|
2016-11-30 22:58:19 -05:00
|
|
|
// Get image data by AMI name
|
|
|
|
ec2conn, _ := testEC2Conn()
|
2018-01-08 14:54:33 -05:00
|
|
|
describeInput := &ec2.DescribeImagesInput{Filters: []*ec2.Filter{
|
|
|
|
{
|
|
|
|
Name: aws.String("name"),
|
|
|
|
Values: []*string{aws.String(amiName)},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
ec2conn.WaitUntilImageExists(describeInput)
|
|
|
|
imageResp, _ := ec2conn.DescribeImages(describeInput)
|
2016-11-30 22:58:19 -05:00
|
|
|
image := imageResp.Images[0]
|
|
|
|
|
|
|
|
// Get snapshot ids for image
|
|
|
|
snapshotIds := []*string{}
|
|
|
|
for _, device := range image.BlockDeviceMappings {
|
|
|
|
if device.Ebs != nil && device.Ebs.SnapshotId != nil {
|
|
|
|
snapshotIds = append(snapshotIds, device.Ebs.SnapshotId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:28:34 -05:00
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
2016-11-30 22:58:19 -05:00
|
|
|
Template: buildForceDeleteSnapshotConfig("true", amiName),
|
|
|
|
Check: checkSnapshotsDeleted(snapshotIds),
|
2016-11-30 16:28:34 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:58:19 -05:00
|
|
|
func checkSnapshotsDeleted(snapshotIds []*string) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
|
|
|
// Verify the snapshots are gone
|
|
|
|
ec2conn, _ := testEC2Conn()
|
|
|
|
snapshotResp, _ := ec2conn.DescribeSnapshots(
|
|
|
|
&ec2.DescribeSnapshotsInput{SnapshotIds: snapshotIds},
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(snapshotResp.Snapshots) > 0 {
|
|
|
|
return fmt.Errorf("Snapshots weren't successfully deleted by `force_delete_snapshot`")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 12:41:55 -04:00
|
|
|
func TestBuilderAcc_amiSharing(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
2020-09-02 11:44:38 -04:00
|
|
|
PreCheck: func() { testAccSharingPreCheck(t) },
|
2015-06-24 12:41:55 -04:00
|
|
|
Builder: &Builder{},
|
2020-09-02 11:44:38 -04:00
|
|
|
Template: buildSharingConfig(os.Getenv("TESTACC_AWS_ACCOUNT_ID")),
|
|
|
|
Check: checkAMISharing(2, os.Getenv("TESTACC_AWS_ACCOUNT_ID"), "all"),
|
2015-06-24 12:41:55 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-25 14:56:17 -04:00
|
|
|
func TestBuilderAcc_encryptedBoot(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: testBuilderAccEncrypted,
|
|
|
|
Check: checkBootEncrypted(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:48:38 -04:00
|
|
|
func checkAMISharing(count int, uid, group string) builderT.TestCheckFunc {
|
2015-06-24 12:41:55 -04:00
|
|
|
return func(artifacts []packer.Artifact) error {
|
|
|
|
if len(artifacts) > 1 {
|
|
|
|
return fmt.Errorf("more than 1 artifact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the actual *Artifact pointer so we can access the AMIs directly
|
|
|
|
artifactRaw := artifacts[0]
|
|
|
|
artifact, ok := artifactRaw.(*common.Artifact)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown artifact: %#v", artifactRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// describe the image, get block devices with a snapshot
|
|
|
|
ec2conn, _ := testEC2Conn()
|
|
|
|
imageResp, err := ec2conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{
|
|
|
|
Attribute: aws.String("launchPermission"),
|
2015-08-17 20:44:01 -04:00
|
|
|
ImageId: aws.String(artifact.Amis["us-east-1"]),
|
2015-06-24 12:41:55 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving Image Attributes for AMI Artifact (%#v) in AMI Sharing Test: %s", artifact, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch Permissions are in addition to the userid that created it, so if
|
|
|
|
// you add 3 additional ami_users, you expect 2 Launch Permissions here
|
|
|
|
if len(imageResp.LaunchPermissions) != count {
|
|
|
|
return fmt.Errorf("Error in Image Attributes, expected (%d) Launch Permissions, got (%d)", count, len(imageResp.LaunchPermissions))
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:48:38 -04:00
|
|
|
userFound := false
|
2015-06-24 12:41:55 -04:00
|
|
|
for _, lp := range imageResp.LaunchPermissions {
|
2015-08-17 20:44:01 -04:00
|
|
|
if lp.UserId != nil && uid == *lp.UserId {
|
2015-06-25 20:48:38 -04:00
|
|
|
userFound = true
|
2015-06-24 12:41:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:48:38 -04:00
|
|
|
if !userFound {
|
2015-06-24 12:41:55 -04:00
|
|
|
return fmt.Errorf("Error in Image Attributes, expected User ID (%s) to have Launch Permissions, but was not found", uid)
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:48:38 -04:00
|
|
|
groupFound := false
|
|
|
|
for _, lp := range imageResp.LaunchPermissions {
|
|
|
|
if lp.Group != nil && group == *lp.Group {
|
|
|
|
groupFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !groupFound {
|
|
|
|
return fmt.Errorf("Error in Image Attributes, expected Group ID (%s) to have Launch Permissions, but was not found", group)
|
|
|
|
}
|
|
|
|
|
2015-06-24 12:41:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 12:16:01 -04:00
|
|
|
func checkRegionCopy(regions []string) builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
|
|
|
if len(artifacts) > 1 {
|
|
|
|
return fmt.Errorf("more than 1 artifact")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the actual *Artifact pointer so we can access the AMIs directly
|
|
|
|
artifactRaw := artifacts[0]
|
|
|
|
artifact, ok := artifactRaw.(*common.Artifact)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown artifact: %#v", artifactRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that we copied to only the regions given
|
|
|
|
regionSet := make(map[string]struct{})
|
|
|
|
for _, r := range regions {
|
|
|
|
regionSet[r] = struct{}{}
|
|
|
|
}
|
2016-11-01 17:08:04 -04:00
|
|
|
for r := range artifact.Amis {
|
2015-06-08 12:16:01 -04:00
|
|
|
if _, ok := regionSet[r]; !ok {
|
|
|
|
return fmt.Errorf("unknown region: %s", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(regionSet, r)
|
|
|
|
}
|
|
|
|
if len(regionSet) > 0 {
|
|
|
|
return fmt.Errorf("didn't copy to: %#v", regionSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 14:56:17 -04:00
|
|
|
func checkBootEncrypted() builderT.TestCheckFunc {
|
|
|
|
return func(artifacts []packer.Artifact) error {
|
|
|
|
|
|
|
|
// Get the actual *Artifact pointer so we can access the AMIs directly
|
|
|
|
artifactRaw := artifacts[0]
|
|
|
|
artifact, ok := artifactRaw.(*common.Artifact)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown artifact: %#v", artifactRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// describe the image, get block devices with a snapshot
|
|
|
|
ec2conn, _ := testEC2Conn()
|
|
|
|
imageResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{
|
|
|
|
ImageIds: []*string{aws.String(artifact.Amis["us-east-1"])},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving Image Attributes for AMI (%s) in AMI Encrypted Boot Test: %s", artifact, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
image := imageResp.Images[0] // Only requested a single AMI ID
|
|
|
|
|
|
|
|
rootDeviceName := image.RootDeviceName
|
|
|
|
|
|
|
|
for _, bd := range image.BlockDeviceMappings {
|
|
|
|
if *bd.DeviceName == *rootDeviceName {
|
|
|
|
if *bd.Ebs.Encrypted != true {
|
|
|
|
return fmt.Errorf("volume not encrypted: %s", *bd.Ebs.SnapshotId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 09:39:19 -04:00
|
|
|
func TestBuilderAcc_SessionManagerInterface(t *testing.T) {
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: testBuilderAccSessionManagerInterface,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:47:24 -04:00
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
}
|
|
|
|
|
2020-09-02 11:44:38 -04:00
|
|
|
func testAccSharingPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("TESTACC_AWS_ACCOUNT_ID"); v == "" {
|
|
|
|
t.Fatal(fmt.Sprintf("TESTACC_AWS_ACCOUNT_ID must be set for acceptance tests"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 12:16:01 -04:00
|
|
|
func testEC2Conn() (*ec2.EC2, error) {
|
|
|
|
access := &common.AccessConfig{RawRegion: "us-east-1"}
|
2017-03-01 19:43:09 -05:00
|
|
|
session, err := access.Session()
|
2015-06-08 12:16:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-31 14:04:50 -04:00
|
|
|
return ec2.New(session), nil
|
2015-06-08 12:16:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-26 16:47:24 -04:00
|
|
|
const testBuilderAccBasic = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami": "ami-76b2a71e",
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"ami_name": "packer-test {{timestamp}}"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
2015-06-08 12:16:01 -04:00
|
|
|
|
|
|
|
const testBuilderAccRegionCopy = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami": "ami-76b2a71e",
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"ami_name": "packer-test {{timestamp}}",
|
|
|
|
"ami_regions": ["us-east-1", "us-west-2"]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
2015-06-12 14:05:15 -04:00
|
|
|
|
|
|
|
const testBuilderAccForceDeregister = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami": "ami-76b2a71e",
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"force_deregister": "%s",
|
2018-01-08 14:54:33 -05:00
|
|
|
"ami_name": "%s"
|
2015-06-12 14:05:15 -04:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-11-30 16:28:34 -05:00
|
|
|
const testBuilderAccForceDeleteSnapshot = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami": "ami-76b2a71e",
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"force_deregister": "%s",
|
|
|
|
"force_delete_snapshot": "%s",
|
2018-01-08 14:54:33 -05:00
|
|
|
"ami_name": "%s"
|
2016-11-30 16:28:34 -05:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-06-24 12:41:55 -04:00
|
|
|
const testBuilderAccSharing = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami": "ami-76b2a71e",
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"ami_name": "packer-test {{timestamp}}",
|
2020-09-02 11:44:38 -04:00
|
|
|
"ami_users":["%s"],
|
2015-06-25 20:48:38 -04:00
|
|
|
"ami_groups":["all"]
|
2015-06-24 12:41:55 -04:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-03-25 14:56:17 -04:00
|
|
|
const testBuilderAccEncrypted = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami":"ami-c15bebaa",
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"ami_name": "packer-enc-test {{timestamp}}",
|
2016-11-30 22:58:19 -05:00
|
|
|
"encrypt_boot": true
|
2016-03-25 14:56:17 -04:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2020-10-29 09:39:19 -04:00
|
|
|
const testBuilderAccSessionManagerInterface = `
|
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "us-east-1",
|
|
|
|
"instance_type": "m3.medium",
|
|
|
|
"source_ami_filter": {
|
|
|
|
"filters": {
|
|
|
|
"virtualization-type": "hvm",
|
|
|
|
"name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
|
|
|
|
"root-device-type": "ebs"
|
|
|
|
},
|
|
|
|
"owners": [
|
|
|
|
"099720109477"
|
|
|
|
],
|
|
|
|
"most_recent": true
|
|
|
|
},
|
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"ssh_interface": "session_manager",
|
|
|
|
"iam_instance_profile": "SSMInstanceProfile",
|
|
|
|
"ami_name": "packer-ssm-test-{{timestamp}}"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-11-30 16:28:34 -05:00
|
|
|
func buildForceDeregisterConfig(val, name string) string {
|
|
|
|
return fmt.Sprintf(testBuilderAccForceDeregister, val, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildForceDeleteSnapshotConfig(val, name string) string {
|
|
|
|
return fmt.Sprintf(testBuilderAccForceDeleteSnapshot, val, val, name)
|
2015-06-12 14:05:15 -04:00
|
|
|
}
|
2020-09-02 11:44:38 -04:00
|
|
|
|
|
|
|
func buildSharingConfig(val string) string {
|
|
|
|
return fmt.Sprintf(testBuilderAccSharing, val)
|
|
|
|
}
|