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

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
     7  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/portsbinding"
     8  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/ports"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  )
    11  
    12  func TestList(t *testing.T) {
    13  	th.SetupHTTP()
    14  	defer th.TeardownHTTP()
    15  
    16  	HandleListSuccessfully(t)
    17  
    18  	type PortWithExt struct {
    19  		ports.Port
    20  		portsbinding.PortsBindingExt
    21  	}
    22  	var actual []PortWithExt
    23  
    24  	expected := []PortWithExt{
    25  		{
    26  			Port: ports.Port{
    27  				Status:       "ACTIVE",
    28  				Name:         "",
    29  				AdminStateUp: true,
    30  				NetworkID:    "70c1db1f-b701-45bd-96e0-a313ee3430b3",
    31  				TenantID:     "",
    32  				DeviceOwner:  "network:router_gateway",
    33  				MACAddress:   "fa:16:3e:58:42:ed",
    34  				FixedIPs: []ports.IP{
    35  					{
    36  						SubnetID:  "008ba151-0b8c-4a67-98b5-0d2b87666062",
    37  						IPAddress: "172.24.4.2",
    38  					},
    39  				},
    40  				ID:             "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
    41  				SecurityGroups: []string{},
    42  				DeviceID:       "9ae135f4-b6e0-4dad-9e91-3c223e385824",
    43  			},
    44  			PortsBindingExt: portsbinding.PortsBindingExt{
    45  				VNICType: "normal",
    46  				HostID:   "devstack",
    47  			},
    48  		},
    49  	}
    50  
    51  	allPages, err := ports.List(fake.ServiceClient(), ports.ListOpts{}).AllPages()
    52  	th.AssertNoErr(t, err)
    53  
    54  	err = ports.ExtractPortsInto(allPages, &actual)
    55  	th.AssertNoErr(t, err)
    56  
    57  	th.CheckDeepEquals(t, expected, actual)
    58  }
    59  
    60  func TestGet(t *testing.T) {
    61  	th.SetupHTTP()
    62  	defer th.TeardownHTTP()
    63  
    64  	HandleGet(t)
    65  
    66  	var s struct {
    67  		ports.Port
    68  		portsbinding.PortsBindingExt
    69  	}
    70  
    71  	err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").ExtractInto(&s)
    72  	th.AssertNoErr(t, err)
    73  
    74  	th.AssertEquals(t, s.Status, "ACTIVE")
    75  	th.AssertEquals(t, s.Name, "")
    76  	th.AssertEquals(t, s.AdminStateUp, true)
    77  	th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
    78  	th.AssertEquals(t, s.TenantID, "7e02058126cc4950b75f9970368ba177")
    79  	th.AssertEquals(t, s.DeviceOwner, "network:router_interface")
    80  	th.AssertEquals(t, s.MACAddress, "fa:16:3e:23:fd:d7")
    81  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
    82  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"},
    83  	})
    84  	th.AssertEquals(t, s.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2")
    85  	th.AssertDeepEquals(t, s.SecurityGroups, []string{})
    86  	th.AssertEquals(t, s.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e")
    87  
    88  	th.AssertEquals(t, s.HostID, "devstack")
    89  	th.AssertEquals(t, s.VNICType, "normal")
    90  	th.AssertEquals(t, s.VIFType, "ovs")
    91  	th.AssertDeepEquals(t, s.VIFDetails, map[string]interface{}{"port_filter": true, "ovs_hybrid_plug": true})
    92  }
    93  
    94  func TestCreate(t *testing.T) {
    95  	th.SetupHTTP()
    96  	defer th.TeardownHTTP()
    97  
    98  	HandleCreate(t)
    99  
   100  	var s struct {
   101  		ports.Port
   102  		portsbinding.PortsBindingExt
   103  	}
   104  
   105  	asu := true
   106  	portCreateOpts := ports.CreateOpts{
   107  		Name:         "private-port",
   108  		AdminStateUp: &asu,
   109  		NetworkID:    "a87cc70a-3e15-4acf-8205-9b711a3531b7",
   110  		FixedIPs: []ports.IP{
   111  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   112  		},
   113  		SecurityGroups: &[]string{"foo"},
   114  	}
   115  
   116  	createOpts := portsbinding.CreateOptsExt{
   117  		CreateOptsBuilder: portCreateOpts,
   118  		HostID:            "HOST1",
   119  		VNICType:          "normal",
   120  	}
   121  
   122  	err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&s)
   123  	th.AssertNoErr(t, err)
   124  
   125  	th.AssertEquals(t, s.Status, "DOWN")
   126  	th.AssertEquals(t, s.Name, "private-port")
   127  	th.AssertEquals(t, s.AdminStateUp, true)
   128  	th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   129  	th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   130  	th.AssertEquals(t, s.DeviceOwner, "")
   131  	th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0")
   132  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   133  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"},
   134  	})
   135  	th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   136  	th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   137  	th.AssertEquals(t, s.HostID, "HOST1")
   138  	th.AssertEquals(t, s.VNICType, "normal")
   139  }
   140  
   141  func TestRequiredCreateOpts(t *testing.T) {
   142  	res := ports.Create(fake.ServiceClient(), portsbinding.CreateOptsExt{CreateOptsBuilder: ports.CreateOpts{}})
   143  	if res.Err == nil {
   144  		t.Fatalf("Expected error, got none")
   145  	}
   146  }
   147  
   148  func TestUpdate(t *testing.T) {
   149  	th.SetupHTTP()
   150  	defer th.TeardownHTTP()
   151  
   152  	HandleUpdate(t)
   153  
   154  	var s struct {
   155  		ports.Port
   156  		portsbinding.PortsBindingExt
   157  	}
   158  
   159  	portUpdateOpts := ports.UpdateOpts{
   160  		Name: "new_port_name",
   161  		FixedIPs: []ports.IP{
   162  			{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   163  		},
   164  		SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
   165  	}
   166  
   167  	updateOpts := portsbinding.UpdateOptsExt{
   168  		UpdateOptsBuilder: portUpdateOpts,
   169  		HostID:            "HOST1",
   170  		VNICType:          "normal",
   171  	}
   172  
   173  	err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&s)
   174  	th.AssertNoErr(t, err)
   175  
   176  	th.AssertEquals(t, s.Name, "new_port_name")
   177  	th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{
   178  		{SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"},
   179  	})
   180  	th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"})
   181  	th.AssertEquals(t, s.HostID, "HOST1")
   182  	th.AssertEquals(t, s.VNICType, "normal")
   183  }