File: images_test.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 3,068 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package ecs

import (
	"testing"

	"github.com/denverdino/aliyungo/common"
)

func TestImageCreationAndDeletion(t *testing.T) {

	client := NewTestClient()

	instance, err := client.DescribeInstanceAttribute(TestInstanceId)
	if err != nil {
		t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
	}

	args := DescribeSnapshotsArgs{}

	args.InstanceId = TestInstanceId
	args.RegionId = instance.RegionId
	snapshots, _, err := client.DescribeSnapshots(&args)

	if err != nil {
		t.Errorf("Failed to DescribeSnapshots for instance %s: %v", TestInstanceId, err)
	}

	if len(snapshots) > 0 {

		createImageArgs := CreateImageArgs{
			RegionId:   instance.RegionId,
			SnapshotId: snapshots[0].SnapshotId,

			ImageName:    "My_Test_Image_for_AliyunGo",
			ImageVersion: "1.0",
			Description:  "My Test Image for AliyunGo description",
			ClientToken:  client.GenerateClientToken(),
		}
		imageId, err := client.CreateImage(&createImageArgs)
		if err != nil {
			t.Errorf("Failed to CreateImage for SnapshotId %s: %v", createImageArgs.SnapshotId, err)
		}
		t.Logf("Image %s is created successfully.", imageId)

		err = client.DeleteImage(instance.RegionId, imageId)
		if err != nil {
			t.Errorf("Failed to DeleteImage for %s: %v", imageId, err)
		}
		t.Logf("Image %s is deleted successfully.", imageId)

	}
}

func TestModifyImageSharePermission(t *testing.T) {
	req := ModifyImageSharePermissionArgs{
		RegionId:   common.Beijing,
		ImageId:    TestImageId,
		AddAccount: []string{TestAccountId},
	}
	client := NewTestClient()
	err := client.ModifyImageSharePermission(&req)
	if err != nil {
		t.Errorf("Failed to ShareImage: %v", err)
	}

	shareInfo, err := client.DescribeImageSharePermission(&req)
	if err != nil {
		t.Errorf("Failed to ShareImage: %v", err)
	}
	t.Logf("result:image: %++v", shareInfo)
}

func TestCopyImage(t *testing.T) {
	client := NewTestClient()
	req := CopyImageArgs{
		RegionId:               common.Beijing,
		ImageId:                TestImageId,
		DestinationRegionId:    common.Hangzhou,
		DestinationImageName:   "My_Test_Image_NAME_for_AliyunGo",
		DestinationDescription: "My Test Image for AliyunGo description",
		ClientToken:            client.GenerateClientToken(),
	}

	imageId, err := client.CopyImage(&req)
	if err != nil {
		t.Errorf("Failed to CopyImage: %v", err)
	}
	t.Logf("result:image: %++v", imageId)

	if err := client.WaitForImageReady(common.Hangzhou, imageId, 600); err != nil {
		t.Errorf("Failed to WaitImage: %v", err)
		//return
	}

	describeReq := DescribeImagesArgs{
		RegionId:        common.Hangzhou,
		ImageId:         imageId,
		Status:          ImageStatusAvailable,
		ImageOwnerAlias: ImageOwnerSelf,
	}

	images, _, err := client.DescribeImages(&describeReq)
	if err != nil {
		t.Errorf("Failed to describeImage: %v", err)
	}
	t.Logf("result: images %++v", images)
}

func TestCancelCopyImage(t *testing.T) {
	client := NewTestClient()
	if err := client.CancelCopyImage(common.Hangzhou, TestImageId); err != nil {
		t.Errorf("Failed to CancelCopyImage: %v", err)
	}
}