github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas/members/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/members"
    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/members", 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     "members":[
    29        {
    30           "status":"ACTIVE",
    31           "weight":1,
    32           "admin_state_up":true,
    33           "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    34           "pool_id":"72741b06-df4d-4715-b142-276b6bce75ab",
    35           "address":"10.0.0.4",
    36           "protocol_port":80,
    37           "id":"701b531b-111a-4f21-ad85-4795b7b12af6"
    38        },
    39        {
    40           "status":"ACTIVE",
    41           "weight":1,
    42           "admin_state_up":true,
    43           "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    44           "pool_id":"72741b06-df4d-4715-b142-276b6bce75ab",
    45           "address":"10.0.0.3",
    46           "protocol_port":80,
    47           "id":"beb53b4d-230b-4abd-8118-575b8fa006ef"
    48        }
    49     ]
    50  }
    51        `)
    52  	})
    53  
    54  	count := 0
    55  
    56  	members.List(fake.ServiceClient(), members.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    57  		count++
    58  		actual, err := members.ExtractMembers(page)
    59  		if err != nil {
    60  			t.Errorf("Failed to extract members: %v", err)
    61  			return false, err
    62  		}
    63  
    64  		expected := []members.Member{
    65  			{
    66  				Status:       "ACTIVE",
    67  				Weight:       1,
    68  				AdminStateUp: true,
    69  				TenantID:     "83657cfcdfe44cd5920adaf26c48ceea",
    70  				PoolID:       "72741b06-df4d-4715-b142-276b6bce75ab",
    71  				Address:      "10.0.0.4",
    72  				ProtocolPort: 80,
    73  				ID:           "701b531b-111a-4f21-ad85-4795b7b12af6",
    74  			},
    75  			{
    76  				Status:       "ACTIVE",
    77  				Weight:       1,
    78  				AdminStateUp: true,
    79  				TenantID:     "83657cfcdfe44cd5920adaf26c48ceea",
    80  				PoolID:       "72741b06-df4d-4715-b142-276b6bce75ab",
    81  				Address:      "10.0.0.3",
    82  				ProtocolPort: 80,
    83  				ID:           "beb53b4d-230b-4abd-8118-575b8fa006ef",
    84  			},
    85  		}
    86  
    87  		th.CheckDeepEquals(t, expected, actual)
    88  
    89  		return true, nil
    90  	})
    91  
    92  	if count != 1 {
    93  		t.Errorf("Expected 1 page, got %d", count)
    94  	}
    95  }
    96  
    97  func TestCreate(t *testing.T) {
    98  	th.SetupHTTP()
    99  	defer th.TeardownHTTP()
   100  
   101  	th.Mux.HandleFunc("/v2.0/lb/members", func(w http.ResponseWriter, r *http.Request) {
   102  		th.TestMethod(t, r, "POST")
   103  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   104  		th.TestHeader(t, r, "Content-Type", "application/json")
   105  		th.TestHeader(t, r, "Accept", "application/json")
   106  		th.TestJSONRequest(t, r, `
   107  {
   108    "member": {
   109      "tenant_id": "453105b9-1754-413f-aab1-55f1af620750",
   110  		"pool_id": "foo",
   111      "address": "192.0.2.14",
   112      "protocol_port":8080
   113    }
   114  }
   115        `)
   116  
   117  		w.Header().Add("Content-Type", "application/json")
   118  		w.WriteHeader(http.StatusCreated)
   119  
   120  		fmt.Fprintf(w, `
   121  {
   122    "member": {
   123      "id": "975592ca-e308-48ad-8298-731935ee9f45",
   124      "address": "192.0.2.14",
   125      "protocol_port": 8080,
   126      "tenant_id": "453105b9-1754-413f-aab1-55f1af620750",
   127      "admin_state_up":true,
   128      "weight": 1,
   129      "status": "DOWN"
   130    }
   131  }
   132      `)
   133  	})
   134  
   135  	options := members.CreateOpts{
   136  		TenantID:     "453105b9-1754-413f-aab1-55f1af620750",
   137  		Address:      "192.0.2.14",
   138  		ProtocolPort: 8080,
   139  		PoolID:       "foo",
   140  	}
   141  	_, err := members.Create(fake.ServiceClient(), options).Extract()
   142  	th.AssertNoErr(t, err)
   143  }
   144  
   145  func TestGet(t *testing.T) {
   146  	th.SetupHTTP()
   147  	defer th.TeardownHTTP()
   148  
   149  	th.Mux.HandleFunc("/v2.0/lb/members/975592ca-e308-48ad-8298-731935ee9f45", func(w http.ResponseWriter, r *http.Request) {
   150  		th.TestMethod(t, r, "GET")
   151  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   152  
   153  		w.Header().Add("Content-Type", "application/json")
   154  		w.WriteHeader(http.StatusOK)
   155  
   156  		fmt.Fprintf(w, `
   157  {
   158     "member":{
   159        "id":"975592ca-e308-48ad-8298-731935ee9f45",
   160        "address":"192.0.2.14",
   161        "protocol_port":8080,
   162        "tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
   163        "admin_state_up":true,
   164        "weight":1,
   165        "status":"DOWN"
   166     }
   167  }
   168        `)
   169  	})
   170  
   171  	m, err := members.Get(fake.ServiceClient(), "975592ca-e308-48ad-8298-731935ee9f45").Extract()
   172  	th.AssertNoErr(t, err)
   173  
   174  	th.AssertEquals(t, "975592ca-e308-48ad-8298-731935ee9f45", m.ID)
   175  	th.AssertEquals(t, "192.0.2.14", m.Address)
   176  	th.AssertEquals(t, 8080, m.ProtocolPort)
   177  	th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", m.TenantID)
   178  	th.AssertEquals(t, true, m.AdminStateUp)
   179  	th.AssertEquals(t, 1, m.Weight)
   180  	th.AssertEquals(t, "DOWN", m.Status)
   181  }
   182  
   183  func TestUpdate(t *testing.T) {
   184  	th.SetupHTTP()
   185  	defer th.TeardownHTTP()
   186  
   187  	th.Mux.HandleFunc("/v2.0/lb/members/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
   188  		th.TestMethod(t, r, "PUT")
   189  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   190  		th.TestHeader(t, r, "Content-Type", "application/json")
   191  		th.TestHeader(t, r, "Accept", "application/json")
   192  		th.TestJSONRequest(t, r, `
   193  {
   194     "member":{
   195        "admin_state_up":false
   196     }
   197  }
   198        `)
   199  
   200  		w.Header().Add("Content-Type", "application/json")
   201  		w.WriteHeader(http.StatusOK)
   202  
   203  		fmt.Fprintf(w, `
   204  {
   205     "member":{
   206        "status":"PENDING_UPDATE",
   207        "protocol_port":8080,
   208        "weight":1,
   209        "admin_state_up":false,
   210        "tenant_id":"4fd44f30292945e481c7b8a0c8908869",
   211        "pool_id":"7803631d-f181-4500-b3a2-1b68ba2a75fd",
   212        "address":"10.0.0.5",
   213        "status_description":null,
   214        "id":"48a471ea-64f1-4eb6-9be7-dae6bbe40a0f"
   215     }
   216  }
   217      `)
   218  	})
   219  
   220  	options := members.UpdateOpts{AdminStateUp: golangsdk.Disabled}
   221  
   222  	_, err := members.Update(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", options).Extract()
   223  	th.AssertNoErr(t, err)
   224  }
   225  
   226  func TestDelete(t *testing.T) {
   227  	th.SetupHTTP()
   228  	defer th.TeardownHTTP()
   229  
   230  	th.Mux.HandleFunc("/v2.0/lb/members/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) {
   231  		th.TestMethod(t, r, "DELETE")
   232  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   233  		w.WriteHeader(http.StatusNoContent)
   234  	})
   235  
   236  	res := members.Delete(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853")
   237  	th.AssertNoErr(t, res.Err)
   238  }