File: disks_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 (181 lines) | stat: -rw-r--r-- 4,732 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ecs

import (
	"os"
	"testing"
	"time"
)

func TestDisks(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 := DescribeDisksArgs{}

	args.InstanceId = TestInstanceId
	args.RegionId = instance.RegionId
	disks, _, err := client.DescribeDisks(&args)

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

	for _, disk := range disks {
		t.Logf("Disk of instance %s: %++v", TestInstanceId, disk)
	}
}

func TestDiskCreationAndDeletion(t *testing.T) {

	if TestIAmRich == false { //Avoid payment
		return
	}

	client := NewTestClient()

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

	args := CreateDiskArgs{
		RegionId: instance.RegionId,
		ZoneId:   instance.ZoneId,
		DiskName: "test-disk",
		Size:     5,
	}

	diskId, err := client.CreateDisk(&args)
	if err != nil {
		t.Fatalf("Failed to create disk: %v", err)
	}
	t.Logf("Create disk %s successfully", diskId)

	attachArgs := AttachDiskArgs{
		InstanceId: instance.InstanceId,
		DiskId:     diskId,
	}

	err = client.AttachDisk(&attachArgs)
	if err != nil {
		t.Errorf("Failed to create disk: %v", err)
	} else {
		t.Logf("Attach disk %s to instance %s successfully", diskId, instance.InstanceId)

		instance, err = client.DescribeInstanceAttribute(TestInstanceId)
		if err != nil {
			t.Errorf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
		} else {
			t.Logf("Instance: %++v  %v", instance, err)
		}
		err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusInUse, 0)
		if err != nil {
			t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusInUse, err)
		}
		err = client.DetachDisk(instance.InstanceId, diskId)
		if err != nil {
			t.Errorf("Failed to detach disk: %v", err)
		} else {
			t.Logf("Detach disk %s to instance %s successfully", diskId, instance.InstanceId)
		}

		err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusAvailable, 0)
		if err != nil {
			t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusAvailable, err)
		}
	}
	err = client.DeleteDisk(diskId)
	if err != nil {
		t.Fatalf("Failed to delete disk %s: %v", diskId, err)
	}
	t.Logf("Delete disk %s successfully", diskId)
}

func TestReplaceSystemDiskUsingSizeParam(t *testing.T) {
	client := NewTestClientForDebug()

	args := ReplaceSystemDiskArgs{
		InstanceId: TestInstanceId,
		ImageId:    TestImageId,
		SystemDisk: SystemDiskType{
			Size: 192,
		},
		ClientToken: client.GenerateClientToken(),
	}

	diskId, err := client.ReplaceSystemDisk(&args)
	if err != nil {
		t.Errorf("Failed to replace system disk %v", err)
	} else {
		t.Logf("diskId is %s", diskId)
	}
}

func TestReplaceSystemDisk(t *testing.T) {
	client := NewTestClient()

	err := client.WaitForInstance(TestInstanceId, Running, 0)
	err = client.StopInstance(TestInstanceId, true)
	if err != nil {
		t.Errorf("Failed to stop instance %s: %v", TestInstanceId, err)
	}
	err = client.WaitForInstance(TestInstanceId, Stopped, 0)
	if err != nil {
		t.Errorf("Instance %s is failed to stop: %v", TestInstanceId, err)
	}
	t.Logf("Instance %s is stopped successfully.", TestInstanceId)

	args := ReplaceSystemDiskArgs{
		InstanceId: TestInstanceId,
		ImageId:    TestImageId,
	}

	diskId, err := client.ReplaceSystemDisk(&args)
	if err != nil {
		t.Errorf("Failed to replace system disk %v", err)
	}
	err = client.WaitForInstance(TestInstanceId, Stopped, 60)
	err = client.StartInstance(TestInstanceId)
	if err != nil {
		t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
	} else {
		err = client.WaitForInstance(TestInstanceId, Running, 0)
		if err != nil {
			t.Errorf("Failed to wait for instance %s running: %v", TestInstanceId, err)
		}
	}
	t.Logf("Replace system disk %s successfully ", diskId)
}

func TestResizeDisk(t *testing.T) {
	accessKeyId := os.Getenv("ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")

	client := NewClient(accessKeyId, accessKeySecret)

	args := CreateDiskArgs{
		RegionId:     "cn-beijing",
		ZoneId:       "cn-beijing-a",
		Size:         40,
		DiskCategory: DiskCategoryCloudEfficiency,
	}
	diskId, err := client.CreateDisk(&args)

	if err != nil {
		t.Errorf("CreateDisk failed %v", err)
	}

	time.Sleep(time.Duration(90) * time.Second) // wait for disk inner status ready to resize
	err = client.ResizeDisk(diskId, 60)

	if err != nil {
		t.Errorf("ResizeDisk failed %v", err)
	}

	_ = client.DeleteDisk(diskId)
}