github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas/vips/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/lbaas/vips"
    11  	"github.com/huaweicloud/golangsdk/pagination"
    12  	th "github.com/huaweicloud/golangsdk/testhelper"
    13  )
    14  
    15  func TestList(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/lb/vips", func(w http.ResponseWriter, r *http.Request) {
    20  		th.TestMethod(t, r, "GET")
    21  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    22  
    23  		w.Header().Add("Content-Type", "application/json")
    24  		w.WriteHeader(http.StatusOK)
    25  
    26  		fmt.Fprintf(w, `
    27  {
    28    "vips":[
    29           {
    30             "id": "db902c0c-d5ff-4753-b465-668ad9656918",
    31             "tenant_id": "310df60f-2a10-4ee5-9554-98393092194c",
    32             "name": "web_vip",
    33             "description": "lb config for the web tier",
    34             "subnet_id": "96a4386a-f8c3-42ed-afce-d7954eee77b3",
    35             "address" : "10.30.176.47",
    36             "port_id" : "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
    37             "protocol": "HTTP",
    38             "protocol_port": 80,
    39             "pool_id" : "cfc6589d-f949-4c66-99d2-c2da56ef3764",
    40             "admin_state_up": true,
    41             "status": "ACTIVE"
    42           },
    43           {
    44             "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
    45             "tenant_id": "310df60f-2a10-4ee5-9554-98393092194c",
    46             "name": "db_vip",
    47  					 "description": "lb config for the db tier",
    48             "subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    49             "address" : "10.30.176.48",
    50             "port_id" : "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
    51             "protocol": "TCP",
    52             "protocol_port": 3306,
    53             "pool_id" : "41efe233-7591-43c5-9cf7-923964759f9e",
    54             "session_persistence" : {"type" : "SOURCE_IP"},
    55             "connection_limit" : 2000,
    56             "admin_state_up": true,
    57             "status": "INACTIVE"
    58           }
    59        ]
    60  }
    61  			`)
    62  	})
    63  
    64  	count := 0
    65  
    66  	vips.List(fake.ServiceClient(), vips.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    67  		count++
    68  		actual, err := vips.ExtractVIPs(page)
    69  		if err != nil {
    70  			t.Errorf("Failed to extract LBs: %v", err)
    71  			return false, err
    72  		}
    73  
    74  		expected := []vips.VirtualIP{
    75  			{
    76  				ID:           "db902c0c-d5ff-4753-b465-668ad9656918",
    77  				TenantID:     "310df60f-2a10-4ee5-9554-98393092194c",
    78  				Name:         "web_vip",
    79  				Description:  "lb config for the web tier",
    80  				SubnetID:     "96a4386a-f8c3-42ed-afce-d7954eee77b3",
    81  				Address:      "10.30.176.47",
    82  				PortID:       "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
    83  				Protocol:     "HTTP",
    84  				ProtocolPort: 80,
    85  				PoolID:       "cfc6589d-f949-4c66-99d2-c2da56ef3764",
    86  				Persistence:  vips.SessionPersistence{},
    87  				ConnLimit:    0,
    88  				AdminStateUp: true,
    89  				Status:       "ACTIVE",
    90  			},
    91  			{
    92  				ID:           "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
    93  				TenantID:     "310df60f-2a10-4ee5-9554-98393092194c",
    94  				Name:         "db_vip",
    95  				Description:  "lb config for the db tier",
    96  				SubnetID:     "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    97  				Address:      "10.30.176.48",
    98  				PortID:       "cd1f7a47-4fa6-449c-9ee7-632838aedfea",
    99  				Protocol:     "TCP",
   100  				ProtocolPort: 3306,
   101  				PoolID:       "41efe233-7591-43c5-9cf7-923964759f9e",
   102  				Persistence:  vips.SessionPersistence{Type: "SOURCE_IP"},
   103  				ConnLimit:    2000,
   104  				AdminStateUp: true,
   105  				Status:       "INACTIVE",
   106  			},
   107  		}
   108  
   109  		th.CheckDeepEquals(t, expected, actual)
   110  
   111  		return true, nil
   112  	})
   113  
   114  	if count != 1 {
   115  		t.Errorf("Expected 1 page, got %d", count)
   116  	}
   117  }
   118  
   119  func TestCreate(t *testing.T) {
   120  	th.SetupHTTP()
   121  	defer th.TeardownHTTP()
   122  
   123  	th.Mux.HandleFunc("/v2.0/lb/vips", func(w http.ResponseWriter, r *http.Request) {
   124  		th.TestMethod(t, r, "POST")
   125  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   126  		th.TestHeader(t, r, "Content-Type", "application/json")
   127  		th.TestHeader(t, r, "Accept", "application/json")
   128  		th.TestJSONRequest(t, r, `
   129  {
   130      "vip": {
   131          "protocol": "HTTP",
   132          "name": "NewVip",
   133          "admin_state_up": true,
   134          "subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
   135          "pool_id": "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
   136          "protocol_port": 80,
   137  				"session_persistence": {"type": "SOURCE_IP"}
   138      }
   139  }
   140  			`)
   141  
   142  		w.Header().Add("Content-Type", "application/json")
   143  		w.WriteHeader(http.StatusCreated)
   144  
   145  		fmt.Fprintf(w, `
   146  {
   147      "vip": {
   148          "status": "PENDING_CREATE",
   149          "protocol": "HTTP",
   150          "description": "",
   151          "admin_state_up": true,
   152          "subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
   153          "tenant_id": "83657cfcdfe44cd5920adaf26c48ceea",
   154          "connection_limit": -1,
   155          "pool_id": "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
   156          "address": "10.0.0.11",
   157          "protocol_port": 80,
   158          "port_id": "f7e6fe6a-b8b5-43a8-8215-73456b32e0f5",
   159          "id": "c987d2be-9a3c-4ac9-a046-e8716b1350e2",
   160          "name": "NewVip"
   161      }
   162  }
   163  		`)
   164  	})
   165  
   166  	opts := vips.CreateOpts{
   167  		Protocol:     "HTTP",
   168  		Name:         "NewVip",
   169  		AdminStateUp: golangsdk.Enabled,
   170  		SubnetID:     "8032909d-47a1-4715-90af-5153ffe39861",
   171  		PoolID:       "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
   172  		ProtocolPort: 80,
   173  		Persistence:  &vips.SessionPersistence{Type: "SOURCE_IP"},
   174  	}
   175  
   176  	r, err := vips.Create(fake.ServiceClient(), opts).Extract()
   177  	th.AssertNoErr(t, err)
   178  
   179  	th.AssertEquals(t, "PENDING_CREATE", r.Status)
   180  	th.AssertEquals(t, "HTTP", r.Protocol)
   181  	th.AssertEquals(t, "", r.Description)
   182  	th.AssertEquals(t, true, r.AdminStateUp)
   183  	th.AssertEquals(t, "8032909d-47a1-4715-90af-5153ffe39861", r.SubnetID)
   184  	th.AssertEquals(t, "83657cfcdfe44cd5920adaf26c48ceea", r.TenantID)
   185  	th.AssertEquals(t, -1, r.ConnLimit)
   186  	th.AssertEquals(t, "61b1f87a-7a21-4ad3-9dda-7f81d249944f", r.PoolID)
   187  	th.AssertEquals(t, "10.0.0.11", r.Address)
   188  	th.AssertEquals(t, 80, r.ProtocolPort)
   189  	th.AssertEquals(t, "f7e6fe6a-b8b5-43a8-8215-73456b32e0f5", r.PortID)
   190  	th.AssertEquals(t, "c987d2be-9a3c-4ac9-a046-e8716b1350e2", r.ID)
   191  	th.AssertEquals(t, "NewVip", r.Name)
   192  }
   193  
   194  func TestRequiredCreateOpts(t *testing.T) {
   195  	res := vips.Create(fake.ServiceClient(), vips.CreateOpts{})
   196  	if res.Err == nil {
   197  		t.Fatalf("Expected error, got none")
   198  	}
   199  	res = vips.Create(fake.ServiceClient(), vips.CreateOpts{Name: "foo"})
   200  	if res.Err == nil {
   201  		t.Fatalf("Expected error, got none")
   202  	}
   203  	res = vips.Create(fake.ServiceClient(), vips.CreateOpts{Name: "foo", SubnetID: "bar"})
   204  	if res.Err == nil {
   205  		t.Fatalf("Expected error, got none")
   206  	}
   207  	res = vips.Create(fake.ServiceClient(), vips.CreateOpts{Name: "foo", SubnetID: "bar", Protocol: "bar"})
   208  	if res.Err == nil {
   209  		t.Fatalf("Expected error, got none")
   210  	}
   211  	res = vips.Create(fake.ServiceClient(), vips.CreateOpts{Name: "foo", SubnetID: "bar", Protocol: "bar", ProtocolPort: 80})
   212  	if res.Err == nil {
   213  		t.Fatalf("Expected error, got none")
   214  	}
   215  }
   216  
   217  func TestGet(t *testing.T) {
   218  	th.SetupHTTP()
   219  	defer th.TeardownHTTP()
   220  
   221  	th.Mux.HandleFunc("/v2.0/lb/vips/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   222  		th.TestMethod(t, r, "GET")
   223  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   224  
   225  		w.Header().Add("Content-Type", "application/json")
   226  		w.WriteHeader(http.StatusOK)
   227  
   228  		fmt.Fprintf(w, `
   229  {
   230      "vip": {
   231          "status": "ACTIVE",
   232          "protocol": "HTTP",
   233          "description": "",
   234          "admin_state_up": true,
   235          "subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
   236          "tenant_id": "83657cfcdfe44cd5920adaf26c48ceea",
   237          "connection_limit": 1000,
   238          "pool_id": "72741b06-df4d-4715-b142-276b6bce75ab",
   239          "session_persistence": {
   240              "cookie_name": "MyAppCookie",
   241              "type": "APP_COOKIE"
   242          },
   243          "address": "10.0.0.10",
   244          "protocol_port": 80,
   245          "port_id": "b5a743d6-056b-468b-862d-fb13a9aa694e",
   246          "id": "4ec89087-d057-4e2c-911f-60a3b47ee304",
   247          "name": "my-vip"
   248      }
   249  }
   250  			`)
   251  	})
   252  
   253  	vip, err := vips.Get(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304").Extract()
   254  	th.AssertNoErr(t, err)
   255  
   256  	th.AssertEquals(t, "ACTIVE", vip.Status)
   257  	th.AssertEquals(t, "HTTP", vip.Protocol)
   258  	th.AssertEquals(t, "", vip.Description)
   259  	th.AssertEquals(t, true, vip.AdminStateUp)
   260  	th.AssertEquals(t, 1000, vip.ConnLimit)
   261  	th.AssertEquals(t, vips.SessionPersistence{Type: "APP_COOKIE", CookieName: "MyAppCookie"}, vip.Persistence)
   262  }
   263  
   264  func TestUpdate(t *testing.T) {
   265  	th.SetupHTTP()
   266  	defer th.TeardownHTTP()
   267  
   268  	th.Mux.HandleFunc("/v2.0/lb/vips/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   269  		th.TestMethod(t, r, "PUT")
   270  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   271  		th.TestHeader(t, r, "Content-Type", "application/json")
   272  		th.TestHeader(t, r, "Accept", "application/json")
   273  		th.TestJSONRequest(t, r, `
   274  {
   275      "vip": {
   276          "connection_limit": 1000,
   277  				"session_persistence": {"type": "SOURCE_IP"}
   278      }
   279  }
   280  			`)
   281  
   282  		w.Header().Add("Content-Type", "application/json")
   283  		w.WriteHeader(http.StatusAccepted)
   284  
   285  		fmt.Fprintf(w, `
   286  {
   287      "vip": {
   288          "status": "PENDING_UPDATE",
   289          "protocol": "HTTP",
   290          "description": "",
   291          "admin_state_up": true,
   292          "subnet_id": "8032909d-47a1-4715-90af-5153ffe39861",
   293          "tenant_id": "83657cfcdfe44cd5920adaf26c48ceea",
   294          "connection_limit": 1000,
   295          "pool_id": "61b1f87a-7a21-4ad3-9dda-7f81d249944f",
   296          "address": "10.0.0.11",
   297          "protocol_port": 80,
   298          "port_id": "f7e6fe6a-b8b5-43a8-8215-73456b32e0f5",
   299          "id": "c987d2be-9a3c-4ac9-a046-e8716b1350e2",
   300          "name": "NewVip"
   301      }
   302  }
   303  		`)
   304  	})
   305  
   306  	i1000 := 1000
   307  	options := vips.UpdateOpts{
   308  		ConnLimit:   &i1000,
   309  		Persistence: &vips.SessionPersistence{Type: "SOURCE_IP"},
   310  	}
   311  	vip, err := vips.Update(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304", options).Extract()
   312  	th.AssertNoErr(t, err)
   313  
   314  	th.AssertEquals(t, "PENDING_UPDATE", vip.Status)
   315  	th.AssertEquals(t, 1000, vip.ConnLimit)
   316  }
   317  
   318  func TestDelete(t *testing.T) {
   319  	th.SetupHTTP()
   320  	defer th.TeardownHTTP()
   321  
   322  	th.Mux.HandleFunc("/v2.0/lb/vips/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) {
   323  		th.TestMethod(t, r, "DELETE")
   324  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   325  		w.WriteHeader(http.StatusNoContent)
   326  	})
   327  
   328  	res := vips.Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304")
   329  	th.AssertNoErr(t, res.Err)
   330  }