Clean-up dependencies

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot
2015-11-12 19:16:27 +01:00
parent 7a4915e999
commit 89675067eb
612 changed files with 1869 additions and 151003 deletions

View File

@@ -1,182 +0,0 @@
package apiversions
import (
"fmt"
"net/http"
"testing"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
func TestListVersions(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"versions": [
{
"status": "CURRENT",
"id": "v2.0",
"links": [
{
"href": "http://23.253.228.211:9696/v2.0",
"rel": "self"
}
]
}
]
}`)
})
count := 0
ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractAPIVersions(page)
if err != nil {
t.Errorf("Failed to extract API versions: %v", err)
return false, err
}
expected := []APIVersion{
APIVersion{
Status: "CURRENT",
ID: "v2.0",
},
}
th.AssertDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestNonJSONCannotBeExtractedIntoAPIVersions(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
ListVersions(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
if _, err := ExtractAPIVersions(page); err == nil {
t.Fatalf("Expected error, got nil")
}
return true, nil
})
}
func TestAPIInfo(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"resources": [
{
"links": [
{
"href": "http://23.253.228.211:9696/v2.0/subnets",
"rel": "self"
}
],
"name": "subnet",
"collection": "subnets"
},
{
"links": [
{
"href": "http://23.253.228.211:9696/v2.0/networks",
"rel": "self"
}
],
"name": "network",
"collection": "networks"
},
{
"links": [
{
"href": "http://23.253.228.211:9696/v2.0/ports",
"rel": "self"
}
],
"name": "port",
"collection": "ports"
}
]
}
`)
})
count := 0
ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractVersionResources(page)
if err != nil {
t.Errorf("Failed to extract version resources: %v", err)
return false, err
}
expected := []APIVersionResource{
APIVersionResource{
Name: "subnet",
Collection: "subnets",
},
APIVersionResource{
Name: "network",
Collection: "networks",
},
APIVersionResource{
Name: "port",
Collection: "ports",
},
}
th.AssertDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestNonJSONCannotBeExtractedIntoAPIVersionResources(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
ListVersionResources(fake.ServiceClient(), "v2.0").EachPage(func(page pagination.Page) (bool, error) {
if _, err := ExtractVersionResources(page); err == nil {
t.Fatalf("Expected error, got nil")
}
return true, nil
})
}

View File

@@ -1,26 +0,0 @@
package apiversions
import (
"testing"
"github.com/rackspace/gophercloud"
th "github.com/rackspace/gophercloud/testhelper"
)
const endpoint = "http://localhost:57909/"
func endpointClient() *gophercloud.ServiceClient {
return &gophercloud.ServiceClient{Endpoint: endpoint}
}
func TestAPIVersionsURL(t *testing.T) {
actual := apiVersionsURL(endpointClient())
expected := endpoint
th.AssertEquals(t, expected, actual)
}
func TestAPIInfoURL(t *testing.T) {
actual := apiInfoURL(endpointClient(), "v2.0")
expected := endpoint + "v2.0/"
th.AssertEquals(t, expected, actual)
}

View File

@@ -1,105 +0,0 @@
package extensions
import (
"fmt"
"net/http"
"testing"
common "github.com/rackspace/gophercloud/openstack/common/extensions"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/extensions", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `
{
"extensions": [
{
"updated": "2013-01-20T00:00:00-00:00",
"name": "Neutron Service Type Management",
"links": [],
"namespace": "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
"alias": "service-type",
"description": "API for retrieving service providers for Neutron advanced services"
}
]
}
`)
})
count := 0
List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractExtensions(page)
if err != nil {
t.Errorf("Failed to extract extensions: %v", err)
}
expected := []Extension{
Extension{
common.Extension{
Updated: "2013-01-20T00:00:00-00:00",
Name: "Neutron Service Type Management",
Links: []interface{}{},
Namespace: "http://docs.openstack.org/ext/neutron/service-type/api/v1.0",
Alias: "service-type",
Description: "API for retrieving service providers for Neutron advanced services",
},
},
}
th.AssertDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/extensions/agent", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"extension": {
"updated": "2013-02-03T10:00:00-00:00",
"name": "agent",
"links": [],
"namespace": "http://docs.openstack.org/ext/agent/api/v2.0",
"alias": "agent",
"description": "The agent management extension."
}
}
`)
})
ext, err := Get(fake.ServiceClient(), "agent").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
th.AssertEquals(t, ext.Name, "agent")
th.AssertEquals(t, ext.Namespace, "http://docs.openstack.org/ext/agent/api/v2.0")
th.AssertEquals(t, ext.Alias, "agent")
th.AssertEquals(t, ext.Description, "The agent management extension.")
}

View File

@@ -1,254 +0,0 @@
package external
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"networks": [
{
"admin_state_up": true,
"id": "0f38d5ad-10a6-428f-a5fc-825cfe0f1970",
"name": "net1",
"router:external": false,
"shared": false,
"status": "ACTIVE",
"subnets": [
"25778974-48a8-46e7-8998-9dc8c70d2f06"
],
"tenant_id": "b575417a6c444a6eb5cc3a58eb4f714a"
},
{
"admin_state_up": true,
"id": "8d05a1b1-297a-46ca-8974-17debf51ca3c",
"name": "ext_net",
"router:external": true,
"shared": false,
"status": "ACTIVE",
"subnets": [
"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"
],
"tenant_id": "5eb8995cf717462c9df8d1edfa498010"
}
]
}
`)
})
count := 0
networks.List(fake.ServiceClient(), networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractList(page)
if err != nil {
t.Errorf("Failed to extract networks: %v", err)
return false, err
}
expected := []NetworkExternal{
NetworkExternal{
Status: "ACTIVE",
Subnets: []string{"25778974-48a8-46e7-8998-9dc8c70d2f06"},
Name: "net1",
AdminStateUp: true,
TenantID: "b575417a6c444a6eb5cc3a58eb4f714a",
Shared: false,
ID: "0f38d5ad-10a6-428f-a5fc-825cfe0f1970",
External: false,
},
NetworkExternal{
Status: "ACTIVE",
Subnets: []string{"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"},
Name: "ext_net",
AdminStateUp: true,
TenantID: "5eb8995cf717462c9df8d1edfa498010",
Shared: false,
ID: "8d05a1b1-297a-46ca-8974-17debf51ca3c",
External: true,
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"admin_state_up": true,
"id": "8d05a1b1-297a-46ca-8974-17debf51ca3c",
"name": "ext_net",
"router:external": true,
"shared": false,
"status": "ACTIVE",
"subnets": [
"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"
],
"tenant_id": "5eb8995cf717462c9df8d1edfa498010"
}
}
`)
})
res := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
n, err := ExtractGet(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, true, n.External)
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"admin_state_up": true,
"name": "ext_net",
"router:external": true
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"network": {
"admin_state_up": true,
"id": "8d05a1b1-297a-46ca-8974-17debf51ca3c",
"name": "ext_net",
"router:external": true,
"shared": false,
"status": "ACTIVE",
"subnets": [
"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"
],
"tenant_id": "5eb8995cf717462c9df8d1edfa498010"
}
}
`)
})
options := CreateOpts{networks.CreateOpts{Name: "ext_net", AdminStateUp: Up}, true}
res := networks.Create(fake.ServiceClient(), options)
n, err := ExtractCreate(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, true, n.External)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"router:external": true,
"name": "new_name"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"admin_state_up": true,
"id": "8d05a1b1-297a-46ca-8974-17debf51ca3c",
"name": "new_name",
"router:external": true,
"shared": false,
"status": "ACTIVE",
"subnets": [
"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"
],
"tenant_id": "5eb8995cf717462c9df8d1edfa498010"
}
}
`)
})
options := UpdateOpts{networks.UpdateOpts{Name: "new_name"}, true}
res := networks.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options)
n, err := ExtractUpdate(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, true, n.External)
}
func TestExtractFnsReturnsErrWhenResultContainsErr(t *testing.T) {
gr := networks.GetResult{}
gr.Err = errors.New("")
if _, err := ExtractGet(gr); err == nil {
t.Fatalf("Expected error, got one")
}
ur := networks.UpdateResult{}
ur.Err = errors.New("")
if _, err := ExtractUpdate(ur); err == nil {
t.Fatalf("Expected error, got one")
}
cr := networks.CreateResult{}
cr.Err = errors.New("")
if _, err := ExtractCreate(cr); err == nil {
t.Fatalf("Expected error, got one")
}
}

View File

