Debian Codegraph @

ecs/disks_test.go

raw symbols SCIP
1 package ecs
2
3 import (
4 "os"
5 "testing"
6 "time"
7 )
8
9 func TestDisks(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 := DescribeDisksArgs{}
19
20 args.InstanceId = TestInstanceId
21 args.RegionId = instance.RegionId
22 disks, _, err := client.DescribeDisks(&args)
23
24 if err != nil {
25 t.Fatalf("Failed to DescribeDisks for instance %s: %v", TestInstanceId, err)
26 }
27
28 for _, disk := range disks {
29 t.Logf("Disk of instance %s: %++v", TestInstanceId, disk)
30 }
31 }
32
33 func TestDiskCreationAndDeletion(t *testing.T) {
34
35 if TestIAmRich == false { //Avoid payment
36 return
37 }
38
39 client := NewTestClient()
40
41 instance, err := client.DescribeInstanceAttribute(TestInstanceId)
42 if err != nil {
43 t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
44 }
45
46 args := CreateDiskArgs{
47 RegionId: instance.RegionId,
48 ZoneId: instance.ZoneId,
49 DiskName: "test-disk",
50 Size: 5,
51 }
52
53 diskId, err := client.CreateDisk(&args)
54 if err != nil {
55 t.Fatalf("Failed to create disk: %v", err)
56 }
57 t.Logf("Create disk %s successfully", diskId)
58
59 attachArgs := AttachDiskArgs{
60 InstanceId: instance.InstanceId,
61 DiskId: diskId,
62 }
63
64 err = client.AttachDisk(&attachArgs)
65 if err != nil {
66 t.Errorf("Failed to create disk: %v", err)
67 } else {
68 t.Logf("Attach disk %s to instance %s successfully", diskId, instance.InstanceId)
69
70 instance, err = client.DescribeInstanceAttribute(TestInstanceId)
71 if err != nil {
72 t.Errorf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
73 } else {
74 t.Logf("Instance: %++v %v", instance, err)
75 }
76 err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusInUse, 0)
77 if err != nil {
78 t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusInUse, err)
79 }
80 err = client.DetachDisk(instance.InstanceId, diskId)
81 if err != nil {
82 t.Errorf("Failed to detach disk: %v", err)
83 } else {
84 t.Logf("Detach disk %s to instance %s successfully", diskId, instance.InstanceId)
85 }
86
87 err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusAvailable, 0)
88 if err != nil {
89 t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusAvailable, err)
90 }
91 }
92 err = client.DeleteDisk(diskId)
93 if err != nil {
94 t.Fatalf("Failed to delete disk %s: %v", diskId, err)
95 }
96 t.Logf("Delete disk %s successfully", diskId)
97 }
98
99 func TestReplaceSystemDiskUsingSizeParam(t *testing.T) {
100 client := NewTestClientForDebug()
101
102 args := ReplaceSystemDiskArgs{
103 InstanceId: TestInstanceId,
104 ImageId: TestImageId,
105 SystemDisk: SystemDiskType{
106 Size: 192,
107 },
108 ClientToken: client.GenerateClientToken(),
109 }
110
111 diskId, err := client.ReplaceSystemDisk(&args)
112 if err != nil {
113 t.Errorf("Failed to replace system disk %v", err)
114 } else {
115 t.Logf("diskId is %s", diskId)
116 }
117 }
118
119 func TestReplaceSystemDisk(t *testing.T) {
120 client := NewTestClient()
121
122 err := client.WaitForInstance(TestInstanceId, Running, 0)
123 err = client.StopInstance(TestInstanceId, true)
124 if err != nil {
125 t.Errorf("Failed to stop instance %s: %v", TestInstanceId, err)
126 }
127 err = client.WaitForInstance(TestInstanceId, Stopped, 0)
128 if err != nil {
129 t.Errorf("Instance %s is failed to stop: %v", TestInstanceId, err)
130 }
131 t.Logf("Instance %s is stopped successfully.", TestInstanceId)
132
133 args := ReplaceSystemDiskArgs{
134 InstanceId: TestInstanceId,
135 ImageId: TestImageId,
136 }
137
138 diskId, err := client.ReplaceSystemDisk(&args)
139 if err != nil {
140 t.Errorf("Failed to replace system disk %v", err)
141 }
142 err = client.WaitForInstance(TestInstanceId, Stopped, 60)
143 err = client.StartInstance(TestInstanceId)
144 if err != nil {
145 t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
146 } else {
147 err = client.WaitForInstance(TestInstanceId, Running, 0)
148 if err != nil {
149 t.Errorf("Failed to wait for instance %s running: %v", TestInstanceId, err)
150 }
151 }
152 t.Logf("Replace system disk %s successfully ", diskId)
153 }
154
155 func TestResizeDisk(t *testing.T) {
156 accessKeyId := os.Getenv("ACCESS_KEY_ID")
157 accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")
158
159 client := NewClient(accessKeyId, accessKeySecret)
160
161 args := CreateDiskArgs{
162 RegionId: "cn-beijing",
163 ZoneId: "cn-beijing-a",
164 Size: 40,
165 DiskCategory: DiskCategoryCloudEfficiency,
166 }
167 diskId, err := client.CreateDisk(&args)
168
169 if err != nil {
170 t.Errorf("CreateDisk failed %v", err)
171 }
172
173 time.Sleep(time.Duration(90) * time.Second) // wait for disk inner status ready to resize
174 err = client.ResizeDisk(diskId, 60)
175
176 if err != nil {
177 t.Errorf("ResizeDisk failed %v", err)
178 }
179
180 _ = client.DeleteDisk(diskId)
181 }