github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/layer3/routers/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/extensions/layer3/routers"
    10  	"github.com/huaweicloud/golangsdk/pagination"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  )
    13  
    14  func TestList(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  
    18  	th.Mux.HandleFunc("/v2.0/routers", func(w http.ResponseWriter, r *http.Request) {
    19  		th.TestMethod(t, r, "GET")
    20  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    21  
    22  		w.Header().Add("Content-Type", "application/json")
    23  		w.WriteHeader(http.StatusOK)
    24  
    25  		fmt.Fprintf(w, `
    26  {
    27      "routers": [
    28          {
    29              "status": "ACTIVE",
    30              "external_gateway_info": null,
    31              "name": "second_routers",
    32              "admin_state_up": true,
    33              "tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
    34              "distributed": false,
    35              "id": "7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b"
    36          },
    37          {
    38              "status": "ACTIVE",
    39              "external_gateway_info": {
    40                  "network_id": "3c5bcddd-6af9-4e6b-9c3e-c153e521cab8"
    41              },
    42              "name": "router1",
    43              "admin_state_up": true,
    44              "tenant_id": "33a40233088643acb66ff6eb0ebea679",
    45              "distributed": false,
    46              "id": "a9254bdb-2613-4a13-ac4c-adc581fba50d"
    47          },
    48          {
    49              "status": "ACTIVE",
    50              "external_gateway_info": {
    51                  "network_id": "2b37576e-b050-4891-8b20-e1e37a93942a",
    52                  "external_fixed_ips": [
    53                      {"ip_address": "192.0.2.17", "subnet_id": "ab561bc4-1a8e-48f2-9fbd-376fcb1a1def"},
    54                      {"ip_address": "198.51.100.33", "subnet_id": "1d699529-bdfd-43f8-bcaa-bff00c547af2"}
    55                  ]
    56              },
    57              "name": "gateway",
    58              "admin_state_up": true,
    59              "tenant_id": "a3e881e0a6534880c5473d95b9442099",
    60              "distributed": false,
    61              "id": "308a035c-005d-4452-a9fe-6f8f2f0c28d8"
    62          }
    63      ]
    64  }
    65  			`)
    66  	})
    67  
    68  	count := 0
    69  
    70  	routers.List(fake.ServiceClient(), routers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    71  		count++
    72  		actual, err := routers.ExtractRouters(page)
    73  		if err != nil {
    74  			t.Errorf("Failed to extract routers: %v", err)
    75  			return false, err
    76  		}
    77  
    78  		expected := []routers.Router{
    79  			{
    80  				Status:       "ACTIVE",
    81  				GatewayInfo:  routers.GatewayInfo{NetworkID: ""},
    82  				AdminStateUp: true,
    83  				Distributed:  false,
    84  				Name:         "second_routers",
    85  				ID:           "7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b",
    86  				TenantID:     "6b96ff0cb17a4b859e1e575d221683d3",
    87  			},
    88  			{
    89  				Status:       "ACTIVE",
    90  				GatewayInfo:  routers.GatewayInfo{NetworkID: "3c5bcddd-6af9-4e6b-9c3e-c153e521cab8"},
    91  				AdminStateUp: true,
    92  				Distributed:  false,
    93  				Name:         "router1",
    94  				ID:           "a9254bdb-2613-4a13-ac4c-adc581fba50d",
    95  				TenantID:     "33a40233088643acb66ff6eb0ebea679",
    96  			},
    97  			{
    98  				Status: "ACTIVE",
    99  				GatewayInfo: routers.GatewayInfo{
   100  					NetworkID: "2b37576e-b050-4891-8b20-e1e37a93942a",
   101  					ExternalFixedIPs: []routers.ExternalFixedIP{
   102  						{IPAddress: "192.0.2.17", SubnetID: "ab561bc4-1a8e-48f2-9fbd-376fcb1a1def"},
   103  						{IPAddress: "198.51.100.33", SubnetID: "1d699529-bdfd-43f8-bcaa-bff00c547af2"},
   104  					},
   105  				},
   106  				AdminStateUp: true,
   107  				Distributed:  false,
   108  				Name:         "gateway",
   109  				ID:           "308a035c-005d-4452-a9fe-6f8f2f0c28d8",
   110  				TenantID:     "a3e881e0a6534880c5473d95b9442099",
   111  			},
   112  		}
   113  
   114  		th.CheckDeepEquals(t, expected, actual)
   115  
   116  		return true, nil
   117  	})
   118  
   119  	if count != 1 {
   120  		t.Errorf("Expected 1 page, got %d", count)
   121  	}
   122  }
   123  
   124  func TestCreate(t *testing.T) {
   125  	th.SetupHTTP()
   126  	defer th.TeardownHTTP()
   127  
   128  	th.Mux.HandleFunc("/v2.0/routers", func(w http.ResponseWriter, r *http.Request) {
   129  		th.TestMethod(t, r, "POST")
   130  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   131  		th.TestHeader(t, r, "Content-Type", "application/json")
   132  		th.TestHeader(t, r, "Accept", "application/json")
   133  		th.TestJSONRequest(t, r, `
   134  {
   135     "router":{
   136        "name": "foo_router",
   137        "admin_state_up": false,
   138        "external_gateway_info":{
   139           "enable_snat": false,
   140           "network_id":"8ca37218-28ff-41cb-9b10-039601ea7e6b"
   141  	  },
   142  	  "availability_zone_hints": ["zone1", "zone2"]
   143     }
   144  }
   145  			`)
   146  
   147  		w.Header().Add("Content-Type", "application/json")
   148  		w.WriteHeader(http.StatusCreated)
   149  
   150  		fmt.Fprintf(w, `
   151  {
   152      "router": {
   153          "status": "ACTIVE",
   154          "external_gateway_info": {
   155              "network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b",
   156              "enable_snat": false,
   157              "external_fixed_ips": [
   158                  {"ip_address": "192.0.2.17", "subnet_id": "ab561bc4-1a8e-48f2-9fbd-376fcb1a1def"}
   159              ]
   160          },
   161          "name": "foo_router",
   162          "admin_state_up": false,
   163          "tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
   164  		"distributed": false,
   165  		"availability_zone_hints": ["zone1", "zone2"],
   166          "id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e"
   167      }
   168  }
   169  		`)
   170  	})
   171  
   172  	asu := false
   173  	enableSNAT := false
   174  	gwi := routers.GatewayInfo{
   175  		NetworkID:  "8ca37218-28ff-41cb-9b10-039601ea7e6b",
   176  		EnableSNAT: &enableSNAT,
   177  	}
   178  	options := routers.CreateOpts{
   179  		Name:                  "foo_router",
   180  		AdminStateUp:          &asu,
   181  		GatewayInfo:           &gwi,
   182  		AvailabilityZoneHints: []string{"zone1", "zone2"},
   183  	}
   184  	r, err := routers.Create(fake.ServiceClient(), options).Extract()
   185  	th.AssertNoErr(t, err)
   186  
   187  	gwi.ExternalFixedIPs = []routers.ExternalFixedIP{{
   188  		IPAddress: "192.0.2.17",
   189  		SubnetID:  "ab561bc4-1a8e-48f2-9fbd-376fcb1a1def",
   190  	}}
   191  
   192  	th.AssertEquals(t, "foo_router", r.Name)
   193  	th.AssertEquals(t, false, r.AdminStateUp)
   194  	th.AssertDeepEquals(t, gwi, r.GatewayInfo)
   195  	th.AssertDeepEquals(t, []string{"zone1", "zone2"}, r.AvailabilityZoneHints)
   196  }
   197  
   198  func TestGet(t *testing.T) {
   199  	th.SetupHTTP()
   200  	defer th.TeardownHTTP()
   201  
   202  	th.Mux.HandleFunc("/v2.0/routers/a07eea83-7710-4860-931b-5fe220fae533", func(w http.ResponseWriter, r *http.Request) {
   203  		th.TestMethod(t, r, "GET")
   204  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   205  
   206  		w.Header().Add("Content-Type", "application/json")
   207  		w.WriteHeader(http.StatusOK)
   208  
   209  		fmt.Fprintf(w, `
   210  {
   211      "router": {
   212          "status": "ACTIVE",
   213          "external_gateway_info": {
   214              "network_id": "85d76829-6415-48ff-9c63-5c5ca8c61ac6",
   215              "external_fixed_ips": [
   216                  {"ip_address": "198.51.100.33", "subnet_id": "1d699529-bdfd-43f8-bcaa-bff00c547af2"}
   217              ]
   218          },
   219          "routes": [
   220              {
   221                  "nexthop": "10.1.0.10",
   222                  "destination": "40.0.1.0/24"
   223              }
   224          ],
   225          "name": "router1",
   226          "admin_state_up": true,
   227          "tenant_id": "d6554fe62e2f41efbb6e026fad5c1542",
   228  		"distributed": false,
   229  		"availability_zone_hints": ["zone1", "zone2"],
   230          "id": "a07eea83-7710-4860-931b-5fe220fae533"
   231      }
   232  }
   233  			`)
   234  	})
   235  
   236  	n, err := routers.Get(fake.ServiceClient(), "a07eea83-7710-4860-931b-5fe220fae533").Extract()
   237  	th.AssertNoErr(t, err)
   238  
   239  	th.AssertEquals(t, n.Status, "ACTIVE")
   240  	th.AssertDeepEquals(t, n.GatewayInfo, routers.GatewayInfo{
   241  		NetworkID: "85d76829-6415-48ff-9c63-5c5ca8c61ac6",
   242  		ExternalFixedIPs: []routers.ExternalFixedIP{
   243  			{IPAddress: "198.51.100.33", SubnetID: "1d699529-bdfd-43f8-bcaa-bff00c547af2"},
   244  		},
   245  	})
   246  	th.AssertEquals(t, n.Name, "router1")
   247  	th.AssertEquals(t, n.AdminStateUp, true)
   248  	th.AssertEquals(t, n.TenantID, "d6554fe62e2f41efbb6e026fad5c1542")
   249  	th.AssertEquals(t, n.ID, "a07eea83-7710-4860-931b-5fe220fae533")
   250  	th.AssertDeepEquals(t, n.Routes, []routers.Route{{DestinationCIDR: "40.0.1.0/24", NextHop: "10.1.0.10"}})
   251  	th.AssertDeepEquals(t, n.AvailabilityZoneHints, []string{"zone1", "zone2"})
   252  }
   253  
   254  func TestUpdate(t *testing.T) {
   255  	th.SetupHTTP()
   256  	defer th.TeardownHTTP()
   257  
   258  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   259  		th.TestMethod(t, r, "PUT")
   260  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   261  		th.TestHeader(t, r, "Content-Type", "application/json")
   262  		th.TestHeader(t, r, "Accept", "application/json")
   263  		th.TestJSONRequest(t, r, `
   264  {
   265      "router": {
   266  			"name": "new_name",
   267          "external_gateway_info": {
   268              "network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b"
   269  		},
   270          "routes": [
   271              {
   272                  "nexthop": "10.1.0.10",
   273                  "destination": "40.0.1.0/24"
   274              }
   275          ]
   276      }
   277  }
   278  			`)
   279  
   280  		w.Header().Add("Content-Type", "application/json")
   281  		w.WriteHeader(http.StatusOK)
   282  
   283  		fmt.Fprintf(w, `
   284  {
   285      "router": {
   286          "status": "ACTIVE",
   287          "external_gateway_info": {
   288              "network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b",
   289              "external_fixed_ips": [
   290                  {"ip_address": "192.0.2.17", "subnet_id": "ab561bc4-1a8e-48f2-9fbd-376fcb1a1def"}
   291              ]
   292          },
   293          "name": "new_name",
   294          "admin_state_up": true,
   295          "tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
   296          "distributed": false,
   297          "id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e",
   298          "routes": [
   299              {
   300                  "nexthop": "10.1.0.10",
   301                  "destination": "40.0.1.0/24"
   302              }
   303          ]
   304      }
   305  }
   306  		`)
   307  	})
   308  
   309  	gwi := routers.GatewayInfo{NetworkID: "8ca37218-28ff-41cb-9b10-039601ea7e6b"}
   310  	r := []routers.Route{{DestinationCIDR: "40.0.1.0/24", NextHop: "10.1.0.10"}}
   311  	options := routers.UpdateOpts{Name: "new_name", GatewayInfo: &gwi, Routes: r}
   312  
   313  	n, err := routers.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
   314  	th.AssertNoErr(t, err)
   315  
   316  	gwi.ExternalFixedIPs = []routers.ExternalFixedIP{
   317  		{IPAddress: "192.0.2.17", SubnetID: "ab561bc4-1a8e-48f2-9fbd-376fcb1a1def"},
   318  	}
   319  
   320  	th.AssertEquals(t, n.Name, "new_name")
   321  	th.AssertDeepEquals(t, n.GatewayInfo, gwi)
   322  	th.AssertDeepEquals(t, n.Routes, []routers.Route{{DestinationCIDR: "40.0.1.0/24", NextHop: "10.1.0.10"}})
   323  }
   324  
   325  func TestAllRoutesRemoved(t *testing.T) {
   326  	th.SetupHTTP()
   327  	defer th.TeardownHTTP()
   328  
   329  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   330  		th.TestMethod(t, r, "PUT")
   331  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   332  		th.TestHeader(t, r, "Content-Type", "application/json")
   333  		th.TestHeader(t, r, "Accept", "application/json")
   334  		th.TestJSONRequest(t, r, `
   335  {
   336      "router": {
   337          "routes": []
   338      }
   339  }
   340  			`)
   341  
   342  		w.Header().Add("Content-Type", "application/json")
   343  		w.WriteHeader(http.StatusOK)
   344  
   345  		fmt.Fprintf(w, `
   346  {
   347      "router": {
   348          "status": "ACTIVE",
   349          "external_gateway_info": {
   350              "network_id": "8ca37218-28ff-41cb-9b10-039601ea7e6b"
   351          },
   352          "name": "name",
   353          "admin_state_up": true,
   354          "tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
   355          "distributed": false,
   356          "id": "8604a0de-7f6b-409a-a47c-a1cc7bc77b2e",
   357          "routes": []
   358      }
   359  }
   360  		`)
   361  	})
   362  
   363  	r := []routers.Route{}
   364  	options := routers.UpdateOpts{Routes: r}
   365  
   366  	n, err := routers.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
   367  	th.AssertNoErr(t, err)
   368  
   369  	th.AssertDeepEquals(t, n.Routes, []routers.Route{})
   370  }
   371  
   372  func TestDelete(t *testing.T) {
   373  	th.SetupHTTP()
   374  	defer th.TeardownHTTP()
   375  
   376  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   377  		th.TestMethod(t, r, "DELETE")
   378  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   379  		w.WriteHeader(http.StatusNoContent)
   380  	})
   381  
   382  	res := routers.Delete(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
   383  	th.AssertNoErr(t, res.Err)
   384  }
   385  
   386  func TestAddInterface(t *testing.T) {
   387  	th.SetupHTTP()
   388  	defer th.TeardownHTTP()
   389  
   390  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c/add_router_interface", func(w http.ResponseWriter, r *http.Request) {
   391  		th.TestMethod(t, r, "PUT")
   392  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   393  		th.TestHeader(t, r, "Content-Type", "application/json")
   394  		th.TestHeader(t, r, "Accept", "application/json")
   395  		th.TestJSONRequest(t, r, `
   396  {
   397      "subnet_id": "a2f1f29d-571b-4533-907f-5803ab96ead1"
   398  }
   399  	`)
   400  
   401  		w.Header().Add("Content-Type", "application/json")
   402  		w.WriteHeader(http.StatusOK)
   403  
   404  		fmt.Fprintf(w, `
   405  {
   406      "subnet_id": "0d32a837-8069-4ec3-84c4-3eef3e10b188",
   407      "tenant_id": "017d8de156df4177889f31a9bd6edc00",
   408      "port_id": "3f990102-4485-4df1-97a0-2c35bdb85b31",
   409      "id": "9a83fa11-8da5-436e-9afe-3d3ac5ce7770"
   410  }
   411  `)
   412  	})
   413  
   414  	opts := routers.AddInterfaceOpts{SubnetID: "a2f1f29d-571b-4533-907f-5803ab96ead1"}
   415  	res, err := routers.AddInterface(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", opts).Extract()
   416  	th.AssertNoErr(t, err)
   417  
   418  	th.AssertEquals(t, "0d32a837-8069-4ec3-84c4-3eef3e10b188", res.SubnetID)
   419  	th.AssertEquals(t, "017d8de156df4177889f31a9bd6edc00", res.TenantID)
   420  	th.AssertEquals(t, "3f990102-4485-4df1-97a0-2c35bdb85b31", res.PortID)
   421  	th.AssertEquals(t, "9a83fa11-8da5-436e-9afe-3d3ac5ce7770", res.ID)
   422  }
   423  
   424  func TestAddInterfaceRequiredOpts(t *testing.T) {
   425  	_, err := routers.AddInterface(fake.ServiceClient(), "foo", routers.AddInterfaceOpts{}).Extract()
   426  	if err == nil {
   427  		t.Fatalf("Expected error, got none")
   428  	}
   429  	_, err = routers.AddInterface(fake.ServiceClient(), "foo", routers.AddInterfaceOpts{SubnetID: "bar", PortID: "baz"}).Extract()
   430  	if err == nil {
   431  		t.Fatalf("Expected error, got none")
   432  	}
   433  }
   434  
   435  func TestRemoveInterface(t *testing.T) {
   436  	th.SetupHTTP()
   437  	defer th.TeardownHTTP()
   438  
   439  	th.Mux.HandleFunc("/v2.0/routers/4e8e5957-649f-477b-9e5b-f1f75b21c03c/remove_router_interface", func(w http.ResponseWriter, r *http.Request) {
   440  		th.TestMethod(t, r, "PUT")
   441  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   442  		th.TestHeader(t, r, "Content-Type", "application/json")
   443  		th.TestHeader(t, r, "Accept", "application/json")
   444  		th.TestJSONRequest(t, r, `
   445  {
   446  		"subnet_id": "a2f1f29d-571b-4533-907f-5803ab96ead1"
   447  }
   448  	`)
   449  
   450  		w.Header().Add("Content-Type", "application/json")
   451  		w.WriteHeader(http.StatusOK)
   452  
   453  		fmt.Fprintf(w, `
   454  {
   455  		"subnet_id": "0d32a837-8069-4ec3-84c4-3eef3e10b188",
   456  		"tenant_id": "017d8de156df4177889f31a9bd6edc00",
   457  		"port_id": "3f990102-4485-4df1-97a0-2c35bdb85b31",
   458  		"id": "9a83fa11-8da5-436e-9afe-3d3ac5ce7770"
   459  }
   460  `)
   461  	})
   462  
   463  	opts := routers.RemoveInterfaceOpts{SubnetID: "a2f1f29d-571b-4533-907f-5803ab96ead1"}
   464  	res, err := routers.RemoveInterface(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", opts).Extract()
   465  	th.AssertNoErr(t, err)
   466  
   467  	th.AssertEquals(t, "0d32a837-8069-4ec3-84c4-3eef3e10b188", res.SubnetID)
   468  	th.AssertEquals(t, "017d8de156df4177889f31a9bd6edc00", res.TenantID)
   469  	th.AssertEquals(t, "3f990102-4485-4df1-97a0-2c35bdb85b31", res.PortID)
   470  	th.AssertEquals(t, "9a83fa11-8da5-436e-9afe-3d3ac5ce7770", res.ID)
   471  }