github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/vpnaas/services/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/vnpaycloud-console/gophercloud/v2"
    10  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/vpnaas/services"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    13  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    14  )
    15  
    16  func TestCreate(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  
    20  	th.Mux.HandleFunc("/v2.0/vpn/vpnservices", func(w http.ResponseWriter, r *http.Request) {
    21  		th.TestMethod(t, r, "POST")
    22  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  		th.TestHeader(t, r, "Content-Type", "application/json")
    24  		th.TestHeader(t, r, "Accept", "application/json")
    25  		th.TestJSONRequest(t, r, `
    26  {
    27      "vpnservice": {
    28          "router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
    29          "name": "vpn",
    30          "admin_state_up": true,
    31  		"description": "OpenStack VPN service",
    32  		"tenant_id":  "10039663455a446d8ba2cbb058b0f578"
    33      }
    34  }      `)
    35  
    36  		w.Header().Add("Content-Type", "application/json")
    37  		w.WriteHeader(http.StatusCreated)
    38  
    39  		fmt.Fprint(w, `
    40  {
    41      "vpnservice": {
    42          "router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
    43          "status": "PENDING_CREATE",
    44          "name": "vpn",
    45          "external_v6_ip": "2001:db8::1",
    46          "admin_state_up": true,
    47          "subnet_id": null,
    48          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
    49          "external_v4_ip": "172.32.1.11",
    50          "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    51          "description": "OpenStack VPN service",
    52  		"project_id": "10039663455a446d8ba2cbb058b0f578"
    53      }
    54  }
    55      `)
    56  	})
    57  
    58  	options := services.CreateOpts{
    59  		TenantID:     "10039663455a446d8ba2cbb058b0f578",
    60  		Name:         "vpn",
    61  		Description:  "OpenStack VPN service",
    62  		AdminStateUp: gophercloud.Enabled,
    63  		RouterID:     "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
    64  	}
    65  	actual, err := services.Create(context.TODO(), fake.ServiceClient(), options).Extract()
    66  	th.AssertNoErr(t, err)
    67  	expected := services.Service{
    68  		RouterID:     "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
    69  		Status:       "PENDING_CREATE",
    70  		Name:         "vpn",
    71  		ExternalV6IP: "2001:db8::1",
    72  		AdminStateUp: true,
    73  		SubnetID:     "",
    74  		TenantID:     "10039663455a446d8ba2cbb058b0f578",
    75  		ProjectID:    "10039663455a446d8ba2cbb058b0f578",
    76  		ExternalV4IP: "172.32.1.11",
    77  		ID:           "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    78  		Description:  "OpenStack VPN service",
    79  	}
    80  	th.AssertDeepEquals(t, expected, *actual)
    81  }
    82  
    83  func TestList(t *testing.T) {
    84  	th.SetupHTTP()
    85  	defer th.TeardownHTTP()
    86  
    87  	th.Mux.HandleFunc("/v2.0/vpn/vpnservices", func(w http.ResponseWriter, r *http.Request) {
    88  		th.TestMethod(t, r, "GET")
    89  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    90  
    91  		w.Header().Add("Content-Type", "application/json")
    92  		w.WriteHeader(http.StatusOK)
    93  
    94  		fmt.Fprint(w, `
    95  {
    96     "vpnservices":[
    97          {
    98              "router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
    99              "status": "PENDING_CREATE",
   100              "name": "vpnservice1",
   101              "admin_state_up": true,
   102              "subnet_id": null,
   103              "project_id": "10039663455a446d8ba2cbb058b0f578",
   104              "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   105              "description": "Test VPN service"
   106          }
   107     ]
   108  }
   109        `)
   110  	})
   111  
   112  	count := 0
   113  
   114  	err := services.List(fake.ServiceClient(), services.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   115  		count++
   116  		actual, err := services.ExtractServices(page)
   117  		if err != nil {
   118  			t.Errorf("Failed to extract members: %v", err)
   119  			return false, err
   120  		}
   121  
   122  		expected := []services.Service{
   123  			{
   124  				Status:       "PENDING_CREATE",
   125  				Name:         "vpnservice1",
   126  				AdminStateUp: true,
   127  				TenantID:     "10039663455a446d8ba2cbb058b0f578",
   128  				ProjectID:    "10039663455a446d8ba2cbb058b0f578",
   129  				Description:  "Test VPN service",
   130  				SubnetID:     "",
   131  				RouterID:     "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
   132  			},
   133  		}
   134  
   135  		th.CheckDeepEquals(t, expected, actual)
   136  
   137  		return true, nil
   138  	})
   139  	th.AssertNoErr(t, err)
   140  
   141  	if count != 1 {
   142  		t.Errorf("Expected 1 page, got %d", count)
   143  	}
   144  }
   145  
   146  func TestGet(t *testing.T) {
   147  	th.SetupHTTP()
   148  	defer th.TeardownHTTP()
   149  
   150  	th.Mux.HandleFunc("/v2.0/vpn/vpnservices/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   151  		th.TestMethod(t, r, "GET")
   152  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   153  
   154  		w.Header().Add("Content-Type", "application/json")
   155  		w.WriteHeader(http.StatusOK)
   156  
   157  		fmt.Fprint(w, `
   158  {
   159      "vpnservice": {
   160          "router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
   161          "status": "PENDING_CREATE",
   162          "name": "vpnservice1",
   163          "admin_state_up": true,
   164          "subnet_id": null,
   165          "project_id": "10039663455a446d8ba2cbb058b0f578",
   166          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   167          "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   168          "description": "VPN test service"
   169      }
   170  }
   171          `)
   172  	})
   173  
   174  	actual, err := services.Get(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract()
   175  	th.AssertNoErr(t, err)
   176  	expected := services.Service{
   177  		Status:       "PENDING_CREATE",
   178  		Name:         "vpnservice1",
   179  		Description:  "VPN test service",
   180  		AdminStateUp: true,
   181  		ID:           "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   182  		TenantID:     "10039663455a446d8ba2cbb058b0f578",
   183  		ProjectID:    "10039663455a446d8ba2cbb058b0f578",
   184  		RouterID:     "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
   185  		SubnetID:     "",
   186  	}
   187  	th.AssertDeepEquals(t, expected, *actual)
   188  }
   189  
   190  func TestDelete(t *testing.T) {
   191  	th.SetupHTTP()
   192  	defer th.TeardownHTTP()
   193  
   194  	th.Mux.HandleFunc("/v2.0/vpn/vpnservices/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   195  		th.TestMethod(t, r, "DELETE")
   196  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   197  		w.WriteHeader(http.StatusNoContent)
   198  	})
   199  	res := services.Delete(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828")
   200  	th.AssertNoErr(t, res.Err)
   201  }
   202  
   203  func TestUpdate(t *testing.T) {
   204  	th.SetupHTTP()
   205  	defer th.TeardownHTTP()
   206  
   207  	th.Mux.HandleFunc("/v2.0/vpn/vpnservices/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   208  
   209  		th.TestMethod(t, r, "PUT")
   210  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   211  		th.TestHeader(t, r, "Content-Type", "application/json")
   212  		th.TestHeader(t, r, "Accept", "application/json")
   213  		th.TestJSONRequest(t, r, `
   214  {
   215      "vpnservice":{
   216          "name": "updatedname",
   217          "description": "updated service",
   218          "admin_state_up": false
   219      }
   220  }
   221        `)
   222  
   223  		w.Header().Add("Content-Type", "application/json")
   224  		w.WriteHeader(http.StatusOK)
   225  
   226  		fmt.Fprint(w, `
   227  {
   228      "vpnservice": {
   229          "router_id": "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
   230          "status": "PENDING_CREATE",
   231          "name": "updatedname",
   232          "admin_state_up": false,
   233          "subnet_id": null,
   234          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   235          "project_id": "10039663455a446d8ba2cbb058b0f578",
   236          "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   237          "description": "updated service",
   238  		"external_v4_ip": "172.32.1.11",
   239  		"external_v6_ip": "2001:db8::1"
   240      }
   241  }
   242      `)
   243  	})
   244  	updatedName := "updatedname"
   245  	updatedServiceDescription := "updated service"
   246  	options := services.UpdateOpts{
   247  		Name:         &updatedName,
   248  		Description:  &updatedServiceDescription,
   249  		AdminStateUp: gophercloud.Disabled,
   250  	}
   251  
   252  	actual, err := services.Update(context.TODO(), fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract()
   253  	th.AssertNoErr(t, err)
   254  	expected := services.Service{
   255  		RouterID:     "66e3b16c-8ce5-40fb-bb49-ab6d8dc3f2aa",
   256  		Status:       "PENDING_CREATE",
   257  		Name:         "updatedname",
   258  		ExternalV6IP: "2001:db8::1",
   259  		AdminStateUp: false,
   260  		SubnetID:     "",
   261  		TenantID:     "10039663455a446d8ba2cbb058b0f578",
   262  		ProjectID:    "10039663455a446d8ba2cbb058b0f578",
   263  		ExternalV4IP: "172.32.1.11",
   264  		ID:           "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   265  		Description:  "updated service",
   266  	}
   267  	th.AssertDeepEquals(t, expected, *actual)
   268  
   269  }