disks_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package ecs
  2. import (
  3. "testing"
  4. )
  5. func TestDisks(t *testing.T) {
  6. client := NewTestClient()
  7. instance, err := client.DescribeInstanceAttribute(TestInstanceId)
  8. if err != nil {
  9. t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
  10. }
  11. args := DescribeDisksArgs{}
  12. args.InstanceId = TestInstanceId
  13. args.RegionId = instance.RegionId
  14. disks, _, err := client.DescribeDisks(&args)
  15. if err != nil {
  16. t.Fatalf("Failed to DescribeDisks for instance %s: %v", TestInstanceId, err)
  17. }
  18. for _, disk := range disks {
  19. t.Logf("Disk of instance %s: %++v", TestInstanceId, disk)
  20. }
  21. }
  22. func TestDiskCreationAndDeletion(t *testing.T) {
  23. if TestIAmRich == false { //Avoid payment
  24. return
  25. }
  26. client := NewTestClient()
  27. instance, err := client.DescribeInstanceAttribute(TestInstanceId)
  28. if err != nil {
  29. t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
  30. }
  31. args := CreateDiskArgs{
  32. RegionId: instance.RegionId,
  33. ZoneId: instance.ZoneId,
  34. DiskName: "test-disk",
  35. Size: 5,
  36. }
  37. diskId, err := client.CreateDisk(&args)
  38. if err != nil {
  39. t.Fatalf("Failed to create disk: %v", err)
  40. }
  41. t.Logf("Create disk %s successfully", diskId)
  42. attachArgs := AttachDiskArgs{
  43. InstanceId: instance.InstanceId,
  44. DiskId: diskId,
  45. }
  46. err = client.AttachDisk(&attachArgs)
  47. if err != nil {
  48. t.Errorf("Failed to create disk: %v", err)
  49. } else {
  50. t.Logf("Attach disk %s to instance %s successfully", diskId, instance.InstanceId)
  51. instance, err = client.DescribeInstanceAttribute(TestInstanceId)
  52. if err != nil {
  53. t.Errorf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
  54. } else {
  55. t.Logf("Instance: %++v %v", instance, err)
  56. }
  57. err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusInUse, 0)
  58. if err != nil {
  59. t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusInUse, err)
  60. }
  61. err = client.DetachDisk(instance.InstanceId, diskId)
  62. if err != nil {
  63. t.Errorf("Failed to detach disk: %v", err)
  64. } else {
  65. t.Logf("Detach disk %s to instance %s successfully", diskId, instance.InstanceId)
  66. }
  67. err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusAvailable, 0)
  68. if err != nil {
  69. t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusAvailable, err)
  70. }
  71. }
  72. err = client.DeleteDisk(diskId)
  73. if err != nil {
  74. t.Fatalf("Failed to delete disk %s: %v", diskId, err)
  75. }
  76. t.Logf("Delete disk %s successfully", diskId)
  77. }
  78. func TestReplaceSystemDiskUsingSizeParam(t *testing.T) {
  79. client := NewTestClientForDebug()
  80. args := ReplaceSystemDiskArgs{
  81. InstanceId: TestInstanceId,
  82. ImageId: TestImageId,
  83. SystemDisk: SystemDiskType{
  84. Size: 192,
  85. },
  86. ClientToken: client.GenerateClientToken(),
  87. }
  88. diskId, err := client.ReplaceSystemDisk(&args)
  89. if err != nil {
  90. t.Errorf("Failed to replace system disk %v", err)
  91. } else {
  92. t.Logf("diskId is %s", diskId)
  93. }
  94. }
  95. func TestReplaceSystemDisk(t *testing.T) {
  96. client := NewTestClient()
  97. err := client.WaitForInstance(TestInstanceId, Running, 0)
  98. err = client.StopInstance(TestInstanceId, true)
  99. if err != nil {
  100. t.Errorf("Failed to stop instance %s: %v", TestInstanceId, err)
  101. }
  102. err = client.WaitForInstance(TestInstanceId, Stopped, 0)
  103. if err != nil {
  104. t.Errorf("Instance %s is failed to stop: %v", TestInstanceId, err)
  105. }
  106. t.Logf("Instance %s is stopped successfully.", TestInstanceId)
  107. args := ReplaceSystemDiskArgs{
  108. InstanceId: TestInstanceId,
  109. ImageId: TestImageId,
  110. }
  111. diskId, err := client.ReplaceSystemDisk(&args)
  112. if err != nil {
  113. t.Errorf("Failed to replace system disk %v", err)
  114. }
  115. err = client.WaitForInstance(TestInstanceId, Stopped, 60)
  116. err = client.StartInstance(TestInstanceId)
  117. if err != nil {
  118. t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
  119. } else {
  120. err = client.WaitForInstance(TestInstanceId, Running, 0)
  121. if err != nil {
  122. t.Errorf("Failed to wait for instance %s running: %v", TestInstanceId, err)
  123. }
  124. }
  125. t.Logf("Replace system disk %s successfully ", diskId)
  126. }