github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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 regionsToString() []string {
    59  	var regions []string
    60  	for _, region := range common.ValidRegions {
    61  		regions = append(regions, string(region))
    62  	}
    63  	return regions
    64  }