github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/ports/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/extradhcpopts"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/portsecurity"
    11  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/ports"
    12  	"github.com/huaweicloud/golangsdk/pagination"
    13  	th "github.com/huaweicloud/golangsdk/testhelper"
    14  )
    15  
    16  func TestList(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  
    20  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
    21  		th.TestMethod(t, r, "GET")
    22  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  
    24  		w.Header().Add("Content-Type", "application/json")
    25  		w.WriteHeader(http.StatusOK)
    26  
    27  		fmt.Fprintf(w, ListResponse)
    28  	})
    29  
    30  	count := 0
    31  
    32  	ports.List(fake.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    33  		count++
    34  		actual, err := ports.ExtractPorts(page)
    35  		if err != nil {
    36  			t.Errorf("Failed to extract subnets: %v", err)
    37  			return false, nil
    38  		}
    39  
    40  		expected := []ports.Port{
    41  			{
    42  				Status:       "ACTIVE",
    43  				Name:         "",
    44  				AdminStateUp: true,
    45  				NetworkID:    "70c1db1f-b701-45bd-96e0-a313ee3430b3",
    46  				TenantID:     "",
    47  				DeviceOwner:  "network:router_gateway",
    48  				MACAddress:   "fa:16:3e:58:42:ed",
    49  				FixedIPs: []ports.IP{
    50  					{
    51  						SubnetID:  "008ba151-0b8c-4a67-98b5-0d2b87666062",
    52  						IPAddress: "172.24.4.2",
    53  					},
    54  				},
    55  				ID:             "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
    56  				SecurityGroups: []string{},
    57  				DeviceID:       "9ae135f4-b6e0-4dad-9e91-3c223e385824",
    58  			},
    59  		}
    60  
    61  		th.CheckDeepEquals(t, expected, actual)
    62  
    63  		return true, nil
    64  	})
    65  
    66  	if count != 1 {
    67  		t.Errorf("Expected 1 page, got %d", count)
    68  	}
    69  }
    70  
    71  func TestListWithExtensions(t *testing.T) {
    72  	th.SetupHTTP()
    73  	defer th.TeardownHTTP()
    74  
    75  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
    76  		th.TestMethod(t, r, "GET")
    77  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    78  
    79  		w.Header().Add("Content-Type", "application/json")
    80  		w.WriteHeader(http.StatusOK)
    81  
    82  		fmt.Fprintf(w, ListResponse)
    83  	})
    84  
    85  	type portWithExt struct {
    86  		ports.Port
    87  		portsecurity.PortSecurityExt
    88  	}
    89  
    90  	var allPorts []portWithExt
    91  
    92  	allPages, err := ports.List(fake.ServiceClient(), ports.ListOpts{}).AllPages()
    93  	th.AssertNoErr(t, err)
    94  
    95  	err = ports.ExtractPortsInto(allPages, &allPorts)
    96  
    97  	th.AssertEquals(t, allPorts[0].Status, "ACTIVE")
    98  	th.AssertEquals(t, allPorts[0].PortSecurityEnabled, false)
    99  }
   100  
   101  func TestGet(t *testing.T) {
   102  	th.SetupHTTP()
   103  	defer th.TeardownHTTP()
   104  
   105  	th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
   106  		th.TestMethod(t, r, "GET")
   107  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   108  
   109  		w.Header().Add("Content-Type", "application/json")
   110  		w.WriteHeader(http.StatusOK)
   111  
   112  		fmt.Fprintf(w, GetResponse)
   113  	})
   114  
   115  	n, err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").Extract()
   116  	th.AssertNoErr(t, err)
   117  
   118  	th.AssertEquals(t, n.Status, "ACTIVE")
   119  	th.AssertEquals(t, n.Name, "")
   120  	th.AssertEquals(t, n.AdminStateUp, true)
   121  	th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   122  	th.AssertEquals(t, n.TenantID, "7e02058126cc4950b75f9970368ba177")
   123  	th.AssertEquals(t, n.DeviceOwner, "network:router_interface")
   124  	th.AssertEquals(t, n.MACAddress, "fa:16:3e:23:fd:d7")
   125  	th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{
   126  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"},
   127  	})
   128  	th.AssertEquals(t, n.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2")
   129  	th.AssertDeepEquals(t, n.SecurityGroups, []string{})
   130  	th.AssertEquals(t, n.Status, "ACTIVE")
   131  	th.AssertEquals(t, n.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e")
   132  }
   133  
   134  func TestGetWithExtensions(t *testing.T) {
   135  	th.SetupHTTP()
   136  	defer th.TeardownHTTP()
   137  
   138  	th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
   139  		th.TestMethod(t, r, "GET")
   140  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   141  
   142  		w.Header().Add("Content-Type", "application/json")
   143  		w.WriteHeader(http.StatusOK)
   144  
   145  		fmt.Fprintf(w, GetResponse)
   146  	})
   147  
   148  	var portWithExtensions struct {
   149  		ports.Port
   150  		portsecurity.PortSecurityExt
   151  	}
   152  
   153  	err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").ExtractInto(&portWithExtensions)
   154  	th.AssertNoErr(t, err)
   155  
   156  	th.AssertEquals(t, portWithExtensions.Status, "ACTIVE")
   157  	th.AssertEquals(t, portWithExtensions.PortSecurityEnabled, false)
   158  }
   159  
   160  func TestCreate(t *testing.T) {
   161  	th.SetupHTTP()
   162  	defer th.TeardownHTTP()
   163  
   164  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
   165  		th.TestMethod(t, r, "POST")
   166  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   167  		th.TestHeader(t, r, "Content-Type", "application/json")
   168  		th.TestHeader(t, r, "Accept", "application/json")
   169  		th.TestJSONRequest(t, r, CreateRequest)
   170  
   171  		w.Header().Add("Content-Type", "application/json")
   172  		w.WriteHeader(http.StatusCreated)
   173  
   174  		fmt.Fprintf(w, CreateResponse)
   175  	})
   176  
   177  	asu := true
   178  	options := ports.CreateOpts{
   179  		Name:         "private-port",
   180  		AdminStateUp: &asu,
   181  		NetworkID:    "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   182  		FixedIPs: []ports.IP{
   183  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   184  		},
   185  		SecurityGroups: &[]string{"foo"},
   186  		AllowedAddressPairs: []ports.AddressPair{
   187  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   188  		},
   189  	}
   190  	n, err := ports.Create(fake.ServiceClient(), options).Extract()
   191  	th.AssertNoErr(t, err)
   192  
   193  	th.AssertEquals(t, n.Status, "DOWN")
   194  	th.AssertEquals(t, n.Name, "private-port")
   195  	th.AssertEquals(t, n.AdminStateUp, true)
   196  	th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   197  	th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   198  	th.AssertEquals(t, n.DeviceOwner, "")
   199  	th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0")
   200  	th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{
   201  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   202  	})
   203  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   204  	th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   205  	th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{
   206  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   207  	})
   208  }
   209  
   210  func TestCreateOmitSecurityGroups(t *testing.T) {
   211  	th.SetupHTTP()
   212  	defer th.TeardownHTTP()
   213  
   214  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
   215  		th.TestMethod(t, r, "POST")
   216  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   217  		th.TestHeader(t, r, "Content-Type", "application/json")
   218  		th.TestHeader(t, r, "Accept", "application/json")
   219  		th.TestJSONRequest(t, r, CreateOmitSecurityGroupsRequest)
   220  
   221  		w.Header().Add("Content-Type", "application/json")
   222  		w.WriteHeader(http.StatusCreated)
   223  
   224  		fmt.Fprintf(w, CreateOmitSecurityGroupsResponse)
   225  	})
   226  
   227  	asu := true
   228  	options := ports.CreateOpts{
   229  		Name:         "private-port",
   230  		AdminStateUp: &asu,
   231  		NetworkID:    "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   232  		FixedIPs: []ports.IP{
   233  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   234  		},
   235  		AllowedAddressPairs: []ports.AddressPair{
   236  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   237  		},
   238  	}
   239  	n, err := ports.Create(fake.ServiceClient(), options).Extract()
   240  	th.AssertNoErr(t, err)
   241  
   242  	th.AssertEquals(t, n.Status, "DOWN")
   243  	th.AssertEquals(t, n.Name, "private-port")
   244  	th.AssertEquals(t, n.AdminStateUp, true)
   245  	th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   246  	th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   247  	th.AssertEquals(t, n.DeviceOwner, "")
   248  	th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0")
   249  	th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{
   250  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   251  	})
   252  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   253  	th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   254  	th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{
   255  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   256  	})
   257  }
   258  
   259  func TestCreateWithNoSecurityGroup(t *testing.T) {
   260  	th.SetupHTTP()
   261  	defer th.TeardownHTTP()
   262  
   263  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
   264  		th.TestMethod(t, r, "POST")
   265  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   266  		th.TestHeader(t, r, "Content-Type", "application/json")
   267  		th.TestHeader(t, r, "Accept", "application/json")
   268  		th.TestJSONRequest(t, r, CreateWithNoSecurityGroupsRequest)
   269  
   270  		w.Header().Add("Content-Type", "application/json")
   271  		w.WriteHeader(http.StatusCreated)
   272  
   273  		fmt.Fprintf(w, CreateWithNoSecurityGroupsResponse)
   274  	})
   275  
   276  	asu := true
   277  	options := ports.CreateOpts{
   278  		Name:         "private-port",
   279  		AdminStateUp: &asu,
   280  		NetworkID:    "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   281  		FixedIPs: []ports.IP{
   282  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   283  		},
   284  		SecurityGroups: &[]string{},
   285  		AllowedAddressPairs: []ports.AddressPair{
   286  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   287  		},
   288  	}
   289  	n, err := ports.Create(fake.ServiceClient(), options).Extract()
   290  	th.AssertNoErr(t, err)
   291  
   292  	th.AssertEquals(t, n.Status, "DOWN")
   293  	th.AssertEquals(t, n.Name, "private-port")
   294  	th.AssertEquals(t, n.AdminStateUp, true)
   295  	th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   296  	th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   297  	th.AssertEquals(t, n.DeviceOwner, "")
   298  	th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0")
   299  	th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{
   300  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   301  	})
   302  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   303  	th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{
   304  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   305  	})
   306  }
   307  
   308  func TestRequiredCreateOpts(t *testing.T) {
   309  	res := ports.Create(fake.ServiceClient(), ports.CreateOpts{})
   310  	if res.Err == nil {
   311  		t.Fatalf("Expected error, got none")
   312  	}
   313  }
   314  
   315  func TestCreatePortSecurity(t *testing.T) {
   316  	th.SetupHTTP()
   317  	defer th.TeardownHTTP()
   318  
   319  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
   320  		th.TestMethod(t, r, "POST")
   321  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   322  		th.TestHeader(t, r, "Content-Type", "application/json")
   323  		th.TestHeader(t, r, "Accept", "application/json")
   324  		th.TestJSONRequest(t, r, CreatePortSecurityRequest)
   325  
   326  		w.Header().Add("Content-Type", "application/json")
   327  		w.WriteHeader(http.StatusCreated)
   328  
   329  		fmt.Fprintf(w, CreatePortSecurityResponse)
   330  	})
   331  
   332  	var portWithExt struct {
   333  		ports.Port
   334  		portsecurity.PortSecurityExt
   335  	}
   336  
   337  	asu := true
   338  	iFalse := false
   339  	portCreateOpts := ports.CreateOpts{
   340  		Name:         "private-port",
   341  		AdminStateUp: &asu,
   342  		NetworkID:    "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   343  		FixedIPs: []ports.IP{
   344  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   345  		},
   346  		SecurityGroups: &[]string{"foo"},
   347  		AllowedAddressPairs: []ports.AddressPair{
   348  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   349  		},
   350  	}
   351  	createOpts := portsecurity.PortCreateOptsExt{
   352  		CreateOptsBuilder:   portCreateOpts,
   353  		PortSecurityEnabled: &iFalse,
   354  	}
   355  
   356  	err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&portWithExt)
   357  	th.AssertNoErr(t, err)
   358  
   359  	th.AssertEquals(t, portWithExt.Status, "DOWN")
   360  	th.AssertEquals(t, portWithExt.PortSecurityEnabled, false)
   361  }
   362  
   363  func TestUpdate(t *testing.T) {
   364  	th.SetupHTTP()
   365  	defer th.TeardownHTTP()
   366  
   367  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   368  		th.TestMethod(t, r, "PUT")
   369  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   370  		th.TestHeader(t, r, "Content-Type", "application/json")
   371  		th.TestHeader(t, r, "Accept", "application/json")
   372  		th.TestJSONRequest(t, r, UpdateRequest)
   373  
   374  		w.Header().Add("Content-Type", "application/json")
   375  		w.WriteHeader(http.StatusOK)
   376  
   377  		fmt.Fprintf(w, UpdateResponse)
   378  	})
   379  
   380  	options := ports.UpdateOpts{
   381  		Name: "new_port_name",
   382  		FixedIPs: []ports.IP{
   383  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   384  		},
   385  		SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
   386  		AllowedAddressPairs: &[]ports.AddressPair{
   387  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   388  		},
   389  	}
   390  
   391  	s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
   392  	th.AssertNoErr(t, err)
   393  
   394  	th.AssertEquals(t, s.Name, "new_port_name")
   395  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   396  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   397  	})
   398  	th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{
   399  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   400  	})
   401  	th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   402  }
   403  
   404  func TestUpdateOmitSecurityGroups(t *testing.T) {
   405  	th.SetupHTTP()
   406  	defer th.TeardownHTTP()
   407  
   408  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   409  		th.TestMethod(t, r, "PUT")
   410  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   411  		th.TestHeader(t, r, "Content-Type", "application/json")
   412  		th.TestHeader(t, r, "Accept", "application/json")
   413  		th.TestJSONRequest(t, r, UpdateOmitSecurityGroupsRequest)
   414  
   415  		w.Header().Add("Content-Type", "application/json")
   416  		w.WriteHeader(http.StatusOK)
   417  
   418  		fmt.Fprintf(w, UpdateOmitSecurityGroupsResponse)
   419  	})
   420  
   421  	options := ports.UpdateOpts{
   422  		Name: "new_port_name",
   423  		FixedIPs: []ports.IP{
   424  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   425  		},
   426  		AllowedAddressPairs: &[]ports.AddressPair{
   427  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   428  		},
   429  	}
   430  
   431  	s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
   432  	th.AssertNoErr(t, err)
   433  
   434  	th.AssertEquals(t, s.Name, "new_port_name")
   435  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   436  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   437  	})
   438  	th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{
   439  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   440  	})
   441  	th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   442  }
   443  
   444  func TestUpdatePortSecurity(t *testing.T) {
   445  	th.SetupHTTP()
   446  	defer th.TeardownHTTP()
   447  
   448  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   449  		th.TestMethod(t, r, "PUT")
   450  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   451  		th.TestHeader(t, r, "Content-Type", "application/json")
   452  		th.TestHeader(t, r, "Accept", "application/json")
   453  		th.TestJSONRequest(t, r, UpdatePortSecurityRequest)
   454  
   455  		w.Header().Add("Content-Type", "application/json")
   456  		w.WriteHeader(http.StatusOK)
   457  
   458  		fmt.Fprintf(w, UpdatePortSecurityResponse)
   459  	})
   460  
   461  	var portWithExt struct {
   462  		ports.Port
   463  		portsecurity.PortSecurityExt
   464  	}
   465  
   466  	iFalse := false
   467  	portUpdateOpts := ports.UpdateOpts{}
   468  	updateOpts := portsecurity.PortUpdateOptsExt{
   469  		UpdateOptsBuilder:   portUpdateOpts,
   470  		PortSecurityEnabled: &iFalse,
   471  	}
   472  
   473  	err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&portWithExt)
   474  	th.AssertNoErr(t, err)
   475  
   476  	th.AssertEquals(t, portWithExt.Status, "DOWN")
   477  	th.AssertEquals(t, portWithExt.Name, "private-port")
   478  	th.AssertEquals(t, portWithExt.PortSecurityEnabled, false)
   479  }
   480  
   481  func TestRemoveSecurityGroups(t *testing.T) {
   482  	th.SetupHTTP()
   483  	defer th.TeardownHTTP()
   484  
   485  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   486  		th.TestMethod(t, r, "PUT")
   487  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   488  		th.TestHeader(t, r, "Content-Type", "application/json")
   489  		th.TestHeader(t, r, "Accept", "application/json")
   490  		th.TestJSONRequest(t, r, RemoveSecurityGroupRequest)
   491  
   492  		w.Header().Add("Content-Type", "application/json")
   493  		w.WriteHeader(http.StatusOK)
   494  
   495  		fmt.Fprintf(w, RemoveSecurityGroupResponse)
   496  	})
   497  
   498  	options := ports.UpdateOpts{
   499  		Name: "new_port_name",
   500  		FixedIPs: []ports.IP{
   501  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   502  		},
   503  		SecurityGroups: &[]string{},
   504  		AllowedAddressPairs: &[]ports.AddressPair{
   505  			{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   506  		},
   507  	}
   508  
   509  	s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
   510  	th.AssertNoErr(t, err)
   511  
   512  	th.AssertEquals(t, s.Name, "new_port_name")
   513  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   514  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   515  	})
   516  	th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{
   517  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   518  	})
   519  	th.AssertDeepEquals(t, s.SecurityGroups, []string(nil))
   520  }
   521  
   522  func TestRemoveAllowedAddressPairs(t *testing.T) {
   523  	th.SetupHTTP()
   524  	defer th.TeardownHTTP()
   525  
   526  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   527  		th.TestMethod(t, r, "PUT")
   528  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   529  		th.TestHeader(t, r, "Content-Type", "application/json")
   530  		th.TestHeader(t, r, "Accept", "application/json")
   531  		th.TestJSONRequest(t, r, RemoveAllowedAddressPairsRequest)
   532  
   533  		w.Header().Add("Content-Type", "application/json")
   534  		w.WriteHeader(http.StatusOK)
   535  
   536  		fmt.Fprintf(w, RemoveAllowedAddressPairsResponse)
   537  	})
   538  
   539  	options := ports.UpdateOpts{
   540  		Name: "new_port_name",
   541  		FixedIPs: []ports.IP{
   542  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   543  		},
   544  		SecurityGroups:      &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
   545  		AllowedAddressPairs: &[]ports.AddressPair{},
   546  	}
   547  
   548  	s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
   549  	th.AssertNoErr(t, err)
   550  
   551  	th.AssertEquals(t, s.Name, "new_port_name")
   552  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   553  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   554  	})
   555  	th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair(nil))
   556  	th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   557  }
   558  
   559  func TestDontUpdateAllowedAddressPairs(t *testing.T) {
   560  	th.SetupHTTP()
   561  	defer th.TeardownHTTP()
   562  
   563  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   564  		th.TestMethod(t, r, "PUT")
   565  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   566  		th.TestHeader(t, r, "Content-Type", "application/json")
   567  		th.TestHeader(t, r, "Accept", "application/json")
   568  		th.TestJSONRequest(t, r, DontUpdateAllowedAddressPairsRequest)
   569  
   570  		w.Header().Add("Content-Type", "application/json")
   571  		w.WriteHeader(http.StatusOK)
   572  
   573  		fmt.Fprintf(w, DontUpdateAllowedAddressPairsResponse)
   574  	})
   575  
   576  	options := ports.UpdateOpts{
   577  		Name: "new_port_name",
   578  		FixedIPs: []ports.IP{
   579  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   580  		},
   581  		SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
   582  	}
   583  
   584  	s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
   585  	th.AssertNoErr(t, err)
   586  
   587  	th.AssertEquals(t, s.Name, "new_port_name")
   588  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   589  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   590  	})
   591  	th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{
   592  		{IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"},
   593  	})
   594  	th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   595  }
   596  
   597  func TestDelete(t *testing.T) {
   598  	th.SetupHTTP()
   599  	defer th.TeardownHTTP()
   600  
   601  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   602  		th.TestMethod(t, r, "DELETE")
   603  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   604  		w.WriteHeader(http.StatusNoContent)
   605  	})
   606  
   607  	res := ports.Delete(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d")
   608  	th.AssertNoErr(t, res.Err)
   609  }
   610  
   611  func TestGetWithExtraDHCPOpts(t *testing.T) {
   612  	th.SetupHTTP()
   613  	defer th.TeardownHTTP()
   614  
   615  	th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) {
   616  		th.TestMethod(t, r, "GET")
   617  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   618  
   619  		w.Header().Add("Content-Type", "application/json")
   620  		w.WriteHeader(http.StatusOK)
   621  
   622  		fmt.Fprintf(w, GetWithExtraDHCPOptsResponse)
   623  	})
   624  
   625  	var s struct {
   626  		ports.Port
   627  		extradhcpopts.ExtraDHCPOptsExt
   628  	}
   629  
   630  	err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").ExtractInto(&s)
   631  	th.AssertNoErr(t, err)
   632  
   633  	th.AssertEquals(t, s.Status, "ACTIVE")
   634  	th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   635  	th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   636  	th.AssertEquals(t, s.AdminStateUp, true)
   637  	th.AssertEquals(t, s.Name, "port-with-extra-dhcp-opts")
   638  	th.AssertEquals(t, s.DeviceOwner, "")
   639  	th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0")
   640  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   641  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.4"},
   642  	})
   643  	th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   644  	th.AssertEquals(t, s.DeviceID, "")
   645  
   646  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptName, "option1")
   647  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value1")
   648  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, "4")
   649  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[1].OptName, "option2")
   650  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[1].OptValue, "value2")
   651  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[1].IPVersion, "4")
   652  }
   653  
   654  func TestCreateWithExtraDHCPOpts(t *testing.T) {
   655  	th.SetupHTTP()
   656  	defer th.TeardownHTTP()
   657  
   658  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
   659  		th.TestMethod(t, r, "POST")
   660  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   661  		th.TestHeader(t, r, "Content-Type", "application/json")
   662  		th.TestHeader(t, r, "Accept", "application/json")
   663  		th.TestJSONRequest(t, r, CreateWithExtraDHCPOptsRequest)
   664  
   665  		w.Header().Add("Content-Type", "application/json")
   666  		w.WriteHeader(http.StatusCreated)
   667  
   668  		fmt.Fprintf(w, CreateWithExtraDHCPOptsResponse)
   669  	})
   670  
   671  	adminStateUp := true
   672  	portCreateOpts := ports.CreateOpts{
   673  		Name:         "port-with-extra-dhcp-opts",
   674  		AdminStateUp: &adminStateUp,
   675  		NetworkID:    "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   676  		FixedIPs: []ports.IP{
   677  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   678  		},
   679  	}
   680  
   681  	createOpts := extradhcpopts.CreateOptsExt{
   682  		CreateOptsBuilder: portCreateOpts,
   683  		ExtraDHCPOpts: []extradhcpopts.CreateExtraDHCPOpt{
   684  			{
   685  				OptName:  "option1",
   686  				OptValue: "value1",
   687  			},
   688  		},
   689  	}
   690  
   691  	var s struct {
   692  		ports.Port
   693  		extradhcpopts.ExtraDHCPOptsExt
   694  	}
   695  
   696  	err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&s)
   697  	th.AssertNoErr(t, err)
   698  
   699  	th.AssertEquals(t, s.Status, "DOWN")
   700  	th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   701  	th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   702  	th.AssertEquals(t, s.AdminStateUp, true)
   703  	th.AssertEquals(t, s.Name, "port-with-extra-dhcp-opts")
   704  	th.AssertEquals(t, s.DeviceOwner, "")
   705  	th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0")
   706  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   707  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   708  	})
   709  	th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   710  	th.AssertEquals(t, s.DeviceID, "")
   711  
   712  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptName, "option1")
   713  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value1")
   714  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, "4")
   715  }
   716  
   717  func TestUpdateWithExtraDHCPOpts(t *testing.T) {
   718  	th.SetupHTTP()
   719  	defer th.TeardownHTTP()
   720  
   721  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   722  		th.TestMethod(t, r, "PUT")
   723  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   724  		th.TestHeader(t, r, "Content-Type", "application/json")
   725  		th.TestHeader(t, r, "Accept", "application/json")
   726  		th.TestJSONRequest(t, r, UpdateWithExtraDHCPOptsRequest)
   727  
   728  		w.Header().Add("Content-Type", "application/json")
   729  		w.WriteHeader(http.StatusOK)
   730  
   731  		fmt.Fprintf(w, UpdateWithExtraDHCPOptsResponse)
   732  	})
   733  
   734  	portUpdateOpts := ports.UpdateOpts{
   735  		Name: "updated-port-with-dhcp-opts",
   736  		FixedIPs: []ports.IP{
   737  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   738  		},
   739  	}
   740  
   741  	edoValue2 := "value2"
   742  	updateOpts := extradhcpopts.UpdateOptsExt{
   743  		UpdateOptsBuilder: portUpdateOpts,
   744  		ExtraDHCPOpts: []extradhcpopts.UpdateExtraDHCPOpt{
   745  			{
   746  				OptName: "option1",
   747  			},
   748  			{
   749  				OptName:  "option2",
   750  				OptValue: &edoValue2,
   751  			},
   752  		},
   753  	}
   754  
   755  	var s struct {
   756  		ports.Port
   757  		extradhcpopts.ExtraDHCPOptsExt
   758  	}
   759  
   760  	err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&s)
   761  	th.AssertNoErr(t, err)
   762  
   763  	th.AssertEquals(t, s.Status, "DOWN")
   764  	th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   765  	th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   766  	th.AssertEquals(t, s.AdminStateUp, true)
   767  	th.AssertEquals(t, s.Name, "updated-port-with-dhcp-opts")
   768  	th.AssertEquals(t, s.DeviceOwner, "")
   769  	th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0")
   770  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   771  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   772  	})
   773  	th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   774  	th.AssertEquals(t, s.DeviceID, "")
   775  
   776  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptName, "option2")
   777  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value2")
   778  	th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, "4")
   779  }