123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package ecs
- import (
- "testing"
- )
- func TestDisks(t *testing.T) {
- client := NewTestClient()
- instance, err := client.DescribeInstanceAttribute(TestInstanceId)
- if err != nil {
- t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
- }
- args := DescribeDisksArgs{}
- args.InstanceId = TestInstanceId
- args.RegionId = instance.RegionId
- disks, _, err := client.DescribeDisks(&args)
- if err != nil {
- t.Fatalf("Failed to DescribeDisks for instance %s: %v", TestInstanceId, err)
- }
- for _, disk := range disks {
- t.Logf("Disk of instance %s: %++v", TestInstanceId, disk)
- }
- }
- func TestDiskCreationAndDeletion(t *testing.T) {
- if TestIAmRich == false { //Avoid payment
- return
- }
- client := NewTestClient()
- instance, err := client.DescribeInstanceAttribute(TestInstanceId)
- if err != nil {
- t.Fatalf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
- }
- args := CreateDiskArgs{
- RegionId: instance.RegionId,
- ZoneId: instance.ZoneId,
- DiskName: "test-disk",
- Size: 5,
- }
- diskId, err := client.CreateDisk(&args)
- if err != nil {
- t.Fatalf("Failed to create disk: %v", err)
- }
- t.Logf("Create disk %s successfully", diskId)
- attachArgs := AttachDiskArgs{
- InstanceId: instance.InstanceId,
- DiskId: diskId,
- }
- err = client.AttachDisk(&attachArgs)
- if err != nil {
- t.Errorf("Failed to create disk: %v", err)
- } else {
- t.Logf("Attach disk %s to instance %s successfully", diskId, instance.InstanceId)
- instance, err = client.DescribeInstanceAttribute(TestInstanceId)
- if err != nil {
- t.Errorf("Failed to DescribeInstanceAttribute for instance %s: %v", TestInstanceId, err)
- } else {
- t.Logf("Instance: %++v %v", instance, err)
- }
- err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusInUse, 0)
- if err != nil {
- t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusInUse, err)
- }
- err = client.DetachDisk(instance.InstanceId, diskId)
- if err != nil {
- t.Errorf("Failed to detach disk: %v", err)
- } else {
- t.Logf("Detach disk %s to instance %s successfully", diskId, instance.InstanceId)
- }
- err = client.WaitForDisk(instance.RegionId, diskId, DiskStatusAvailable, 0)
- if err != nil {
- t.Fatalf("Failed to wait for disk %s to status %s: %v", diskId, DiskStatusAvailable, err)
- }
- }
- err = client.DeleteDisk(diskId)
- if err != nil {
- t.Fatalf("Failed to delete disk %s: %v", diskId, err)
- }
- t.Logf("Delete disk %s successfully", diskId)
- }
- func TestReplaceSystemDiskUsingSizeParam(t *testing.T) {
- client := NewTestClientForDebug()
- args := ReplaceSystemDiskArgs{
- InstanceId: TestInstanceId,
- ImageId: TestImageId,
- SystemDisk: SystemDiskType{
- Size: 192,
- },
- ClientToken: client.GenerateClientToken(),
- }
- diskId, err := client.ReplaceSystemDisk(&args)
- if err != nil {
- t.Errorf("Failed to replace system disk %v", err)
- } else {
- t.Logf("diskId is %s", diskId)
- }
- }
- func TestReplaceSystemDisk(t *testing.T) {
- client := NewTestClient()
- err := client.WaitForInstance(TestInstanceId, Running, 0)
- err = client.StopInstance(TestInstanceId, true)
- if err != nil {
- t.Errorf("Failed to stop instance %s: %v", TestInstanceId, err)
- }
- err = client.WaitForInstance(TestInstanceId, Stopped, 0)
- if err != nil {
- t.Errorf("Instance %s is failed to stop: %v", TestInstanceId, err)
- }
- t.Logf("Instance %s is stopped successfully.", TestInstanceId)
- args := ReplaceSystemDiskArgs{
- InstanceId: TestInstanceId,
- ImageId: TestImageId,
- }
- diskId, err := client.ReplaceSystemDisk(&args)
- if err != nil {
- t.Errorf("Failed to replace system disk %v", err)
- }
- err = client.WaitForInstance(TestInstanceId, Stopped, 60)
- err = client.StartInstance(TestInstanceId)
- if err != nil {
- t.Errorf("Failed to start instance %s: %v", TestInstanceId, err)
- } else {
- err = client.WaitForInstance(TestInstanceId, Running, 0)
- if err != nil {
- t.Errorf("Failed to wait for instance %s running: %v", TestInstanceId, err)
- }
- }
- t.Logf("Replace system disk %s successfully ", diskId)
- }
|