github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/vpnaas/ikepolicies/testing/requests_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/vpnaas/ikepolicies"
    10  	"github.com/huaweicloud/golangsdk/pagination"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  )
    13  
    14  func TestCreate(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  
    18  	th.Mux.HandleFunc("/v2.0/vpn/ikepolicies", func(w http.ResponseWriter, r *http.Request) {
    19  		th.TestMethod(t, r, "POST")
    20  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    21  		th.TestHeader(t, r, "Content-Type", "application/json")
    22  		th.TestHeader(t, r, "Accept", "application/json")
    23  		th.TestJSONRequest(t, r, `
    24  {
    25      "ikepolicy":{
    26          "name": "policy",
    27          "description": "IKE policy",
    28  		"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
    29  		"ike_version": "v2"
    30      }
    31  }
    32        `)
    33  
    34  		w.Header().Add("Content-Type", "application/json")
    35  		w.WriteHeader(http.StatusCreated)
    36  
    37  		fmt.Fprintf(w, `
    38  {
    39      "ikepolicy":{
    40          "name": "policy",
    41          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
    42          "project_id": "9145d91459d248b1b02fdaca97c6a75d",
    43          "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
    44          "description": "IKE policy",
    45  		"auth_algorithm": "sha1",
    46  		"encryption_algorithm": "aes-128",
    47  		"pfs": "Group5",
    48  		"lifetime": {
    49  			"value": 3600,
    50  			"units": "seconds"
    51  		},
    52  		"phase1_negotiation_mode": "main",
    53  		"ike_version": "v2"
    54      }
    55  }
    56          `)
    57  	})
    58  
    59  	options := ikepolicies.CreateOpts{
    60  		TenantID:    "9145d91459d248b1b02fdaca97c6a75d",
    61  		Name:        "policy",
    62  		Description: "IKE policy",
    63  		IKEVersion:  ikepolicies.IKEVersionv2,
    64  	}
    65  
    66  	actual, err := ikepolicies.Create(fake.ServiceClient(), options).Extract()
    67  	th.AssertNoErr(t, err)
    68  	expectedLifetime := ikepolicies.Lifetime{
    69  		Units: "seconds",
    70  		Value: 3600,
    71  	}
    72  	expected := ikepolicies.Policy{
    73  		AuthAlgorithm:         "sha1",
    74  		IKEVersion:            "v2",
    75  		TenantID:              "9145d91459d248b1b02fdaca97c6a75d",
    76  		Phase1NegotiationMode: "main",
    77  		PFS:                   "Group5",
    78  		EncryptionAlgorithm:   "aes-128",
    79  		Description:           "IKE policy",
    80  		Name:                  "policy",
    81  		ID:                    "f2b08c1e-aa81-4668-8ae1-1401bcb0576c",
    82  		Lifetime:              expectedLifetime,
    83  		ProjectID:             "9145d91459d248b1b02fdaca97c6a75d",
    84  	}
    85  	th.AssertDeepEquals(t, expected, *actual)
    86  }
    87  
    88  func TestGet(t *testing.T) {
    89  	th.SetupHTTP()
    90  	defer th.TeardownHTTP()
    91  
    92  	th.Mux.HandleFunc("/v2.0/vpn/ikepolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
    93  		th.TestMethod(t, r, "GET")
    94  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    95  
    96  		w.Header().Add("Content-Type", "application/json")
    97  		w.WriteHeader(http.StatusOK)
    98  
    99  		fmt.Fprintf(w, `
   100  {
   101      "ikepolicy":{
   102          "name": "policy",
   103          "tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   104          "project_id": "9145d91459d248b1b02fdaca97c6a75d",
   105          "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   106          "description": "IKE policy",
   107  		"auth_algorithm": "sha1",
   108  		"encryption_algorithm": "aes-128",
   109  		"pfs": "Group5",
   110  		"lifetime": {
   111  			"value": 3600,
   112  			"units": "seconds"
   113  		},
   114  		"phase1_negotiation_mode": "main",
   115  		"ike_version": "v2"
   116      }
   117  }
   118          `)
   119  	})
   120  
   121  	actual, err := ikepolicies.Get(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract()
   122  	th.AssertNoErr(t, err)
   123  	expectedLifetime := ikepolicies.Lifetime{
   124  		Units: "seconds",
   125  		Value: 3600,
   126  	}
   127  	expected := ikepolicies.Policy{
   128  		AuthAlgorithm:         "sha1",
   129  		IKEVersion:            "v2",
   130  		TenantID:              "9145d91459d248b1b02fdaca97c6a75d",
   131  		ProjectID:             "9145d91459d248b1b02fdaca97c6a75d",
   132  		Phase1NegotiationMode: "main",
   133  		PFS:                   "Group5",
   134  		EncryptionAlgorithm:   "aes-128",
   135  		Description:           "IKE policy",
   136  		Name:                  "policy",
   137  		ID:                    "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   138  		Lifetime:              expectedLifetime,
   139  	}
   140  	th.AssertDeepEquals(t, expected, *actual)
   141  }
   142  
   143  func TestDelete(t *testing.T) {
   144  	th.SetupHTTP()
   145  	defer th.TeardownHTTP()
   146  
   147  	th.Mux.HandleFunc("/v2.0/vpn/ikepolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   148  		th.TestMethod(t, r, "DELETE")
   149  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   150  		w.WriteHeader(http.StatusNoContent)
   151  	})
   152  
   153  	res := ikepolicies.Delete(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828")
   154  	th.AssertNoErr(t, res.Err)
   155  }
   156  
   157  func TestList(t *testing.T) {
   158  	th.SetupHTTP()
   159  	defer th.TeardownHTTP()
   160  
   161  	th.Mux.HandleFunc("/v2.0/vpn/ikepolicies", func(w http.ResponseWriter, r *http.Request) {
   162  		th.TestMethod(t, r, "GET")
   163  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   164  		w.Header().Add("Content-Type", "application/json")
   165  		w.WriteHeader(http.StatusOK)
   166  
   167  		fmt.Fprintf(w, `
   168  		{
   169  	"ikepolicies": [
   170  		{
   171  			"name": "policy",
   172  			"tenant_id": "9145d91459d248b1b02fdaca97c6a75d",
   173  			"project_id": "9145d91459d248b1b02fdaca97c6a75d",
   174  			"id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   175  			"description": "IKE policy",
   176  			"auth_algorithm": "sha1",
   177  			"encryption_algorithm": "aes-128",
   178  			"pfs": "Group5",
   179  			"lifetime": {
   180  				"value": 3600,
   181  				"units": "seconds"
   182  			},
   183  			"phase1_negotiation_mode": "main",
   184  			"ike_version": "v2"
   185  		}
   186  	]
   187  }
   188  	  `)
   189  	})
   190  
   191  	count := 0
   192  
   193  	ikepolicies.List(fake.ServiceClient(), ikepolicies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
   194  		count++
   195  		actual, err := ikepolicies.ExtractPolicies(page)
   196  		if err != nil {
   197  			t.Errorf("Failed to extract members: %v", err)
   198  			return false, err
   199  		}
   200  		expectedLifetime := ikepolicies.Lifetime{
   201  			Units: "seconds",
   202  			Value: 3600,
   203  		}
   204  		expected := []ikepolicies.Policy{
   205  			{
   206  				AuthAlgorithm:         "sha1",
   207  				IKEVersion:            "v2",
   208  				TenantID:              "9145d91459d248b1b02fdaca97c6a75d",
   209  				ProjectID:             "9145d91459d248b1b02fdaca97c6a75d",
   210  				Phase1NegotiationMode: "main",
   211  				PFS:                   "Group5",
   212  				EncryptionAlgorithm:   "aes-128",
   213  				Description:           "IKE policy",
   214  				Name:                  "policy",
   215  				ID:                    "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   216  				Lifetime:              expectedLifetime,
   217  			},
   218  		}
   219  
   220  		th.CheckDeepEquals(t, expected, actual)
   221  
   222  		return true, nil
   223  	})
   224  
   225  	if count != 1 {
   226  		t.Errorf("Expected 1 page, got %d", count)
   227  	}
   228  }
   229  
   230  func TestUpdate(t *testing.T) {
   231  	th.SetupHTTP()
   232  	defer th.TeardownHTTP()
   233  
   234  	th.Mux.HandleFunc("/v2.0/vpn/ikepolicies/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   235  		th.TestMethod(t, r, "PUT")
   236  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   237  		th.TestHeader(t, r, "Content-Type", "application/json")
   238  		th.TestHeader(t, r, "Accept", "application/json")
   239  		th.TestJSONRequest(t, r, `
   240  			{
   241  				"ikepolicy":{
   242  			"name": "updatedname",
   243  			"description": "updated policy",
   244  			"lifetime": {
   245  			"value": 7000
   246  			}
   247  			}
   248  			}
   249  			`)
   250  
   251  		w.Header().Add("Content-Type", "application/json")
   252  		w.WriteHeader(http.StatusOK)
   253  
   254  		fmt.Fprintf(w, `
   255  {
   256      "ikepolicy": {
   257          "name": "updatedname",
   258          "transform_protocol": "esp",
   259          "auth_algorithm": "sha1",
   260          "encapsulation_mode": "tunnel",
   261          "encryption_algorithm": "aes-128",
   262          "pfs": "group5",
   263          "tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   264          "project_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
   265          "lifetime": {
   266              "units": "seconds",
   267              "value": 7000
   268          },
   269          "id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   270          "description": "updated policy"
   271      }
   272  }
   273  `)
   274  	})
   275  
   276  	updatedName := "updatedname"
   277  	updatedDescription := "updated policy"
   278  	options := ikepolicies.UpdateOpts{
   279  		Name:        &updatedName,
   280  		Description: &updatedDescription,
   281  		Lifetime: &ikepolicies.LifetimeUpdateOpts{
   282  			Value: 7000,
   283  		},
   284  	}
   285  
   286  	actual, err := ikepolicies.Update(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract()
   287  	th.AssertNoErr(t, err)
   288  	expectedLifetime := ikepolicies.Lifetime{
   289  		Units: "seconds",
   290  		Value: 7000,
   291  	}
   292  	expected := ikepolicies.Policy{
   293  		TenantID:            "b4eedccc6fb74fa8a7ad6b08382b852b",
   294  		ProjectID:           "b4eedccc6fb74fa8a7ad6b08382b852b",
   295  		Name:                "updatedname",
   296  		AuthAlgorithm:       "sha1",
   297  		EncryptionAlgorithm: "aes-128",
   298  		PFS:                 "group5",
   299  		Description:         "updated policy",
   300  		Lifetime:            expectedLifetime,
   301  		ID:                  "5c561d9d-eaea-45f6-ae3e-08d1a7080828",
   302  	}
   303  	th.AssertDeepEquals(t, expected, *actual)
   304  }