| 1 |
package ecs |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/denverdino/aliyungo/common" |
| 7 |
) |
| 8 |
|
| 9 |
func TestImageCreationAndDeletion(t *testing.T) { |
| 10 |
|
| 11 |
client := NewTestClient() |
| 12 |
|
| 13 |
instance, err := client.DescribeInstanceAttribute(TestInstanceId) |
| 14 |
if err != nil { |
| 15 |
t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err) |
| 16 |
} |
| 17 |
|
| 18 |
args := DescribeSnapshotsArgs{} |
| 19 |
|
| 20 |
args.InstanceId = TestInstanceId |
| 21 |
args.RegionId = instance.RegionId |
| 22 |
snapshots, _, err := client.DescribeSnapshots(&args) |
| 23 |
|
| 24 |
if err != nil { |
| 25 |
t.Errorf("Failed to DescribeSnapshots for instance %s: %v", TestInstanceId, err) |
| 26 |
} |
| 27 |
|
| 28 |
if len(snapshots) > 0 { |
| 29 |
|
| 30 |
createImageArgs := CreateImageArgs{ |
| 31 |
RegionId: instance.RegionId, |
| 32 |
SnapshotId: snapshots[0].SnapshotId, |
| 33 |
|
| 34 |
ImageName: "My_Test_Image_for_AliyunGo", |
| 35 |
ImageVersion: "1.0", |
| 36 |
Description: "My Test Image for AliyunGo description", |
| 37 |
ClientToken: client.GenerateClientToken(), |
| 38 |
} |
| 39 |
imageId, err := client.CreateImage(&createImageArgs) |
| 40 |
if err != nil { |
| 41 |
t.Errorf("Failed to CreateImage for SnapshotId %s: %v", createImageArgs.SnapshotId, err) |
| 42 |
} |
| 43 |
t.Logf("Image %s is created successfully.", imageId) |
| 44 |
|
| 45 |
err = client.DeleteImage(instance.RegionId, imageId) |
| 46 |
if err != nil { |
| 47 |
t.Errorf("Failed to DeleteImage for %s: %v", imageId, err) |
| 48 |
} |
| 49 |
t.Logf("Image %s is deleted successfully.", imageId) |
| 50 |
|
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestModifyImageSharePermission(t *testing.T) { |
| 55 |
req := ModifyImageSharePermissionArgs{ |
| 56 |
RegionId: common.Beijing, |
| 57 |
ImageId: TestImageId, |
| 58 |
AddAccount: []string{TestAccountId}, |
| 59 |
} |
| 60 |
client := NewTestClient() |
| 61 |
err := client.ModifyImageSharePermission(&req) |
| 62 |
if err != nil { |
| 63 |
t.Errorf("Failed to ShareImage: %v", err) |
| 64 |
} |
| 65 |
|
| 66 |
shareInfo, err := client.DescribeImageSharePermission(&req) |
| 67 |
if err != nil { |
| 68 |
t.Errorf("Failed to ShareImage: %v", err) |
| 69 |
} |
| 70 |
t.Logf("result:image: %++v", shareInfo) |
| 71 |
} |
| 72 |
|
| 73 |
func TestCopyImage(t *testing.T) { |
| 74 |
client := NewTestClient() |
| 75 |
req := CopyImageArgs{ |
| 76 |
RegionId: common.Beijing, |
| 77 |
ImageId: TestImageId, |
| 78 |
DestinationRegionId: common.Hangzhou, |
| 79 |
DestinationImageName: "My_Test_Image_NAME_for_AliyunGo", |
| 80 |
DestinationDescription: "My Test Image for AliyunGo description", |
| 81 |
ClientToken: client.GenerateClientToken(), |
| 82 |
} |
| 83 |
|
| 84 |
imageId, err := client.CopyImage(&req) |
| 85 |
if err != nil { |
| 86 |
t.Errorf("Failed to CopyImage: %v", err) |
| 87 |
} |
| 88 |
t.Logf("result:image: %++v", imageId) |
| 89 |
|
| 90 |
if err := client.WaitForImageReady(common.Hangzhou, imageId, 600); err != nil { |
| 91 |
t.Errorf("Failed to WaitImage: %v", err) |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
describeReq := DescribeImagesArgs{ |
| 96 |
RegionId: common.Hangzhou, |
| 97 |
ImageId: imageId, |
| 98 |
Status: ImageStatusAvailable, |
| 99 |
ImageOwnerAlias: ImageOwnerSelf, |
| 100 |
} |
| 101 |
|
| 102 |
images, _, err := client.DescribeImages(&describeReq) |
| 103 |
if err != nil { |
| 104 |
t.Errorf("Failed to describeImage: %v", err) |
| 105 |
} |
| 106 |
t.Logf("result: images %++v", images) |
| 107 |
} |
| 108 |
|
| 109 |
func TestCancelCopyImage(t *testing.T) { |
| 110 |
client := NewTestClient() |
| 111 |
if err := client.CancelCopyImage(common.Hangzhou, TestImageId); err != nil { |
| 112 |
t.Errorf("Failed to CancelCopyImage: %v", err) |
| 113 |
} |
| 114 |
} |