blob: 001dc9faa47b4b198f4ddfe29a148d11016db66e [file] [log] [blame]
Joe Topjiandee32222015-02-09 23:56:26 +00001// +build acceptance compute servers
2
3package v2
4
5import (
Joe Topjiandee32222015-02-09 23:56:26 +00006 "testing"
7
Joe Topjian1c15e3f2016-08-08 10:48:38 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian48209e32016-07-25 16:31:06 +00009 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Joe Topjiandee32222015-02-09 23:56:26 +000011)
12
Joe Topjian48209e32016-07-25 16:31:06 +000013func TestFloatingIPsList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060014 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000015 if err != nil {
16 t.Fatalf("Unable to create a compute client: %v", err)
17 }
18
19 allPages, err := floatingips.List(client).AllPages()
20 if err != nil {
21 t.Fatalf("Unable to retrieve floating IPs: %v", err)
22 }
23
24 allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages)
25 if err != nil {
26 t.Fatalf("Unable to extract floating IPs: %v", err)
27 }
28
29 for _, floatingIP := range allFloatingIPs {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060030 PrintFloatingIP(t, &floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000031 }
32}
33
34func TestFloatingIPsCreate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060035 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000036 if err != nil {
37 t.Fatalf("Unable to create a compute client: %v", err)
38 }
39
Joe Topjian1c15e3f2016-08-08 10:48:38 -060040 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian48209e32016-07-25 16:31:06 +000041 if err != nil {
42 t.Fatal(err)
43 }
44
Joe Topjian1c15e3f2016-08-08 10:48:38 -060045 floatingIP, err := CreateFloatingIP(t, client, choices)
Joe Topjian48209e32016-07-25 16:31:06 +000046 if err != nil {
47 t.Fatalf("Unable to create floating IP: %v", err)
48 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060049 defer DeleteFloatingIP(t, client, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000050
Joe Topjian1c15e3f2016-08-08 10:48:38 -060051 PrintFloatingIP(t, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000052}
53
54func TestFloatingIPsAssociate(t *testing.T) {
Joe Topjiandee32222015-02-09 23:56:26 +000055 if testing.Short() {
56 t.Skip("Skipping test that requires server creation in short mode.")
57 }
58
Joe Topjian1c15e3f2016-08-08 10:48:38 -060059 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000060 if err != nil {
61 t.Fatalf("Unable to create a compute client: %v", err)
62 }
Joe Topjiandee32222015-02-09 23:56:26 +000063
Joe Topjian1c15e3f2016-08-08 10:48:38 -060064 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian48209e32016-07-25 16:31:06 +000065 if err != nil {
66 t.Fatal(err)
67 }
Joe Topjiandee32222015-02-09 23:56:26 +000068
Joe Topjian1c15e3f2016-08-08 10:48:38 -060069 server, err := CreateServer(t, client, choices)
Joe Topjiandee32222015-02-09 23:56:26 +000070 if err != nil {
71 t.Fatalf("Unable to create server: %v", err)
72 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060073 defer DeleteServer(t, client, server)
Joe Topjiandee32222015-02-09 23:56:26 +000074
Joe Topjian1c15e3f2016-08-08 10:48:38 -060075 floatingIP, err := CreateFloatingIP(t, client, choices)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000076 if err != nil {
Joe Topjian48209e32016-07-25 16:31:06 +000077 t.Fatalf("Unable to create floating IP: %v", err)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000078 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060079 defer DeleteFloatingIP(t, client, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000080
Joe Topjian1c15e3f2016-08-08 10:48:38 -060081 PrintFloatingIP(t, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000082
Joe Topjian1c15e3f2016-08-08 10:48:38 -060083 err = AssociateFloatingIP(t, client, floatingIP, server)
Joe Topjian48209e32016-07-25 16:31:06 +000084 if err != nil {
85 t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err)
86 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060087 defer DisassociateFloatingIP(t, client, floatingIP, server)
Joe Topjian48209e32016-07-25 16:31:06 +000088
89 newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
90 if err != nil {
91 t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
92 }
93
Joe Topjian1c15e3f2016-08-08 10:48:38 -060094 t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP)
95
96 PrintFloatingIP(t, newFloatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000097}
98
99func TestFloatingIPsFixedIPAssociate(t *testing.T) {
100 if testing.Short() {
101 t.Skip("Skipping test that requires server creation in short mode.")
102 }
103
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600104 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +0000105 if err != nil {
106 t.Fatalf("Unable to create a compute client: %v", err)
107 }
108
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600109 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian48209e32016-07-25 16:31:06 +0000110 if err != nil {
111 t.Fatal(err)
112 }
113
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600114 server, err := CreateServer(t, client, choices)
Joe Topjian48209e32016-07-25 16:31:06 +0000115 if err != nil {
116 t.Fatalf("Unable to create server: %v", err)
117 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600118 defer DeleteServer(t, client, server)
Joe Topjian48209e32016-07-25 16:31:06 +0000119
120 newServer, err := servers.Get(client, server.ID).Extract()
121 if err != nil {
122 t.Fatalf("Unable to get server %s: %v", server.ID, err)
123 }
124
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600125 floatingIP, err := CreateFloatingIP(t, client, choices)
Joe Topjian48209e32016-07-25 16:31:06 +0000126 if err != nil {
127 t.Fatalf("Unable to create floating IP: %v", err)
128 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600129 defer DeleteFloatingIP(t, client, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +0000130
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600131 PrintFloatingIP(t, floatingIP)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000132
133 var fixedIP string
Joe Topjian48209e32016-07-25 16:31:06 +0000134 for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) {
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000135 address := networkAddresses.(map[string]interface{})
136 if address["OS-EXT-IPS:type"] == "fixed" {
137 if address["version"].(float64) == 4 {
138 fixedIP = address["addr"].(string)
139 }
140 }
141 }
142
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600143 err = AssociateFloatingIPWithFixedIP(t, client, floatingIP, newServer, fixedIP)
Joe Topjian48209e32016-07-25 16:31:06 +0000144 if err != nil {
145 t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err)
146 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600147 defer DisassociateFloatingIP(t, client, floatingIP, newServer)
Joe Topjian48209e32016-07-25 16:31:06 +0000148
149 newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
150 if err != nil {
151 t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
152 }
153
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600154 t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000155
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600156 PrintFloatingIP(t, newFloatingIP)
Joe Topjiandee32222015-02-09 23:56:26 +0000157}