github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/vpnaas/endpointgroups/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/endpointgroups"
    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/endpoint-groups", 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      "endpoint_group": {
    26          "endpoints": [
    27              "10.2.0.0/24",
    28              "10.3.0.0/24"
    29          ],
    30          "type": "cidr",
    31          "name": "peers"
    32      }
    33  }     `)
    34  
    35  		w.Header().Add("Content-Type", "application/json")
    36  		w.WriteHeader(http.StatusCreated)
    37  
    38  		fmt.Fprintf(w, `
    39  {
    40      "endpoint_group": {
    41          "description": "",
    42          "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d",
    43          "project_id": "4ad57e7ce0b24fca8f12b9834d91079d",
    44          "endpoints": [
    45              "10.2.0.0/24",
    46              "10.3.0.0/24"
    47          ],
    48          "type": "cidr",
    49          "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
    50          "name": "peers"
    51      }
    52  }
    53      `)
    54  	})
    55  
    56  	options := endpointgroups.CreateOpts{
    57  		Name: "peers",
    58  		Type: endpointgroups.TypeCIDR,
    59  		Endpoints: []string{
    60  			"10.2.0.0/24",
    61  			"10.3.0.0/24",
    62  		},
    63  	}
    64  	actual, err := endpointgroups.Create(fake.ServiceClient(), options).Extract()
    65  	th.AssertNoErr(t, err)
    66  	expected := endpointgroups.EndpointGroup{
    67  		Name:        "peers",
    68  		TenantID:    "4ad57e7ce0b24fca8f12b9834d91079d",
    69  		ProjectID:   "4ad57e7ce0b24fca8f12b9834d91079d",
    70  		ID:          "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
    71  		Description: "",
    72  		Endpoints: []string{
    73  			"10.2.0.0/24",
    74  			"10.3.0.0/24",
    75  		},
    76  		Type: "cidr",
    77  	}
    78  	th.AssertDeepEquals(t, expected, *actual)
    79  }
    80  
    81  func TestGet(t *testing.T) {
    82  	th.SetupHTTP()
    83  	defer th.TeardownHTTP()
    84  
    85  	th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
    86  		th.TestMethod(t, r, "GET")
    87  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    88  
    89  		w.Header().Add("Content-Type", "application/json")
    90  		w.WriteHeader(http.StatusOK)
    91  
    92  		fmt.Fprintf(w, `
    93  {
    94      "endpoint_group": {
    95          "description": "",
    96          "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d",
    97          "project_id": "4ad57e7ce0b24fca8f12b9834d91079d",
    98          "endpoints": [
    99              "10.2.0.0/24",
   100              "10.3.0.0/24"
   101          ],
   102          "type": "cidr",
   103          "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
   104          "name": "peers"
   105      }
   106  }
   107          `)
   108  	})
   109  
   110  	actual, err := endpointgroups.Get(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract()
   111  	th.AssertNoErr(t, err)
   112  	expected := endpointgroups.EndpointGroup{
   113  		Name:        "peers",
   114  		TenantID:    "4ad57e7ce0b24fca8f12b9834d91079d",
   115  		ProjectID:   "4ad57e7ce0b24fca8f12b9834d91079d",
   116  		ID:          "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
   117  		Description: "",
   118  		Endpoints: []string{
   119  			"10.2.0.0/24",
   120  			"10.3.0.0/24",
   121  		},
   122  		Type: "cidr",
   123  	}
   124  	th.AssertDeepEquals(t, expected, *actual)
   125  }
   126  
   127  func TestList(t *testing.T) {
   128  	th.SetupHTTP()
   129  	defer th.TeardownHTTP()
   130  
   131  	th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups", func(w http.ResponseWriter, r *http.Request) {
   132  		th.TestMethod(t, r, "GET")
   133  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   134  		w.Header().Add("Content-Type", "application/json")
   135  		w.WriteHeader(http.StatusOK)
   136  
   137  		fmt.Fprintf(w, `
   138  		{
   139  	"endpoint_groups": [
   140  		{
   141          "description": "",
   142          "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d",
   143          "project_id": "4ad57e7ce0b24fca8f12b9834d91079d",
   144          "endpoints": [
   145              "10.2.0.0/24",
   146              "10.3.0.0/24"
   147          ],
   148          "type": "cidr",
   149          "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
   150          "name": "peers"
   151  		}
   152  	]
   153  }
   154  	  `)
   155  	})
   156  
   157  	count := 0
   158  
   159  	endpointgroups.List(fake.ServiceClient(), endpointgroups.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
   160  		count++
   161  		actual, err := endpointgroups.ExtractEndpointGroups(page)
   162  		if err != nil {
   163  			t.Errorf("Failed to extract members: %v", err)
   164  			return false, err
   165  		}
   166  		expected := []endpointgroups.EndpointGroup{
   167  			{
   168  				Name:        "peers",
   169  				TenantID:    "4ad57e7ce0b24fca8f12b9834d91079d",
   170  				ProjectID:   "4ad57e7ce0b24fca8f12b9834d91079d",
   171  				ID:          "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
   172  				Description: "",
   173  				Endpoints: []string{
   174  					"10.2.0.0/24",
   175  					"10.3.0.0/24",
   176  				},
   177  				Type: "cidr",
   178  			},
   179  		}
   180  		th.CheckDeepEquals(t, expected, actual)
   181  
   182  		return true, nil
   183  	})
   184  
   185  	if count != 1 {
   186  		t.Errorf("Expected 1 page, got %d", count)
   187  	}
   188  }
   189  
   190  func TestDelete(t *testing.T) {
   191  	th.SetupHTTP()
   192  	defer th.TeardownHTTP()
   193  
   194  	th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups/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  
   200  	res := endpointgroups.Delete(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828")
   201  	th.AssertNoErr(t, res.Err)
   202  }
   203  
   204  func TestUpdate(t *testing.T) {
   205  	th.SetupHTTP()
   206  	defer th.TeardownHTTP()
   207  
   208  	th.Mux.HandleFunc("/v2.0/vpn/endpoint-groups/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) {
   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      "endpoint_group": {
   216          "description": "updated description",
   217          "name": "updatedname"
   218      }
   219  }
   220  			`)
   221  
   222  		w.Header().Add("Content-Type", "application/json")
   223  		w.WriteHeader(http.StatusOK)
   224  
   225  		fmt.Fprintf(w, `
   226  {
   227      "endpoint_group": {
   228          "description": "updated description",
   229          "tenant_id": "4ad57e7ce0b24fca8f12b9834d91079d",
   230          "project_id": "4ad57e7ce0b24fca8f12b9834d91079d",
   231          "endpoints": [
   232              "10.2.0.0/24",
   233              "10.3.0.0/24"
   234          ],
   235          "type": "cidr",
   236          "id": "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
   237          "name": "updatedname"
   238      }
   239  }
   240  `)
   241  	})
   242  
   243  	updatedName := "updatedname"
   244  	updatedDescription := "updated description"
   245  	options := endpointgroups.UpdateOpts{
   246  		Name:        &updatedName,
   247  		Description: &updatedDescription,
   248  	}
   249  
   250  	actual, err := endpointgroups.Update(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract()
   251  	th.AssertNoErr(t, err)
   252  	expected := endpointgroups.EndpointGroup{
   253  		Name:        "updatedname",
   254  		TenantID:    "4ad57e7ce0b24fca8f12b9834d91079d",
   255  		ProjectID:   "4ad57e7ce0b24fca8f12b9834d91079d",
   256  		ID:          "6ecd9cf3-ca64-46c7-863f-f2eb1b9e838a",
   257  		Description: "updated description",
   258  		Endpoints: []string{
   259  			"10.2.0.0/24",
   260  			"10.3.0.0/24",
   261  		},
   262  		Type: "cidr",
   263  	}
   264  	th.AssertDeepEquals(t, expected, *actual)
   265  }