github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/qos/policies/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/policies"
    11  	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
    12  	"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
    13  	"github.com/gophercloud/gophercloud/pagination"
    14  	th "github.com/gophercloud/gophercloud/testhelper"
    15  )
    16  
    17  func TestGetPort(t *testing.T) {
    18  	th.SetupHTTP()
    19  	defer th.TeardownHTTP()
    20  
    21  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
    22  		th.TestMethod(t, r, "GET")
    23  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    24  
    25  		w.Header().Add("Content-Type", "application/json")
    26  		w.WriteHeader(http.StatusOK)
    27  
    28  		_, err := fmt.Fprintf(w, GetPortResponse)
    29  		th.AssertNoErr(t, err)
    30  	})
    31  
    32  	var p struct {
    33  		ports.Port
    34  		policies.QoSPolicyExt
    35  	}
    36  	err := ports.Get(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d").ExtractInto(&p)
    37  	th.AssertNoErr(t, err)
    38  
    39  	th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
    40  	th.AssertEquals(t, p.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08")
    41  }
    42  
    43  func TestCreatePort(t *testing.T) {
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  
    47  	th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) {
    48  		th.TestMethod(t, r, "POST")
    49  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    50  		th.TestHeader(t, r, "Content-Type", "application/json")
    51  		th.TestHeader(t, r, "Accept", "application/json")
    52  		th.TestJSONRequest(t, r, CreatePortRequest)
    53  
    54  		w.Header().Add("Content-Type", "application/json")
    55  		w.WriteHeader(http.StatusCreated)
    56  
    57  		_, err := fmt.Fprintf(w, CreatePortResponse)
    58  		th.AssertNoErr(t, err)
    59  	})
    60  
    61  	var p struct {
    62  		ports.Port
    63  		policies.QoSPolicyExt
    64  	}
    65  	portCreateOpts := ports.CreateOpts{
    66  		NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7",
    67  	}
    68  	createOpts := policies.PortCreateOptsExt{
    69  		CreateOptsBuilder: portCreateOpts,
    70  		QoSPolicyID:       "591e0597-39a6-4665-8149-2111d8de9a08",
    71  	}
    72  	err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&p)
    73  	th.AssertNoErr(t, err)
    74  
    75  	th.AssertEquals(t, p.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
    76  	th.AssertEquals(t, p.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
    77  	th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
    78  	th.AssertEquals(t, p.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08")
    79  }
    80  
    81  func TestUpdatePortWithPolicy(t *testing.T) {
    82  	th.SetupHTTP()
    83  	defer th.TeardownHTTP()
    84  
    85  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
    86  		th.TestMethod(t, r, "PUT")
    87  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    88  		th.TestHeader(t, r, "Content-Type", "application/json")
    89  		th.TestHeader(t, r, "Accept", "application/json")
    90  		th.TestJSONRequest(t, r, UpdatePortWithPolicyRequest)
    91  
    92  		w.Header().Add("Content-Type", "application/json")
    93  		w.WriteHeader(http.StatusOK)
    94  
    95  		_, err := fmt.Fprintf(w, UpdatePortWithPolicyResponse)
    96  		th.AssertNoErr(t, err)
    97  	})
    98  
    99  	policyID := "591e0597-39a6-4665-8149-2111d8de9a08"
   100  
   101  	var p struct {
   102  		ports.Port
   103  		policies.QoSPolicyExt
   104  	}
   105  	portUpdateOpts := ports.UpdateOpts{}
   106  	updateOpts := policies.PortUpdateOptsExt{
   107  		UpdateOptsBuilder: portUpdateOpts,
   108  		QoSPolicyID:       &policyID,
   109  	}
   110  	err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&p)
   111  	th.AssertNoErr(t, err)
   112  
   113  	th.AssertEquals(t, p.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   114  	th.AssertEquals(t, p.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   115  	th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   116  	th.AssertEquals(t, p.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08")
   117  }
   118  
   119  func TestUpdatePortWithoutPolicy(t *testing.T) {
   120  	th.SetupHTTP()
   121  	defer th.TeardownHTTP()
   122  
   123  	th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   124  		th.TestMethod(t, r, "PUT")
   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, UpdatePortWithoutPolicyRequest)
   129  
   130  		w.Header().Add("Content-Type", "application/json")
   131  		w.WriteHeader(http.StatusOK)
   132  
   133  		_, err := fmt.Fprintf(w, UpdatePortWithoutPolicyResponse)
   134  		th.AssertNoErr(t, err)
   135  	})
   136  
   137  	policyID := ""
   138  
   139  	var p struct {
   140  		ports.Port
   141  		policies.QoSPolicyExt
   142  	}
   143  	portUpdateOpts := ports.UpdateOpts{}
   144  	updateOpts := policies.PortUpdateOptsExt{
   145  		UpdateOptsBuilder: portUpdateOpts,
   146  		QoSPolicyID:       &policyID,
   147  	}
   148  	err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&p)
   149  	th.AssertNoErr(t, err)
   150  
   151  	th.AssertEquals(t, p.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7")
   152  	th.AssertEquals(t, p.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa")
   153  	th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   154  	th.AssertEquals(t, p.QoSPolicyID, "")
   155  }
   156  
   157  func TestGetNetwork(t *testing.T) {
   158  	th.SetupHTTP()
   159  	defer th.TeardownHTTP()
   160  
   161  	th.Mux.HandleFunc("/v2.0/networks/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   162  		th.TestMethod(t, r, "GET")
   163  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   164  
   165  		w.Header().Add("Content-Type", "application/json")
   166  		w.WriteHeader(http.StatusOK)
   167  
   168  		_, err := fmt.Fprintf(w, GetNetworkResponse)
   169  		th.AssertNoErr(t, err)
   170  	})
   171  
   172  	var n struct {
   173  		networks.Network
   174  		policies.QoSPolicyExt
   175  	}
   176  	err := networks.Get(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d").ExtractInto(&n)
   177  	th.AssertNoErr(t, err)
   178  
   179  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   180  	th.AssertEquals(t, n.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08")
   181  }
   182  
   183  func TestCreateNetwork(t *testing.T) {
   184  	th.SetupHTTP()
   185  	defer th.TeardownHTTP()
   186  
   187  	th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) {
   188  		th.TestMethod(t, r, "POST")
   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, CreateNetworkRequest)
   193  
   194  		w.Header().Add("Content-Type", "application/json")
   195  		w.WriteHeader(http.StatusCreated)
   196  
   197  		_, err := fmt.Fprintf(w, CreateNetworkResponse)
   198  		th.AssertNoErr(t, err)
   199  	})
   200  
   201  	var n struct {
   202  		networks.Network
   203  		policies.QoSPolicyExt
   204  	}
   205  	networkCreateOpts := networks.CreateOpts{
   206  		Name: "private",
   207  	}
   208  	createOpts := policies.NetworkCreateOptsExt{
   209  		CreateOptsBuilder: networkCreateOpts,
   210  		QoSPolicyID:       "591e0597-39a6-4665-8149-2111d8de9a08",
   211  	}
   212  	err := networks.Create(fake.ServiceClient(), createOpts).ExtractInto(&n)
   213  	th.AssertNoErr(t, err)
   214  
   215  	th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
   216  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   217  	th.AssertEquals(t, n.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08")
   218  }
   219  
   220  func TestUpdateNetworkWithPolicy(t *testing.T) {
   221  	th.SetupHTTP()
   222  	defer th.TeardownHTTP()
   223  
   224  	th.Mux.HandleFunc("/v2.0/networks/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   225  		th.TestMethod(t, r, "PUT")
   226  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   227  		th.TestHeader(t, r, "Content-Type", "application/json")
   228  		th.TestHeader(t, r, "Accept", "application/json")
   229  		th.TestJSONRequest(t, r, UpdateNetworkWithPolicyRequest)
   230  
   231  		w.Header().Add("Content-Type", "application/json")
   232  		w.WriteHeader(http.StatusOK)
   233  
   234  		_, err := fmt.Fprintf(w, UpdateNetworkWithPolicyResponse)
   235  		th.AssertNoErr(t, err)
   236  	})
   237  
   238  	policyID := "591e0597-39a6-4665-8149-2111d8de9a08"
   239  	name := "updated"
   240  
   241  	var n struct {
   242  		networks.Network
   243  		policies.QoSPolicyExt
   244  	}
   245  	networkUpdateOpts := networks.UpdateOpts{
   246  		Name: &name,
   247  	}
   248  	updateOpts := policies.NetworkUpdateOptsExt{
   249  		UpdateOptsBuilder: networkUpdateOpts,
   250  		QoSPolicyID:       &policyID,
   251  	}
   252  	err := networks.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&n)
   253  	th.AssertNoErr(t, err)
   254  
   255  	th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
   256  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   257  	th.AssertEquals(t, n.Name, "updated")
   258  	th.AssertEquals(t, n.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08")
   259  }
   260  
   261  func TestUpdateNetworkWithoutPolicy(t *testing.T) {
   262  	th.SetupHTTP()
   263  	defer th.TeardownHTTP()
   264  
   265  	th.Mux.HandleFunc("/v2.0/networks/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) {
   266  		th.TestMethod(t, r, "PUT")
   267  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   268  		th.TestHeader(t, r, "Content-Type", "application/json")
   269  		th.TestHeader(t, r, "Accept", "application/json")
   270  		th.TestJSONRequest(t, r, UpdateNetworkWithoutPolicyRequest)
   271  
   272  		w.Header().Add("Content-Type", "application/json")
   273  		w.WriteHeader(http.StatusOK)
   274  
   275  		_, err := fmt.Fprintf(w, UpdateNetworkWithoutPolicyResponse)
   276  		th.AssertNoErr(t, err)
   277  	})
   278  
   279  	policyID := ""
   280  
   281  	var n struct {
   282  		networks.Network
   283  		policies.QoSPolicyExt
   284  	}
   285  	networkUpdateOpts := networks.UpdateOpts{}
   286  	updateOpts := policies.NetworkUpdateOptsExt{
   287  		UpdateOptsBuilder: networkUpdateOpts,
   288  		QoSPolicyID:       &policyID,
   289  	}
   290  	err := networks.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&n)
   291  	th.AssertNoErr(t, err)
   292  
   293  	th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869")
   294  	th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d")
   295  	th.AssertEquals(t, n.QoSPolicyID, "")
   296  }
   297  
   298  func TestListPolicies(t *testing.T) {
   299  	th.SetupHTTP()
   300  	defer th.TeardownHTTP()
   301  
   302  	th.Mux.HandleFunc("/v2.0/qos/policies", func(w http.ResponseWriter, r *http.Request) {
   303  		th.TestMethod(t, r, "GET")
   304  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   305  
   306  		w.Header().Add("Content-Type", "application/json")
   307  		w.WriteHeader(http.StatusOK)
   308  
   309  		fmt.Fprintf(w, ListPoliciesResponse)
   310  	})
   311  
   312  	count := 0
   313  
   314  	err := policies.List(fake.ServiceClient(), policies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
   315  		count++
   316  		actual, err := policies.ExtractPolicies(page)
   317  		if err != nil {
   318  			t.Errorf("Failed to extract policies: %v", err)
   319  			return false, nil
   320  		}
   321  
   322  		expected := []policies.Policy{
   323  			Policy1,
   324  			Policy2,
   325  		}
   326  
   327  		th.CheckDeepEquals(t, expected, actual)
   328  
   329  		return true, nil
   330  	})
   331  	th.AssertNoErr(t, err)
   332  
   333  	if count != 1 {
   334  		t.Errorf("Expected 1 page, got %d", count)
   335  	}
   336  }
   337  
   338  func TestGetPolicy(t *testing.T) {
   339  	th.SetupHTTP()
   340  	defer th.TeardownHTTP()
   341  
   342  	th.Mux.HandleFunc("/v2.0/qos/policies/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   343  		th.TestMethod(t, r, "GET")
   344  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   345  
   346  		w.Header().Add("Content-Type", "application/json")
   347  		w.WriteHeader(http.StatusOK)
   348  
   349  		fmt.Fprintf(w, GetPolicyResponse)
   350  	})
   351  
   352  	p, err := policies.Get(fake.ServiceClient(), "30a57f4a-336b-4382-8275-d708babd2241").Extract()
   353  	th.AssertNoErr(t, err)
   354  
   355  	th.AssertEquals(t, "bw-limiter", p.Name)
   356  	th.AssertDeepEquals(t, []string{}, p.Tags)
   357  	th.AssertDeepEquals(t, []map[string]interface{}{
   358  		{
   359  			"max_kbps":       float64(3000),
   360  			"direction":      "egress",
   361  			"qos_policy_id":  "d6ae28ce-fcb5-4180-aa62-d260a27e09ae",
   362  			"type":           "bandwidth_limit",
   363  			"id":             "30a57f4a-336b-4382-8275-d708babd2241",
   364  			"max_burst_kbps": float64(300),
   365  		},
   366  	}, p.Rules)
   367  	th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID)
   368  	th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID)
   369  	th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt)
   370  	th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 57, 0, time.UTC), p.UpdatedAt)
   371  	th.AssertEquals(t, 1, p.RevisionNumber)
   372  	th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
   373  }
   374  
   375  func TestCreatePolicy(t *testing.T) {
   376  	th.SetupHTTP()
   377  	defer th.TeardownHTTP()
   378  
   379  	th.Mux.HandleFunc("/v2.0/qos/policies", func(w http.ResponseWriter, r *http.Request) {
   380  		th.TestMethod(t, r, "POST")
   381  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   382  		th.TestHeader(t, r, "Content-Type", "application/json")
   383  		th.TestHeader(t, r, "Accept", "application/json")
   384  		th.TestJSONRequest(t, r, CreatePolicyRequest)
   385  
   386  		w.Header().Add("Content-Type", "application/json")
   387  		w.WriteHeader(http.StatusCreated)
   388  
   389  		fmt.Fprintf(w, CreatePolicyResponse)
   390  	})
   391  
   392  	opts := policies.CreateOpts{
   393  		Name:        "shared-default-policy",
   394  		Shared:      true,
   395  		IsDefault:   true,
   396  		Description: "use-me",
   397  	}
   398  	p, err := policies.Create(fake.ServiceClient(), opts).Extract()
   399  	th.AssertNoErr(t, err)
   400  
   401  	th.AssertEquals(t, "shared-default-policy", p.Name)
   402  	th.AssertEquals(t, true, p.Shared)
   403  	th.AssertEquals(t, true, p.IsDefault)
   404  	th.AssertEquals(t, "use-me", p.Description)
   405  	th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID)
   406  	th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID)
   407  	th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt)
   408  	th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 57, 0, time.UTC), p.UpdatedAt)
   409  	th.AssertEquals(t, 0, p.RevisionNumber)
   410  	th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
   411  }
   412  
   413  func TestUpdatePolicy(t *testing.T) {
   414  	th.SetupHTTP()
   415  	defer th.TeardownHTTP()
   416  
   417  	th.Mux.HandleFunc("/v2.0/qos/policies/d6ae28ce-fcb5-4180-aa62-d260a27e09ae", func(w http.ResponseWriter, r *http.Request) {
   418  		th.TestMethod(t, r, "PUT")
   419  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   420  		th.TestHeader(t, r, "Content-Type", "application/json")
   421  		th.TestHeader(t, r, "Accept", "application/json")
   422  		th.TestJSONRequest(t, r, UpdatePolicyRequest)
   423  
   424  		w.Header().Add("Content-Type", "application/json")
   425  		w.WriteHeader(http.StatusOK)
   426  
   427  		fmt.Fprintf(w, UpdatePolicyResponse)
   428  	})
   429  
   430  	shared := true
   431  	description := ""
   432  	opts := policies.UpdateOpts{
   433  		Name:        "new-name",
   434  		Shared:      &shared,
   435  		Description: &description,
   436  	}
   437  	p, err := policies.Update(fake.ServiceClient(), "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", opts).Extract()
   438  	th.AssertNoErr(t, err)
   439  
   440  	th.AssertEquals(t, "new-name", p.Name)
   441  	th.AssertEquals(t, true, p.Shared)
   442  	th.AssertEquals(t, false, p.IsDefault)
   443  	th.AssertEquals(t, "", p.Description)
   444  	th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID)
   445  	th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID)
   446  	th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt)
   447  	th.AssertEquals(t, time.Date(2019, 6, 1, 13, 17, 57, 0, time.UTC), p.UpdatedAt)
   448  	th.AssertEquals(t, 1, p.RevisionNumber)
   449  	th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID)
   450  }
   451  
   452  func TestDeletePolicy(t *testing.T) {
   453  	th.SetupHTTP()
   454  	defer th.TeardownHTTP()
   455  
   456  	th.Mux.HandleFunc("/v2.0/qos/policies/d6ae28ce-fcb5-4180-aa62-d260a27e09ae", func(w http.ResponseWriter, r *http.Request) {
   457  		th.TestMethod(t, r, "DELETE")
   458  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   459  		w.WriteHeader(http.StatusNoContent)
   460  	})
   461  
   462  	res := policies.Delete(fake.ServiceClient(), "d6ae28ce-fcb5-4180-aa62-d260a27e09ae")
   463  	th.AssertNoErr(t, res.Err)
   464  }