github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/vpcep/v1/services/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/vpcep/v1/services"
     9  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/testhelper/client"
    11  )
    12  
    13  func TestCreateRequest(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/vpc-endpoint-services", func(w http.ResponseWriter, r *http.Request) {
    18  		th.TestMethod(t, r, "POST")
    19  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    20  		th.TestJSONRequest(t, r, createRequest)
    21  
    22  		w.Header().Add("Content-Type", "application/json")
    23  		w.WriteHeader(http.StatusCreated)
    24  		_, _ = fmt.Fprint(w, createResponse)
    25  	})
    26  
    27  	iFalse := false
    28  	opts := &services.CreateOpts{
    29  		PortID:          "4189d3c2-8882-4871-a3c2-d380272eed88",
    30  		RouterID:        "4189d3c2-8882-4871-a3c2-d380272eed80",
    31  		ApprovalEnabled: &iFalse,
    32  		ServiceType:     services.ServiceTypeInterface,
    33  		ServerType:      services.ServerTypeVM,
    34  		Ports: []services.PortMapping{
    35  			{
    36  				ClientPort: 8080,
    37  				ServerPort: 90,
    38  				Protocol:   "TCP",
    39  			},
    40  			{
    41  				ClientPort: 8081,
    42  				ServerPort: 80,
    43  				Protocol:   "TCP",
    44  			},
    45  		},
    46  	}
    47  
    48  	created, err := services.Create(client.ServiceClient(), opts).Extract()
    49  	th.AssertNoErr(t, err)
    50  	th.AssertEquals(t, created.RouterID, opts.RouterID)
    51  }
    52  
    53  func TestGetRequest(t *testing.T) {
    54  	th.SetupHTTP()
    55  	defer th.TeardownHTTP()
    56  	id := "4189d3c2-8882-4871-a3c2-d380272eed80"
    57  
    58  	th.Mux.HandleFunc(fmt.Sprintf("/vpc-endpoint-services/%s", id), func(w http.ResponseWriter, r *http.Request) {
    59  		th.TestMethod(t, r, "GET")
    60  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    61  
    62  		w.Header().Add("Content-Type", "application/json")
    63  		w.WriteHeader(http.StatusOK)
    64  		_, _ = fmt.Fprint(w, createResponse)
    65  	})
    66  
    67  	svc, err := services.Get(client.ServiceClient(), id).Extract()
    68  	th.AssertNoErr(t, err)
    69  	th.AssertEquals(t, id, svc.RouterID)
    70  }
    71  
    72  func TestUpdateRequest(t *testing.T) {
    73  	th.SetupHTTP()
    74  	defer th.TeardownHTTP()
    75  	id := "4189d3c2-8882-4871-a3c2-d380272eed80"
    76  
    77  	th.Mux.HandleFunc(fmt.Sprintf("/vpc-endpoint-services/%s", id), func(w http.ResponseWriter, r *http.Request) {
    78  		th.TestMethod(t, r, "PUT")
    79  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    80  		th.TestJSONRequest(t, r, updateRequest)
    81  
    82  		w.Header().Add("Content-Type", "application/json")
    83  		w.WriteHeader(http.StatusOK)
    84  		_, _ = fmt.Fprint(w, updateResponse)
    85  	})
    86  
    87  	iTrue := true
    88  	opts := &services.UpdateOpts{
    89  		ServiceName:     "test",
    90  		ApprovalEnabled: &iTrue,
    91  		Ports: []services.PortMapping{
    92  			{
    93  				ClientPort: 8081,
    94  				ServerPort: 22,
    95  				Protocol:   "TCP",
    96  			},
    97  			{
    98  				ClientPort: 8082,
    99  				ServerPort: 23,
   100  				Protocol:   "UDP",
   101  			},
   102  		},
   103  	}
   104  
   105  	updated, err := services.Update(client.ServiceClient(), id, opts).Extract()
   106  	th.AssertNoErr(t, err)
   107  	th.AssertEquals(t, updated.ApprovalEnabled, *opts.ApprovalEnabled)
   108  	th.AssertDeepEquals(t, updated.Ports, opts.Ports)
   109  }
   110  
   111  func TestListRequest(t *testing.T) {
   112  	th.SetupHTTP()
   113  	defer th.TeardownHTTP()
   114  
   115  	name := "test123"
   116  
   117  	th.Mux.HandleFunc("/vpc-endpoint-services", func(w http.ResponseWriter, r *http.Request) {
   118  		th.TestMethod(t, r, "GET")
   119  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   120  		th.TestFormValues(t, r, map[string]string{"endpoint_service_name": name})
   121  
   122  		w.Header().Add("Content-Type", "application/json")
   123  		w.WriteHeader(http.StatusOK)
   124  		_, _ = fmt.Fprint(w, listResponse)
   125  	})
   126  
   127  	opts := services.ListOpts{
   128  		Name: name,
   129  	}
   130  	pages, err := services.List(client.ServiceClient(), opts).AllPages()
   131  	th.AssertNoErr(t, err)
   132  	list, err := services.ExtractServices(pages)
   133  	th.AssertNoErr(t, err)
   134  	th.AssertEquals(t, 1, len(list))
   135  	th.AssertEquals(t, name, list[0].ServiceName)
   136  }