github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/external/testing/results_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/external"
    10  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/networks"
    11  	nettest "github.com/huaweicloud/golangsdk/openstack/networking/v2/networks/testing"
    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, nettest.ListResponse)
    27  	})
    28  
    29  	type NetworkWithExternalExt struct {
    30  		networks.Network
    31  		external.NetworkExternalExt
    32  	}
    33  	var actual []NetworkWithExternalExt
    34  
    35  	allPages, err := networks.List(fake.ServiceClient(), networks.ListOpts{}).AllPages()
    36  	th.AssertNoErr(t, err)
    37  
    38  	err = networks.ExtractNetworksInto(allPages, &actual)
    39  	th.AssertNoErr(t, err)
    40  
    41  	th.AssertEquals(t, "d32019d3-bc6e-4319-9c1d-6722fc136a22", actual[0].ID)
    42  	th.AssertEquals(t, true, actual[0].External)
    43  }
    44  
    45  func TestGet(t *testing.T) {
    46  	th.SetupHTTP()
    47  	defer th.TeardownHTTP()
    48  
    49  	th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
    50  		th.TestMethod(t, r, "GET")
    51  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    52  
    53  		w.Header().Add("Content-Type", "application/json")
    54  		w.WriteHeader(http.StatusOK)
    55  
    56  		fmt.Fprintf(w, nettest.GetResponse)
    57  	})
    58  
    59  	var s struct {
    60  		networks.Network
    61  		external.NetworkExternalExt
    62  	}
    63  
    64  	err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&s)
    65  	th.AssertNoErr(t, err)
    66  
    67  	th.AssertEquals(t, "d32019d3-bc6e-4319-9c1d-6722fc136a22", s.ID)
    68  	th.AssertEquals(t, true, s.External)
    69  }
    70  
    71  func TestCreate(t *testing.T) {
    72  	th.SetupHTTP()
    73  	defer th.TeardownHTTP()
    74  
    75  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
    76  		th.TestMethod(t, r, "POST")
    77  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    78  		th.TestHeader(t, r, "Content-Type", "application/json")
    79  		th.TestHeader(t, r, "Accept", "application/json")
    80  		th.TestJSONRequest(t, r, CreateRequest)
    81  
    82  		w.Header().Add("Content-Type", "application/json")
    83  		w.WriteHeader(http.StatusCreated)
    84  
    85  		fmt.Fprintf(w, CreateResponse)
    86  	})
    87  
    88  	iTrue := true
    89  	iFalse := false
    90  	networkCreateOpts := networks.CreateOpts{
    91  		Name:         "private",
    92  		AdminStateUp: &iTrue,
    93  	}
    94  
    95  	externalCreateOpts := external.CreateOptsExt{
    96  		CreateOptsBuilder: &networkCreateOpts,
    97  		External:          &iFalse,
    98  	}
    99  
   100  	_, err := networks.Create(fake.ServiceClient(), externalCreateOpts).Extract()
   101  	th.AssertNoErr(t, err)
   102  
   103  	th.AssertNoErr(t, err)
   104  }
   105  
   106  func TestUpdate(t *testing.T) {
   107  	th.SetupHTTP()
   108  	defer th.TeardownHTTP()
   109  
   110  	th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) {
   111  		th.TestMethod(t, r, "PUT")
   112  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   113  		th.TestHeader(t, r, "Content-Type", "application/json")
   114  		th.TestHeader(t, r, "Accept", "application/json")
   115  		th.TestJSONRequest(t, r, UpdateRequest)
   116  
   117  		w.Header().Add("Content-Type", "application/json")
   118  		w.WriteHeader(http.StatusOK)
   119  
   120  		fmt.Fprintf(w, UpdateResponse)
   121  	})
   122  
   123  	iTrue := true
   124  	iFalse := false
   125  	networkUpdateOpts := networks.UpdateOpts{
   126  		Name:         "new_network_name",
   127  		AdminStateUp: &iFalse,
   128  		Shared:       &iTrue,
   129  	}
   130  
   131  	externalUpdateOpts := external.UpdateOptsExt{
   132  		UpdateOptsBuilder: &networkUpdateOpts,
   133  		External:          &iFalse,
   134  	}
   135  
   136  	_, err := networks.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", externalUpdateOpts).Extract()
   137  	th.AssertNoErr(t, err)
   138  }