@@ -1,246 +0,0 @@
package firewalls
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/fw/firewalls", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewalls":[
{
"status": "ACTIVE",
"name": "fw1",
"admin_state_up": false,
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
"firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
"id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
"description": "OpenStack firewall 1"
},
{
"status": "PENDING_UPDATE",
"name": "fw2",
"admin_state_up": true,
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
"firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e299",
"id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f99",
"description": "OpenStack firewall 2"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractFirewalls(page)
if err != nil {
t.Errorf("Failed to extract members: %v", err)
return false, err
}
expected := []Firewall{
Firewall{
Status: "ACTIVE",
Name: "fw1",
AdminStateUp: false,
TenantID: "b4eedccc6fb74fa8a7ad6b08382b852b",
PolicyID: "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
ID: "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
Description: "OpenStack firewall 1",
},
Firewall{
Status: "PENDING_UPDATE",
Name: "fw2",
AdminStateUp: true,
TenantID: "b4eedccc6fb74fa8a7ad6b08382b852b",
PolicyID: "34be8c83-4d42-4dca-a74e-b77fffb8e299",
ID: "fb5b5315-64f6-4ea3-8e58-981cc37c6f99",
Description: "OpenStack firewall 2",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewalls", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"firewall":{
"name": "fw",
"description": "OpenStack firewall",
"admin_state_up": true,
"firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"firewall":{
"status": "PENDING_CREATE",
"name": "fw",
"description": "OpenStack firewall",
"admin_state_up": true,
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
"firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
}
}
`)
})
options := CreateOpts{
TenantID: "b4eedccc6fb74fa8a7ad6b08382b852b",
Name: "fw",
Description: "OpenStack firewall",
AdminStateUp: Up,
PolicyID: "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
}
_, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewalls/fb5b5315-64f6-4ea3-8e58-981cc37c6f61", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall": {
"status": "ACTIVE",
"name": "fw",
"admin_state_up": true,
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
"firewall_policy_id": "34be8c83-4d42-4dca-a74e-b77fffb8e28a",
"id": "fb5b5315-64f6-4ea3-8e58-981cc37c6f61",
"description": "OpenStack firewall"
}
}
`)
})
fw, err := Get(fake.ServiceClient(), "fb5b5315-64f6-4ea3-8e58-981cc37c6f61").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "ACTIVE", fw.Status)
th.AssertEquals(t, "fw", fw.Name)
th.AssertEquals(t, "OpenStack firewall", fw.Description)
th.AssertEquals(t, true, fw.AdminStateUp)
th.AssertEquals(t, "34be8c83-4d42-4dca-a74e-b77fffb8e28a", fw.PolicyID)
th.AssertEquals(t, "fb5b5315-64f6-4ea3-8e58-981cc37c6f61", fw.ID)
th.AssertEquals(t, "b4eedccc6fb74fa8a7ad6b08382b852b", fw.TenantID)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewalls/ea5b5315-64f6-4ea3-8e58-981cc37c6576", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"firewall":{
"name": "fw",
"description": "updated fw",
"admin_state_up":false,
"firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall": {
"status": "ACTIVE",
"name": "fw",
"admin_state_up": false,
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
"firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
"id": "ea5b5315-64f6-4ea3-8e58-981cc37c6576",
"description": "OpenStack firewall",
}
}
`)
})
options := UpdateOpts{
Name: "fw",
Description: "updated fw",
AdminStateUp: Down,
PolicyID: "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
}
_, err := Update(fake.ServiceClient(), "ea5b5315-64f6-4ea3-8e58-981cc37c6576", options).Extract()
th.AssertNoErr(t, err)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewalls/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,279 +0,0 @@
package policies
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/fw/firewall_policies", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_policies", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall_policies": [
{
"name": "policy1",
"firewall_rules": [
"75452b36-268e-4e75-aaf4-f0e7ed50bc97",
"c9e77ca0-1bc8-497d-904d-948107873dc6"
],
"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
"audited": true,
"shared": false,
"id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
"description": "Firewall policy 1"
},
{
"name": "policy2",
"firewall_rules": [
"03d2a6ad-633f-431a-8463-4370d06a22c8"
],
"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
"audited": false,
"shared": true,
"id": "c854fab5-bdaf-4a86-9359-78de93e5df01",
"description": "Firewall policy 2"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractPolicies(page)
if err != nil {
t.Errorf("Failed to extract members: %v", err)
return false, err
}
expected := []Policy{
Policy{
Name: "policy1",
Rules: []string{
"75452b36-268e-4e75-aaf4-f0e7ed50bc97",
"c9e77ca0-1bc8-497d-904d-948107873dc6",
},
TenantID: "9145d91459d248b1b02fdaca97c6a75d",
Audited: true,
Shared: false,
ID: "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
Description: "Firewall policy 1",
},
Policy{
Name: "policy2",
Rules: []string{
"03d2a6ad-633f-431a-8463-4370d06a22c8",
},
TenantID: "9145d91459d248b1b02fdaca97c6a75d",
Audited: false,
Shared: true,
ID: "c854fab5-bdaf-4a86-9359-78de93e5df01",
Description: "Firewall policy 2",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_policies", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"firewall_policy":{
"name": "policy",
"firewall_rules": [
"98a58c87-76be-ae7c-a74e-b77fffb88d95",
"11a58c87-76be-ae7c-a74e-b77fffb88a32"
],
"description": "Firewall policy",
"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
"audited": true,
"shared": false
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"firewall_policy":{
"name": "policy",
"firewall_rules": [
"98a58c87-76be-ae7c-a74e-b77fffb88d95",
"11a58c87-76be-ae7c-a74e-b77fffb88a32"
],
"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
"audited": false,
"id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
"description": "Firewall policy"
}
}
`)
})
options := CreateOpts{
TenantID: "9145d91459d248b1b02fdaca97c6a75d",
Name: "policy",
Description: "Firewall policy",
Shared: No,
Audited: Yes,
Rules: []string{
"98a58c87-76be-ae7c-a74e-b77fffb88d95",
"11a58c87-76be-ae7c-a74e-b77fffb88a32",
},
}
_, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_policies/bcab5315-64f6-4ea3-8e58-981cc37c6f61", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall_policy":{
"name": "www",
"firewall_rules": [
"75452b36-268e-4e75-aaf4-f0e7ed50bc97",
"c9e77ca0-1bc8-497d-904d-948107873dc6",
"03d2a6ad-633f-431a-8463-4370d06a22c8"
],
"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
"audited": false,
"id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
"description": "Firewall policy web"
}
}
`)
})
policy, err := Get(fake.ServiceClient(), "bcab5315-64f6-4ea3-8e58-981cc37c6f61").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "www", policy.Name)
th.AssertEquals(t, "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", policy.ID)
th.AssertEquals(t, "Firewall policy web", policy.Description)
th.AssertEquals(t, 3, len(policy.Rules))
th.AssertEquals(t, "75452b36-268e-4e75-aaf4-f0e7ed50bc97", policy.Rules[0])
th.AssertEquals(t, "c9e77ca0-1bc8-497d-904d-948107873dc6", policy.Rules[1])
th.AssertEquals(t, "03d2a6ad-633f-431a-8463-4370d06a22c8", policy.Rules[2])
th.AssertEquals(t, "9145d91459d248b1b02fdaca97c6a75d", policy.TenantID)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_policies/f2b08c1e-aa81-4668-8ae1-1401bcb0576c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"firewall_policy":{
"name": "policy",
"firewall_rules": [
"98a58c87-76be-ae7c-a74e-b77fffb88d95",
"11a58c87-76be-ae7c-a74e-b77fffb88a32"
],
"description": "Firewall policy"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall_policy":{
"name": "policy",
"firewall_rules": [
"75452b36-268e-4e75-aaf4-f0e7ed50bc97",
"c9e77ca0-1bc8-497d-904d-948107873dc6",
"03d2a6ad-633f-431a-8463-4370d06a22c8"
],
"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
"audited": false,
"id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
"description": "Firewall policy"
}
}
`)
})
options := UpdateOpts{
Name: "policy",
Description: "Firewall policy",
Rules: []string{
"98a58c87-76be-ae7c-a74e-b77fffb88d95",
"11a58c87-76be-ae7c-a74e-b77fffb88a32",
},
}
_, err := Update(fake.ServiceClient(), "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", options).Extract()
th.AssertNoErr(t, err)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_policies/4ec89077-d057-4a2b-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4ec89077-d057-4a2b-911f-60a3b47ee304")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,328 +0,0 @@
package rules
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/fw/firewall_rules", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_rules", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall_rules": [
{
"protocol": "tcp",
"description": "ssh rule",
"source_port": null,
"source_ip_address": null,
"destination_ip_address": "192.168.1.0/24",
"firewall_policy_id": "e2a5fb51-698c-4898-87e8-f1eee6b50919",
"position": 2,
"destination_port": "22",
"id": "f03bd950-6c56-4f5e-a307-45967078f507",
"name": "ssh_form_any",
"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
"enabled": true,
"action": "allow",
"ip_version": 4,
"shared": false
},
{
"protocol": "udp",
"description": "udp rule",
"source_port": null,
"source_ip_address": null,
"destination_ip_address": null,
"firewall_policy_id": "98d7fb51-698c-4123-87e8-f1eee6b5ab7e",
"position": 1,
"destination_port": null,
"id": "ab7bd950-6c56-4f5e-a307-45967078f890",
"name": "deny_all_udp",
"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
"enabled": true,
"action": "deny",
"ip_version": 4,
"shared": false
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractRules(page)
if err != nil {
t.Errorf("Failed to extract members: %v", err)
return false, err
}
expected := []Rule{
Rule{
Protocol: "tcp",
Description: "ssh rule",
SourcePort: "",
SourceIPAddress: "",
DestinationIPAddress: "192.168.1.0/24",
PolicyID: "e2a5fb51-698c-4898-87e8-f1eee6b50919",
Position: 2,
DestinationPort: "22",
ID: "f03bd950-6c56-4f5e-a307-45967078f507",
Name: "ssh_form_any",
TenantID: "80cf934d6ffb4ef5b244f1c512ad1e61",
Enabled: true,
Action: "allow",
IPVersion: 4,
Shared: false,
},
Rule{
Protocol: "udp",
Description: "udp rule",
SourcePort: "",
SourceIPAddress: "",
DestinationIPAddress: "",
PolicyID: "98d7fb51-698c-4123-87e8-f1eee6b5ab7e",
Position: 1,
DestinationPort: "",
ID: "ab7bd950-6c56-4f5e-a307-45967078f890",
Name: "deny_all_udp",
TenantID: "80cf934d6ffb4ef5b244f1c512ad1e61",
Enabled: true,
Action: "deny",
IPVersion: 4,
Shared: false,
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_rules", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"firewall_rule": {
"protocol": "tcp",
"description": "ssh rule",
"destination_ip_address": "192.168.1.0/24",
"destination_port": "22",
"name": "ssh_form_any",
"action": "allow",
"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"firewall_rule":{
"protocol": "tcp",
"description": "ssh rule",
"source_port": null,
"source_ip_address": null,
"destination_ip_address": "192.168.1.0/24",
"firewall_policy_id": "e2a5fb51-698c-4898-87e8-f1eee6b50919",
"position": 2,
"destination_port": "22",
"id": "f03bd950-6c56-4f5e-a307-45967078f507",
"name": "ssh_form_any",
"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
"enabled": true,
"action": "allow",
"ip_version": 4,
"shared": false
}
}
`)
})
options := CreateOpts{
TenantID: "80cf934d6ffb4ef5b244f1c512ad1e61",
Protocol: "tcp",
Description: "ssh rule",
DestinationIPAddress: "192.168.1.0/24",
DestinationPort: "22",
Name: "ssh_form_any",
Action: "allow",
}
_, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_rules/f03bd950-6c56-4f5e-a307-45967078f507", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall_rule":{
"protocol": "tcp",
"description": "ssh rule",
"source_port": null,
"source_ip_address": null,
"destination_ip_address": "192.168.1.0/24",
"firewall_policy_id": "e2a5fb51-698c-4898-87e8-f1eee6b50919",
"position": 2,
"destination_port": "22",
"id": "f03bd950-6c56-4f5e-a307-45967078f507",
"name": "ssh_form_any",
"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
"enabled": true,
"action": "allow",
"ip_version": 4,
"shared": false
}
}
`)
})
rule, err := Get(fake.ServiceClient(), "f03bd950-6c56-4f5e-a307-45967078f507").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "tcp", rule.Protocol)
th.AssertEquals(t, "ssh rule", rule.Description)
th.AssertEquals(t, "192.168.1.0/24", rule.DestinationIPAddress)
th.AssertEquals(t, "e2a5fb51-698c-4898-87e8-f1eee6b50919", rule.PolicyID)
th.AssertEquals(t, 2, rule.Position)
th.AssertEquals(t, "22", rule.DestinationPort)
th.AssertEquals(t, "f03bd950-6c56-4f5e-a307-45967078f507", rule.ID)
th.AssertEquals(t, "ssh_form_any", rule.Name)
th.AssertEquals(t, "80cf934d6ffb4ef5b244f1c512ad1e61", rule.TenantID)
th.AssertEquals(t, true, rule.Enabled)
th.AssertEquals(t, "allow", rule.Action)
th.AssertEquals(t, 4, rule.IPVersion)
th.AssertEquals(t, false, rule.Shared)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_rules/f03bd950-6c56-4f5e-a307-45967078f507", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"firewall_rule":{
"protocol": "tcp",
"description": "ssh rule",
"destination_ip_address": "192.168.1.0/24",
"destination_port": "22",
"source_ip_address": null,
"source_port": null,
"name": "ssh_form_any",
"action": "allow",
"enabled": false
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"firewall_rule":{
"protocol": "tcp",
"description": "ssh rule",
"source_port": null,
"source_ip_address": null,
"destination_ip_address": "192.168.1.0/24",
"firewall_policy_id": "e2a5fb51-698c-4898-87e8-f1eee6b50919",
"position": 2,
"destination_port": "22",
"id": "f03bd950-6c56-4f5e-a307-45967078f507",
"name": "ssh_form_any",
"tenant_id": "80cf934d6ffb4ef5b244f1c512ad1e61",
"enabled": false,
"action": "allow",
"ip_version": 4,
"shared": false
}
}
`)
})
destinationIPAddress := "192.168.1.0/24"
destinationPort := "22"
empty := ""
options := UpdateOpts{
Protocol: "tcp",
Description: "ssh rule",
DestinationIPAddress: &destinationIPAddress,
DestinationPort: &destinationPort,
Name: "ssh_form_any",
SourceIPAddress: &empty,
SourcePort: &empty,
Action: "allow",
Enabled: No,
}
_, err := Update(fake.ServiceClient(), "f03bd950-6c56-4f5e-a307-45967078f507", options).Extract()
th.AssertNoErr(t, err)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/fw/firewall_rules/4ec89077-d057-4a2b-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4ec89077-d057-4a2b-911f-60a3b47ee304")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,355 +0,0 @@
package floatingips
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"floatingips": [
{
"floating_network_id": "6d67c30a-ddb4-49a1-bec3-a65b286b4170",
"router_id": null,
"fixed_ip_address": null,
"floating_ip_address": "192.0.0.4",
"tenant_id": "017d8de156df4177889f31a9bd6edc00",
"status": "DOWN",
"port_id": null,
"id": "2f95fd2b-9f6a-4e8e-9e9a-2cbe286cbf9e"
},
{
"floating_network_id": "90f742b1-6d17-487b-ba95-71881dbc0b64",
"router_id": "0a24cb83-faf5-4d7f-b723-3144ed8a2167",
"fixed_ip_address": "192.0.0.2",
"floating_ip_address": "10.0.0.3",
"tenant_id": "017d8de156df4177889f31a9bd6edc00",
"status": "DOWN",
"port_id": "74a342ce-8e07-4e91-880c-9f834b68fa25",
"id": "ada25a95-f321-4f59-b0e0-f3a970dd3d63"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractFloatingIPs(page)
if err != nil {
t.Errorf("Failed to extract floating IPs: %v", err)
return false, err
}
expected := []FloatingIP{
FloatingIP{
FloatingNetworkID: "6d67c30a-ddb4-49a1-bec3-a65b286b4170",
FixedIP: "",
FloatingIP: "192.0.0.4",
TenantID: "017d8de156df4177889f31a9bd6edc00",
Status: "DOWN",
PortID: "",
ID: "2f95fd2b-9f6a-4e8e-9e9a-2cbe286cbf9e",
},
FloatingIP{
FloatingNetworkID: "90f742b1-6d17-487b-ba95-71881dbc0b64",
FixedIP: "192.0.0.2",
FloatingIP: "10.0.0.3",
TenantID: "017d8de156df4177889f31a9bd6edc00",
Status: "DOWN",
PortID: "74a342ce-8e07-4e91-880c-9f834b68fa25",
ID: "ada25a95-f321-4f59-b0e0-f3a970dd3d63",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestInvalidNextPageURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"floatingips": [{}], "floatingips_links": {}}`)
})
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
ExtractFloatingIPs(page)
return true, nil
})
}
func TestRequiredFieldsForCreate(t *testing.T) {
res1 := Create(fake.ServiceClient(), CreateOpts{FloatingNetworkID: ""})
if res1.Err == nil {
t.Fatalf("Expected error, got none")
}
res2 := Create(fake.ServiceClient(), CreateOpts{FloatingNetworkID: "foo", PortID: ""})
if res2.Err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"floatingip": {
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
"port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"floatingip": {
"router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f",
"tenant_id": "4969c491a3c74ee4af974e6d800c62de",
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
"fixed_ip_address": "10.0.0.3",
"floating_ip_address": "",
"port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab",
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
}
}
`)
})
options := CreateOpts{
FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57",
PortID: "ce705c24-c1ef-408a-bda3-7bbd946164ab",
}
ip, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID)
th.AssertEquals(t, "4969c491a3c74ee4af974e6d800c62de", ip.TenantID)
th.AssertEquals(t, "376da547-b977-4cfe-9cba-275c80debf57", ip.FloatingNetworkID)
th.AssertEquals(t, "", ip.FloatingIP)
th.AssertEquals(t, "ce705c24-c1ef-408a-bda3-7bbd946164ab", ip.PortID)
th.AssertEquals(t, "10.0.0.3", ip.FixedIP)
}
func TestCreateEmptyPort(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"floatingip": {
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"floatingip": {
"router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f",
"tenant_id": "4969c491a3c74ee4af974e6d800c62de",
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
"fixed_ip_address": "10.0.0.3",
"floating_ip_address": "",
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
}
}
`)
})
options := CreateOpts{
FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57",
}
ip, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID)
th.AssertEquals(t, "4969c491a3c74ee4af974e6d800c62de", ip.TenantID)
th.AssertEquals(t, "376da547-b977-4cfe-9cba-275c80debf57", ip.FloatingNetworkID)
th.AssertEquals(t, "", ip.FloatingIP)
th.AssertEquals(t, "", ip.PortID)
th.AssertEquals(t, "10.0.0.3", ip.FixedIP)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"floatingip": {
"floating_network_id": "90f742b1-6d17-487b-ba95-71881dbc0b64",
"fixed_ip_address": "192.0.0.2",
"floating_ip_address": "10.0.0.3",
"tenant_id": "017d8de156df4177889f31a9bd6edc00",
"status": "DOWN",
"port_id": "74a342ce-8e07-4e91-880c-9f834b68fa25",
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
}
}
`)
})
ip, err := Get(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "90f742b1-6d17-487b-ba95-71881dbc0b64", ip.FloatingNetworkID)
th.AssertEquals(t, "10.0.0.3", ip.FloatingIP)
th.AssertEquals(t, "74a342ce-8e07-4e91-880c-9f834b68fa25", ip.PortID)
th.AssertEquals(t, "192.0.0.2", ip.FixedIP)
th.AssertEquals(t, "017d8de156df4177889f31a9bd6edc00", ip.TenantID)
th.AssertEquals(t, "DOWN", ip.Status)
th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID)
}
func TestAssociate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"floatingip": {
"port_id": "423abc8d-2991-4a55-ba98-2aaea84cc72e"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"floatingip": {
"router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f",
"tenant_id": "4969c491a3c74ee4af974e6d800c62de",
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
"fixed_ip_address": null,
"floating_ip_address": "172.24.4.228",
"port_id": "423abc8d-2991-4a55-ba98-2aaea84cc72e",
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
}
}
`)
})
ip, err := Update(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7", UpdateOpts{PortID: "423abc8d-2991-4a55-ba98-2aaea84cc72e"}).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, "423abc8d-2991-4a55-ba98-2aaea84cc72e", ip.PortID)
}
func TestDisassociate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"floatingip": {
"port_id": null
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"floatingip": {
"router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f",
"tenant_id": "4969c491a3c74ee4af974e6d800c62de",
"floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57",
"fixed_ip_address": null,
"floating_ip_address": "172.24.4.228",
"port_id": null,
"id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7"
}
}
`)
})
ip, err := Update(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7", UpdateOpts{}).Extract()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, "", ip.FixedIP)
th.AssertDeepEquals(t, "", ip.PortID)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,338 +0,0 @@
package routers
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/routers", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"routers": [
{
"status": "ACTIVE",
"external_gateway_info": null,
"name": "second_routers",
"admin_state_up": true,
"tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
"id": "7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b"
},
{
"status": "ACTIVE",
"external_gateway_info": {
"network_id": "3c5bcddd-6af9-4e6b-9c3e-c153e521cab8"
},
"name": "router1",
"admin_state_up": true,
"tenant_id": "33a40233088643acb66ff6eb0ebea679",
"id": "a9254bdb-2613-4a13-ac4c-adc581fba50d"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractRouters(page)
if err != nil {
t.Errorf("Failed to extract routers: %v", err)
return false, err
}
expected := []Router{
Router{
Status: "ACTIVE",
GatewayInfo: GatewayInfo{NetworkID: ""},
AdminStateUp: true,
Name: "second_routers",
ID: "7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b",
TenantID: "6b96ff0cb17a4b859e1e575d221683d3",
},
Router{
Status: "ACTIVE",
GatewayInfo: GatewayInfo{NetworkID: "3c5bcddd-6af9-4e6b-9c3e-c153e521cab8"},
AdminStateUp: true,
Name: "router1",
ID: "a9254bdb-2613-4a13-ac4c-adc581fba50d",
TenantID: "33a40233088643acb66ff6eb0ebea679",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"router":{
"name": "foo_router",
"admin_state_up": false,
"external_gateway_info":{
"network_id":"8ca37218-28ff-41cb-9b10-039601ea7e6b"
}
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"router": {
"status": "ACTIVE",
"external_gateway_info": {
"network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b"
},
"name": "foo_router",
"admin_state_up": false,
"tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
"id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e"
}
}
`)
})
asu := false
gwi := GatewayInfo{NetworkID: "8ca37218-28ff-41cb-9b10-039601ea7e6b"}
options := CreateOpts{
Name: "foo_router",
AdminStateUp: &asu,
GatewayInfo: &gwi,
}
r, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "foo_router", r.Name)
th.AssertEquals(t, false, r.AdminStateUp)
th.AssertDeepEquals(t, GatewayInfo{NetworkID: "8ca37218-28ff-41cb-9b10-039601ea7e6b"}, r.GatewayInfo)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers/a07eea83-7710-4860-931b-5fe220fae533", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"router": {
"status": "ACTIVE",
"external_gateway_info": {
"network_id": "85d76829-6415-48ff-9c63-5c5ca8c61ac6"
},
"name": "router1",
"admin_state_up": true,
"tenant_id": "d6554fe62e2f41efbb6e026fad5c1542",
"id": "a07eea83-7710-4860-931b-5fe220fae533"
}
}
`)
})
n, err := Get(fake.ServiceClient(), "a07eea83-7710-4860-931b-5fe220fae533").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
th.AssertDeepEquals(t, n.GatewayInfo, GatewayInfo{NetworkID: "85d76829-6415-48ff-9c63-5c5ca8c61ac6"})
th.AssertEquals(t, n.Name, "router1")
th.AssertEquals(t, n.AdminStateUp, true)
th.AssertEquals(t, n.TenantID, "d6554fe62e2f41efbb6e026fad5c1542")
th.AssertEquals(t, n.ID, "a07eea83-7710-4860-931b-5fe220fae533")
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"router": {
"name": "new_name",
"external_gateway_info": {
"network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b"
}
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"router": {
"status": "ACTIVE",
"external_gateway_info": {
"network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b"
},
"name": "new_name",
"admin_state_up": true,
"tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
"id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e"
}
}
`)
})
gwi := GatewayInfo{NetworkID: "8ca37218-28ff-41cb-9b10-039601ea7e6b"}
options := UpdateOpts{Name: "new_name", GatewayInfo: &gwi}
n, err := Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Name, "new_name")
th.AssertDeepEquals(t, n.GatewayInfo, GatewayInfo{NetworkID: "8ca37218-28ff-41cb-9b10-039601ea7e6b"})
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
th.AssertNoErr(t, res.Err)
}
func TestAddInterface(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c/add_router_interface", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"subnet_id": "a2f1f29d-571b-4533-907f-5803ab96ead1"
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"subnet_id": "0d32a837-8069-4ec3-84c4-3eef3e10b188",
"tenant_id": "017d8de156df4177889f31a9bd6edc00",
"port_id": "3f990102-4485-4df1-97a0-2c35bdb85b31",
"id": "9a83fa11-8da5-436e-9afe-3d3ac5ce7770"
}
`)
})
opts := InterfaceOpts{SubnetID: "a2f1f29d-571b-4533-907f-5803ab96ead1"}
res, err := AddInterface(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "0d32a837-8069-4ec3-84c4-3eef3e10b188", res.SubnetID)
th.AssertEquals(t, "017d8de156df4177889f31a9bd6edc00", res.TenantID)
th.AssertEquals(t, "3f990102-4485-4df1-97a0-2c35bdb85b31", res.PortID)
th.AssertEquals(t, "9a83fa11-8da5-436e-9afe-3d3ac5ce7770", res.ID)
}
func TestAddInterfaceRequiredOpts(t *testing.T) {
_, err := AddInterface(fake.ServiceClient(), "foo", InterfaceOpts{}).Extract()
if err == nil {
t.Fatalf("Expected error, got none")
}
_, err = AddInterface(fake.ServiceClient(), "foo", InterfaceOpts{SubnetID: "bar", PortID: "baz"}).Extract()
if err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestRemoveInterface(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c/remove_router_interface", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"subnet_id": "a2f1f29d-571b-4533-907f-5803ab96ead1"
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"subnet_id": "0d32a837-8069-4ec3-84c4-3eef3e10b188",
"tenant_id": "017d8de156df4177889f31a9bd6edc00",
"port_id": "3f990102-4485-4df1-97a0-2c35bdb85b31",
"id": "9a83fa11-8da5-436e-9afe-3d3ac5ce7770"
}
`)
})
opts := InterfaceOpts{SubnetID: "a2f1f29d-571b-4533-907f-5803ab96ead1"}
res, err := RemoveInterface(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "0d32a837-8069-4ec3-84c4-3eef3e10b188", res.SubnetID)
th.AssertEquals(t, "017d8de156df4177889f31a9bd6edc00", res.TenantID)
th.AssertEquals(t, "3f990102-4485-4df1-97a0-2c35bdb85b31", res.PortID)
th.AssertEquals(t, "9a83fa11-8da5-436e-9afe-3d3ac5ce7770", res.ID)
}

