Debian Codegraph @

ecs/instances_test.go

raw symbols SCIP
1 package ecs
2
3 import (
4 "encoding/json"
5 "fmt"
6 "testing"
7
8 "github.com/denverdino/aliyungo/common"
9 )
10
11 func ExampleClient_DescribeInstanceStatus() {
12 fmt.Printf("DescribeInstanceStatus Example\n")
13
14 args := DescribeInstanceStatusArgs{
15 RegionId: "cn-beijing",
16 ZoneId: "cn-beijing-b",
17 Pagination: common.Pagination{
18 PageNumber: 1,
19 PageSize: 1,
20 },
21 }
22
23 client := NewTestClient()
24 instanceStatus, _, err := client.DescribeInstanceStatus(&args)
25
26 if err != nil {
27 fmt.Printf("Failed to describe Instance: %s status:%v \n", TestInstanceId, err)
28 } else {
29 for i := 0; i < len(instanceStatus); i++ {
30 fmt.Printf("Instance %s Status: %s \n", instanceStatus[i].InstanceId, instanceStatus[i].Status)
31 }
32 }
33 }
34
35 func ExampleClient_DescribeInstanceAttribute() {
36 fmt.Printf("DescribeInstanceAttribute Example\n")
37
38 client := NewTestClient()
39
40 instanceAttributeType, err := client.DescribeInstanceAttribute(TestInstanceId)
41
42 if err != nil {
43 fmt.Printf("Failed to describe Instance %s attribute: %v\n", TestInstanceId, err)
44 } else {
45 fmt.Printf("Instance Information\n")
46 fmt.Printf("InstanceId = %s \n", instanceAttributeType.InstanceId)
47 fmt.Printf("InstanceName = %s \n", instanceAttributeType.InstanceName)
48 fmt.Printf("HostName = %s \n", instanceAttributeType.HostName)
49 fmt.Printf("ZoneId = %s \n", instanceAttributeType.ZoneId)
50 fmt.Printf("RegionId = %s \n", instanceAttributeType.RegionId)
51 }
52 }
53
54 func ExampleClient_DescribeInstanceVncUrl() {
55 fmt.Printf("DescribeInstanceVncUrl Example\n")
56
57 args := DescribeInstanceVncUrlArgs{
58 RegionId: "cn-beijing",
59 InstanceId: TestInstanceId,
60 }
61
62 client := NewTestClient()
63
64 instanceVncUrl, err := client.DescribeInstanceVncUrl(&args)
65
66 if err != nil {
67 fmt.Printf("Failed to describe Instance %s vnc url: %v \n", TestInstanceId, err)
68 } else {
69 fmt.Printf("VNC URL = %s \n", instanceVncUrl)
70 }
71 }
72
73 func ExampleClient_StopInstance() {
74 fmt.Printf("Stop Instance Example\n")
75
76 client := NewTestClient()
77
78 err := client.StopInstance(TestInstanceId, true)
79
80 if err != nil {
81 fmt.Printf("Failed to stop Instance %s vnc url: %v \n", TestInstanceId, err)
82 }
83 }
84
85 func ExampleClient_DeleteInstance() {
86 fmt.Printf("Delete Instance Example")
87
88 client := NewTestClient()
89
90 err := client.DeleteInstance(TestInstanceId)
91
92 if err != nil {
93 fmt.Printf("Failed to delete Instance %s vnc url: %v \n", TestInstanceId, err)
94 }
95 }
96
97 func TestECSInstance(t *testing.T) {
98 if TestQuick {
99 return
100 }
101 client := NewTestClient()
102 instance, err := client.DescribeInstanceAttribute(TestInstanceId)
103 if err != nil {
104 t.Fatalf("Failed to describe instance %s: %v", TestInstanceId, err)
105 }
106 t.Logf("Instance: %++v %v", instance, err)
107 err = client.StopInstance(TestInstanceId, true)
108 if err != nil {
109 t.Errorf("Failed to stop instance %s: %v", TestInstanceId, err)
110 }
111 err = client.WaitForInstance(TestInstanceId, Stopped, 0)
112 if err != nil {
113 t.Errorf("Instance %s is failed to stop: %v", TestInstanceId, err)
114 }
115 t.Logf("Instance %s is stopped successfully.", TestInstanceId)
116 err = client.StartInstance(TestInstanceId)
117 if err != nil {
118 t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
119 }
120 err = client.WaitForInstance(TestInstanceId, Running, 0)
121 if err != nil {
122 t.Errorf("Instance %s is failed to start: %v", TestInstanceId, err)
123 }
124 t.Logf("Instance %s is running successfully.", TestInstanceId)
125 err = client.RebootInstance(TestInstanceId, true)
126 if err != nil {
127 t.Errorf("Failed to restart instance %s: %v", TestInstanceId, err)
128 }
129 err = client.WaitForInstance(TestInstanceId, Running, 0)
130 if err != nil {
131 t.Errorf("Instance %s is failed to restart: %v", TestInstanceId, err)
132 }
133 t.Logf("Instance %s is running successfully.", TestInstanceId)
134 }
135
136 func TestECSInstanceCreationAndDeletion(t *testing.T) {
137
138 if TestIAmRich == false { // Avoid payment
139 return
140 }
141
142 client := NewTestClient()
143 instance, err := client.DescribeInstanceAttribute(TestInstanceId)
144 t.Logf("Instance: %++v %v", instance, err)
145
146 args := CreateInstanceArgs{
147 RegionId: instance.RegionId,
148 ImageId: instance.ImageId,
149 InstanceType: "ecs.t1.small",
150 SecurityGroupId: instance.SecurityGroupIds.SecurityGroupId[0],
151 }
152
153 instanceId, err := client.CreateInstance(&args)
154 if err != nil {
155 t.Errorf("Failed to create instance from Image %s: %v", args.ImageId, err)
156 }
157 t.Logf("Instance %s is created successfully.", instanceId)
158
159 instance, err = client.DescribeInstanceAttribute(instanceId)
160 t.Logf("Instance: %++v %v", instance, err)
161
162 err = client.WaitForInstance(instanceId, Stopped, 60)
163
164 err = client.StartInstance(instanceId)
165 if err != nil {
166 t.Errorf("Failed to start instance %s: %v", instanceId, err)
167 }
168 err = client.WaitForInstance(instanceId, Running, 0)
169
170 err = client.StopInstance(instanceId, true)
171 if err != nil {
172 t.Errorf("Failed to stop instance %s: %v", instanceId, err)
173 }
174 err = client.WaitForInstance(instanceId, Stopped, 0)
175 if err != nil {
176 t.Errorf("Instance %s is failed to stop: %v", instanceId, err)
177 }
178 t.Logf("Instance %s is stopped successfully.", instanceId)
179
180 err = client.DeleteInstance(instanceId)
181
182 if err != nil {
183 t.Errorf("Failed to delete instance %s: %v", instanceId, err)
184 }
185 t.Logf("Instance %s is deleted successfully.", instanceId)
186 }
187
188 func TestModifyInstanceAttribute(t *testing.T) {
189 client := NewTestClient()
190
191 args := ModifyInstanceAttributeArgs{
192 InstanceId: TestInstanceId,
193 Password: "Just$test",
194 }
195
196 err := client.ModifyInstanceAttribute(&args)
197 if err != nil {
198 t.Errorf("Failed to modify instance attribute %s: %v", TestInstanceId, err)
199 }
200
201 t.Logf("Modify instance attribute successfully")
202 }
203
204 func TestIoOptimized(t *testing.T) {
205
206 type TestStruct struct {
207 Str string
208 Flag StringOrBool
209 }
210
211 var test TestStruct
212
213 txt := "{\"Str\":\"abc\", \"Flag\": true}"
214
215 err := json.Unmarshal([]byte(txt), &test)
216 if err != nil {
217 t.Errorf("Failed to Unmarshal IoOptimized: %v", err)
218 } else {
219 if test.Flag.Value != true {
220 t.Errorf("Failed to Unmarshal IoOptimized with expected value: %s", test.Flag)
221 }
222 }
223
224 txt1 := "{\"Str\":\"abc\", \"Flag\": \"false\"}"
225
226 err = json.Unmarshal([]byte(txt1), &test)
227 if err != nil {
228 t.Errorf("Failed to Unmarshal IoOptimized: %v", err)
229 } else {
230 if test.Flag.Value != false {
231 t.Errorf("Failed to Unmarshal IoOptimized with expected value: %s", test.Flag)
232 }
233 }
234 }
235
236 func TestJoinSecurityGroup(t *testing.T) {
237 client := NewTestClient()
238
239 err := client.JoinSecurityGroup(TestInstanceId, TestSecurityGroupId)
240 if err != nil {
241 t.Errorf("Failed to joinSecurityGroup: %v", err)
242 }
243 }
244
245 func TestLeaveSecurityGroup(t *testing.T) {
246 client := NewTestClient()
247
248 err := client.LeaveSecurityGroup(TestInstanceId, TestSecurityGroupId)
249 if err != nil {
250 t.Errorf("Failed to LeaveSecurityGroup: %v", err)
251 }
252 }
253
254 func TestLocationECSClient(t *testing.T) {
255 client := NetTestLocationClientForDebug()
256
257 //CreateInstance
258 args := CreateInstanceArgs{
259 RegionId: TestRegionID,
260 ImageId: TestImageId,
261 InstanceType: TestInstanceType,
262 SecurityGroupId: TestSecurityGroupId,
263 VSwitchId: TestVSwitchID,
264 InstanceChargeType: common.PrePaid,
265 Period: 1,
266 }
267
268 instanceId, err := client.CreateInstance(&args)
269 if err != nil {
270 t.Fatalf("Failed to create instance from Image %s: %++v", TestImageId, err)
271 }
272 t.Logf("Instance %s is created successfully.", instanceId)
273
274 //DescribeInstance Attribute
275 attr, err := client.DescribeInstanceAttribute(instanceId)
276 if err != nil {
277 t.Fatalf("Failed to DescribeInstanceAttribute %++v", err)
278 }
279
280 t.Logf("InstanceAttribute is %++v", attr)
281
282 //DeleteInstance
283 err = client.DeleteInstance(instanceId)
284 if err != nil {
285 t.Fatalf("Failed to delete instance %++v", err)
286 }
287
288 t.Logf("Instance %s is deleted successfully", instanceId)
289 }
290
291 func TestAttachInstanceRamRole(t *testing.T) {
292 client := NewTestClient()
293
294 //AttachInstanceRamRole
295 InstanceIds := []string{"i-6wegya1zr8ysx8adyrt3", "i-6wee4x5wzct8x8pr6sar", "i-6we3c5f1nqem3t9bxgot"}
296 b, _ := json.Marshal(InstanceIds)
297 args := &AttachInstancesArgs{
298 RegionId: TestRegionID,
299 InstanceIds: string(b),
300 RamRoleName: "roletest",
301 }
302
303 // AttachInstanceRamRole
304 err := client.AttachInstanceRamRole(args)
305 if err != nil {
306 t.Fatalf("Failed to attach instances to ram role %s: %++v", args.RamRoleName, err)
307 }
308 t.Logf("Attach successfully.")
309
310 // DescribeInstanceRamRole
311 resp, err := client.DescribeInstanceRamRole(&AttachInstancesArgs{RegionId: TestRegionID, InstanceIds: args.InstanceIds})
312 if err != nil {
313 t.Fatalf("Failed to DescribeInstanceRamRole %++v", err)
314 }
315
316 t.Logf("Attach is %++v", resp.InstanceRamRoleSets.InstanceRamRoleSet)
317
318 // DetachInstanceRamRole
319 err = client.DetachInstanceRamRole(args)
320 if err != nil {
321 t.Fatalf("Failed to DetachInstanceRamRole %++v", err)
322 }
323
324 t.Logf("Detach successfully")
325 }
326
327 func TestClient_DescribeInstances(t *testing.T) {
328 client := NetTestLocationClientForDebug()
329 client.SetSecurityToken(TestSecurityToken)
330
331 args := &DescribeInstancesArgs{
332 RegionId: TestRegionID,
333 Pagination: common.Pagination{
334 PageNumber: 1,
335 PageSize: 100,
336 },
337 //SecurityToken: TestSecurityToken,
338 }
339
340 response, _, err := client.DescribeInstances(args)
341 if err != nil {
342 t.Fatalf("Error %++v", err)
343 } else {
344 for index, instance := range response {
345 t.Logf("response[%d] = %++v", index, instance)
346 }
347 }
348 }