github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/networks/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/portsecurity"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/networks"
    11  	"github.com/huaweicloud/golangsdk/pagination"
    12  	th "github.com/huaweicloud/golangsdk/testhelper"
    13  )
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "GET")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  
    23  		w.Header().Add("Content-Type", "application/json")
    24  		w.WriteHeader(http.StatusOK)
    25  
    26  		fmt.Fprintf(w, ListResponse)
    27  	})
    28  
    29  	client := fake.ServiceClient()
    30  	count := 0
    31  
    32  	networks.List(client, networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    33  		count++
    34  		actual, err := networks.ExtractNetworks(page)
    35  		if err != nil {
    36  			t.Errorf("Failed to extract networks: %v", err)
    37  			return false, err
    38  		}
    39  
    40  		th.CheckDeepEquals(t, ExpectedNetworkSlice, actual)
    41  
    42  		return true, nil
    43  	})
    44  
    45  	if count != 1 {
    46  		t.Errorf("Expected 1 page, got %d", count)
    47  	}
    48  }
    49  
    50  func TestListWithExtensions(t *testing.T) {
    51  	th.SetupHTTP()
    52  	defer th.TeardownHTTP()
    53  
    54  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
    55  		th.TestMethod(t, r, "GET")
    56  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    57  
    58  		w.Header().Add("Content-Type", "application/json")
    59  		w.WriteHeader(http.StatusOK)
    60  
    61  		fmt.Fprintf(w, ListResponse)
    62  	})
    63  
    64  	client := fake.ServiceClient()
    65  
    66  	type networkWithExt struct {
    67  		networks.Network
    68  		portsecurity.PortSecurityExt
    69  	}
    70  
    71  	var allNetworks []networkWithExt
    72  
    73  	allPages, err := networks.List(client, networks.ListOpts{}).AllPages()
    74  	th.AssertNoErr(t, err)
    75  
    76  	err = networks.ExtractNetworksInto(allPages, &allNetworks)
    77  	th.AssertNoErr(t, err)
    78  
    79  	th.AssertEquals(t, allNetworks[0].Status, "ACTIVE")
    80  	th.AssertEquals(t, allNetworks[0].PortSecurityEnabled, true)
    81  	th.AssertEquals(t, allNetworks[0].Subnets[0], "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
    82  	th.AssertEquals(t, allNetworks[1].Subnets[0], "08eae331-0402-425a-923c-34f7cfe39c1b")
    83  }
    84  
    85  func TestGet(t *testing.T) {
    86  	th.SetupHTTP()
    87  	defer th.TeardownHTTP()
    88  
    89  	th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    90  		th.TestMethod(t, r, "GET")
    91  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    92  
    93  		w.Header().Add("Content-Type", "application/json")
    94  		w.WriteHeader(http.StatusOK)
    95  
    96  		fmt.Fprintf(w, GetResponse)
    97  	})
    98  
    99  	n, err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
   100  	th.AssertNoErr(t, err)
   101  	th.CheckDeepEquals(t, &Network1, n)
   102  }
   103  
   104  func TestGetWithExtensions(t *testing.T) {
   105  	th.SetupHTTP()
   106  	defer th.TeardownHTTP()
   107  
   108  	th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
   109  		th.TestMethod(t, r, "GET")
   110  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   111  
   112  		w.Header().Add("Content-Type", "application/json")
   113  		w.WriteHeader(http.StatusOK)
   114  
   115  		fmt.Fprintf(w, GetResponse)
   116  	})
   117  
   118  	var networkWithExtensions struct {
   119  		networks.Network
   120  		portsecurity.PortSecurityExt
   121  	}
   122  
   123  	err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&networkWithExtensions)
   124  	th.AssertNoErr(t, err)
   125  
   126  	th.AssertEquals(t, networkWithExtensions.Status, "ACTIVE")
   127  	th.AssertEquals(t, networkWithExtensions.PortSecurityEnabled, true)
   128  }
   129  
   130  func TestCreate(t *testing.T) {
   131  	th.SetupHTTP()
   132  	defer th.TeardownHTTP()
   133  
   134  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
   135  		th.TestMethod(t, r, "POST")
   136  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   137  		th.TestHeader(t, r, "Content-Type", "application/json")
   138  		th.TestHeader(t, r, "Accept", "application/json")
   139  		th.TestJSONRequest(t, r, CreateRequest)
   140  		w.Header().Add("Content-Type", "application/json")
   141  		w.WriteHeader(http.StatusCreated)
   142  
   143  		fmt.Fprintf(w, CreateResponse)
   144  	})
   145  
   146  	iTrue := true
   147  	options := networks.CreateOpts{Name: "private", AdminStateUp: &iTrue}
   148  	n, err := networks.Create(fake.ServiceClient(), options).Extract()
   149  	th.AssertNoErr(t, err)
   150  
   151  	th.AssertEquals(t, n.Status, "ACTIVE")
   152  	th.AssertDeepEquals(t, &Network2, n)
   153  }
   154  
   155  func TestCreateWithOptionalFields(t *testing.T) {
   156  	th.SetupHTTP()
   157  	defer th.TeardownHTTP()
   158  
   159  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
   160  		th.TestMethod(t, r, "POST")
   161  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   162  		th.TestHeader(t, r, "Content-Type", "application/json")
   163  		th.TestHeader(t, r, "Accept", "application/json")
   164  		th.TestJSONRequest(t, r, CreateOptionalFieldsRequest)
   165  
   166  		w.WriteHeader(http.StatusCreated)
   167  		fmt.Fprintf(w, `{}`)
   168  	})
   169  
   170  	iTrue := true
   171  	options := networks.CreateOpts{
   172  		Name:                  "public",
   173  		AdminStateUp:          &iTrue,
   174  		Shared:                &iTrue,
   175  		TenantID:              "12345",
   176  		AvailabilityZoneHints: []string{"zone1", "zone2"},
   177  	}
   178  	_, err := networks.Create(fake.ServiceClient(), options).Extract()
   179  	th.AssertNoErr(t, err)
   180  }
   181  
   182  func TestUpdate(t *testing.T) {
   183  	th.SetupHTTP()
   184  	defer th.TeardownHTTP()
   185  
   186  	th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   187  		th.TestMethod(t, r, "PUT")
   188  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   189  		th.TestHeader(t, r, "Content-Type", "application/json")
   190  		th.TestHeader(t, r, "Accept", "application/json")
   191  		th.TestJSONRequest(t, r, UpdateRequest)
   192  
   193  		w.Header().Add("Content-Type", "application/json")
   194  		w.WriteHeader(http.StatusOK)
   195  
   196  		fmt.Fprintf(w, UpdateResponse)
   197  	})
   198  
   199  	iTrue, iFalse := true, false
   200  	options := networks.UpdateOpts{Name: "new_network_name", AdminStateUp: &iFalse, Shared: &iTrue}
   201  	n, err := networks.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
   202  	th.AssertNoErr(t, err)
   203  
   204  	th.AssertEquals(t, n.Name, "new_network_name")
   205  	th.AssertEquals(t, n.AdminStateUp, false)
   206  	th.AssertEquals(t, n.Shared, true)
   207  	th.AssertEquals(t, n.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
   208  }
   209  
   210  func TestDelete(t *testing.T) {
   211  	th.SetupHTTP()
   212  	defer th.TeardownHTTP()
   213  
   214  	th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   215  		th.TestMethod(t, r, "DELETE")
   216  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   217  		w.WriteHeader(http.StatusNoContent)
   218  	})
   219  
   220  	res := networks.Delete(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
   221  	th.AssertNoErr(t, res.Err)
   222  }
   223  
   224  func TestCreatePortSecurity(t *testing.T) {
   225  	th.SetupHTTP()
   226  	defer th.TeardownHTTP()
   227  
   228  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
   229  		th.TestMethod(t, r, "POST")
   230  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   231  		th.TestHeader(t, r, "Content-Type", "application/json")
   232  		th.TestHeader(t, r, "Accept", "application/json")
   233  		th.TestJSONRequest(t, r, CreatePortSecurityRequest)
   234  		w.Header().Add("Content-Type", "application/json")
   235  		w.WriteHeader(http.StatusCreated)
   236  
   237  		fmt.Fprintf(w, CreatePortSecurityResponse)
   238  	})
   239  
   240  	var networkWithExtensions struct {
   241  		networks.Network
   242  		portsecurity.PortSecurityExt
   243  	}
   244  
   245  	iTrue := true
   246  	iFalse := false
   247  	networkCreateOpts := networks.CreateOpts{Name: "private", AdminStateUp: &iTrue}
   248  	createOpts := portsecurity.NetworkCreateOptsExt{
   249  		CreateOptsBuilder:   networkCreateOpts,
   250  		PortSecurityEnabled: &iFalse,
   251  	}
   252  
   253  	err := networks.Create(fake.ServiceClient(), createOpts).ExtractInto(&networkWithExtensions)
   254  	th.AssertNoErr(t, err)
   255  
   256  	th.AssertEquals(t, networkWithExtensions.Status, "ACTIVE")
   257  	th.AssertEquals(t, networkWithExtensions.PortSecurityEnabled, false)
   258  }
   259  
   260  func TestUpdatePortSecurity(t *testing.T) {
   261  	th.SetupHTTP()
   262  	defer th.TeardownHTTP()
   263  
   264  	th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   265  		th.TestMethod(t, r, "PUT")
   266  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   267  		th.TestHeader(t, r, "Content-Type", "application/json")
   268  		th.TestHeader(t, r, "Accept", "application/json")
   269  		th.TestJSONRequest(t, r, UpdatePortSecurityRequest)
   270  
   271  		w.Header().Add("Content-Type", "application/json")
   272  		w.WriteHeader(http.StatusOK)
   273  
   274  		fmt.Fprintf(w, UpdatePortSecurityResponse)
   275  	})
   276  
   277  	var networkWithExtensions struct {
   278  		networks.Network
   279  		portsecurity.PortSecurityExt
   280  	}
   281  
   282  	iFalse := false
   283  	networkUpdateOpts := networks.UpdateOpts{}
   284  	updateOpts := portsecurity.NetworkUpdateOptsExt{
   285  		UpdateOptsBuilder:   networkUpdateOpts,
   286  		PortSecurityEnabled: &iFalse,
   287  	}
   288  
   289  	err := networks.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", updateOpts).ExtractInto(&networkWithExtensions)
   290  	th.AssertNoErr(t, err)
   291  
   292  	th.AssertEquals(t, networkWithExtensions.Name, "private")
   293  	th.AssertEquals(t, networkWithExtensions.AdminStateUp, true)
   294  	th.AssertEquals(t, networkWithExtensions.Shared, false)
   295  	th.AssertEquals(t, networkWithExtensions.ID, "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
   296  	th.AssertEquals(t, networkWithExtensions.PortSecurityEnabled, false)
   297  }