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