github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/layer3/extraroutes/testing/requests_test.go (about)

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