github.phpd.cn/hashicorp/packer@v1.3.2/builder/alicloud/ecs/image_config_test.go (about)

     1  package ecs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/denverdino/aliyungo/common"
     7  )
     8  
     9  func testAlicloudImageConfig() *AlicloudImageConfig {
    10  	return &AlicloudImageConfig{
    11  		AlicloudImageName: "foo",
    12  	}
    13  }
    14  
    15  func TestECSImageConfigPrepare_name(t *testing.T) {
    16  	c := testAlicloudImageConfig()
    17  	if err := c.Prepare(nil); err != nil {
    18  		t.Fatalf("shouldn't have err: %s", err)
    19  	}
    20  
    21  	c.AlicloudImageName = ""
    22  	if err := c.Prepare(nil); err == nil {
    23  		t.Fatal("should have error")
    24  	}
    25  }
    26  
    27  func TestAMIConfigPrepare_regions(t *testing.T) {
    28  	c := testAlicloudImageConfig()
    29  	c.AlicloudImageDestinationRegions = nil
    30  	if err := c.Prepare(nil); err != nil {
    31  		t.Fatalf("shouldn't have err: %s", err)
    32  	}
    33  
    34  	c.AlicloudImageDestinationRegions = regionsToString()
    35  	if err := c.Prepare(nil); err != nil {
    36  		t.Fatalf("shouldn't have err: %s", err)
    37  	}
    38  
    39  	c.AlicloudImageDestinationRegions = []string{"foo"}
    40  	if err := c.Prepare(nil); err == nil {
    41  		t.Fatal("should have error")
    42  	}
    43  
    44  	c.AlicloudImageDestinationRegions = []string{"cn-beijing", "cn-hangzhou", "eu-central-1"}
    45  	if err := c.Prepare(nil); err != nil {
    46  		t.Fatalf("bad: %s", err)
    47  	}
    48  
    49  	c.AlicloudImageDestinationRegions = []string{"unknow"}
    50  	c.AlicloudImageSkipRegionValidation = true
    51  	if err := c.Prepare(nil); err != nil {
    52  		t.Fatal("shouldn't have error")
    53  	}
    54  	c.AlicloudImageSkipRegionValidation = false
    55  
    56  }
    57  
    58  func TestECSImageConfigPrepare_imageTags(t *testing.T) {
    59  	c := testAlicloudImageConfig()
    60  	c.AlicloudImageTags = map[string]string{
    61  		"TagKey1": "TagValue1",
    62  		"TagKey2": "TagValue2",
    63  	}
    64  	if err := c.Prepare(nil); len(err) != 0 {
    65  		t.Fatalf("err: %s", err)
    66  	}
    67  	if len(c.AlicloudImageTags) != 2 || c.AlicloudImageTags["TagKey1"] != "TagValue1" ||
    68  		c.AlicloudImageTags["TagKey2"] != "TagValue2" {
    69  		t.Fatalf("invalid value, expected: %s, actual: %s", map[string]string{
    70  			"TagKey1": "TagValue1",
    71  			"TagKey2": "TagValue2",
    72  		}, c.AlicloudImageTags)
    73  	}
    74  }
    75  
    76  func regionsToString() []string {
    77  	var regions []string
    78  	for _, region := range common.ValidRegions {
    79  		regions = append(regions, string(region))
    80  	}
    81  	return regions
    82  }