View File

@@ -1,243 +0,0 @@
package members
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/lb/members", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/members", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"members":[
{
"status":"ACTIVE",
"weight":1,
"admin_state_up":true,
"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
"pool_id":"72741b06-df4d-4715-b142-276b6bce75ab",
"address":"10.0.0.4",
"protocol_port":80,
"id":"701b531b-111a-4f21-ad85-4795b7b12af6"
},
{
"status":"ACTIVE",
"weight":1,
"admin_state_up":true,
"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
"pool_id":"72741b06-df4d-4715-b142-276b6bce75ab",
"address":"10.0.0.3",
"protocol_port":80,
"id":"beb53b4d-230b-4abd-8118-575b8fa006ef"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractMembers(page)
if err != nil {
t.Errorf("Failed to extract members: %v", err)
return false, err
}
expected := []Member{
Member{
Status: "ACTIVE",
Weight: 1,
AdminStateUp: true,
TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
PoolID: "72741b06-df4d-4715-b142-276b6bce75ab",
Address: "10.0.0.4",
ProtocolPort: 80,
ID: "701b531b-111a-4f21-ad85-4795b7b12af6",
},
Member{
Status: "ACTIVE",
Weight: 1,
AdminStateUp: true,
TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
PoolID: "72741b06-df4d-4715-b142-276b6bce75ab",
Address: "10.0.0.3",
ProtocolPort: 80,
ID: "beb53b4d-230b-4abd-8118-575b8fa006ef",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/members", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"member": {
"tenant_id": "453105b9-1754-413f-aab1-55f1af620750",
"pool_id": "foo",
"address": "192.0.2.14",
"protocol_port":8080
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"member": {
"id": "975592ca-e308-48ad-8298-731935ee9f45",
"address": "192.0.2.14",
"protocol_port": 8080,
"tenant_id": "453105b9-1754-413f-aab1-55f1af620750",
"admin_state_up":true,
"weight": 1,
"status": "DOWN"
}
}
`)
})
options := CreateOpts{
TenantID: "453105b9-1754-413f-aab1-55f1af620750",
Address: "192.0.2.14",
ProtocolPort: 8080,
PoolID: "foo",
}
_, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/members/975592ca-e308-48ad-8298-731935ee9f45", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"member":{
"id":"975592ca-e308-48ad-8298-731935ee9f45",
"address":"192.0.2.14",
"protocol_port":8080,
"tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
"admin_state_up":true,
"weight":1,
"status":"DOWN"
}
}
`)
})
m, err := Get(fake.ServiceClient(), "975592ca-e308-48ad-8298-731935ee9f45").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "975592ca-e308-48ad-8298-731935ee9f45", m.ID)
th.AssertEquals(t, "192.0.2.14", m.Address)
th.AssertEquals(t, 8080, m.ProtocolPort)
th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", m.TenantID)
th.AssertEquals(t, true, m.AdminStateUp)
th.AssertEquals(t, 1, m.Weight)
th.AssertEquals(t, "DOWN", m.Status)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/members/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"member":{
"admin_state_up":false
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"member":{
"status":"PENDING_UPDATE",
"protocol_port":8080,
"weight":1,
"admin_state_up":false,
"tenant_id":"4fd44f30292945e481c7b8a0c8908869",
"pool_id":"7803631d-f181-4500-b3a2-1b68ba2a75fd",
"address":"10.0.0.5",
"status_description":null,
"id":"48a471ea-64f1-4eb6-9be7-dae6bbe40a0f"
}
}
`)
})
options := UpdateOpts{AdminStateUp: false}
_, err := Update(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", options).Extract()
th.AssertNoErr(t, err)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/members/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,312 +0,0 @@
package monitors
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/lb/health_monitors", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/health_monitors", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"health_monitors":[
{
"admin_state_up":true,
"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
"delay":10,
"max_retries":1,
"timeout":1,
"type":"PING",
"id":"466c8345-28d8-4f84-a246-e04380b0461d"
},
{
"admin_state_up":true,
"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
"delay":5,
"expected_codes":"200",
"max_retries":2,
"http_method":"GET",
"timeout":2,
"url_path":"/",
"type":"HTTP",
"id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractMonitors(page)
if err != nil {
t.Errorf("Failed to extract monitors: %v", err)
return false, err
}
expected := []Monitor{
Monitor{
AdminStateUp: true,
TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
Delay: 10,
MaxRetries: 1,
Timeout: 1,
Type: "PING",
ID: "466c8345-28d8-4f84-a246-e04380b0461d",
},
Monitor{
AdminStateUp: true,
TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
Delay: 5,
ExpectedCodes: "200",
MaxRetries: 2,
Timeout: 2,
URLPath: "/",
Type: "HTTP",
HTTPMethod: "GET",
ID: "5d4b5228-33b0-4e60-b225-9b727c1a20e7",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestDelayMustBeGreaterOrEqualThanTimeout(t *testing.T) {
_, err := Create(fake.ServiceClient(), CreateOpts{
Type: "HTTP",
Delay: 1,
Timeout: 10,
MaxRetries: 5,
URLPath: "/check",
ExpectedCodes: "200-299",
}).Extract()
if err == nil {
t.Fatalf("Expected error, got none")
}
_, err = Update(fake.ServiceClient(), "453105b9-1754-413f-aab1-55f1af620750", UpdateOpts{
Delay: 1,
Timeout: 10,
}).Extract()
if err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/health_monitors", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"health_monitor":{
"type":"HTTP",
"tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
"delay":20,
"timeout":10,
"max_retries":5,
"url_path":"/check",
"expected_codes":"200-299"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"health_monitor":{
"id":"f3eeab00-8367-4524-b662-55e64d4cacb5",
"tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
"type":"HTTP",
"delay":20,
"timeout":10,
"max_retries":5,
"http_method":"GET",
"url_path":"/check",
"expected_codes":"200-299",
"admin_state_up":true,
"status":"ACTIVE"
}
}
`)
})
_, err := Create(fake.ServiceClient(), CreateOpts{
Type: "HTTP",
TenantID: "453105b9-1754-413f-aab1-55f1af620750",
Delay: 20,
Timeout: 10,
MaxRetries: 5,
URLPath: "/check",
ExpectedCodes: "200-299",
}).Extract()
th.AssertNoErr(t, err)
}
func TestRequiredCreateOpts(t *testing.T) {
res := Create(fake.ServiceClient(), CreateOpts{})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Type: TypeHTTP})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/health_monitors/f3eeab00-8367-4524-b662-55e64d4cacb5", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"health_monitor":{
"id":"f3eeab00-8367-4524-b662-55e64d4cacb5",
"tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
"type":"HTTP",
"delay":20,
"timeout":10,
"max_retries":5,
"http_method":"GET",
"url_path":"/check",
"expected_codes":"200-299",
"admin_state_up":true,
"status":"ACTIVE"
}
}
`)
})
hm, err := Get(fake.ServiceClient(), "f3eeab00-8367-4524-b662-55e64d4cacb5").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "f3eeab00-8367-4524-b662-55e64d4cacb5", hm.ID)
th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", hm.TenantID)
th.AssertEquals(t, "HTTP", hm.Type)
th.AssertEquals(t, 20, hm.Delay)
th.AssertEquals(t, 10, hm.Timeout)
th.AssertEquals(t, 5, hm.MaxRetries)
th.AssertEquals(t, "GET", hm.HTTPMethod)
th.AssertEquals(t, "/check", hm.URLPath)
th.AssertEquals(t, "200-299", hm.ExpectedCodes)
th.AssertEquals(t, true, hm.AdminStateUp)
th.AssertEquals(t, "ACTIVE", hm.Status)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/health_monitors/b05e44b5-81f9-4551-b474-711a722698f7", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"health_monitor":{
"delay": 3,
"timeout": 20,
"max_retries": 10,
"url_path": "/another_check",
"expected_codes": "301"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, `
{
"health_monitor": {
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"delay": 3,
"max_retries": 10,
"http_method": "GET",
"timeout": 20,
"pools": [
{
"status": "PENDING_CREATE",
"status_description": null,
"pool_id": "6e55751f-6ad4-4e53-b8d4-02e442cd21df"
}
],
"type": "PING",
"id": "b05e44b5-81f9-4551-b474-711a722698f7"
}
}
`)
})
_, err := Update(fake.ServiceClient(), "b05e44b5-81f9-4551-b474-711a722698f7", UpdateOpts{
Delay: 3,
Timeout: 20,
MaxRetries: 10,
URLPath: "/another_check",
ExpectedCodes: "301",
}).Extract()
th.AssertNoErr(t, err)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/health_monitors/b05e44b5-81f9-4551-b474-711a722698f7", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "b05e44b5-81f9-4551-b474-711a722698f7")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,317 +0,0 @@
package pools
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/lb/pools", rootURL(fake.ServiceClient()))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"pools":[
{
"status":"ACTIVE",
"lb_method":"ROUND_ROBIN",
"protocol":"HTTP",
"description":"",
"health_monitors":[
"466c8345-28d8-4f84-a246-e04380b0461d",
"5d4b5228-33b0-4e60-b225-9b727c1a20e7"
],
"members":[
"701b531b-111a-4f21-ad85-4795b7b12af6",
"beb53b4d-230b-4abd-8118-575b8fa006ef"
],
"status_description": null,
"id":"72741b06-df4d-4715-b142-276b6bce75ab",
"vip_id":"4ec89087-d057-4e2c-911f-60a3b47ee304",
"name":"app_pool",
"admin_state_up":true,
"subnet_id":"8032909d-47a1-4715-90af-5153ffe39861",
"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
"health_monitors_status": [],
"provider": "haproxy"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractPools(page)
if err != nil {
t.Errorf("Failed to extract pools: %v", err)
return false, err
}
expected := []Pool{
Pool{
Status: "ACTIVE",
LBMethod: "ROUND_ROBIN",
Protocol: "HTTP",
Description: "",
MonitorIDs: []string{
"466c8345-28d8-4f84-a246-e04380b0461d",
"5d4b5228-33b0-4e60-b225-9b727c1a20e7",
},
SubnetID: "8032909d-47a1-4715-90af-5153ffe39861",
TenantID: "83657cfcdfe44cd5920adaf26c48ceea",
AdminStateUp: true,
Name: "app_pool",
MemberIDs: []string{
"701b531b-111a-4f21-ad85-4795b7b12af6",
"beb53b4d-230b-4abd-8118-575b8fa006ef",
},
ID: "72741b06-df4d-4715-b142-276b6bce75ab",
VIPID: "4ec89087-d057-4e2c-911f-60a3b47ee304",
Provider: "haproxy",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"pool": {
"lb_method": "ROUND_ROBIN",
"protocol": "HTTP",
"name": "Example pool",
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"tenant_id": "2ffc6e22aae24e4795f87155d24c896f"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"pool": {
"status": "PENDING_CREATE",
"lb_method": "ROUND_ROBIN",
"protocol": "HTTP",
"description": "",
"health_monitors": [],
"members": [],
"status_description": null,
"id": "69055154-f603-4a28-8951-7cc2d9e54a9a",
"vip_id": null,
"name": "Example pool",
"admin_state_up": true,
"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
"tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
"health_monitors_status": []
}
}
`)
})
options := CreateOpts{
LBMethod: LBMethodRoundRobin,
Protocol: "HTTP",
Name: "Example pool",
SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9",
TenantID: "2ffc6e22aae24e4795f87155d24c896f",
}
p, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "PENDING_CREATE", p.Status)
th.AssertEquals(t, "ROUND_ROBIN", p.LBMethod)
th.AssertEquals(t, "HTTP", p.Protocol)
th.AssertEquals(t, "", p.Description)
th.AssertDeepEquals(t, []string{}, p.MonitorIDs)
th.AssertDeepEquals(t, []string{}, p.MemberIDs)
th.AssertEquals(t, "69055154-f603-4a28-8951-7cc2d9e54a9a", p.ID)
th.AssertEquals(t, "Example pool", p.Name)
th.AssertEquals(t, "1981f108-3c48-48d2-b908-30f7d28532c9", p.SubnetID)
th.AssertEquals(t, "2ffc6e22aae24e4795f87155d24c896f", p.TenantID)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"pool":{
"id":"332abe93-f488-41ba-870b-2ac66be7f853",
"tenant_id":"19eaa775-cf5d-49bc-902e-2f85f668d995",
"name":"Example pool",
"description":"",
"protocol":"tcp",
"lb_algorithm":"ROUND_ROBIN",
"session_persistence":{
},
"healthmonitor_id":null,
"members":[
],
"admin_state_up":true,
"status":"ACTIVE"
}
}
`)
})
n, err := Get(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.ID, "332abe93-f488-41ba-870b-2ac66be7f853")
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"pool":{
"name":"SuperPool",
"lb_method": "LEAST_CONNECTIONS"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"pool":{
"status":"PENDING_UPDATE",
"lb_method":"LEAST_CONNECTIONS",
"protocol":"TCP",
"description":"",
"health_monitors":[
],
"subnet_id":"8032909d-47a1-4715-90af-5153ffe39861",
"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
"admin_state_up":true,
"name":"SuperPool",
"members":[
],
"id":"61b1f87a-7a21-4ad3-9dda-7f81d249944f",
"vip_id":null
}
}
`)
})
options := UpdateOpts{Name: "SuperPool", LBMethod: LBMethodLeastConnections}
n, err := Update(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "SuperPool", n.Name)
th.AssertDeepEquals(t, "LEAST_CONNECTIONS", n.LBMethod)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853")
th.AssertNoErr(t, res.Err)
}
func TestAssociateHealthMonitor(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853/health_monitors", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"health_monitor":{
"id":"b624decf-d5d3-4c66-9a3d-f047e7786181"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
})
_, err := AssociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181").Extract()
th.AssertNoErr(t, err)
}
func TestDisassociateHealthMonitor(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853/health_monitors/b624decf-d5d3-4c66-9a3d-f047e7786181", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := DisassociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,336 +0,0 @@
package vips
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/lb/vips", rootURL(fake.ServiceClient()))
th.AssertEquals(t, th.Endpoint()+"v2.0/lb/vips/foo", resourceURL(fake.ServiceClient(), "foo"))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/vips", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"vips":[
{
"id": "db902c0c-d5ff-4753-b465-668ad9656918",
"tenant_id": "310df60f-2a10-4ee5-9554-98393092194c",
"name": "web_vip",
"description": "lb config for the web tier",
"subnet_id": "96a4386a-f8c3-42ed-afce-d7954eee77b3",
"address" : "10.30.176.47",
"port_id" : "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
"protocol": "HTTP",
"protocol_port": 80,
"pool_id" : "cfc6589d-f949-4c66-99d2-c2da56ef3764",
"admin_state_up": true,
"status": "ACTIVE"
},
{
"id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
"tenant_id": "310df60f-2a10-4ee5-9554-98393092194c",
"name": "db_vip",
"description": "lb config for the db tier",
"subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
"address" : "10.30.176.48",
"port_id" : "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
"protocol": "TCP",
"protocol_port": 3306,
"pool_id" : "41efe233-7591-43c5-9cf7-923964759f9e",
"session_persistence" : {"type" : "SOURCE_IP"},
"connection_limit" : 2000,
"admin_state_up": true,
"status": "INACTIVE"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractVIPs(page)
if err != nil {
t.Errorf("Failed to extract LBs: %v", err)
return false, err
}
expected := []VirtualIP{
VirtualIP{
ID: "db902c0c-d5ff-4753-b465-668ad9656918",
TenantID: "310df60f-2a10-4ee5-9554-98393092194c",
Name: "web_vip",
Description: "lb config for the web tier",
SubnetID: "96a4386a-f8c3-42ed-afce-d7954eee77b3",
Address: "10.30.176.47",
PortID: "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
Protocol: "HTTP",
ProtocolPort: 80,
PoolID: "cfc6589d-f949-4c66-99d2-c2da56ef3764",
Persistence: SessionPersistence{},
ConnLimit: 0,
AdminStateUp: true,
Status: "ACTIVE",
},
VirtualIP{
ID: "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
TenantID: "310df60f-2a10-4ee5-9554-98393092194c",
Name: "db_vip",
Description: "lb config for the db tier",
SubnetID: "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
Address: "10.30.176.48",
PortID: "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
Protocol: "TCP",
ProtocolPort: 3306,
PoolID: "41efe233-7591-43c5-9cf7-923964759f9e",
Persistence: SessionPersistence{Type: "SOURCE_IP"},
ConnLimit: 2000,
AdminStateUp: true,
Status: "INACTIVE",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/vips", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"vip": {
"protocol": "HTTP",
"name": "NewVip",
"admin_state_up": true,
"subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
"pool_id": "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
"protocol_port": 80,
"session_persistence": {"type": "SOURCE_IP"}
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"vip": {
"status": "PENDING_CREATE",
"protocol": "HTTP",
"description": "",
"admin_state_up": true,
"subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
"tenant_id": "83657cfcdfe44cd5920adaf26c48ceea",
"connection_limit": -1,
"pool_id": "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
"address": "10.0.0.11",
"protocol_port": 80,
"port_id": "f7e6fe6a-b8b5-43a8-8215-73456b32e0f5",
"id": "c987d2be-9a3c-4ac9-a046-e8716b1350e2",
"name": "NewVip"
}
}
`)
})
opts := CreateOpts{
Protocol: "HTTP",
Name: "NewVip",
AdminStateUp: Up,
SubnetID: "8032909d-47a1-4715-90af-5153ffe39861",
PoolID: "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
ProtocolPort: 80,
Persistence: &SessionPersistence{Type: "SOURCE_IP"},
}
r, err := Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "PENDING_CREATE", r.Status)
th.AssertEquals(t, "HTTP", r.Protocol)
th.AssertEquals(t, "", r.Description)
th.AssertEquals(t, true, r.AdminStateUp)
th.AssertEquals(t, "8032909d-47a1-4715-90af-5153ffe39861", r.SubnetID)
th.AssertEquals(t, "83657cfcdfe44cd5920adaf26c48ceea", r.TenantID)
th.AssertEquals(t, -1, r.ConnLimit)
th.AssertEquals(t, "61b1f87a-7a21-4ad3-9dda-7f81d249944f", r.PoolID)
th.AssertEquals(t, "10.0.0.11", r.Address)
th.AssertEquals(t, 80, r.ProtocolPort)
th.AssertEquals(t, "f7e6fe6a-b8b5-43a8-8215-73456b32e0f5", r.PortID)
th.AssertEquals(t, "c987d2be-9a3c-4ac9-a046-e8716b1350e2", r.ID)
th.AssertEquals(t, "NewVip", r.Name)
}
func TestRequiredCreateOpts(t *testing.T) {
res := Create(fake.ServiceClient(), CreateOpts{})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Name: "foo"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Name: "foo", SubnetID: "bar"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Name: "foo", SubnetID: "bar", Protocol: "bar"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Name: "foo", SubnetID: "bar", Protocol: "bar", ProtocolPort: 80})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/vips/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"vip": {
"status": "ACTIVE",
"protocol": "HTTP",
"description": "",
"admin_state_up": true,
"subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
"tenant_id": "83657cfcdfe44cd5920adaf26c48ceea",
"connection_limit": 1000,
"pool_id": "72741b06-df4d-4715-b142-276b6bce75ab",
"session_persistence": {
"cookie_name": "MyAppCookie",
"type": "APP_COOKIE"
},
"address": "10.0.0.10",
"protocol_port": 80,
"port_id": "b5a743d6-056b-468b-862d-fb13a9aa694e",
"id": "4ec89087-d057-4e2c-911f-60a3b47ee304",
"name": "my-vip"
}
}
`)
})
vip, err := Get(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "ACTIVE", vip.Status)
th.AssertEquals(t, "HTTP", vip.Protocol)
th.AssertEquals(t, "", vip.Description)
th.AssertEquals(t, true, vip.AdminStateUp)
th.AssertEquals(t, 1000, vip.ConnLimit)
th.AssertEquals(t, SessionPersistence{Type: "APP_COOKIE", CookieName: "MyAppCookie"}, vip.Persistence)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/vips/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"vip": {
"connection_limit": 1000,
"session_persistence": {"type": "SOURCE_IP"}
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, `
{
"vip": {
"status": "PENDING_UPDATE",
"protocol": "HTTP",
"description": "",
"admin_state_up": true,
"subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
"tenant_id": "83657cfcdfe44cd5920adaf26c48ceea",
"connection_limit": 1000,
"pool_id": "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
"address": "10.0.0.11",
"protocol_port": 80,
"port_id": "f7e6fe6a-b8b5-43a8-8215-73456b32e0f5",
"id": "c987d2be-9a3c-4ac9-a046-e8716b1350e2",
"name": "NewVip"
}
}
`)
})
i1000 := 1000
options := UpdateOpts{
ConnLimit: &i1000,
Persistence: &SessionPersistence{Type: "SOURCE_IP"},
}
vip, err := Update(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "PENDING_UPDATE", vip.Status)
th.AssertEquals(t, 1000, vip.ConnLimit)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/lb/vips/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,253 +0,0 @@
package provider
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"networks": [
{
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"provider:segmentation_id": null,
"provider:physical_network": null,
"provider:network_type": "local"
},
{
"status": "ACTIVE",
"subnets": [
"08eae331-0402-425a-923c-34f7cfe39c1b"
],
"name": "private",
"admin_state_up": true,
"tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
"shared": true,
"id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
"provider:segmentation_id": 1234567890,
"provider:physical_network": null,
"provider:network_type": "local"
}
]
}
`)
})
count := 0
networks.List(fake.ServiceClient(), networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractList(page)
if err != nil {
t.Errorf("Failed to extract networks: %v", err)
return false, err
}
expected := []NetworkExtAttrs{
NetworkExtAttrs{
Status: "ACTIVE",
Subnets: []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"},
Name: "private-network",
AdminStateUp: true,
TenantID: "4fd44f30292945e481c7b8a0c8908869",
Shared: true,
ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
NetworkType: "local",
PhysicalNetwork: "",
SegmentationID: "",
},
NetworkExtAttrs{
Status: "ACTIVE",
Subnets: []string{"08eae331-0402-425a-923c-34f7cfe39c1b"},
Name: "private",
AdminStateUp: true,
TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Shared: true,
ID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
NetworkType: "local",
PhysicalNetwork: "",
SegmentationID: "1234567890",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"provider:physical_network": null,
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"provider:network_type": "local",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"provider:segmentation_id": null
}
}
`)
})
res := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
n, err := ExtractGet(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, "", n.PhysicalNetwork)
th.AssertEquals(t, "local", n.NetworkType)
th.AssertEquals(t, "", n.SegmentationID)
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"name": "sample_network",
"admin_state_up": true
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"network": {
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"provider:physical_network": null,
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"provider:network_type": "local",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"provider:segmentation_id": null
}
}
`)
})
options := networks.CreateOpts{Name: "sample_network", AdminStateUp: Up}
res := networks.Create(fake.ServiceClient(), options)
n, err := ExtractCreate(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, "", n.PhysicalNetwork)
th.AssertEquals(t, "local", n.NetworkType)
th.AssertEquals(t, "", n.SegmentationID)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"name": "new_network_name",
"admin_state_up": false,
"shared": true
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"provider:physical_network": null,
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"provider:network_type": "local",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"provider:segmentation_id": null
}
}
`)
})
iTrue := true
options := networks.UpdateOpts{Name: "new_network_name", AdminStateUp: Down, Shared: &iTrue}
res := networks.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options)
n, err := ExtractUpdate(res)
th.AssertNoErr(t, err)
th.AssertEquals(t, "", n.PhysicalNetwork)
th.AssertEquals(t, "local", n.NetworkType)
th.AssertEquals(t, "", n.SegmentationID)
}

