github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/layer3/extraroutes/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
     9  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/extraroutes"
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
    11  	th "github.com/gophercloud/gophercloud/testhelper"
    12  )
    13  
    14  func TestAddExtraRoutes(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  
    18  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c/add_extraroutes", func(w http.ResponseWriter, r *http.Request) {
    19  		th.TestMethod(t, r, "PUT")
    20  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    21  		th.TestHeader(t, r, "Content-Type", "application/json")
    22  		th.TestHeader(t, r, "Accept", "application/json")
    23  		th.TestJSONRequest(t, r, `
    24  {
    25      "router": {
    26          "routes": [
    27              { "destination" : "10.0.3.0/24", "nexthop" : "10.0.0.13" },
    28              { "destination" : "10.0.4.0/24", "nexthop" : "10.0.0.14" }
    29          ]
    30      }
    31  }
    32  			`)
    33  
    34  		w.Header().Add("Content-Type", "application/json")
    35  		w.WriteHeader(http.StatusOK)
    36  
    37  		fmt.Fprintf(w, `
    38  {
    39      "router": {
    40          "name": "name",
    41          "id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e",
    42          "routes": [
    43              { "destination" : "10.0.1.0/24", "nexthop" : "10.0.0.11" },
    44              { "destination" : "10.0.2.0/24", "nexthop" : "10.0.0.12" },
    45              { "destination" : "10.0.3.0/24", "nexthop" : "10.0.0.13" },
    46              { "destination" : "10.0.4.0/24", "nexthop" : "10.0.0.14" }
    47          ]
    48      }
    49  }
    50  		`)
    51  	})
    52  
    53  	r := []routers.Route{
    54  		{
    55  			DestinationCIDR: "10.0.3.0/24",
    56  			NextHop:         "10.0.0.13",
    57  		},
    58  		{
    59  			DestinationCIDR: "10.0.4.0/24",
    60  			NextHop:         "10.0.0.14",
    61  		},
    62  	}
    63  	options := extraroutes.Opts{Routes: &r}
    64  
    65  	n, err := extraroutes.Add(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
    66  	th.AssertNoErr(t, err)
    67  
    68  	th.AssertDeepEquals(t, n.Routes, []routers.Route{
    69  		{
    70  			DestinationCIDR: "10.0.1.0/24",
    71  			NextHop:         "10.0.0.11",
    72  		},
    73  		{
    74  			DestinationCIDR: "10.0.2.0/24",
    75  			NextHop:         "10.0.0.12",
    76  		},
    77  		{
    78  			DestinationCIDR: "10.0.3.0/24",
    79  			NextHop:         "10.0.0.13",
    80  		},
    81  		{
    82  			DestinationCIDR: "10.0.4.0/24",
    83  			NextHop:         "10.0.0.14",
    84  		},
    85  	})
    86  }
    87  
    88  func TestRemoveExtraRoutes(t *testing.T) {
    89  	th.SetupHTTP()
    90  	defer th.TeardownHTTP()
    91  
    92  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c/remove_extraroutes", func(w http.ResponseWriter, r *http.Request) {
    93  		th.TestMethod(t, r, "PUT")
    94  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    95  		th.TestHeader(t, r, "Content-Type", "application/json")
    96  		th.TestHeader(t, r, "Accept", "application/json")
    97  		th.TestJSONRequest(t, r, `
    98  {
    99      "router": {
   100          "routes": [
   101              { "destination" : "10.0.3.0/24", "nexthop" : "10.0.0.13" },
   102              { "destination" : "10.0.4.0/24", "nexthop" : "10.0.0.14" }
   103          ]
   104      }
   105  }
   106  			`)
   107  
   108  		w.Header().Add("Content-Type", "application/json")
   109  		w.WriteHeader(http.StatusOK)
   110  
   111  		fmt.Fprintf(w, `
   112  {
   113      "router": {
   114          "name": "name",
   115          "id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e",
   116          "routes": [
   117              { "destination" : "10.0.1.0/24", "nexthop" : "10.0.0.11" },
   118              { "destination" : "10.0.2.0/24", "nexthop" : "10.0.0.12" }
   119          ]
   120      }
   121  }
   122  		`)
   123  	})
   124  
   125  	r := []routers.Route{
   126  		{
   127  			DestinationCIDR: "10.0.3.0/24",
   128  			NextHop:         "10.0.0.13",
   129  		},
   130  		{
   131  			DestinationCIDR: "10.0.4.0/24",
   132  			NextHop:         "10.0.0.14",
   133  		},
   134  	}
   135  	options := extraroutes.Opts{Routes: &r}
   136  
   137  	n, err := extraroutes.Remove(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
   138  	th.AssertNoErr(t, err)
   139  
   140  	th.AssertDeepEquals(t, n.Routes, []routers.Route{
   141  		{
   142  			DestinationCIDR: "10.0.1.0/24",
   143  			NextHop:         "10.0.0.11",
   144  		},
   145  		{
   146  			DestinationCIDR: "10.0.2.0/24",
   147  			NextHop:         "10.0.0.12",
   148  		},
   149  	})
   150  }