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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk"
     9  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/networkipavailabilities"
    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/network-ip-availabilities", 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, NetworkIPAvailabilityListResult)
    27  	})
    28  
    29  	count := 0
    30  
    31  	networkipavailabilities.List(fake.ServiceClient(), networkipavailabilities.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    32  		count++
    33  		actual, err := networkipavailabilities.ExtractNetworkIPAvailabilities(page)
    34  		if err != nil {
    35  			t.Errorf("Failed to extract network IP availabilities: %v", err)
    36  			return false, nil
    37  		}
    38  
    39  		expected := []networkipavailabilities.NetworkIPAvailability{
    40  			NetworkIPAvailability1,
    41  			NetworkIPAvailability2,
    42  		}
    43  
    44  		th.CheckDeepEquals(t, expected, actual)
    45  
    46  		return true, nil
    47  	})
    48  
    49  	if count != 1 {
    50  		t.Errorf("Expected 1 page, got %d", count)
    51  	}
    52  }
    53  
    54  func TestGet(t *testing.T) {
    55  	th.SetupHTTP()
    56  	defer th.TeardownHTTP()
    57  
    58  	th.Mux.HandleFunc("/v2.0/network-ip-availabilities/cf11ab78-2302-49fa-870f-851a08c7afb8", func(w http.ResponseWriter, r *http.Request) {
    59  		th.TestMethod(t, r, "GET")
    60  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    61  
    62  		w.Header().Add("Content-Type", "application/json")
    63  		w.WriteHeader(http.StatusOK)
    64  
    65  		fmt.Fprintf(w, NetworkIPAvailabilityGetResult)
    66  	})
    67  
    68  	s, err := networkipavailabilities.Get(fake.ServiceClient(), "cf11ab78-2302-49fa-870f-851a08c7afb8").Extract()
    69  	th.AssertNoErr(t, err)
    70  
    71  	th.AssertEquals(t, s.NetworkID, "cf11ab78-2302-49fa-870f-851a08c7afb8")
    72  	th.AssertEquals(t, s.NetworkName, "public")
    73  	th.AssertEquals(t, s.ProjectID, "424e7cf0243c468ca61732ba45973b3e")
    74  	th.AssertEquals(t, s.TenantID, "424e7cf0243c468ca61732ba45973b3e")
    75  	th.AssertEquals(t, s.TotalIPs, 253)
    76  	th.AssertEquals(t, s.UsedIPs, 3)
    77  	th.AssertDeepEquals(t, s.SubnetIPAvailabilities, []networkipavailabilities.SubnetIPAvailability{
    78  		{
    79  			SubnetID:   "4afe6e5f-9649-40db-b18f-64c7ead942bd",
    80  			SubnetName: "public-subnet",
    81  			CIDR:       "203.0.113.0/24",
    82  			IPVersion:  int(golangsdk.IPv4),
    83  			TotalIPs:   253,
    84  			UsedIPs:    3,
    85  		},
    86  	})
    87  }