snapshots_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package ecs
  2. import (
  3. "testing"
  4. )
  5. func TestSnapshot(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 := DescribeSnapshotsArgs{}
  12. args.InstanceId = TestInstanceId
  13. args.RegionId = instance.RegionId
  14. snapshots, _, err := client.DescribeSnapshots(&args)
  15. if err != nil {
  16. t.Errorf("Failed to DescribeSnapshots for instance %s: %v", TestInstanceId, err)
  17. }
  18. for _, snapshot := range snapshots {
  19. t.Logf("Snapshot of instance %s: %++v", TestInstanceId, snapshot)
  20. }
  21. }
  22. func TestSnapshotCreationAndDeletion(t *testing.T) {
  23. if TestQuick {
  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. //Describe disk monitor data
  32. diskArgs := DescribeDisksArgs{
  33. InstanceId: TestInstanceId,
  34. RegionId: instance.RegionId,
  35. }
  36. disks, _, err := client.DescribeDisks(&diskArgs)
  37. if err != nil {
  38. t.Fatalf("Failed to DescribeDisks for instance %s: %v", TestInstanceId, err)
  39. }
  40. diskId := disks[0].DiskId
  41. args := CreateSnapshotArgs{
  42. DiskId: diskId,
  43. SnapshotName: "My_Test_Snapshot",
  44. Description: "My Test Snapshot Description",
  45. ClientToken: client.GenerateClientToken(),
  46. }
  47. snapshotId, err := client.CreateSnapshot(&args)
  48. if err != nil {
  49. t.Errorf("Failed to CreateSnapshot for disk %s: %v", diskId, err)
  50. }
  51. client.WaitForSnapShotReady(instance.RegionId, snapshotId, 0)
  52. err = client.DeleteSnapshot(snapshotId)
  53. if err != nil {
  54. t.Errorf("Failed to DeleteSnapshot for disk %s: %v", diskId, err)
  55. }
  56. t.Logf("Snapshot %s is deleted successfully.", snapshotId)
  57. }