github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/routes/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
     9  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/routes"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func TestListRoutes(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/v2.0/vpc/routes", func(w http.ResponseWriter, r *http.Request) {
    18  		th.TestMethod(t, r, "GET")
    19  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    20  
    21  		w.Header().Add("Content-Type", "application/json")
    22  		w.WriteHeader(http.StatusOK)
    23  
    24  		fmt.Fprintf(w, `
    25  {
    26      "routes": [
    27          {
    28              "destination": "172.31.8.192/26",
    29              "tenant_id": "87a56a48977e42068f70ad3280c50f0e",
    30              "nexthop": "283aabd7-dab4-409d-96ff-6c878b9a0219",
    31              "vpc_id": "93e94d8e-31a6-4c22-bdf7-8b23c7b67329",
    32              "type": "peering",
    33              "id": "4ae95cd4-292d-4a27-b3de-1be835eb32e1"
    34          },
    35          {
    36              "destination": "172.31.8.128/26",
    37              "tenant_id": "87a56a48977e42068f70ad3280c50f0e",
    38              "nexthop": "283aabd7-dab4-409d-96ff-6c878b9a0219",
    39              "vpc_id": "93e94d8e-31a6-4c22-bdf7-8b23c7b67329",
    40              "type": "peering",
    41              "id": "804d5d09-cee2-418d-8d1b-be29b8e8e9e8"
    42          },
    43          {
    44              "destination": "172.31.8.112/28",
    45              "tenant_id": "87a56a48977e42068f70ad3280c50f0e",
    46              "nexthop": "283aabd7-dab4-409d-96ff-6c878b9a0219",
    47              "vpc_id": "93e94d8e-31a6-4c22-bdf7-8b23c7b67329",
    48              "type": "peering",
    49              "id": "9f54e4ac-e052-4198-bb73-51b22ad41035"
    50          }
    51      ]
    52  }
    53  			`)
    54  	})
    55  
    56  	pages, err := routes.List(fake.ServiceClient(), routes.ListOpts{}).AllPages()
    57  	if err != nil {
    58  		t.Errorf("Failed to get routes: %v", err)
    59  	}
    60  
    61  	actual, err := routes.ExtractRoutes(pages)
    62  	if err != nil {
    63  		t.Errorf("Failed to extract routes: %v", err)
    64  	}
    65  
    66  	expected := []routes.Route{
    67  		{
    68  			Type:        "peering",
    69  			NextHop:     "283aabd7-dab4-409d-96ff-6c878b9a0219",
    70  			Destination: "172.31.8.192/26",
    71  			VPC_ID:      "93e94d8e-31a6-4c22-bdf7-8b23c7b67329",
    72  			Tenant_Id:   "87a56a48977e42068f70ad3280c50f0e",
    73  			RouteID:     "4ae95cd4-292d-4a27-b3de-1be835eb32e1",
    74  		},
    75  		{
    76  			Type:        "peering",
    77  			NextHop:     "283aabd7-dab4-409d-96ff-6c878b9a0219",
    78  			Destination: "172.31.8.128/26",
    79  			VPC_ID:      "93e94d8e-31a6-4c22-bdf7-8b23c7b67329",
    80  			Tenant_Id:   "87a56a48977e42068f70ad3280c50f0e",
    81  			RouteID:     "804d5d09-cee2-418d-8d1b-be29b8e8e9e8",
    82  		},
    83  		{
    84  			Type:        "peering",
    85  			NextHop:     "283aabd7-dab4-409d-96ff-6c878b9a0219",
    86  			Destination: "172.31.8.112/28",
    87  			VPC_ID:      "93e94d8e-31a6-4c22-bdf7-8b23c7b67329",
    88  			Tenant_Id:   "87a56a48977e42068f70ad3280c50f0e",
    89  			RouteID:     "9f54e4ac-e052-4198-bb73-51b22ad41035",
    90  		},
    91  	}
    92  
    93  	th.AssertDeepEquals(t, expected, actual)
    94  }
    95  
    96  func TestGetRoutes(t *testing.T) {
    97  	th.SetupHTTP()
    98  	defer th.TeardownHTTP()
    99  
   100  	th.Mux.HandleFunc("/v2.0/vpc/routes/39a07dcb-f30e-41c1-97ac-182c8f0d43c1", func(w http.ResponseWriter, r *http.Request) {
   101  		th.TestMethod(t, r, "GET")
   102  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   103  
   104  		w.Header().Add("Content-Type", "application/json")
   105  		w.WriteHeader(http.StatusOK)
   106  
   107  		fmt.Fprintf(w, `
   108  {
   109      "route": {
   110          "destination": "192.168.0.0/16",
   111          "tenant_id": "87a56a48977e42068f70ad3280c50f0e",
   112          "nexthop": "d2dea4ba-e988-4e9c-8162-652e74b2560c",
   113          "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf",
   114          "type": "peering",
   115          "id": "39a07dcb-f30e-41c1-97ac-182c8f0d43c1"
   116      }
   117  }
   118  		`)
   119  	})
   120  
   121  	n, err := routes.Get(fake.ServiceClient(), "39a07dcb-f30e-41c1-97ac-182c8f0d43c1").Extract()
   122  	th.AssertNoErr(t, err)
   123  	th.AssertEquals(t, "39a07dcb-f30e-41c1-97ac-182c8f0d43c1", n.RouteID)
   124  	th.AssertEquals(t, "192.168.0.0/16", n.Destination)
   125  	th.AssertEquals(t, "87a56a48977e42068f70ad3280c50f0e", n.Tenant_Id)
   126  	th.AssertEquals(t, "d2dea4ba-e988-4e9c-8162-652e74b2560c", n.NextHop)
   127  	th.AssertEquals(t, "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", n.VPC_ID)
   128  	th.AssertEquals(t, "peering", n.Type)
   129  }
   130  
   131  func TestCreateRoute(t *testing.T) {
   132  	th.SetupHTTP()
   133  	defer th.TeardownHTTP()
   134  
   135  	th.Mux.HandleFunc("/v2.0/vpc/routes", func(w http.ResponseWriter, r *http.Request) {
   136  		th.TestMethod(t, r, "POST")
   137  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   138  		th.TestHeader(t, r, "Content-Type", "application/json")
   139  		th.TestHeader(t, r, "Accept", "application/json")
   140  		th.TestJSONRequest(t, r, `
   141  { 
   142      "route": { 
   143          "type": "peering",  
   144          "nexthop": "d2dea4ba-e988-4e9c-8162-652e74b2560c",  
   145          "destination": "192.168.0.0/16",  
   146          "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf"
   147      }
   148  }
   149  			`)
   150  
   151  		w.Header().Add("Content-Type", "application/json")
   152  		w.WriteHeader(http.StatusCreated)
   153  
   154  		fmt.Fprintf(w, `
   155  {
   156      "route": {
   157          "destination": "192.168.0.0/16",
   158          "tenant_id": "87a56a48977e42068f70ad3280c50f0e",
   159          "nexthop": "d2dea4ba-e988-4e9c-8162-652e74b2560c",
   160          "vpc_id": "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf",
   161          "type": "peering",
   162          "id": "39a07dcb-f30e-41c1-97ac-182c8f0d43c1"
   163      }
   164  }		`)
   165  	})
   166  
   167  	options := routes.CreateOpts{
   168  		Type:        "peering",
   169  		NextHop:     "d2dea4ba-e988-4e9c-8162-652e74b2560c",
   170  		Destination: "192.168.0.0/16",
   171  		VPC_ID:      "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf",
   172  	}
   173  	n, err := routes.Create(fake.ServiceClient(), options).Extract()
   174  	th.AssertNoErr(t, err)
   175  	th.AssertEquals(t, "peering", n.Type)
   176  	th.AssertEquals(t, "d2dea4ba-e988-4e9c-8162-652e74b2560c", n.NextHop)
   177  	th.AssertEquals(t, "192.168.0.0/16", n.Destination)
   178  	th.AssertEquals(t, "3127e30b-5f8e-42d1-a3cc-fdadf412c5bf", n.VPC_ID)
   179  	th.AssertEquals(t, "39a07dcb-f30e-41c1-97ac-182c8f0d43c1", n.RouteID)
   180  }
   181  
   182  func TestDeleteRoute(t *testing.T) {
   183  	th.SetupHTTP()
   184  	defer th.TeardownHTTP()
   185  
   186  	th.Mux.HandleFunc("/v2.0/vpc/routes/39a07dcb-f30e-41c1-97ac-182c8f0d43c1", func(w http.ResponseWriter, r *http.Request) {
   187  		th.TestMethod(t, r, "DELETE")
   188  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   189  		w.WriteHeader(http.StatusNoContent)
   190  	})
   191  
   192  	res := routes.Delete(fake.ServiceClient(), "39a07dcb-f30e-41c1-97ac-182c8f0d43c1")
   193  	th.AssertNoErr(t, res.Err)
   194  }