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