github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/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(2, "932021504756", "all"),
    54  	})
    55  }
    56  
    57  func checkAMISharing(count int, uid, group 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  		userFound := false
    88  		for _, lp := range imageResp.LaunchPermissions {
    89  			if lp.UserID != nil && uid == *lp.UserID {
    90  				userFound = true
    91  			}
    92  		}
    93  
    94  		if !userFound {
    95  			return fmt.Errorf("Error in Image Attributes, expected User ID (%s) to have Launch Permissions, but was not found", uid)
    96  		}
    97  
    98  		groupFound := false
    99  		for _, lp := range imageResp.LaunchPermissions {
   100  			if lp.Group != nil && group == *lp.Group {
   101  				groupFound = true
   102  			}
   103  		}
   104  
   105  		if !groupFound {
   106  			return fmt.Errorf("Error in Image Attributes, expected Group ID (%s) to have Launch Permissions, but was not found", group)
   107  		}
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func checkRegionCopy(regions []string) builderT.TestCheckFunc {
   114  	return func(artifacts []packer.Artifact) error {
   115  		if len(artifacts) > 1 {
   116  			return fmt.Errorf("more than 1 artifact")
   117  		}
   118  
   119  		// Get the actual *Artifact pointer so we can access the AMIs directly
   120  		artifactRaw := artifacts[0]
   121  		artifact, ok := artifactRaw.(*common.Artifact)
   122  		if !ok {
   123  			return fmt.Errorf("unknown artifact: %#v", artifactRaw)
   124  		}
   125  
   126  		// Verify that we copied to only the regions given
   127  		regionSet := make(map[string]struct{})
   128  		for _, r := range regions {
   129  			regionSet[r] = struct{}{}
   130  		}
   131  		for r, _ := range artifact.Amis {
   132  			if _, ok := regionSet[r]; !ok {
   133  				return fmt.Errorf("unknown region: %s", r)
   134  			}
   135  
   136  			delete(regionSet, r)
   137  		}
   138  		if len(regionSet) > 0 {
   139  			return fmt.Errorf("didn't copy to: %#v", regionSet)
   140  		}
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  func testAccPreCheck(t *testing.T) {
   147  	if v := os.Getenv("AWS_ACCESS_KEY_ID"); v == "" {
   148  		t.Fatal("AWS_ACCESS_KEY_ID must be set for acceptance tests")
   149  	}
   150  
   151  	if v := os.Getenv("AWS_SECRET_ACCESS_KEY"); v == "" {
   152  		t.Fatal("AWS_SECRET_ACCESS_KEY must be set for acceptance tests")
   153  	}
   154  }
   155  
   156  func testEC2Conn() (*ec2.EC2, error) {
   157  	access := &common.AccessConfig{RawRegion: "us-east-1"}
   158  	config, err := access.Config()
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	return ec2.New(config), nil
   164  }
   165  
   166  const testBuilderAccBasic = `
   167  {
   168  	"builders": [{
   169  		"type": "test",
   170  		"region": "us-east-1",
   171  		"instance_type": "m3.medium",
   172  		"source_ami": "ami-76b2a71e",
   173  		"ssh_username": "ubuntu",
   174  		"ami_name": "packer-test {{timestamp}}"
   175  	}]
   176  }
   177  `
   178  
   179  const testBuilderAccRegionCopy = `
   180  {
   181  	"builders": [{
   182  		"type": "test",
   183  		"region": "us-east-1",
   184  		"instance_type": "m3.medium",
   185  		"source_ami": "ami-76b2a71e",
   186  		"ssh_username": "ubuntu",
   187  		"ami_name": "packer-test {{timestamp}}",
   188  		"ami_regions": ["us-east-1", "us-west-2"]
   189  	}]
   190  }
   191  `
   192  
   193  const testBuilderAccForceDeregister = `
   194  {
   195  	"builders": [{
   196  		"type": "test",
   197  		"region": "us-east-1",
   198  		"instance_type": "m3.medium",
   199  		"source_ami": "ami-76b2a71e",
   200  		"ssh_username": "ubuntu",
   201  		"force_deregister": "%s",
   202  		"ami_name": "packer-test-%s"
   203  	}]
   204  }
   205  `
   206  
   207  // share with catsby
   208  const testBuilderAccSharing = `
   209  {
   210  	"builders": [{
   211  		"type": "test",
   212  		"region": "us-east-1",
   213  		"instance_type": "m3.medium",
   214  		"source_ami": "ami-76b2a71e",
   215  		"ssh_username": "ubuntu",
   216  		"ami_name": "packer-test {{timestamp}}",
   217  		"ami_users":["932021504756"],
   218  		"ami_groups":["all"]
   219  	}]
   220  }
   221  `
   222  
   223  func buildForceDeregisterConfig(name, flag string) string {
   224  	return fmt.Sprintf(testBuilderAccForceDeregister, name, flag)
   225  }