github.com/angdraug/packer@v1.3.2/builder/amazon/ebs/tags_acc_test.go (about)

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