github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/vpnaas/siteconnections/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/siteconnections"
    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/ipsec-site-connections", 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      
    27      "ipsec_site_connection": {
    28          "psk": "secret",
    29          "initiator": "bi-directional",
    30          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
    31          "admin_state_up": true,
    32          "mtu": 1500,
    33          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
    34          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
    35          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    36          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
    37          "peer_address": "172.24.4.233",
    38          "peer_id": "172.24.4.233",
    39          "name": "vpnconnection1"
    40      
    41  }
    42  }      `)
    43  
    44  		w.Header().Add("Content-Type", "application/json")
    45  		w.WriteHeader(http.StatusCreated)
    46  
    47  		fmt.Fprintf(w, `
    48  {
    49      "ipsec_site_connection": {
    50          "status": "PENDING_CREATE",
    51          "psk": "secret",
    52          "initiator": "bi-directional",
    53          "name": "vpnconnection1",
    54          "admin_state_up": true,
    55          "project_id": "10039663455a446d8ba2cbb058b0f578",
    56          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
    57          "auth_mode": "psk",
    58          "peer_cidrs": [],
    59          "mtu": 1500,
    60          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
    61          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
    62          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    63          "dpd": {
    64              "action": "hold",
    65              "interval": 30,
    66              "timeout": 120
    67          },
    68          "route_mode": "static",
    69          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
    70          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
    71          "peer_address": "172.24.4.233",
    72          "peer_id": "172.24.4.233",
    73          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
    74          "description": ""
    75      }
    76  }
    77      `)
    78  	})
    79  
    80  	options := siteconnections.CreateOpts{
    81  		Name:           "vpnconnection1",
    82  		AdminStateUp:   golangsdk.Enabled,
    83  		PSK:            "secret",
    84  		Initiator:      siteconnections.InitiatorBiDirectional,
    85  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
    86  		MTU:            1500,
    87  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
    88  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
    89  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
    90  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
    91  		PeerAddress:    "172.24.4.233",
    92  		PeerID:         "172.24.4.233",
    93  	}
    94  	actual, err := siteconnections.Create(fake.ServiceClient(), options).Extract()
    95  	th.AssertNoErr(t, err)
    96  	expectedDPD := siteconnections.DPD{
    97  		Action:   "hold",
    98  		Interval: 30,
    99  		Timeout:  120,
   100  	}
   101  	expected := siteconnections.Connection{
   102  		TenantID:       "10039663455a446d8ba2cbb058b0f578",
   103  		Name:           "vpnconnection1",
   104  		AdminStateUp:   true,
   105  		PSK:            "secret",
   106  		Initiator:      "bi-directional",
   107  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   108  		MTU:            1500,
   109  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   110  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   111  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   112  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   113  		PeerAddress:    "172.24.4.233",
   114  		PeerID:         "172.24.4.233",
   115  		Status:         "PENDING_CREATE",
   116  		ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   117  		AuthMode:       "psk",
   118  		PeerCIDRs:      []string{},
   119  		DPD:            expectedDPD,
   120  		RouteMode:      "static",
   121  		ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   122  		Description:    "",
   123  	}
   124  	th.AssertDeepEquals(t, expected, *actual)
   125  }
   126  
   127  func TestDelete(t *testing.T) {
   128  	th.SetupHTTP()
   129  	defer th.TeardownHTTP()
   130  
   131  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   132  		th.TestMethod(t, r, "DELETE")
   133  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   134  		w.WriteHeader(http.StatusNoContent)
   135  	})
   136  
   137  	res := siteconnections.Delete(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828")
   138  	th.AssertNoErr(t, res.Err)
   139  }
   140  
   141  func TestGet(t *testing.T) {
   142  	th.SetupHTTP()
   143  	defer th.TeardownHTTP()
   144  
   145  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   146  		th.TestMethod(t, r, "GET")
   147  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   148  
   149  		w.Header().Add("Content-Type", "application/json")
   150  		w.WriteHeader(http.StatusOK)
   151  
   152  		fmt.Fprintf(w, `
   153  {
   154      "ipsec_site_connection": {
   155          "status": "PENDING_CREATE",
   156          "psk": "secret",
   157          "initiator": "bi-directional",
   158          "name": "vpnconnection1",
   159          "admin_state_up": true,
   160          "project_id": "10039663455a446d8ba2cbb058b0f578",
   161          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   162          "auth_mode": "psk",
   163          "peer_cidrs": [],
   164          "mtu": 1500,
   165          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   166          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   167          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   168          "dpd": {
   169              "action": "hold",
   170              "interval": 30,
   171              "timeout": 120
   172          },
   173          "route_mode": "static",
   174          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   175          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
   176          "peer_address": "172.24.4.233",
   177          "peer_id": "172.24.4.233",
   178          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
   179          "description": ""
   180      }
   181  }
   182          `)
   183  	})
   184  
   185  	actual, err := siteconnections.Get(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract()
   186  	th.AssertNoErr(t, err)
   187  	expectedDPD := siteconnections.DPD{
   188  		Action:   "hold",
   189  		Interval: 30,
   190  		Timeout:  120,
   191  	}
   192  	expected := siteconnections.Connection{
   193  		TenantID:       "10039663455a446d8ba2cbb058b0f578",
   194  		Name:           "vpnconnection1",
   195  		AdminStateUp:   true,
   196  		PSK:            "secret",
   197  		Initiator:      "bi-directional",
   198  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   199  		MTU:            1500,
   200  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   201  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   202  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   203  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   204  		PeerAddress:    "172.24.4.233",
   205  		PeerID:         "172.24.4.233",
   206  		Status:         "PENDING_CREATE",
   207  		ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   208  		AuthMode:       "psk",
   209  		PeerCIDRs:      []string{},
   210  		DPD:            expectedDPD,
   211  		RouteMode:      "static",
   212  		ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   213  		Description:    "",
   214  	}
   215  	th.AssertDeepEquals(t, expected, *actual)
   216  }
   217  
   218  func TestList(t *testing.T) {
   219  	th.SetupHTTP()
   220  	defer th.TeardownHTTP()
   221  
   222  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections", func(w http.ResponseWriter, r *http.Request) {
   223  		th.TestMethod(t, r, "GET")
   224  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   225  
   226  		w.Header().Add("Content-Type", "application/json")
   227  		w.WriteHeader(http.StatusOK)
   228  
   229  		fmt.Fprintf(w, `
   230  {
   231      "ipsec_site_connections":[
   232  	{
   233          "status": "PENDING_CREATE",
   234          "psk": "secret",
   235          "initiator": "bi-directional",
   236          "name": "vpnconnection1",
   237          "admin_state_up": true,
   238          "project_id": "10039663455a446d8ba2cbb058b0f578",
   239          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   240          "auth_mode": "psk",
   241          "peer_cidrs": [],
   242          "mtu": 1500,
   243          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   244          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   245          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   246          "dpd": {
   247              "action": "hold",
   248              "interval": 30,
   249              "timeout": 120
   250          },
   251          "route_mode": "static",
   252          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   253          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
   254          "peer_address": "172.24.4.233",
   255          "peer_id": "172.24.4.233",
   256          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
   257          "description": ""
   258      }]
   259  }
   260        `)
   261  	})
   262  
   263  	count := 0
   264  
   265  	siteconnections.List(fake.ServiceClient(), siteconnections.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
   266  		count++
   267  		actual, err := siteconnections.ExtractConnections(page)
   268  		if err != nil {
   269  			t.Errorf("Failed to extract members: %v", err)
   270  			return false, err
   271  		}
   272  
   273  		expectedDPD := siteconnections.DPD{
   274  			Action:   "hold",
   275  			Interval: 30,
   276  			Timeout:  120,
   277  		}
   278  		expected := []siteconnections.Connection{
   279  			{
   280  				TenantID:       "10039663455a446d8ba2cbb058b0f578",
   281  				Name:           "vpnconnection1",
   282  				AdminStateUp:   true,
   283  				PSK:            "secret",
   284  				Initiator:      "bi-directional",
   285  				IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   286  				MTU:            1500,
   287  				PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   288  				IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   289  				VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   290  				LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   291  				PeerAddress:    "172.24.4.233",
   292  				PeerID:         "172.24.4.233",
   293  				Status:         "PENDING_CREATE",
   294  				ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   295  				AuthMode:       "psk",
   296  				PeerCIDRs:      []string{},
   297  				DPD:            expectedDPD,
   298  				RouteMode:      "static",
   299  				ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   300  				Description:    "",
   301  			},
   302  		}
   303  
   304  		th.CheckDeepEquals(t, expected, actual)
   305  
   306  		return true, nil
   307  	})
   308  
   309  	if count != 1 {
   310  		t.Errorf("Expected 1 page, got %d", count)
   311  	}
   312  }
   313  
   314  func TestUpdate(t *testing.T) {
   315  	th.SetupHTTP()
   316  	defer th.TeardownHTTP()
   317  
   318  	th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   319  		th.TestMethod(t, r, "PUT")
   320  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   321  		th.TestHeader(t, r, "Content-Type", "application/json")
   322  		th.TestHeader(t, r, "Accept", "application/json")
   323  		th.TestJSONRequest(t, r, `
   324  	{
   325  		  "ipsec_site_connection": {
   326  				"psk": "updatedsecret",
   327  				"initiator": "response-only",
   328  				"name": "updatedconnection",
   329  				"description": "updateddescription"
   330  		}
   331  	}
   332  	`)
   333  
   334  		w.Header().Add("Content-Type", "application/json")
   335  		w.WriteHeader(http.StatusOK)
   336  
   337  		fmt.Fprintf(w, `
   338  
   339  	{
   340      "ipsec_site_connection": {
   341          "status": "ACTIVE",
   342          "psk": "updatedsecret",
   343          "initiator": "response-only",
   344          "name": "updatedconnection",
   345          "admin_state_up": true,
   346          "project_id": "10039663455a446d8ba2cbb058b0f578",
   347          "tenant_id": "10039663455a446d8ba2cbb058b0f578",
   348          "auth_mode": "psk",
   349          "peer_cidrs": [],
   350          "mtu": 1500,
   351          "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   352          "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   353          "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   354          "dpd": {
   355              "action": "hold",
   356              "interval": 30,
   357              "timeout": 120
   358          },
   359          "route_mode": "static",
   360          "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   361          "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68",
   362          "peer_address": "172.24.4.233",
   363          "peer_id": "172.24.4.233",
   364          "id": "851f280f-5639-4ea3-81aa-e298525ab74b",
   365          "description": "updateddescription"
   366      }
   367  }
   368  }
   369  `)
   370  	})
   371  	updatedName := "updatedconnection"
   372  	updatedDescription := "updateddescription"
   373  	options := siteconnections.UpdateOpts{
   374  		Name:        &updatedName,
   375  		Description: &updatedDescription,
   376  		Initiator:   siteconnections.InitiatorResponseOnly,
   377  		PSK:         "updatedsecret",
   378  	}
   379  
   380  	actual, err := siteconnections.Update(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract()
   381  	th.AssertNoErr(t, err)
   382  
   383  	expectedDPD := siteconnections.DPD{
   384  		Action:   "hold",
   385  		Interval: 30,
   386  		Timeout:  120,
   387  	}
   388  
   389  	expected := siteconnections.Connection{
   390  		TenantID:       "10039663455a446d8ba2cbb058b0f578",
   391  		Name:           "updatedconnection",
   392  		AdminStateUp:   true,
   393  		PSK:            "updatedsecret",
   394  		Initiator:      "response-only",
   395  		IPSecPolicyID:  "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1",
   396  		MTU:            1500,
   397  		PeerEPGroupID:  "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1",
   398  		IKEPolicyID:    "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f",
   399  		VPNServiceID:   "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   400  		LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68",
   401  		PeerAddress:    "172.24.4.233",
   402  		PeerID:         "172.24.4.233",
   403  		Status:         "ACTIVE",
   404  		ProjectID:      "10039663455a446d8ba2cbb058b0f578",
   405  		AuthMode:       "psk",
   406  		PeerCIDRs:      []string{},
   407  		DPD:            expectedDPD,
   408  		RouteMode:      "static",
   409  		ID:             "851f280f-5639-4ea3-81aa-e298525ab74b",
   410  		Description:    "updateddescription",
   411  	}
   412  	th.AssertDeepEquals(t, expected, *actual)
   413  }