Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 1 | // +build acceptance compute servers |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "testing" |
| 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud" |
| 10 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 11 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingip" |
| 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
| 13 | th "github.com/gophercloud/gophercloud/testhelper" |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | func createFIPServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) { |
| 17 | if testing.Short() { |
| 18 | t.Skip("Skipping test that requires server creation in short mode.") |
| 19 | } |
| 20 | |
| 21 | name := tools.RandomString("ACPTTEST", 16) |
| 22 | t.Logf("Attempting to create server: %s\n", name) |
| 23 | |
| 24 | pwd := tools.MakeNewPassword("") |
| 25 | |
| 26 | server, err := servers.Create(client, servers.CreateOpts{ |
| 27 | Name: name, |
| 28 | FlavorRef: choices.FlavorID, |
| 29 | ImageRef: choices.ImageID, |
| 30 | AdminPass: pwd, |
| 31 | }).Extract() |
| 32 | if err != nil { |
| 33 | t.Fatalf("Unable to create server: %v", err) |
| 34 | } |
| 35 | |
| 36 | th.AssertEquals(t, pwd, server.AdminPass) |
| 37 | |
| 38 | return server, err |
| 39 | } |
| 40 | |
| 41 | func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient) (*floatingip.FloatingIP, error) { |
| 42 | pool := os.Getenv("OS_POOL_NAME") |
| 43 | fip, err := floatingip.Create(client, &floatingip.CreateOpts{ |
| 44 | Pool: pool, |
| 45 | }).Extract() |
| 46 | th.AssertNoErr(t, err) |
| 47 | t.Logf("Obtained Floating IP: %v", fip.IP) |
| 48 | |
| 49 | return fip, err |
| 50 | } |
| 51 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 52 | func associateFloatingIPDeprecated(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) { |
| 53 | // This form works, but is considered deprecated. |
| 54 | // See associateFloatingIP or associateFloatingIPFixed |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 55 | err := floatingip.Associate(client, serverId, fip.IP).ExtractErr() |
| 56 | th.AssertNoErr(t, err) |
| 57 | t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId) |
| 58 | defer func() { |
| 59 | err = floatingip.Disassociate(client, serverId, fip.IP).ExtractErr() |
| 60 | th.AssertNoErr(t, err) |
| 61 | t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId) |
| 62 | }() |
| 63 | floatingIp, err := floatingip.Get(client, fip.ID).Extract() |
| 64 | th.AssertNoErr(t, err) |
| 65 | t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP) |
| 66 | } |
| 67 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 68 | func associateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) { |
| 69 | associateOpts := floatingip.AssociateOpts{ |
| 70 | ServerID: serverId, |
| 71 | FloatingIP: fip.IP, |
| 72 | } |
| 73 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 74 | err := floatingip.AssociateInstance(client, associateOpts).ExtractErr() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 75 | th.AssertNoErr(t, err) |
| 76 | t.Logf("Associated floating IP %v from instance %v", fip.IP, serverId) |
| 77 | defer func() { |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 78 | err = floatingip.DisassociateInstance(client, associateOpts).ExtractErr() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 79 | th.AssertNoErr(t, err) |
| 80 | t.Logf("Disassociated floating IP %v from instance %v", fip.IP, serverId) |
| 81 | }() |
| 82 | floatingIp, err := floatingip.Get(client, fip.ID).Extract() |
| 83 | th.AssertNoErr(t, err) |
| 84 | t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP) |
| 85 | } |
| 86 | |
| 87 | func associateFloatingIPFixed(t *testing.T, client *gophercloud.ServiceClient, serverId string, fip *floatingip.FloatingIP) { |
| 88 | |
| 89 | network := os.Getenv("OS_NETWORK_NAME") |
| 90 | server, err := servers.Get(client, serverId).Extract() |
| 91 | if err != nil { |
| 92 | t.Fatalf("%s", err) |
| 93 | } |
| 94 | |
| 95 | var fixedIP string |
| 96 | for _, networkAddresses := range server.Addresses[network].([]interface{}) { |
| 97 | address := networkAddresses.(map[string]interface{}) |
| 98 | if address["OS-EXT-IPS:type"] == "fixed" { |
| 99 | if address["version"].(float64) == 4 { |
| 100 | fixedIP = address["addr"].(string) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | associateOpts := floatingip.AssociateOpts{ |
| 106 | ServerID: serverId, |
| 107 | FloatingIP: fip.IP, |
| 108 | FixedIP: fixedIP, |
| 109 | } |
| 110 | |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 111 | err = floatingip.AssociateInstance(client, associateOpts).ExtractErr() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 112 | th.AssertNoErr(t, err) |
| 113 | t.Logf("Associated floating IP %v from instance %v with Fixed IP %v", fip.IP, serverId, fixedIP) |
| 114 | defer func() { |
Joe Topjian | 94e4cc5 | 2016-01-05 17:01:18 +0000 | [diff] [blame] | 115 | err = floatingip.DisassociateInstance(client, associateOpts).ExtractErr() |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 116 | th.AssertNoErr(t, err) |
| 117 | t.Logf("Disassociated floating IP %v from instance %v with Fixed IP %v", fip.IP, serverId, fixedIP) |
| 118 | }() |
| 119 | floatingIp, err := floatingip.Get(client, fip.ID).Extract() |
| 120 | th.AssertNoErr(t, err) |
| 121 | th.AssertEquals(t, floatingIp.FixedIP, fixedIP) |
| 122 | t.Logf("Floating IP %v is associated with Fixed IP %v", fip.IP, floatingIp.FixedIP) |
| 123 | } |
| 124 | |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 125 | func TestFloatingIP(t *testing.T) { |
| 126 | pool := os.Getenv("OS_POOL_NAME") |
| 127 | if pool == "" { |
| 128 | t.Fatalf("OS_POOL_NAME must be set") |
| 129 | } |
| 130 | |
| 131 | choices, err := ComputeChoicesFromEnv() |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | |
| 136 | client, err := newClient() |
| 137 | if err != nil { |
| 138 | t.Fatalf("Unable to create a compute client: %v", err) |
| 139 | } |
| 140 | |
| 141 | server, err := createFIPServer(t, client, choices) |
| 142 | if err != nil { |
| 143 | t.Fatalf("Unable to create server: %v", err) |
| 144 | } |
| 145 | defer func() { |
| 146 | servers.Delete(client, server.ID) |
| 147 | t.Logf("Server deleted.") |
| 148 | }() |
| 149 | |
| 150 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 151 | t.Fatalf("Unable to wait for server: %v", err) |
| 152 | } |
| 153 | |
| 154 | fip, err := createFloatingIP(t, client) |
| 155 | if err != nil { |
| 156 | t.Fatalf("Unable to create floating IP: %v", err) |
| 157 | } |
| 158 | defer func() { |
| 159 | err = floatingip.Delete(client, fip.ID).ExtractErr() |
| 160 | th.AssertNoErr(t, err) |
| 161 | t.Logf("Floating IP deleted.") |
| 162 | }() |
| 163 | |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 164 | associateFloatingIPDeprecated(t, client, server.ID, fip) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 165 | associateFloatingIP(t, client, server.ID, fip) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 166 | associateFloatingIPFixed(t, client, server.ID, fip) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 167 | |
| 168 | } |