github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/qos/rules/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/qos/rules"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    12  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    13  )
    14  
    15  func TestListBandwidthLimitRule(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules", 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.Fprint(w, BandwidthLimitRulesListResult)
    27  	})
    28  
    29  	count := 0
    30  
    31  	err := rules.ListBandwidthLimitRules(
    32  		fake.ServiceClient(),
    33  		"501005fa-3b56-4061-aaca-3f24995112e1",
    34  		rules.BandwidthLimitRulesListOpts{},
    35  	).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    36  		count++
    37  		actual, err := rules.ExtractBandwidthLimitRules(page)
    38  		if err != nil {
    39  			t.Errorf("Failed to extract bandwith limit rules: %v", err)
    40  			return false, nil
    41  		}
    42  
    43  		expected := []rules.BandwidthLimitRule{
    44  			{
    45  				ID:           "30a57f4a-336b-4382-8275-d708babd2241",
    46  				MaxKBps:      3000,
    47  				MaxBurstKBps: 300,
    48  				Direction:    "egress",
    49  			},
    50  		}
    51  
    52  		th.CheckDeepEquals(t, expected, actual)
    53  
    54  		return true, nil
    55  	})
    56  	th.AssertNoErr(t, err)
    57  
    58  	if count != 1 {
    59  		t.Errorf("Expected 1 page, got %d", count)
    60  	}
    61  }
    62  
    63  func TestGetBandwidthLimitRule(t *testing.T) {
    64  	th.SetupHTTP()
    65  	defer th.TeardownHTTP()
    66  
    67  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
    68  		th.TestMethod(t, r, "GET")
    69  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    70  
    71  		w.Header().Add("Content-Type", "application/json")
    72  		w.WriteHeader(http.StatusOK)
    73  
    74  		fmt.Fprint(w, BandwidthLimitRulesGetResult)
    75  	})
    76  
    77  	r, err := rules.GetBandwidthLimitRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241").ExtractBandwidthLimitRule()
    78  	th.AssertNoErr(t, err)
    79  
    80  	th.AssertEquals(t, r.ID, "30a57f4a-336b-4382-8275-d708babd2241")
    81  	th.AssertEquals(t, r.Direction, "egress")
    82  	th.AssertEquals(t, r.MaxBurstKBps, 300)
    83  	th.AssertEquals(t, r.MaxKBps, 3000)
    84  }
    85  
    86  func TestCreateBandwidthLimitRule(t *testing.T) {
    87  	th.SetupHTTP()
    88  	defer th.TeardownHTTP()
    89  
    90  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules", func(w http.ResponseWriter, r *http.Request) {
    91  		th.TestMethod(t, r, "POST")
    92  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    93  		th.TestHeader(t, r, "Content-Type", "application/json")
    94  		th.TestHeader(t, r, "Accept", "application/json")
    95  		th.TestJSONRequest(t, r, BandwidthLimitRulesCreateRequest)
    96  
    97  		w.Header().Add("Content-Type", "application/json")
    98  		w.WriteHeader(http.StatusCreated)
    99  
   100  		fmt.Fprint(w, BandwidthLimitRulesCreateResult)
   101  	})
   102  
   103  	opts := rules.CreateBandwidthLimitRuleOpts{
   104  		MaxKBps:      2000,
   105  		MaxBurstKBps: 200,
   106  	}
   107  	r, err := rules.CreateBandwidthLimitRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractBandwidthLimitRule()
   108  	th.AssertNoErr(t, err)
   109  
   110  	th.AssertEquals(t, 200, r.MaxBurstKBps)
   111  	th.AssertEquals(t, 2000, r.MaxKBps)
   112  }
   113  
   114  func TestUpdateBandwidthLimitRule(t *testing.T) {
   115  	th.SetupHTTP()
   116  	defer th.TeardownHTTP()
   117  
   118  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   119  		th.TestMethod(t, r, "PUT")
   120  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   121  		th.TestHeader(t, r, "Content-Type", "application/json")
   122  		th.TestHeader(t, r, "Accept", "application/json")
   123  		th.TestJSONRequest(t, r, BandwidthLimitRulesUpdateRequest)
   124  
   125  		w.Header().Add("Content-Type", "application/json")
   126  		w.WriteHeader(http.StatusOK)
   127  
   128  		fmt.Fprint(w, BandwidthLimitRulesUpdateResult)
   129  	})
   130  
   131  	maxKBps := 500
   132  	maxBurstKBps := 0
   133  	opts := rules.UpdateBandwidthLimitRuleOpts{
   134  		MaxKBps:      &maxKBps,
   135  		MaxBurstKBps: &maxBurstKBps,
   136  	}
   137  	r, err := rules.UpdateBandwidthLimitRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractBandwidthLimitRule()
   138  	th.AssertNoErr(t, err)
   139  
   140  	th.AssertEquals(t, 0, r.MaxBurstKBps)
   141  	th.AssertEquals(t, 500, r.MaxKBps)
   142  }
   143  
   144  func TestDeleteBandwidthLimitRule(t *testing.T) {
   145  	th.SetupHTTP()
   146  	defer th.TeardownHTTP()
   147  
   148  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   149  		th.TestMethod(t, r, "DELETE")
   150  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   151  		w.WriteHeader(http.StatusNoContent)
   152  	})
   153  
   154  	res := rules.DeleteBandwidthLimitRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241")
   155  	th.AssertNoErr(t, res.Err)
   156  }
   157  
   158  func TestListDSCPMarkingRule(t *testing.T) {
   159  	th.SetupHTTP()
   160  	defer th.TeardownHTTP()
   161  
   162  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules", func(w http.ResponseWriter, r *http.Request) {
   163  		th.TestMethod(t, r, "GET")
   164  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   165  
   166  		w.Header().Add("Content-Type", "application/json")
   167  		w.WriteHeader(http.StatusOK)
   168  
   169  		fmt.Fprint(w, DSCPMarkingRulesListResult)
   170  	})
   171  
   172  	count := 0
   173  
   174  	err := rules.ListDSCPMarkingRules(
   175  		fake.ServiceClient(),
   176  		"501005fa-3b56-4061-aaca-3f24995112e1",
   177  		rules.DSCPMarkingRulesListOpts{},
   178  	).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   179  		count++
   180  		actual, err := rules.ExtractDSCPMarkingRules(page)
   181  		if err != nil {
   182  			t.Errorf("Failed to extract DSCP marking rules: %v", err)
   183  			return false, nil
   184  		}
   185  
   186  		expected := []rules.DSCPMarkingRule{
   187  			{
   188  				ID:       "30a57f4a-336b-4382-8275-d708babd2241",
   189  				DSCPMark: 20,
   190  			},
   191  		}
   192  
   193  		th.CheckDeepEquals(t, expected, actual)
   194  
   195  		return true, nil
   196  	})
   197  	th.AssertNoErr(t, err)
   198  
   199  	if count != 1 {
   200  		t.Errorf("Expected 1 page, got %d", count)
   201  	}
   202  }
   203  
   204  func TestGetDSCPMarkingRule(t *testing.T) {
   205  	th.SetupHTTP()
   206  	defer th.TeardownHTTP()
   207  
   208  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   209  		th.TestMethod(t, r, "GET")
   210  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   211  
   212  		w.Header().Add("Content-Type", "application/json")
   213  		w.WriteHeader(http.StatusOK)
   214  
   215  		fmt.Fprint(w, DSCPMarkingRuleGetResult)
   216  	})
   217  
   218  	r, err := rules.GetDSCPMarkingRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241").ExtractDSCPMarkingRule()
   219  	th.AssertNoErr(t, err)
   220  
   221  	th.AssertEquals(t, r.ID, "30a57f4a-336b-4382-8275-d708babd2241")
   222  	th.AssertEquals(t, 26, r.DSCPMark)
   223  }
   224  
   225  func TestCreateDSCPMarkingRule(t *testing.T) {
   226  	th.SetupHTTP()
   227  	defer th.TeardownHTTP()
   228  
   229  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules", func(w http.ResponseWriter, r *http.Request) {
   230  		th.TestMethod(t, r, "POST")
   231  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   232  		th.TestHeader(t, r, "Content-Type", "application/json")
   233  		th.TestHeader(t, r, "Accept", "application/json")
   234  		th.TestJSONRequest(t, r, DSCPMarkingRuleCreateRequest)
   235  
   236  		w.Header().Add("Content-Type", "application/json")
   237  		w.WriteHeader(http.StatusCreated)
   238  
   239  		fmt.Fprint(w, DSCPMarkingRuleCreateResult)
   240  	})
   241  
   242  	opts := rules.CreateDSCPMarkingRuleOpts{
   243  		DSCPMark: 20,
   244  	}
   245  	r, err := rules.CreateDSCPMarkingRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractDSCPMarkingRule()
   246  	th.AssertNoErr(t, err)
   247  
   248  	th.AssertEquals(t, "30a57f4a-336b-4382-8275-d708babd2241", r.ID)
   249  	th.AssertEquals(t, 20, r.DSCPMark)
   250  }
   251  
   252  func TestUpdateDSCPMarkingRule(t *testing.T) {
   253  	th.SetupHTTP()
   254  	defer th.TeardownHTTP()
   255  
   256  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   257  		th.TestMethod(t, r, "PUT")
   258  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   259  		th.TestHeader(t, r, "Content-Type", "application/json")
   260  		th.TestHeader(t, r, "Accept", "application/json")
   261  		th.TestJSONRequest(t, r, DSCPMarkingRuleUpdateRequest)
   262  
   263  		w.Header().Add("Content-Type", "application/json")
   264  		w.WriteHeader(http.StatusOK)
   265  
   266  		fmt.Fprint(w, DSCPMarkingRuleUpdateResult)
   267  	})
   268  
   269  	dscpMark := 26
   270  	opts := rules.UpdateDSCPMarkingRuleOpts{
   271  		DSCPMark: &dscpMark,
   272  	}
   273  	r, err := rules.UpdateDSCPMarkingRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractDSCPMarkingRule()
   274  	th.AssertNoErr(t, err)
   275  
   276  	th.AssertEquals(t, "30a57f4a-336b-4382-8275-d708babd2241", r.ID)
   277  	th.AssertEquals(t, 26, r.DSCPMark)
   278  }
   279  
   280  func TestDeleteDSCPMarkingRule(t *testing.T) {
   281  	th.SetupHTTP()
   282  	defer th.TeardownHTTP()
   283  
   284  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   285  		th.TestMethod(t, r, "DELETE")
   286  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   287  		w.WriteHeader(http.StatusNoContent)
   288  	})
   289  
   290  	res := rules.DeleteDSCPMarkingRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241")
   291  	th.AssertNoErr(t, res.Err)
   292  }
   293  
   294  func TestListMinimumBandwidthRule(t *testing.T) {
   295  	th.SetupHTTP()
   296  	defer th.TeardownHTTP()
   297  
   298  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules", func(w http.ResponseWriter, r *http.Request) {
   299  		th.TestMethod(t, r, "GET")
   300  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   301  
   302  		w.Header().Add("Content-Type", "application/json")
   303  		w.WriteHeader(http.StatusOK)
   304  
   305  		fmt.Fprint(w, MinimumBandwidthRulesListResult)
   306  	})
   307  
   308  	count := 0
   309  
   310  	err := rules.ListMinimumBandwidthRules(
   311  		fake.ServiceClient(),
   312  		"501005fa-3b56-4061-aaca-3f24995112e1",
   313  		rules.MinimumBandwidthRulesListOpts{},
   314  	).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   315  		count++
   316  		actual, err := rules.ExtractMinimumBandwidthRules(page)
   317  		if err != nil {
   318  			t.Errorf("Failed to extract minimum bandwith rules: %v", err)
   319  			return false, nil
   320  		}
   321  
   322  		expected := []rules.MinimumBandwidthRule{
   323  			{
   324  				ID:        "30a57f4a-336b-4382-8275-d708babd2241",
   325  				Direction: "egress",
   326  				MinKBps:   3000,
   327  			},
   328  		}
   329  
   330  		th.CheckDeepEquals(t, expected, actual)
   331  
   332  		return true, nil
   333  	})
   334  	th.AssertNoErr(t, err)
   335  
   336  	if count != 1 {
   337  		t.Errorf("Expected 1 page, got %d", count)
   338  	}
   339  }
   340  
   341  func TestGetMinimumBandwidthRule(t *testing.T) {
   342  	th.SetupHTTP()
   343  	defer th.TeardownHTTP()
   344  
   345  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   346  		th.TestMethod(t, r, "GET")
   347  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   348  
   349  		w.Header().Add("Content-Type", "application/json")
   350  		w.WriteHeader(http.StatusOK)
   351  
   352  		fmt.Fprint(w, MinimumBandwidthRulesGetResult)
   353  	})
   354  
   355  	r, err := rules.GetMinimumBandwidthRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241").ExtractMinimumBandwidthRule()
   356  	th.AssertNoErr(t, err)
   357  
   358  	th.AssertEquals(t, r.ID, "30a57f4a-336b-4382-8275-d708babd2241")
   359  	th.AssertEquals(t, r.Direction, "egress")
   360  	th.AssertEquals(t, r.MinKBps, 3000)
   361  }
   362  
   363  func TestCreateMinimumBandwidthRule(t *testing.T) {
   364  	th.SetupHTTP()
   365  	defer th.TeardownHTTP()
   366  
   367  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules", func(w http.ResponseWriter, r *http.Request) {
   368  		th.TestMethod(t, r, "POST")
   369  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   370  		th.TestHeader(t, r, "Content-Type", "application/json")
   371  		th.TestHeader(t, r, "Accept", "application/json")
   372  		th.TestJSONRequest(t, r, MinimumBandwidthRulesCreateRequest)
   373  
   374  		w.Header().Add("Content-Type", "application/json")
   375  		w.WriteHeader(http.StatusCreated)
   376  
   377  		fmt.Fprint(w, MinimumBandwidthRulesCreateResult)
   378  	})
   379  
   380  	opts := rules.CreateMinimumBandwidthRuleOpts{
   381  		MinKBps: 2000,
   382  	}
   383  	r, err := rules.CreateMinimumBandwidthRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractMinimumBandwidthRule()
   384  	th.AssertNoErr(t, err)
   385  
   386  	th.AssertEquals(t, 2000, r.MinKBps)
   387  }
   388  
   389  func TestUpdateMinimumBandwidthRule(t *testing.T) {
   390  	th.SetupHTTP()
   391  	defer th.TeardownHTTP()
   392  
   393  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   394  		th.TestMethod(t, r, "PUT")
   395  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   396  		th.TestHeader(t, r, "Content-Type", "application/json")
   397  		th.TestHeader(t, r, "Accept", "application/json")
   398  		th.TestJSONRequest(t, r, MinimumBandwidthRulesUpdateRequest)
   399  
   400  		w.Header().Add("Content-Type", "application/json")
   401  		w.WriteHeader(http.StatusOK)
   402  
   403  		fmt.Fprint(w, MinimumBandwidthRulesUpdateResult)
   404  	})
   405  
   406  	minKBps := 500
   407  	opts := rules.UpdateMinimumBandwidthRuleOpts{
   408  		MinKBps: &minKBps,
   409  	}
   410  	r, err := rules.UpdateMinimumBandwidthRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractMinimumBandwidthRule()
   411  	th.AssertNoErr(t, err)
   412  
   413  	th.AssertEquals(t, 500, r.MinKBps)
   414  }
   415  
   416  func TestDeleteMinimumBandwidthRule(t *testing.T) {
   417  	th.SetupHTTP()
   418  	defer th.TeardownHTTP()
   419  
   420  	th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
   421  		th.TestMethod(t, r, "DELETE")
   422  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   423  		w.WriteHeader(http.StatusNoContent)
   424  	})
   425  
   426  	res := rules.DeleteMinimumBandwidthRule(context.TODO(), fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241")
   427  	th.AssertNoErr(t, res.Err)
   428  }