instances_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package ecs
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "github.com/denverdino/aliyungo/common"
  7. )
  8. func ExampleClient_DescribeInstanceStatus() {
  9. fmt.Printf("DescribeInstanceStatus Example\n")
  10. args := DescribeInstanceStatusArgs{
  11. RegionId: "cn-beijing",
  12. ZoneId: "cn-beijing-b",
  13. Pagination: common.Pagination{
  14. PageNumber: 1,
  15. PageSize: 1,
  16. },
  17. }
  18. client := NewTestClient()
  19. instanceStatus, _, err := client.DescribeInstanceStatus(&args)
  20. if err != nil {
  21. fmt.Printf("Failed to describe Instance: %s status:%v \n", TestInstanceId, err)
  22. } else {
  23. for i := 0; i < len(instanceStatus); i++ {
  24. fmt.Printf("Instance %s Status: %s \n", instanceStatus[i].InstanceId, instanceStatus[i].Status)
  25. }
  26. }
  27. }
  28. func ExampleClient_DescribeInstanceAttribute() {
  29. fmt.Printf("DescribeInstanceAttribute Example\n")
  30. client := NewTestClient()
  31. instanceAttributeType, err := client.DescribeInstanceAttribute(TestInstanceId)
  32. if err != nil {
  33. fmt.Printf("Failed to describe Instance %s attribute: %v\n", TestInstanceId, err)
  34. } else {
  35. fmt.Printf("Instance Information\n")
  36. fmt.Printf("InstanceId = %s \n", instanceAttributeType.InstanceId)
  37. fmt.Printf("InstanceName = %s \n", instanceAttributeType.InstanceName)
  38. fmt.Printf("HostName = %s \n", instanceAttributeType.HostName)
  39. fmt.Printf("ZoneId = %s \n", instanceAttributeType.ZoneId)
  40. fmt.Printf("RegionId = %s \n", instanceAttributeType.RegionId)
  41. }
  42. }
  43. func ExampleClient_DescribeInstanceVncUrl() {
  44. fmt.Printf("DescribeInstanceVncUrl Example\n")
  45. args := DescribeInstanceVncUrlArgs{
  46. RegionId: "cn-beijing",
  47. InstanceId: TestInstanceId,
  48. }
  49. client := NewTestClient()
  50. instanceVncUrl, err := client.DescribeInstanceVncUrl(&args)
  51. if err != nil {
  52. fmt.Printf("Failed to describe Instance %s vnc url: %v \n", TestInstanceId, err)
  53. } else {
  54. fmt.Printf("VNC URL = %s \n", instanceVncUrl)
  55. }
  56. }
  57. func ExampleClient_StopInstance() {
  58. fmt.Printf("Stop Instance Example\n")
  59. client := NewTestClient()
  60. err := client.StopInstance(TestInstanceId, true)
  61. if err != nil {
  62. fmt.Printf("Failed to stop Instance %s vnc url: %v \n", TestInstanceId, err)
  63. }
  64. }
  65. func ExampleClient_DeleteInstance() {
  66. fmt.Printf("Delete Instance Example")
  67. client := NewTestClient()
  68. err := client.DeleteInstance(TestInstanceId)
  69. if err != nil {
  70. fmt.Printf("Failed to delete Instance %s vnc url: %v \n", TestInstanceId, err)
  71. }
  72. }
  73. func TestECSInstance(t *testing.T) {
  74. if TestQuick {
  75. return
  76. }
  77. client := NewTestClient()
  78. instance, err := client.DescribeInstanceAttribute(TestInstanceId)
  79. if err != nil {
  80. t.Fatalf("Failed to describe instance %s: %v", TestInstanceId, err)
  81. }
  82. t.Logf("Instance: %++v %v", instance, err)
  83. err = client.StopInstance(TestInstanceId, true)
  84. if err != nil {
  85. t.Errorf("Failed to stop instance %s: %v", TestInstanceId, err)
  86. }
  87. err = client.WaitForInstance(TestInstanceId, Stopped, 0)
  88. if err != nil {
  89. t.Errorf("Instance %s is failed to stop: %v", TestInstanceId, err)
  90. }
  91. t.Logf("Instance %s is stopped successfully.", TestInstanceId)
  92. err = client.StartInstance(TestInstanceId)
  93. if err != nil {
  94. t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
  95. }
  96. err = client.WaitForInstance(TestInstanceId, Running, 0)
  97. if err != nil {
  98. t.Errorf("Instance %s is failed to start: %v", TestInstanceId, err)
  99. }
  100. t.Logf("Instance %s is running successfully.", TestInstanceId)
  101. err = client.RebootInstance(TestInstanceId, true)
  102. if err != nil {
  103. t.Errorf("Failed to restart instance %s: %v", TestInstanceId, err)
  104. }
  105. err = client.WaitForInstance(TestInstanceId, Running, 0)
  106. if err != nil {
  107. t.Errorf("Instance %s is failed to restart: %v", TestInstanceId, err)
  108. }
  109. t.Logf("Instance %s is running successfully.", TestInstanceId)
  110. }
  111. func TestECSInstanceCreationAndDeletion(t *testing.T) {
  112. if TestIAmRich == false { // Avoid payment
  113. return
  114. }
  115. client := NewTestClient()
  116. instance, err := client.DescribeInstanceAttribute(TestInstanceId)
  117. t.Logf("Instance: %++v %v", instance, err)
  118. args := CreateInstanceArgs{
  119. RegionId: instance.RegionId,
  120. ImageId: instance.ImageId,
  121. InstanceType: "ecs.t1.small",
  122. SecurityGroupId: instance.SecurityGroupIds.SecurityGroupId[0],
  123. }
  124. instanceId, err := client.CreateInstance(&args)
  125. if err != nil {
  126. t.Errorf("Failed to create instance from Image %s: %v", args.ImageId, err)
  127. }
  128. t.Logf("Instance %s is created successfully.", instanceId)
  129. instance, err = client.DescribeInstanceAttribute(instanceId)
  130. t.Logf("Instance: %++v %v", instance, err)
  131. err = client.WaitForInstance(instanceId, Stopped, 60)
  132. err = client.StartInstance(instanceId)
  133. if err != nil {
  134. t.Errorf("Failed to start instance %s: %v", instanceId, err)
  135. }
  136. err = client.WaitForInstance(instanceId, Running, 0)
  137. err = client.StopInstance(instanceId, true)
  138. if err != nil {
  139. t.Errorf("Failed to stop instance %s: %v", instanceId, err)
  140. }
  141. err = client.WaitForInstance(instanceId, Stopped, 0)
  142. if err != nil {
  143. t.Errorf("Instance %s is failed to stop: %v", instanceId, err)
  144. }
  145. t.Logf("Instance %s is stopped successfully.", instanceId)
  146. err = client.DeleteInstance(instanceId)
  147. if err != nil {
  148. t.Errorf("Failed to delete instance %s: %v", instanceId, err)
  149. }
  150. t.Logf("Instance %s is deleted successfully.", instanceId)
  151. }
  152. func TestModifyInstanceAttribute(t *testing.T) {
  153. client := NewTestClient()
  154. args := ModifyInstanceAttributeArgs{
  155. InstanceId: TestInstanceId,
  156. Password: "Just$test",
  157. }
  158. err := client.ModifyInstanceAttribute(&args)
  159. if err != nil {
  160. t.Errorf("Failed to modify instance attribute %s: %v", TestInstanceId, err)
  161. }
  162. t.Logf("Modify instance attribute successfully")
  163. }
  164. func TestIoOptimized(t *testing.T) {
  165. type TestStruct struct {
  166. Str string
  167. Flag StringOrBool
  168. }
  169. var test TestStruct
  170. txt := "{\"Str\":\"abc\", \"Flag\": true}"
  171. err := json.Unmarshal([]byte(txt), &test)
  172. if err != nil {
  173. t.Errorf("Failed to Unmarshal IoOptimized: %v", err)
  174. } else {
  175. if test.Flag.Value != true {
  176. t.Errorf("Failed to Unmarshal IoOptimized with expected value: %s", test.Flag)
  177. }
  178. }
  179. txt1 := "{\"Str\":\"abc\", \"Flag\": \"false\"}"
  180. err = json.Unmarshal([]byte(txt1), &test)
  181. if err != nil {
  182. t.Errorf("Failed to Unmarshal IoOptimized: %v", err)
  183. } else {
  184. if test.Flag.Value != false {
  185. t.Errorf("Failed to Unmarshal IoOptimized with expected value: %s", test.Flag)
  186. }
  187. }
  188. }
  189. func TestJoinSecurityGroup(t *testing.T) {
  190. client := NewTestClient()
  191. err := client.JoinSecurityGroup(TestInstanceId, TestSecurityGroupId)
  192. if err != nil {
  193. t.Errorf("Failed to joinSecurityGroup: %v", err)
  194. }
  195. }
  196. func TestLeaveSecurityGroup(t *testing.T) {
  197. client := NewTestClient()
  198. err := client.LeaveSecurityGroup(TestInstanceId, TestSecurityGroupId)
  199. if err != nil {
  200. t.Errorf("Failed to LeaveSecurityGroup: %v", err)
  201. }
  202. }