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

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