2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-17 05:25:56 -05:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2021-04-07 05:33:22 -04:00
|
|
|
builderT "github.com/hashicorp/packer/acctest"
|
2019-10-12 04:46:21 -04:00
|
|
|
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
|
2019-09-11 23:33:57 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-13 03:16:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilderAcc_validateRegion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if os.Getenv(builderT.TestEnvVar) == "" {
|
|
|
|
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", builderT.TestEnvVar))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
testAccPreCheck(t)
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
access := &ucloudcommon.AccessConfig{Region: "cn-bj2"}
|
2019-06-13 03:16:49 -04:00
|
|
|
err := access.Config()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error on initing UCloud AccessConfig, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = access.ValidateRegion("cn-sh2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected pass with valid region but failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = access.ValidateRegion("invalidRegion")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected failure due to invalid region but passed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderAcc_basic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
},
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: testBuilderAccBasic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const testBuilderAccBasic = `
|
|
|
|
{ "builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "cn-bj2",
|
|
|
|
"availability_zone": "cn-bj2-02",
|
|
|
|
"instance_type": "n-basic-2",
|
|
|
|
"source_image_id":"uimage-f1chxn",
|
|
|
|
"ssh_username":"root",
|
|
|
|
"image_name": "packer-test-basic_{{timestamp}}"
|
|
|
|
}]
|
|
|
|
}`
|
|
|
|
|
|
|
|
func TestBuilderAcc_ubuntu(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
},
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: testBuilderAccUbuntu,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const testBuilderAccUbuntu = `
|
|
|
|
{ "builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "cn-bj2",
|
|
|
|
"availability_zone": "cn-bj2-02",
|
|
|
|
"instance_type": "n-basic-2",
|
|
|
|
"source_image_id":"uimage-irofn4",
|
|
|
|
"ssh_username":"ubuntu",
|
2019-06-13 06:38:07 -04:00
|
|
|
"image_name": "packer-test-ubuntu_{{timestamp}}"
|
2019-06-13 03:16:49 -04:00
|
|
|
}]
|
|
|
|
}`
|
|
|
|
|
|
|
|
func TestBuilderAcc_regionCopy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
projectId := os.Getenv("UCLOUD_PROJECT_ID")
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
},
|
|
|
|
Builder: &Builder{},
|
2019-06-13 06:38:07 -04:00
|
|
|
Template: testBuilderAccRegionCopy(projectId),
|
2019-06-13 03:16:49 -04:00
|
|
|
Check: checkRegionCopy(
|
|
|
|
projectId,
|
2019-10-12 04:46:21 -04:00
|
|
|
[]ucloudcommon.ImageDestination{
|
|
|
|
{ProjectId: projectId, Region: "cn-sh2", Name: "packer-test-regionCopy-sh", Description: "test"},
|
2019-06-13 03:16:49 -04:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:38:07 -04:00
|
|
|
func testBuilderAccRegionCopy(projectId string) string {
|
|
|
|
return fmt.Sprintf(`
|
2019-06-13 03:16:49 -04:00
|
|
|
{
|
|
|
|
"builders": [{
|
|
|
|
"type": "test",
|
|
|
|
"region": "cn-bj2",
|
|
|
|
"availability_zone": "cn-bj2-02",
|
|
|
|
"instance_type": "n-basic-2",
|
|
|
|
"source_image_id":"uimage-f1chxn",
|
|
|
|
"ssh_username":"root",
|
2019-06-13 06:38:07 -04:00
|
|
|
"image_name": "packer-test-regionCopy-bj",
|
|
|
|
"image_copy_to_mappings": [{
|
|
|
|
"project_id": %q,
|
|
|
|
"region": "cn-sh2",
|
|
|
|
"name": "packer-test-regionCopy-sh",
|
|
|
|
"description": "test"
|
2019-06-13 03:16:49 -04:00
|
|
|
}]
|
|
|
|
}]
|
2019-06-13 06:38:07 -04:00
|
|
|
}`, projectId)
|
|
|
|
}
|
2019-06-13 03:16:49 -04:00
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func checkRegionCopy(projectId string, imageDst []ucloudcommon.ImageDestination) builderT.TestCheckFunc {
|
2020-11-19 15:17:11 -05:00
|
|
|
return func(artifacts []packersdk.Artifact) error {
|
2019-06-13 03:16:49 -04:00
|
|
|
if len(artifacts) > 1 {
|
|
|
|
return fmt.Errorf("more than 1 artifact")
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactSet := artifacts[0]
|
2019-10-12 04:46:21 -04:00
|
|
|
artifact, ok := artifactSet.(*ucloudcommon.Artifact)
|
2019-06-13 03:16:49 -04:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown artifact: %#v", artifactSet)
|
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
destSet := ucloudcommon.NewImageInfoSet(nil)
|
2019-06-13 03:16:49 -04:00
|
|
|
for _, dest := range imageDst {
|
2019-10-12 04:46:21 -04:00
|
|
|
destSet.Set(ucloudcommon.ImageInfo{
|
2019-06-13 03:16:49 -04:00
|
|
|
Region: dest.Region,
|
|
|
|
ProjectId: dest.ProjectId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range artifact.UCloudImages.GetAll() {
|
|
|
|
if r.ProjectId == projectId && r.Region == "cn-bj2" {
|
|
|
|
destSet.Remove(r.Id())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if destSet.Get(r.ProjectId, r.Region) == nil {
|
|
|
|
return fmt.Errorf("project%s : region%s is not the target but found in artifacts", r.ProjectId, r.Region)
|
|
|
|
}
|
|
|
|
|
|
|
|
destSet.Remove(r.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(destSet.GetAll()) > 0 {
|
|
|
|
return fmt.Errorf("the following copying targets not found in corresponding artifacts : %#v", destSet.GetAll())
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _ := testUCloudClient()
|
|
|
|
for _, r := range artifact.UCloudImages.GetAll() {
|
|
|
|
if r.ProjectId == projectId && r.Region == "cn-bj2" {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
imageSet, err := client.DescribeImageByInfo(r.ProjectId, r.Region, r.ImageId)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
if ucloudcommon.IsNotFoundError(err) {
|
2019-06-13 03:16:49 -04:00
|
|
|
return fmt.Errorf("image %s in artifacts can not be found", r.ImageId)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Region == "cn-sh2" && imageSet.ImageName != "packer-test-regionCopy-sh" {
|
|
|
|
return fmt.Errorf("the name of image %q in artifacts should be %s, got %s", r.ImageId, "packer-test-regionCopy-sh", imageSet.ImageName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("UCLOUD_PUBLIC_KEY"); v == "" {
|
|
|
|
t.Fatal("UCLOUD_PUBLIC_KEY must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("UCLOUD_PRIVATE_KEY"); v == "" {
|
|
|
|
t.Fatal("UCLOUD_PRIVATE_KEY must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("UCLOUD_PROJECT_ID"); v == "" {
|
|
|
|
t.Fatal("UCLOUD_PROJECT_ID must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 23:33:57 -04:00
|
|
|
func TestUCloudClientBaseUrlConfigurable(t *testing.T) {
|
|
|
|
const url = "baseUrl"
|
2020-06-24 08:31:05 -04:00
|
|
|
access := &ucloudcommon.AccessConfig{BaseUrl: url, PublicKey: "test", PrivateKey: "test"}
|
2019-09-11 23:33:57 -04:00
|
|
|
client, err := access.Client()
|
|
|
|
assert.Nil(t, err)
|
2019-10-24 01:49:33 -04:00
|
|
|
assert.Equal(t, url, client.UAccountConn.Client.GetConfig().BaseUrl, "account conn's base url not configurable")
|
|
|
|
assert.Equal(t, url, client.UHostConn.Client.GetConfig().BaseUrl, "host conn's base url not configurable")
|
|
|
|
assert.Equal(t, url, client.UNetConn.Client.GetConfig().BaseUrl, "net conn's base url not configurable")
|
|
|
|
assert.Equal(t, url, client.VPCConn.Client.GetConfig().BaseUrl, "vpc conn's base url not configurable")
|
2019-09-11 23:33:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
func testUCloudClient() (*ucloudcommon.UCloudClient, error) {
|
|
|
|
access := &ucloudcommon.AccessConfig{Region: "cn-bj2"}
|
2019-06-13 03:16:49 -04:00
|
|
|
err := access.Config()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client, err := access.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|