github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/alicloud/ecs/builder_acc_test.go (about)

     1  package ecs
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"fmt"
     8  
     9  	"github.com/denverdino/aliyungo/common"
    10  	"github.com/denverdino/aliyungo/ecs"
    11  	builderT "github.com/hashicorp/packer/helper/builder/testing"
    12  	"github.com/hashicorp/packer/packer"
    13  )
    14  
    15  func TestBuilderAcc_basic(t *testing.T) {
    16  	builderT.Test(t, builderT.TestCase{
    17  		PreCheck: func() {
    18  			testAccPreCheck(t)
    19  		},
    20  		Builder:  &Builder{},
    21  		Template: testBuilderAccBasic,
    22  	})
    23  }
    24  
    25  //func TestBuilderAcc_windows(t *testing.T) {
    26  //	builderT.Test(t, builderT.TestCase{
    27  //		PreCheck: func() {
    28  //			testAccPreCheck(t)
    29  //		},
    30  //		Builder:  &Builder{},
    31  //		Template: testBuilderAccWindows,
    32  //	})
    33  //}
    34  
    35  //func TestBuilderAcc_regionCopy(t *testing.T) {
    36  //	builderT.Test(t, builderT.TestCase{
    37  //		PreCheck: func() {
    38  //			testAccPreCheck(t)
    39  //		},
    40  //		Builder:  &Builder{},
    41  //		Template: testBuilderAccRegionCopy,
    42  //		Check:    checkRegionCopy([]string{"cn-hangzhou", "cn-shenzhen"}),
    43  //	})
    44  //}
    45  
    46  func TestBuilderAcc_forceDelete(t *testing.T) {
    47  	// Build the same alicloud image twice, with ecs_image_force_delete on the second run
    48  	builderT.Test(t, builderT.TestCase{
    49  		PreCheck: func() {
    50  			testAccPreCheck(t)
    51  		},
    52  		Builder:              &Builder{},
    53  		Template:             buildForceDeregisterConfig("false", "delete"),
    54  		SkipArtifactTeardown: true,
    55  	})
    56  
    57  	builderT.Test(t, builderT.TestCase{
    58  		PreCheck: func() {
    59  			testAccPreCheck(t)
    60  		},
    61  		Builder:  &Builder{},
    62  		Template: buildForceDeregisterConfig("true", "delete"),
    63  	})
    64  }
    65  
    66  func TestBuilderAcc_ECSImageSharing(t *testing.T) {
    67  	builderT.Test(t, builderT.TestCase{
    68  		PreCheck: func() {
    69  			testAccPreCheck(t)
    70  		},
    71  		Builder:  &Builder{},
    72  		Template: testBuilderAccSharing,
    73  		Check:    checkECSImageSharing("1309208528360047"),
    74  	})
    75  }
    76  
    77  func TestBuilderAcc_forceDeleteSnapshot(t *testing.T) {
    78  	destImageName := "delete"
    79  
    80  	// Build the same alicloud image name twice, with force_delete_snapshot on the second run
    81  	builderT.Test(t, builderT.TestCase{
    82  		PreCheck: func() {
    83  			testAccPreCheck(t)
    84  		},
    85  		Builder:              &Builder{},
    86  		Template:             buildForceDeleteSnapshotConfig("false", destImageName),
    87  		SkipArtifactTeardown: true,
    88  	})
    89  
    90  	// Get image data by image image name
    91  	client, _ := testAliyunClient()
    92  	images, _, _ := client.DescribeImages(&ecs.DescribeImagesArgs{
    93  		ImageName: "packer-test-" + destImageName,
    94  		RegionId:  common.Region("cn-beijing")})
    95  
    96  	image := images[0]
    97  
    98  	// Get snapshot ids for image
    99  	snapshotIds := []string{}
   100  	for _, device := range image.DiskDeviceMappings.DiskDeviceMapping {
   101  		if device.Device != "" && device.SnapshotId != "" {
   102  			snapshotIds = append(snapshotIds, device.SnapshotId)
   103  		}
   104  	}
   105  
   106  	builderT.Test(t, builderT.TestCase{
   107  		PreCheck: func() {
   108  			testAccPreCheck(t)
   109  		},
   110  		Builder:  &Builder{},
   111  		Template: buildForceDeleteSnapshotConfig("true", destImageName),
   112  		Check:    checkSnapshotsDeleted(snapshotIds),
   113  	})
   114  }
   115  
   116  func checkSnapshotsDeleted(snapshotIds []string) builderT.TestCheckFunc {
   117  	return func(artifacts []packer.Artifact) error {
   118  		// Verify the snapshots are gone
   119  		client, _ := testAliyunClient()
   120  		snapshotResp, _, err := client.DescribeSnapshots(
   121  			&ecs.DescribeSnapshotsArgs{RegionId: common.Region("cn-beijing"), SnapshotIds: snapshotIds},
   122  		)
   123  		if err != nil {
   124  			return fmt.Errorf("Query snapshot failed %v", err)
   125  		}
   126  		if len(snapshotResp) > 0 {
   127  			return fmt.Errorf("Snapshots weren't successfully deleted by " +
   128  				"`ecs_image_force_delete_snapshots`")
   129  		}
   130  		return nil
   131  	}
   132  }
   133  
   134  func checkECSImageSharing(uid string) builderT.TestCheckFunc {
   135  	return func(artifacts []packer.Artifact) error {
   136  		if len(artifacts) > 1 {
   137  			return fmt.Errorf("more than 1 artifact")
   138  		}
   139  
   140  		// Get the actual *Artifact pointer so we can access the AMIs directly
   141  		artifactRaw := artifacts[0]
   142  		artifact, ok := artifactRaw.(*Artifact)
   143  		if !ok {
   144  			return fmt.Errorf("unknown artifact: %#v", artifactRaw)
   145  		}
   146  
   147  		// describe the image, get block devices with a snapshot
   148  		client, _ := testAliyunClient()
   149  		imageSharePermissionResponse, err := client.DescribeImageSharePermission(
   150  			&ecs.ModifyImageSharePermissionArgs{
   151  				RegionId: "cn-beijing",
   152  				ImageId:  artifact.AlicloudImages["cn-beijing"],
   153  			})
   154  
   155  		if err != nil {
   156  			return fmt.Errorf("Error retrieving Image Attributes for ECS Image Artifact (%#v) "+
   157  				"in ECS Image Sharing Test: %s", artifact, err)
   158  		}
   159  
   160  		if len(imageSharePermissionResponse.Accounts.Account) != 1 &&
   161  			imageSharePermissionResponse.Accounts.Account[0].AliyunId != uid {
   162  			return fmt.Errorf("share account is incorrect %d",
   163  				len(imageSharePermissionResponse.Accounts.Account))
   164  		}
   165  
   166  		return nil
   167  	}
   168  }
   169  
   170  func checkRegionCopy(regions []string) builderT.TestCheckFunc {
   171  	return func(artifacts []packer.Artifact) error {
   172  		if len(artifacts) > 1 {
   173  			return fmt.Errorf("more than 1 artifact")
   174  		}
   175  
   176  		// Get the actual *Artifact pointer so we can access the AMIs directly
   177  		artifactRaw := artifacts[0]
   178  		artifact, ok := artifactRaw.(*Artifact)
   179  		if !ok {
   180  			return fmt.Errorf("unknown artifact: %#v", artifactRaw)
   181  		}
   182  
   183  		// Verify that we copied to only the regions given
   184  		regionSet := make(map[string]struct{})
   185  		for _, r := range regions {
   186  			regionSet[r] = struct{}{}
   187  		}
   188  		for r := range artifact.AlicloudImages {
   189  			if r == "cn-beijing" {
   190  				delete(regionSet, r)
   191  				continue
   192  			}
   193  			if _, ok := regionSet[r]; !ok {
   194  				return fmt.Errorf("unknown region: %s", r)
   195  			}
   196  
   197  			delete(regionSet, r)
   198  		}
   199  		if len(regionSet) > 0 {
   200  			return fmt.Errorf("didn't copy to: %#v", regionSet)
   201  		}
   202  		client, _ := testAliyunClient()
   203  		for key, value := range artifact.AlicloudImages {
   204  			client.WaitForImageReady(common.Region(key), value, 1800)
   205  		}
   206  		return nil
   207  	}
   208  }
   209  
   210  func testAccPreCheck(t *testing.T) {
   211  	if v := os.Getenv("ALICLOUD_ACCESS_KEY"); v == "" {
   212  		t.Fatal("ALICLOUD_ACCESS_KEY must be set for acceptance tests")
   213  	}
   214  
   215  	if v := os.Getenv("ALICLOUD_SECRET_KEY"); v == "" {
   216  		t.Fatal("ALICLOUD_SECRET_KEY must be set for acceptance tests")
   217  	}
   218  }
   219  
   220  func testAliyunClient() (*ecs.Client, error) {
   221  	access := &AlicloudAccessConfig{AlicloudRegion: "cn-beijing"}
   222  	err := access.Config()
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  	client, err := access.Client()
   227  	if err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	return client, nil
   232  }
   233  
   234  const testBuilderAccBasic = `
   235  {	"builders": [{
   236  		"type": "test",
   237  		"region": "cn-beijing",
   238  		"instance_type": "ecs.n1.tiny",
   239  		"source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
   240  		"ssh_username": "ubuntu",
   241  		"io_optimized":"true",
   242  		"ssh_username":"root",
   243  		"image_name": "packer-test_{{timestamp}}"
   244  	}]
   245  }`
   246  
   247  const testBuilderAccRegionCopy = `
   248  {
   249  	"builders": [{
   250  		"type": "test",
   251  		"region": "cn-beijing",
   252  		"instance_type": "ecs.n1.tiny",
   253  		"source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
   254  		"io_optimized":"true",
   255  		"ssh_username":"root",
   256  		"image_name": "packer-test_{{timestamp}}",
   257  		"image_copy_regions": ["cn-hangzhou", "cn-shenzhen"]
   258  	}]
   259  }
   260  `
   261  
   262  const testBuilderAccForceDelete = `
   263  {
   264  	"builders": [{
   265  		"type": "test",
   266  		"region": "cn-beijing",
   267  		"instance_type": "ecs.n1.tiny",
   268  		"source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
   269  		"io_optimized":"true",
   270  		"ssh_username":"root",
   271  		"image_force_delete": "%s",
   272  		"image_name": "packer-test_%s"
   273  	}]
   274  }
   275  `
   276  
   277  const testBuilderAccForceDeleteSnapshot = `
   278  {
   279  	"builders": [{
   280  		"type": "test",
   281  		"region": "cn-beijing",
   282  		"instance_type": "ecs.n1.tiny",
   283  		"source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
   284  		"io_optimized":"true",
   285  		"ssh_username":"root",
   286  		"image_force_delete_snapshots": "%s",
   287  		"image_force_delete": "%s",
   288  		"image_name": "packer-test-%s"
   289  	}]
   290  }
   291  `
   292  
   293  // share with catsby
   294  const testBuilderAccSharing = `
   295  {
   296  	"builders": [{
   297  		"type": "test",
   298  		"region": "cn-beijing",
   299  		"instance_type": "ecs.n1.tiny",
   300  		"source_image":"ubuntu_16_0402_64_40G_base_20170222.vhd",
   301  		"io_optimized":"true",
   302  		"ssh_username":"root",
   303  		"image_name": "packer-test_{{timestamp}}",
   304  		"image_share_account":["1309208528360047"]
   305  	}]
   306  }
   307  `
   308  
   309  func buildForceDeregisterConfig(val, name string) string {
   310  	return fmt.Sprintf(testBuilderAccForceDelete, val, name)
   311  }
   312  
   313  func buildForceDeleteSnapshotConfig(val, name string) string {
   314  	return fmt.Sprintf(testBuilderAccForceDeleteSnapshot, val, val, name)
   315  }
   316  
   317  const testBuilderAccWindows = `
   318  {	"builders": [{
   319  		"type": "test",
   320  		"region": "cn-beijing",
   321  		"instance_type": "ecs.n1.tiny",
   322  		"source_image":"win2008_64_ent_r2_zh-cn_40G_alibase_20170301.vhd",
   323  		"io_optimized":"true",
   324  		"image_force_delete":"true",
   325  		"communicator": "winrm",
   326  		"winrm_port": 5985,
   327  		"winrm_username": "Administrator",
   328  		"winrm_password": "Test1234",
   329  		"image_name": "packer-test_{{timestamp}}"
   330  	}]
   331  }`