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

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/subnetpools"
    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/subnetpools", 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, SubnetPoolsListResult)
    27  	})
    28  
    29  	count := 0
    30  
    31  	subnetpools.List(fake.ServiceClient(), subnetpools.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    32  		count++
    33  		actual, err := subnetpools.ExtractSubnetPools(page)
    34  		if err != nil {
    35  			t.Errorf("Failed to extract subnetpools: %v", err)
    36  			return false, nil
    37  		}
    38  
    39  		expected := []subnetpools.SubnetPool{
    40  			SubnetPool1,
    41  			SubnetPool2,
    42  			SubnetPool3,
    43  		}
    44  
    45  		th.CheckDeepEquals(t, expected, actual)
    46  
    47  		return true, nil
    48  	})
    49  
    50  	if count != 1 {
    51  		t.Errorf("Expected 1 page, got %d", count)
    52  	}
    53  }
    54  
    55  func TestGet(t *testing.T) {
    56  	th.SetupHTTP()
    57  	defer th.TeardownHTTP()
    58  
    59  	th.Mux.HandleFunc("/v2.0/subnetpools/0a738452-8057-4ad3-89c2-92f6a74afa76", func(w http.ResponseWriter, r *http.Request) {
    60  		th.TestMethod(t, r, "GET")
    61  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    62  
    63  		w.Header().Add("Content-Type", "application/json")
    64  		w.WriteHeader(http.StatusOK)
    65  
    66  		fmt.Fprintf(w, SubnetPoolGetResult)
    67  	})
    68  
    69  	s, err := subnetpools.Get(fake.ServiceClient(), "0a738452-8057-4ad3-89c2-92f6a74afa76").Extract()
    70  	th.AssertNoErr(t, err)
    71  
    72  	th.AssertEquals(t, s.ID, "0a738452-8057-4ad3-89c2-92f6a74afa76")
    73  	th.AssertEquals(t, s.Name, "my-ipv6-pool")
    74  	th.AssertEquals(t, s.DefaultQuota, 2)
    75  	th.AssertEquals(t, s.TenantID, "1e2b9857295a4a3e841809ef492812c5")
    76  	th.AssertEquals(t, s.ProjectID, "1e2b9857295a4a3e841809ef492812c5")
    77  	th.AssertEquals(t, s.CreatedAt, time.Date(2018, 1, 1, 0, 0, 1, 0, time.UTC))
    78  	th.AssertEquals(t, s.UpdatedAt, time.Date(2018, 1, 1, 0, 10, 10, 0, time.UTC))
    79  	th.AssertDeepEquals(t, s.Prefixes, []string{
    80  		"2001:db8::a3/64",
    81  	})
    82  	th.AssertEquals(t, s.DefaultPrefixLen, 64)
    83  	th.AssertEquals(t, s.MinPrefixLen, 64)
    84  	th.AssertEquals(t, s.MaxPrefixLen, 128)
    85  	th.AssertEquals(t, s.AddressScopeID, "")
    86  	th.AssertEquals(t, s.IPversion, 6)
    87  	th.AssertEquals(t, s.Shared, false)
    88  	th.AssertEquals(t, s.Description, "ipv6 prefixes")
    89  	th.AssertEquals(t, s.IsDefault, true)
    90  	th.AssertEquals(t, s.RevisionNumber, 2)
    91  }
    92  func TestCreate(t *testing.T) {
    93  	th.SetupHTTP()
    94  	defer th.TeardownHTTP()
    95  
    96  	th.Mux.HandleFunc("/v2.0/subnetpools", func(w http.ResponseWriter, r *http.Request) {
    97  		th.TestMethod(t, r, "POST")
    98  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    99  		th.TestHeader(t, r, "Content-Type", "application/json")
   100  		th.TestHeader(t, r, "Accept", "application/json")
   101  		th.TestJSONRequest(t, r, SubnetPoolCreateRequest)
   102  
   103  		w.Header().Add("Content-Type", "application/json")
   104  		w.WriteHeader(http.StatusCreated)
   105  
   106  		fmt.Fprintf(w, SubnetPoolCreateResult)
   107  	})
   108  
   109  	opts := subnetpools.CreateOpts{
   110  		Name: "my_ipv4_pool",
   111  		Prefixes: []string{
   112  			"10.10.0.0/16",
   113  			"10.11.11.0/24",
   114  		},
   115  		MinPrefixLen:   25,
   116  		MaxPrefixLen:   30,
   117  		AddressScopeID: "3d4e2e2a-552b-42ad-a16d-820bbf3edaf3",
   118  		Description:    "ipv4 prefixes",
   119  	}
   120  	s, err := subnetpools.Create(fake.ServiceClient(), opts).Extract()
   121  	th.AssertNoErr(t, err)
   122  
   123  	th.AssertEquals(t, s.Name, "my_ipv4_pool")
   124  	th.AssertDeepEquals(t, s.Prefixes, []string{
   125  		"10.10.0.0/16",
   126  		"10.11.11.0/24",
   127  	})
   128  	th.AssertEquals(t, s.MinPrefixLen, 25)
   129  	th.AssertEquals(t, s.MaxPrefixLen, 30)
   130  	th.AssertEquals(t, s.AddressScopeID, "3d4e2e2a-552b-42ad-a16d-820bbf3edaf3")
   131  	th.AssertEquals(t, s.Description, "ipv4 prefixes")
   132  }
   133  
   134  func TestUpdate(t *testing.T) {
   135  	th.SetupHTTP()
   136  	defer th.TeardownHTTP()
   137  
   138  	th.Mux.HandleFunc("/v2.0/subnetpools/099546ca-788d-41e5-a76d-17d8cd282d3e", func(w http.ResponseWriter, r *http.Request) {
   139  		th.TestMethod(t, r, "PUT")
   140  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   141  		th.TestHeader(t, r, "Content-Type", "application/json")
   142  		th.TestHeader(t, r, "Accept", "application/json")
   143  		th.TestJSONRequest(t, r, SubnetPoolUpdateRequest)
   144  
   145  		w.Header().Add("Content-Type", "application/json")
   146  		w.WriteHeader(http.StatusOK)
   147  
   148  		fmt.Fprintf(w, SubnetPoolUpdateResponse)
   149  	})
   150  
   151  	nullString := ""
   152  	nullInt := 0
   153  	updateOpts := subnetpools.UpdateOpts{
   154  		Name: "new_subnetpool_name",
   155  		Prefixes: []string{
   156  			"10.11.12.0/24",
   157  			"10.24.0.0/16",
   158  		},
   159  		MaxPrefixLen:   16,
   160  		AddressScopeID: &nullString,
   161  		DefaultQuota:   &nullInt,
   162  		Description:    &nullString,
   163  	}
   164  	n, err := subnetpools.Update(fake.ServiceClient(), "099546ca-788d-41e5-a76d-17d8cd282d3e", updateOpts).Extract()
   165  	th.AssertNoErr(t, err)
   166  
   167  	th.AssertEquals(t, n.Name, "new_subnetpool_name")
   168  	th.AssertDeepEquals(t, n.Prefixes, []string{
   169  		"10.8.0.0/16",
   170  		"10.11.12.0/24",
   171  		"10.24.0.0/16",
   172  	})
   173  	th.AssertEquals(t, n.MaxPrefixLen, 16)
   174  	th.AssertEquals(t, n.ID, "099546ca-788d-41e5-a76d-17d8cd282d3e")
   175  	th.AssertEquals(t, n.AddressScopeID, "")
   176  	th.AssertEquals(t, n.DefaultQuota, 0)
   177  	th.AssertEquals(t, n.Description, "")
   178  }
   179  
   180  func TestDelete(t *testing.T) {
   181  	th.SetupHTTP()
   182  	defer th.TeardownHTTP()
   183  
   184  	th.Mux.HandleFunc("/v2.0/subnetpools/099546ca-788d-41e5-a76d-17d8cd282d3e", func(w http.ResponseWriter, r *http.Request) {
   185  		th.TestMethod(t, r, "DELETE")
   186  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   187  		w.WriteHeader(http.StatusNoContent)
   188  	})
   189  
   190  	res := subnetpools.Delete(fake.ServiceClient(), "099546ca-788d-41e5-a76d-17d8cd282d3e")
   191  	th.AssertNoErr(t, res.Err)
   192  }