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