github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/amazon/common/ami_config_test.go (about)

     1  package common
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func testAMIConfig() *AMIConfig {
     9  	return &AMIConfig{
    10  		AMIName: "foo",
    11  	}
    12  }
    13  
    14  func getFakeAccessConfig(region string) *AccessConfig {
    15  	return &AccessConfig{
    16  		RawRegion: region,
    17  	}
    18  }
    19  
    20  func TestAMIConfigPrepare_name(t *testing.T) {
    21  	c := testAMIConfig()
    22  	if err := c.Prepare(nil, nil); err != nil {
    23  		t.Fatalf("shouldn't have err: %s", err)
    24  	}
    25  
    26  	c.AMIName = ""
    27  	if err := c.Prepare(nil, nil); err == nil {
    28  		t.Fatal("should have error")
    29  	}
    30  }
    31  
    32  func TestAMIConfigPrepare_regions(t *testing.T) {
    33  	c := testAMIConfig()
    34  	c.AMIRegions = nil
    35  	if err := c.Prepare(nil, nil); err != nil {
    36  		t.Fatalf("shouldn't have err: %s", err)
    37  	}
    38  
    39  	c.AMIRegions = listEC2Regions()
    40  	if err := c.Prepare(nil, nil); err != nil {
    41  		t.Fatalf("shouldn't have err: %s", err)
    42  	}
    43  
    44  	c.AMIRegions = []string{"foo"}
    45  	if err := c.Prepare(nil, nil); err == nil {
    46  		t.Fatal("should have error")
    47  	}
    48  
    49  	c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-1"}
    50  	if err := c.Prepare(nil, nil); err != nil {
    51  		t.Fatalf("bad: %s", err)
    52  	}
    53  
    54  	expected := []string{"us-east-1", "us-west-1"}
    55  	if !reflect.DeepEqual(c.AMIRegions, expected) {
    56  		t.Fatalf("bad: %#v", c.AMIRegions)
    57  	}
    58  
    59  	c.AMIRegions = []string{"custom"}
    60  	c.AMISkipRegionValidation = true
    61  	if err := c.Prepare(nil, nil); err != nil {
    62  		t.Fatal("shouldn't have error")
    63  	}
    64  	c.AMISkipRegionValidation = false
    65  
    66  	c.AMIRegions = []string{"us-east-1", "us-east-2", "us-west-1"}
    67  	c.AMIRegionKMSKeyIDs = map[string]string{
    68  		"us-east-1": "123-456-7890",
    69  		"us-west-1": "789-012-3456",
    70  		"us-east-2": "456-789-0123",
    71  	}
    72  	if err := c.Prepare(nil, nil); err != nil {
    73  		t.Fatal("shouldn't have error")
    74  	}
    75  
    76  	c.AMIRegions = []string{"us-east-1", "us-east-2", "us-west-1"}
    77  	c.AMIRegionKMSKeyIDs = map[string]string{
    78  		"us-east-1": "123-456-7890",
    79  		"us-west-1": "789-012-3456",
    80  		"us-east-2": "",
    81  	}
    82  	if err := c.Prepare(nil, nil); err != nil {
    83  		t.Fatal("should have passed; we are able to use default KMS key if not sharing")
    84  	}
    85  
    86  	c.SnapshotUsers = []string{"user-foo", "user-bar"}
    87  	c.AMIRegions = []string{"us-east-1", "us-east-2", "us-west-1"}
    88  	c.AMIRegionKMSKeyIDs = map[string]string{
    89  		"us-east-1": "123-456-7890",
    90  		"us-west-1": "789-012-3456",
    91  		"us-east-2": "",
    92  	}
    93  	if err := c.Prepare(nil, nil); err == nil {
    94  		t.Fatal("should have an error b/c can't use default KMS key if sharing")
    95  	}
    96  
    97  	c.AMIRegions = []string{"us-east-1", "us-west-1"}
    98  	c.AMIRegionKMSKeyIDs = map[string]string{
    99  		"us-east-1": "123-456-7890",
   100  		"us-west-1": "789-012-3456",
   101  		"us-east-2": "456-789-0123",
   102  	}
   103  	if err := c.Prepare(nil, nil); err == nil {
   104  		t.Fatal("should have error b/c theres a region in the key map that isn't in ami_regions")
   105  	}
   106  
   107  	c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-2"}
   108  	c.AMIRegionKMSKeyIDs = map[string]string{
   109  		"us-east-1": "123-456-7890",
   110  		"us-west-1": "789-012-3456",
   111  	}
   112  	if err := c.Prepare(nil, nil); err == nil {
   113  		t.Fatal("should have error b/c theres a region in in ami_regions that isn't in the key map")
   114  	}
   115  
   116  	c.SnapshotUsers = []string{"foo", "bar"}
   117  	c.AMIKmsKeyId = "123-abc-456"
   118  	c.AMIEncryptBootVolume = true
   119  	c.AMIRegions = []string{"us-east-1", "us-west-1"}
   120  	c.AMIRegionKMSKeyIDs = map[string]string{
   121  		"us-east-1": "123-456-7890",
   122  		"us-west-1": "",
   123  	}
   124  	if err := c.Prepare(nil, nil); err == nil {
   125  		t.Fatal("should have error b/c theres a region in in ami_regions that isn't in the key map")
   126  	}
   127  
   128  	// allow rawregion to exist in ami_regions list.
   129  	accessConf := getFakeAccessConfig("us-east-1")
   130  	c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-2"}
   131  	c.AMIRegionKMSKeyIDs = nil
   132  	if err := c.Prepare(accessConf, nil); err != nil {
   133  		t.Fatal("should allow user to have the raw region in ami_regions")
   134  	}
   135  
   136  }
   137  
   138  func TestAMIConfigPrepare_Share_EncryptedBoot(t *testing.T) {
   139  	c := testAMIConfig()
   140  	c.AMIUsers = []string{"testAccountID"}
   141  	c.AMIEncryptBootVolume = true
   142  
   143  	c.AMIKmsKeyId = ""
   144  	if err := c.Prepare(nil, nil); err == nil {
   145  		t.Fatal("shouldn't be able to share ami with encrypted boot volume")
   146  	}
   147  
   148  	c.AMIKmsKeyId = "89c3fb9a-de87-4f2a-aedc-fddc5138193c"
   149  	if err := c.Prepare(nil, nil); err == nil {
   150  		t.Fatal("shouldn't be able to share ami with encrypted boot volume")
   151  	}
   152  }
   153  
   154  func TestAMINameValidation(t *testing.T) {
   155  	c := testAMIConfig()
   156  
   157  	c.AMIName = "aa"
   158  	if err := c.Prepare(nil, nil); err == nil {
   159  		t.Fatal("shouldn't be able to have an ami name with less than 3 characters")
   160  	}
   161  
   162  	var longAmiName string
   163  	for i := 0; i < 129; i++ {
   164  		longAmiName += "a"
   165  	}
   166  	c.AMIName = longAmiName
   167  	if err := c.Prepare(nil, nil); err == nil {
   168  		t.Fatal("shouldn't be able to have an ami name with great than 128 characters")
   169  	}
   170  
   171  	c.AMIName = "+aaa"
   172  	if err := c.Prepare(nil, nil); err == nil {
   173  		t.Fatal("shouldn't be able to have an ami name with invalid characters")
   174  	}
   175  
   176  	c.AMIName = "fooBAR1()[] ./-'@_"
   177  	if err := c.Prepare(nil, nil); err != nil {
   178  		t.Fatal("should be able to use all of the allowed AMI characters")
   179  	}
   180  
   181  	c.AMIName = `xyz-base-2017-04-05-1934`
   182  	if err := c.Prepare(nil, nil); err != nil {
   183  		t.Fatalf("expected `xyz-base-2017-04-05-1934` to pass validation.")
   184  	}
   185  
   186  }