View File

@@ -1,213 +0,0 @@
package groups
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/rules"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/security-groups", rootURL(fake.ServiceClient()))
th.AssertEquals(t, th.Endpoint()+"v2.0/security-groups/foo", resourceURL(fake.ServiceClient(), "foo"))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-groups", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"security_groups": [
{
"description": "default",
"id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"name": "default",
"security_group_rules": [],
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractGroups(page)
if err != nil {
t.Errorf("Failed to extract secgroups: %v", err)
return false, err
}
expected := []SecGroup{
SecGroup{
Description: "default",
ID: "85cc3048-abc3-43cc-89b3-377341426ac5",
Name: "default",
Rules: []rules.SecGroupRule{},
TenantID: "e4f50856753b4dc6afee5fa6b9b6c550",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-groups", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"security_group": {
"name": "new-webservers",
"description": "security group for webservers"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"security_group": {
"description": "security group for webservers",
"id": "2076db17-a522-4506-91de-c6dd8e837028",
"name": "new-webservers",
"security_group_rules": [
{
"direction": "egress",
"ethertype": "IPv4",
"id": "38ce2d8e-e8f1-48bd-83c2-d33cb9f50c3d",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "2076db17-a522-4506-91de-c6dd8e837028",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
},
{
"direction": "egress",
"ethertype": "IPv6",
"id": "565b9502-12de-4ffd-91e9-68885cff6ae1",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "2076db17-a522-4506-91de-c6dd8e837028",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
],
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
}
`)
})
opts := CreateOpts{Name: "new-webservers", Description: "security group for webservers"}
_, err := Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-groups/85cc3048-abc3-43cc-89b3-377341426ac5", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"security_group": {
"description": "default",
"id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"name": "default",
"security_group_rules": [
{
"direction": "egress",
"ethertype": "IPv6",
"id": "3c0e45ff-adaf-4124-b083-bf390e5482ff",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
},
{
"direction": "egress",
"ethertype": "IPv4",
"id": "93aa42e5-80db-4581-9391-3a608bd0e448",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
],
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
}
`)
})
sg, err := Get(fake.ServiceClient(), "85cc3048-abc3-43cc-89b3-377341426ac5").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "default", sg.Description)
th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sg.ID)
th.AssertEquals(t, "default", sg.Name)
th.AssertEquals(t, 2, len(sg.Rules))
th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sg.TenantID)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-groups/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,243 +0,0 @@
package rules
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestURLs(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.AssertEquals(t, th.Endpoint()+"v2.0/security-group-rules", rootURL(fake.ServiceClient()))
th.AssertEquals(t, th.Endpoint()+"v2.0/security-group-rules/foo", resourceURL(fake.ServiceClient(), "foo"))
}
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"security_group_rules": [
{
"direction": "egress",
"ethertype": "IPv6",
"id": "3c0e45ff-adaf-4124-b083-bf390e5482ff",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
},
{
"direction": "egress",
"ethertype": "IPv4",
"id": "93aa42e5-80db-4581-9391-3a608bd0e448",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractRules(page)
if err != nil {
t.Errorf("Failed to extract secrules: %v", err)
return false, err
}
expected := []SecGroupRule{
SecGroupRule{
Direction: "egress",
EtherType: "IPv6",
ID: "3c0e45ff-adaf-4124-b083-bf390e5482ff",
PortRangeMax: 0,
PortRangeMin: 0,
Protocol: "",
RemoteGroupID: "",
RemoteIPPrefix: "",
SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5",
TenantID: "e4f50856753b4dc6afee5fa6b9b6c550",
},
SecGroupRule{
Direction: "egress",
EtherType: "IPv4",
ID: "93aa42e5-80db-4581-9391-3a608bd0e448",
PortRangeMax: 0,
PortRangeMin: 0,
Protocol: "",
RemoteGroupID: "",
RemoteIPPrefix: "",
SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5",
TenantID: "e4f50856753b4dc6afee5fa6b9b6c550",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"security_group_rule": {
"direction": "ingress",
"port_range_min": 80,
"ethertype": "IPv4",
"port_range_max": 80,
"protocol": "tcp",
"remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"security_group_rule": {
"direction": "ingress",
"ethertype": "IPv4",
"id": "2bc0accf-312e-429a-956e-e4407625eb62",
"port_range_max": 80,
"port_range_min": 80,
"protocol": "tcp",
"remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"remote_ip_prefix": null,
"security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
}
`)
})
opts := CreateOpts{
Direction: "ingress",
PortRangeMin: 80,
EtherType: "IPv4",
PortRangeMax: 80,
Protocol: "tcp",
RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5",
SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a",
}
_, err := Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
}
func TestRequiredCreateOpts(t *testing.T) {
res := Create(fake.ServiceClient(), CreateOpts{Direction: "something"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Direction: DirIngress, EtherType: "something"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Direction: DirIngress, EtherType: Ether4})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{Direction: DirIngress, EtherType: Ether4, SecGroupID: "something", Protocol: "foo"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-group-rules/3c0e45ff-adaf-4124-b083-bf390e5482ff", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"security_group_rule": {
"direction": "egress",
"ethertype": "IPv6",
"id": "3c0e45ff-adaf-4124-b083-bf390e5482ff",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_group_id": null,
"remote_ip_prefix": null,
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
}
}
`)
})
sr, err := Get(fake.ServiceClient(), "3c0e45ff-adaf-4124-b083-bf390e5482ff").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, "egress", sr.Direction)
th.AssertEquals(t, "IPv6", sr.EtherType)
th.AssertEquals(t, "3c0e45ff-adaf-4124-b083-bf390e5482ff", sr.ID)
th.AssertEquals(t, 0, sr.PortRangeMax)
th.AssertEquals(t, 0, sr.PortRangeMin)
th.AssertEquals(t, "", sr.Protocol)
th.AssertEquals(t, "", sr.RemoteGroupID)
th.AssertEquals(t, "", sr.RemoteIPPrefix)
th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sr.SecGroupID)
th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sr.TenantID)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/security-group-rules/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,275 +0,0 @@
package networks
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"networks": [
{
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
},
{
"status": "ACTIVE",
"subnets": [
"08eae331-0402-425a-923c-34f7cfe39c1b"
],
"name": "private",
"admin_state_up": true,
"tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
"shared": true,
"id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324"
}
]
}
`)
})
client := fake.ServiceClient()
count := 0
List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractNetworks(page)
if err != nil {
t.Errorf("Failed to extract networks: %v", err)
return false, err
}
expected := []Network{
Network{
Status: "ACTIVE",
Subnets: []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"},
Name: "private-network",
AdminStateUp: true,
TenantID: "4fd44f30292945e481c7b8a0c8908869",
Shared: true,
ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
},
Network{
Status: "ACTIVE",
Subnets: []string{"08eae331-0402-425a-923c-34f7cfe39c1b"},
Name: "private",
AdminStateUp: true,
TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Shared: true,
ID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
}
}
`)
})
n, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
th.AssertDeepEquals(t, n.Subnets, []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"})
th.AssertEquals(t, n.Name, "private-network")
th.AssertEquals(t, n.AdminStateUp, true)
th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
th.AssertEquals(t, n.Shared, true)
th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"name": "sample_network",
"admin_state_up": true
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"network": {
"status": "ACTIVE",
"subnets": [],
"name": "net1",
"admin_state_up": true,
"tenant_id": "9bacb3c5d39d41a79512987f338cf177",
"shared": false,
"id": "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
}
}
`)
})
iTrue := true
options := CreateOpts{Name: "sample_network", AdminStateUp: &iTrue}
n, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
th.AssertDeepEquals(t, n.Subnets, []string{})
th.AssertEquals(t, n.Name, "net1")
th.AssertEquals(t, n.AdminStateUp, true)
th.AssertEquals(t, n.TenantID, "9bacb3c5d39d41a79512987f338cf177")
th.AssertEquals(t, n.Shared, false)
th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
}
func TestCreateWithOptionalFields(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"name": "sample_network",
"admin_state_up": true,
"shared": true,
"tenant_id": "12345"
}
}
`)
w.WriteHeader(http.StatusCreated)
})
iTrue := true
options := CreateOpts{Name: "sample_network", AdminStateUp: &iTrue, Shared: &iTrue, TenantID: "12345"}
_, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"network": {
"name": "new_network_name",
"admin_state_up": false,
"shared": true
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"network": {
"status": "ACTIVE",
"subnets": [],
"name": "new_network_name",
"admin_state_up": false,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"shared": true,
"id": "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
}
}
`)
})
iTrue, iFalse := true, false
options := UpdateOpts{Name: "new_network_name", AdminStateUp: &iFalse, Shared: &iTrue}
n, err := Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Name, "new_network_name")
th.AssertEquals(t, n.AdminStateUp, false)
th.AssertEquals(t, n.Shared, true)
th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,38 +0,0 @@
package networks
import (
"testing"
"github.com/rackspace/gophercloud"
th "github.com/rackspace/gophercloud/testhelper"
)
const endpoint = "http://localhost:57909/"
func endpointClient() *gophercloud.ServiceClient {
return &gophercloud.ServiceClient{Endpoint: endpoint, ResourceBase: endpoint + "v2.0/"}
}
func TestGetURL(t *testing.T) {
actual := getURL(endpointClient(), "foo")
expected := endpoint + "v2.0/networks/foo"
th.AssertEquals(t, expected, actual)
}
func TestCreateURL(t *testing.T) {
actual := createURL(endpointClient())
expected := endpoint + "v2.0/networks"
th.AssertEquals(t, expected, actual)
}
func TestListURL(t *testing.T) {
actual := createURL(endpointClient())
expected := endpoint + "v2.0/networks"
th.AssertEquals(t, expected, actual)
}
func TestDeleteURL(t *testing.T) {
actual := deleteURL(endpointClient(), "foo")
expected := endpoint + "v2.0/networks/foo"
th.AssertEquals(t, expected, actual)
}

View File

@@ -1,321 +0,0 @@
package ports
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"ports": [
{
"status": "ACTIVE",
"binding:host_id": "devstack",
"name": "",
"admin_state_up": true,
"network_id": "70c1db1f-b701-45bd-96e0-a313ee3430b3",
"tenant_id": "",
"device_owner": "network:router_gateway",
"mac_address": "fa:16:3e:58:42:ed",
"fixed_ips": [
{
"subnet_id": "008ba151-0b8c-4a67-98b5-0d2b87666062",
"ip_address": "172.24.4.2"
}
],
"id": "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
"security_groups": [],
"device_id": "9ae135f4-b6e0-4dad-9e91-3c223e385824"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractPorts(page)
if err != nil {
t.Errorf("Failed to extract subnets: %v", err)
return false, nil
}
expected := []Port{
Port{
Status: "ACTIVE",
Name: "",
AdminStateUp: true,
NetworkID: "70c1db1f-b701-45bd-96e0-a313ee3430b3",
TenantID: "",
DeviceOwner: "network:router_gateway",
MACAddress: "fa:16:3e:58:42:ed",
FixedIPs: []IP{
IP{
SubnetID: "008ba151-0b8c-4a67-98b5-0d2b87666062",
IPAddress: "172.24.4.2",
},
},
ID: "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
SecurityGroups: []string{},
DeviceID: "9ae135f4-b6e0-4dad-9e91-3c223e385824",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"port": {
"status": "ACTIVE",
"name": "",
"admin_state_up": true,
"network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
"tenant_id": "7e02058126cc4950b75f9970368ba177",
"device_owner": "network:router_interface",
"mac_address": "fa:16:3e:23:fd:d7",
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.1"
}
],
"id": "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2",
"security_groups": [],
"device_id": "5e3898d7-11be-483e-9732-b2f5eccd2b2e"
}
}
`)
})
n, err := Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
th.AssertEquals(t, n.Name, "")
th.AssertEquals(t, n.AdminStateUp, true)
th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
th.AssertEquals(t, n.TenantID, "7e02058126cc4950b75f9970368ba177")
th.AssertEquals(t, n.DeviceOwner, "network:router_interface")
th.AssertEquals(t, n.MACAddress, "fa:16:3e:23:fd:d7")
th.AssertDeepEquals(t, n.FixedIPs, []IP{
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"},
})
th.AssertEquals(t, n.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2")
th.AssertDeepEquals(t, n.SecurityGroups, []string{})
th.AssertEquals(t, n.Status, "ACTIVE")
th.AssertEquals(t, n.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e")
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"port": {
"network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
"name": "private-port",
"admin_state_up": true,
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.2"
}
],
"security_groups": ["foo"]
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"port": {
"status": "DOWN",
"name": "private-port",
"allowed_address_pairs": [],
"admin_state_up": true,
"network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
"tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
"device_owner": "",
"mac_address": "fa:16:3e:c9:cb:f0",
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.2"
}
],
"id": "65c0ee9f-d634-4522-8954-51021b570b0d",
"security_groups": [
"f0ac4394-7e4a-4409-9701-ba8be283dbc3"
],
"device_id": ""
}
}
`)
})
asu := true
options := CreateOpts{
Name: "private-port",
AdminStateUp: &asu,
NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7",
FixedIPs: []IP{
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
},
SecurityGroups: []string{"foo"},
}
n, err := Create(fake.ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "DOWN")
th.AssertEquals(t, n.Name, "private-port")
th.AssertEquals(t, n.AdminStateUp, true)
th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
th.AssertEquals(t, n.DeviceOwner, "")
th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0")
th.AssertDeepEquals(t, n.FixedIPs, []IP{
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
})
th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
}
func TestRequiredCreateOpts(t *testing.T) {
res := Create(fake.ServiceClient(), CreateOpts{})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"port": {
"name": "new_port_name",
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.3"
}
],
"security_groups": [
"f0ac4394-7e4a-4409-9701-ba8be283dbc3"
]
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"port": {
"status": "DOWN",
"name": "new_port_name",
"admin_state_up": true,
"network_id": "a87cc70a-3e15-4acf-8205-9b711a3531b7",
"tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
"device_owner": "",
"mac_address": "fa:16:3e:c9:cb:f0",
"fixed_ips": [
{
"subnet_id": "a0304c3a-4f08-4c43-88af-d796509c97d2",
"ip_address": "10.0.0.3"
}
],
"id": "65c0ee9f-d634-4522-8954-51021b570b0d",
"security_groups": [
"f0ac4394-7e4a-4409-9701-ba8be283dbc3"
],
"device_id": ""
}
}
`)
})
options := UpdateOpts{
Name: "new_port_name",
FixedIPs: []IP{
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
},
SecurityGroups: []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
}
s, err := Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "new_port_name")
th.AssertDeepEquals(t, s.FixedIPs, []IP{
IP{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
})
th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,44 +0,0 @@
package ports
import (
"testing"
"github.com/rackspace/gophercloud"
th "github.com/rackspace/gophercloud/testhelper"
)
const endpoint = "http://localhost:57909/"
func endpointClient() *gophercloud.ServiceClient {
return &gophercloud.ServiceClient{Endpoint: endpoint, ResourceBase: endpoint + "v2.0/"}
}
func TestListURL(t *testing.T) {
actual := listURL(endpointClient())
expected := endpoint + "v2.0/ports"
th.AssertEquals(t, expected, actual)
}
func TestGetURL(t *testing.T) {
actual := getURL(endpointClient(), "foo")
expected := endpoint + "v2.0/ports/foo"
th.AssertEquals(t, expected, actual)
}
func TestCreateURL(t *testing.T) {
actual := createURL(endpointClient())
expected := endpoint + "v2.0/ports"
th.AssertEquals(t, expected, actual)
}
func TestUpdateURL(t *testing.T) {
actual := updateURL(endpointClient(), "foo")
expected := endpoint + "v2.0/ports/foo"
th.AssertEquals(t, expected, actual)
}
func TestDeleteURL(t *testing.T) {
actual := deleteURL(endpointClient(), "foo")
expected := endpoint + "v2.0/ports/foo"
th.AssertEquals(t, expected, actual)
}

View File

@@ -1,362 +0,0 @@
package subnets
import (
"fmt"
"net/http"
"testing"
fake "github.com/rackspace/gophercloud/openstack/networking/v2/common"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"subnets": [
{
"name": "private-subnet",
"enable_dhcp": true,
"network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
"tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
"dns_nameservers": [],
"allocation_pools": [
{
"start": "10.0.0.2",
"end": "10.0.0.254"
}
],
"host_routes": [],
"ip_version": 4,
"gateway_ip": "10.0.0.1",
"cidr": "10.0.0.0/24",
"id": "08eae331-0402-425a-923c-34f7cfe39c1b"
},
{
"name": "my_subnet",
"enable_dhcp": true,
"network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"dns_nameservers": [],
"allocation_pools": [
{
"start": "192.0.0.2",
"end": "192.255.255.254"
}
],
"host_routes": [],
"ip_version": 4,
"gateway_ip": "192.0.0.1",
"cidr": "192.0.0.0/8",
"id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
}
]
}
`)
})
count := 0
List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractSubnets(page)
if err != nil {
t.Errorf("Failed to extract subnets: %v", err)
return false, nil
}
expected := []Subnet{
Subnet{
Name: "private-subnet",
EnableDHCP: true,
NetworkID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
DNSNameservers: []string{},
AllocationPools: []AllocationPool{
AllocationPool{
Start: "10.0.0.2",
End: "10.0.0.254",
},
},
HostRoutes: []HostRoute{},
IPVersion: 4,
GatewayIP: "10.0.0.1",
CIDR: "10.0.0.0/24",
ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
},
Subnet{
Name: "my_subnet",
EnableDHCP: true,
NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
TenantID: "4fd44f30292945e481c7b8a0c8908869",
DNSNameservers: []string{},
AllocationPools: []AllocationPool{
AllocationPool{
Start: "192.0.0.2",
End: "192.255.255.254",
},
},
HostRoutes: []HostRoute{},
IPVersion: 4,
GatewayIP: "192.0.0.1",
CIDR: "192.0.0.0/8",
ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"subnet": {
"name": "my_subnet",
"enable_dhcp": true,
"network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"dns_nameservers": [],
"allocation_pools": [
{
"start": "192.0.0.2",
"end": "192.255.255.254"
}
],
"host_routes": [],
"ip_version": 4,
"gateway_ip": "192.0.0.1",
"cidr": "192.0.0.0/8",
"id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
}
}
`)
})
s, err := Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "my_subnet")
th.AssertEquals(t, s.EnableDHCP, true)
th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
th.AssertDeepEquals(t, s.DNSNameservers, []string{})
th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
AllocationPool{
Start: "192.0.0.2",
End: "192.255.255.254",
},
})
th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
th.AssertEquals(t, s.IPVersion, 4)
th.AssertEquals(t, s.GatewayIP, "192.0.0.1")
th.AssertEquals(t, s.CIDR, "192.0.0.0/8")
th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"subnet": {
"network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"ip_version": 4,
"cidr": "192.168.199.0/24",
"dns_nameservers": ["foo"],
"allocation_pools": [
{
"start": "192.168.199.2",
"end": "192.168.199.254"
}
],
"host_routes": [{"destination":"","nexthop": "bar"}]
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"subnet": {
"name": "",
"enable_dhcp": true,
"network_id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"dns_nameservers": [],
"allocation_pools": [
{
"start": "192.168.199.2",
"end": "192.168.199.254"
}
],
"host_routes": [],
"ip_version": 4,
"gateway_ip": "192.168.199.1",
"cidr": "192.168.199.0/24",
"id": "3b80198d-4f7b-4f77-9ef5-774d54e17126"
}
}
`)
})
opts := CreateOpts{
NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
IPVersion: 4,
CIDR: "192.168.199.0/24",
AllocationPools: []AllocationPool{
AllocationPool{
Start: "192.168.199.2",
End: "192.168.199.254",
},
},
DNSNameservers: []string{"foo"},
HostRoutes: []HostRoute{
HostRoute{NextHop: "bar"},
},
}
s, err := Create(fake.ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "")
th.AssertEquals(t, s.EnableDHCP, true)
th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869")
th.AssertDeepEquals(t, s.DNSNameservers, []string{})
th.AssertDeepEquals(t, s.AllocationPools, []AllocationPool{
AllocationPool{
Start: "192.168.199.2",
End: "192.168.199.254",
},
})
th.AssertDeepEquals(t, s.HostRoutes, []HostRoute{})
th.AssertEquals(t, s.IPVersion, 4)
th.AssertEquals(t, s.GatewayIP, "192.168.199.1")
th.AssertEquals(t, s.CIDR, "192.168.199.0/24")
th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126")
}
func TestRequiredCreateOpts(t *testing.T) {
res := Create(fake.ServiceClient(), CreateOpts{})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo"})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
res = Create(fake.ServiceClient(), CreateOpts{NetworkID: "foo", CIDR: "bar", IPVersion: 40})
if res.Err == nil {
t.Fatalf("Expected error, got none")
}
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"subnet": {
"name": "my_new_subnet",
"dns_nameservers": ["foo"],
"host_routes": [{"destination":"","nexthop": "bar"}]
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `
{
"subnet": {
"name": "my_new_subnet",
"enable_dhcp": true,
"network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
"tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
"dns_nameservers": [],
"allocation_pools": [
{
"start": "10.0.0.2",
"end": "10.0.0.254"
}
],
"host_routes": [],
"ip_version": 4,
"gateway_ip": "10.0.0.1",
"cidr": "10.0.0.0/24",
"id": "08eae331-0402-425a-923c-34f7cfe39c1b"
}
}
`)
})
opts := UpdateOpts{
Name: "my_new_subnet",
DNSNameservers: []string{"foo"},
HostRoutes: []HostRoute{
HostRoute{NextHop: "bar"},
},
}
s, err := Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "my_new_subnet")
th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b")
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
res := Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
th.AssertNoErr(t, res.Err)
}

View File

@@ -1,44 +0,0 @@
package subnets
import (
"testing"
"github.com/rackspace/gophercloud"
th "github.com/rackspace/gophercloud/testhelper"
)
const endpoint = "http://localhost:57909/"
func endpointClient() *gophercloud.ServiceClient {
return &gophercloud.ServiceClient{Endpoint: endpoint, ResourceBase: endpoint + "v2.0/"}
}
func TestListURL(t *testing.T) {
actual := listURL(endpointClient())
expected := endpoint + "v2.0/subnets"
th.AssertEquals(t, expected, actual)
}
func TestGetURL(t *testing.T) {
actual := getURL(endpointClient(), "foo")
expected := endpoint + "v2.0/subnets/foo"
th.AssertEquals(t, expected, actual)
}
func TestCreateURL(t *testing.T) {
actual := createURL(endpointClient())
expected := endpoint + "v2.0/subnets"
th.AssertEquals(t, expected, actual)
}
func TestUpdateURL(t *testing.T) {
actual := updateURL(endpointClient(), "foo")
expected := endpoint + "v2.0/subnets/foo"
th.AssertEquals(t, expected, actual)
}
func TestDeleteURL(t *testing.T) {
actual := deleteURL(endpointClient(), "foo")
expected := endpoint + "v2.0/subnets/foo"
th.AssertEquals(t, expected, actual)
}