networks_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package ecs
  2. import (
  3. "testing"
  4. "github.com/denverdino/aliyungo/common"
  5. )
  6. func TestAllocatePublicIpAddress(t *testing.T) {
  7. client := NewTestClient()
  8. instance, err := client.DescribeInstanceAttribute(TestInstanceId)
  9. if err != nil {
  10. t.Fatalf("Failed to describe instance %s: %v", TestInstanceId, err)
  11. }
  12. t.Logf("Instance: %++v %v", instance, err)
  13. ipAddr, err := client.AllocatePublicIpAddress(TestInstanceId)
  14. if err != nil {
  15. t.Fatalf("Failed to allocate public IP address for instance %s: %v", TestInstanceId, err)
  16. }
  17. t.Logf("Public IP address of instance %s: %s", TestInstanceId, ipAddr)
  18. }
  19. func testEipAddress(t *testing.T, client *Client, regionId common.Region, instanceId string) error {
  20. args := AllocateEipAddressArgs{
  21. RegionId: regionId,
  22. Bandwidth: 5,
  23. InternetChargeType: common.PayByTraffic,
  24. ClientToken: client.GenerateClientToken(),
  25. }
  26. ipAddr, allocationId, err := client.AllocateEipAddress(&args)
  27. if err != nil {
  28. t.Errorf("Failed to allocate EIP address: %v", err)
  29. return err
  30. }
  31. t.Logf("EIP address: %s, AllocationId: %s", ipAddr, allocationId)
  32. err = client.WaitForEip(regionId, allocationId, EipStatusAvailable, 0)
  33. if err != nil {
  34. t.Errorf("Failed to wait EIP %s: %v", allocationId, err)
  35. }
  36. err = client.AssociateEipAddress(allocationId, instanceId)
  37. if err != nil {
  38. t.Errorf("Failed to associate EIP address: %v", err)
  39. }
  40. err = client.WaitForEip(regionId, allocationId, EipStatusInUse, 0)
  41. if err != nil {
  42. t.Errorf("Failed to wait EIP %s: %v", allocationId, err)
  43. }
  44. err = client.UnassociateEipAddress(allocationId, instanceId)
  45. if err != nil {
  46. t.Errorf("Failed to unassociate EIP address: %v", err)
  47. }
  48. err = client.WaitForEip(regionId, allocationId, EipStatusAvailable, 0)
  49. if err != nil {
  50. t.Errorf("Failed to wait EIP %s: %v", allocationId, err)
  51. }
  52. err = client.ReleaseEipAddress(allocationId)
  53. if err != nil {
  54. t.Errorf("Failed to release EIP address: %v", err)
  55. }
  56. return err
  57. }