github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/flavors/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/flavors"
    10  	"github.com/huaweicloud/golangsdk/pagination"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    13  )
    14  
    15  const tokenID = "blerb"
    16  
    17  func TestListFlavors(t *testing.T) {
    18  	th.SetupHTTP()
    19  	defer th.TeardownHTTP()
    20  
    21  	th.Mux.HandleFunc("/flavors/detail", 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  		r.ParseForm()
    27  		marker := r.Form.Get("marker")
    28  		switch marker {
    29  		case "":
    30  			fmt.Fprintf(w, `
    31  					{
    32  						"flavors": [
    33  							{
    34  								"id": "1",
    35  								"name": "m1.tiny",
    36  								"vcpus": 1,
    37  								"disk": 1,
    38  								"ram": 512,
    39  								"swap":"",
    40  								"os-flavor-access:is_public": true,
    41  								"OS-FLV-EXT-DATA:ephemeral": 10
    42  							},
    43  							{
    44  								"id": "2",
    45  								"name": "m1.small",
    46  								"vcpus": 1,
    47  								"disk": 20,
    48  								"ram": 2048,
    49  								"swap": 1000,
    50  								"os-flavor-access:is_public": true,
    51  								"OS-FLV-EXT-DATA:ephemeral": 0
    52  							},
    53  							{
    54  								"id": "3",
    55  								"name": "m1.medium",
    56  								"vcpus": 2,
    57  								"disk": 40,
    58  								"ram": 4096,
    59  								"swap": 1000,
    60  								"os-flavor-access:is_public": false,
    61  								"OS-FLV-EXT-DATA:ephemeral": 0
    62  							}
    63  						],
    64  						"flavors_links": [
    65  							{
    66  								"href": "%s/flavors/detail?marker=2",
    67  								"rel": "next"
    68  							}
    69  						]
    70  					}
    71  				`, th.Server.URL)
    72  		case "2":
    73  			fmt.Fprintf(w, `{ "flavors": [] }`)
    74  		default:
    75  			t.Fatalf("Unexpected marker: [%s]", marker)
    76  		}
    77  	})
    78  
    79  	pages := 0
    80  	// Get public and private flavors
    81  	err := flavors.ListDetail(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
    82  		pages++
    83  
    84  		actual, err := flavors.ExtractFlavors(page)
    85  		if err != nil {
    86  			return false, err
    87  		}
    88  
    89  		expected := []flavors.Flavor{
    90  			{ID: "1", Name: "m1.tiny", VCPUs: 1, Disk: 1, RAM: 512, Swap: 0, IsPublic: true, Ephemeral: 10},
    91  			{ID: "2", Name: "m1.small", VCPUs: 1, Disk: 20, RAM: 2048, Swap: 1000, IsPublic: true, Ephemeral: 0},
    92  			{ID: "3", Name: "m1.medium", VCPUs: 2, Disk: 40, RAM: 4096, Swap: 1000, IsPublic: false, Ephemeral: 0},
    93  		}
    94  
    95  		if !reflect.DeepEqual(expected, actual) {
    96  			t.Errorf("Expected %#v, but was %#v", expected, actual)
    97  		}
    98  
    99  		return true, nil
   100  	})
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if pages != 1 {
   105  		t.Errorf("Expected one page, got %d", pages)
   106  	}
   107  }
   108  
   109  func TestGetFlavor(t *testing.T) {
   110  	th.SetupHTTP()
   111  	defer th.TeardownHTTP()
   112  
   113  	th.Mux.HandleFunc("/flavors/12345", func(w http.ResponseWriter, r *http.Request) {
   114  		th.TestMethod(t, r, "GET")
   115  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   116  
   117  		w.Header().Add("Content-Type", "application/json")
   118  		fmt.Fprintf(w, `
   119  			{
   120  				"flavor": {
   121  					"id": "1",
   122  					"name": "m1.tiny",
   123  					"disk": 1,
   124  					"ram": 512,
   125  					"vcpus": 1,
   126  					"rxtx_factor": 1,
   127  					"swap": ""
   128  				}
   129  			}
   130  		`)
   131  	})
   132  
   133  	actual, err := flavors.Get(fake.ServiceClient(), "12345").Extract()
   134  	if err != nil {
   135  		t.Fatalf("Unable to get flavor: %v", err)
   136  	}
   137  
   138  	expected := &flavors.Flavor{
   139  		ID:         "1",
   140  		Name:       "m1.tiny",
   141  		Disk:       1,
   142  		RAM:        512,
   143  		VCPUs:      1,
   144  		RxTxFactor: 1,
   145  		Swap:       0,
   146  	}
   147  	if !reflect.DeepEqual(expected, actual) {
   148  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   149  	}
   150  }
   151  
   152  func TestCreateFlavor(t *testing.T) {
   153  	th.SetupHTTP()
   154  	defer th.TeardownHTTP()
   155  
   156  	th.Mux.HandleFunc("/flavors", func(w http.ResponseWriter, r *http.Request) {
   157  		th.TestMethod(t, r, "POST")
   158  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   159  
   160  		w.Header().Add("Content-Type", "application/json")
   161  		fmt.Fprintf(w, `
   162  			{
   163  				"flavor": {
   164  					"id": "1",
   165  					"name": "m1.tiny",
   166  					"disk": 1,
   167  					"ram": 512,
   168  					"vcpus": 1,
   169  					"rxtx_factor": 1,
   170  					"swap": ""
   171  				}
   172  			}
   173  		`)
   174  	})
   175  
   176  	disk := 1
   177  	opts := &flavors.CreateOpts{
   178  		ID:         "1",
   179  		Name:       "m1.tiny",
   180  		Disk:       &disk,
   181  		RAM:        512,
   182  		VCPUs:      1,
   183  		RxTxFactor: 1.0,
   184  	}
   185  	actual, err := flavors.Create(fake.ServiceClient(), opts).Extract()
   186  	if err != nil {
   187  		t.Fatalf("Unable to create flavor: %v", err)
   188  	}
   189  
   190  	expected := &flavors.Flavor{
   191  		ID:         "1",
   192  		Name:       "m1.tiny",
   193  		Disk:       1,
   194  		RAM:        512,
   195  		VCPUs:      1,
   196  		RxTxFactor: 1,
   197  		Swap:       0,
   198  	}
   199  	if !reflect.DeepEqual(expected, actual) {
   200  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   201  	}
   202  }
   203  
   204  func TestDeleteFlavor(t *testing.T) {
   205  	th.SetupHTTP()
   206  	defer th.TeardownHTTP()
   207  
   208  	th.Mux.HandleFunc("/flavors/12345678", func(w http.ResponseWriter, r *http.Request) {
   209  		th.TestMethod(t, r, "DELETE")
   210  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   211  
   212  		w.WriteHeader(http.StatusAccepted)
   213  	})
   214  
   215  	res := flavors.Delete(fake.ServiceClient(), "12345678")
   216  	th.AssertNoErr(t, res.Err)
   217  }
   218  
   219  func TestFlavorAccessesList(t *testing.T) {
   220  	th.SetupHTTP()
   221  	defer th.TeardownHTTP()
   222  
   223  	th.Mux.HandleFunc("/flavors/12345678/os-flavor-access", func(w http.ResponseWriter, r *http.Request) {
   224  		th.TestMethod(t, r, "GET")
   225  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   226  		w.Header().Add("Content-Type", "application/json")
   227  		fmt.Fprintf(w, `
   228  			{
   229  			  "flavor_access": [
   230  			    {
   231  			      "flavor_id": "12345678",
   232  			      "tenant_id": "2f954bcf047c4ee9b09a37d49ae6db54"
   233  			    }
   234  			  ]
   235  			}
   236  		`)
   237  	})
   238  
   239  	expected := []flavors.FlavorAccess{
   240  		{
   241  			FlavorID: "12345678",
   242  			TenantID: "2f954bcf047c4ee9b09a37d49ae6db54",
   243  		},
   244  	}
   245  
   246  	allPages, err := flavors.ListAccesses(fake.ServiceClient(), "12345678").AllPages()
   247  	th.AssertNoErr(t, err)
   248  
   249  	actual, err := flavors.ExtractAccesses(allPages)
   250  	th.AssertNoErr(t, err)
   251  
   252  	if !reflect.DeepEqual(expected, actual) {
   253  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   254  	}
   255  }
   256  
   257  func TestFlavorAccessAdd(t *testing.T) {
   258  	th.SetupHTTP()
   259  	defer th.TeardownHTTP()
   260  
   261  	th.Mux.HandleFunc("/flavors/12345678/action", func(w http.ResponseWriter, r *http.Request) {
   262  		th.TestMethod(t, r, "POST")
   263  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   264  		th.TestHeader(t, r, "accept", "application/json")
   265  		th.TestJSONRequest(t, r, `
   266  			{
   267  			  "addTenantAccess": {
   268  			    "tenant": "2f954bcf047c4ee9b09a37d49ae6db54"
   269  			  }
   270  			}
   271  		`)
   272  
   273  		w.Header().Add("Content-Type", "application/json")
   274  		w.WriteHeader(http.StatusOK)
   275  		fmt.Fprintf(w, `
   276  			{
   277  			  "flavor_access": [
   278  			    {
   279  			      "flavor_id": "12345678",
   280  			      "tenant_id": "2f954bcf047c4ee9b09a37d49ae6db54"
   281  			    }
   282  			  ]
   283  			}
   284  			`)
   285  	})
   286  
   287  	expected := []flavors.FlavorAccess{
   288  		{
   289  			FlavorID: "12345678",
   290  			TenantID: "2f954bcf047c4ee9b09a37d49ae6db54",
   291  		},
   292  	}
   293  
   294  	addAccessOpts := flavors.AddAccessOpts{
   295  		Tenant: "2f954bcf047c4ee9b09a37d49ae6db54",
   296  	}
   297  
   298  	actual, err := flavors.AddAccess(fake.ServiceClient(), "12345678", addAccessOpts).Extract()
   299  	th.AssertNoErr(t, err)
   300  
   301  	if !reflect.DeepEqual(expected, actual) {
   302  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   303  	}
   304  }
   305  
   306  func TestFlavorAccessRemove(t *testing.T) {
   307  	th.SetupHTTP()
   308  	defer th.TeardownHTTP()
   309  
   310  	th.Mux.HandleFunc("/flavors/12345678/action", func(w http.ResponseWriter, r *http.Request) {
   311  		th.TestMethod(t, r, "POST")
   312  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   313  		th.TestHeader(t, r, "accept", "application/json")
   314  		th.TestJSONRequest(t, r, `
   315  			{
   316  			  "removeTenantAccess": {
   317  			    "tenant": "2f954bcf047c4ee9b09a37d49ae6db54"
   318  			  }
   319  			}
   320  		`)
   321  
   322  		w.Header().Add("Content-Type", "application/json")
   323  		w.WriteHeader(http.StatusOK)
   324  		fmt.Fprintf(w, `
   325  			{
   326  			  "flavor_access": []
   327  			}
   328  			`)
   329  	})
   330  
   331  	expected := []flavors.FlavorAccess{}
   332  	removeAccessOpts := flavors.RemoveAccessOpts{
   333  		Tenant: "2f954bcf047c4ee9b09a37d49ae6db54",
   334  	}
   335  
   336  	actual, err := flavors.RemoveAccess(fake.ServiceClient(), "12345678", removeAccessOpts).Extract()
   337  	th.AssertNoErr(t, err)
   338  
   339  	if !reflect.DeepEqual(expected, actual) {
   340  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   341  	}
   342  }
   343  
   344  func TestFlavorExtraSpecsList(t *testing.T) {
   345  	th.SetupHTTP()
   346  	defer th.TeardownHTTP()
   347  	HandleExtraSpecsListSuccessfully(t)
   348  
   349  	expected := ExtraSpecs
   350  	actual, err := flavors.ListExtraSpecs(fake.ServiceClient(), "1").Extract()
   351  	th.AssertNoErr(t, err)
   352  	th.CheckDeepEquals(t, expected, actual)
   353  }
   354  
   355  func TestFlavorExtraSpecGet(t *testing.T) {
   356  	th.SetupHTTP()
   357  	defer th.TeardownHTTP()
   358  	HandleExtraSpecGetSuccessfully(t)
   359  
   360  	expected := ExtraSpec
   361  	actual, err := flavors.GetExtraSpec(fake.ServiceClient(), "1", "hw:cpu_policy").Extract()
   362  	th.AssertNoErr(t, err)
   363  	th.CheckDeepEquals(t, expected, actual)
   364  }
   365  
   366  func TestFlavorExtraSpecsCreate(t *testing.T) {
   367  	th.SetupHTTP()
   368  	defer th.TeardownHTTP()
   369  	HandleExtraSpecsCreateSuccessfully(t)
   370  
   371  	createOpts := flavors.ExtraSpecsOpts{
   372  		"hw:cpu_policy":        "CPU-POLICY",
   373  		"hw:cpu_thread_policy": "CPU-THREAD-POLICY",
   374  	}
   375  	expected := ExtraSpecs
   376  	actual, err := flavors.CreateExtraSpecs(fake.ServiceClient(), "1", createOpts).Extract()
   377  	th.AssertNoErr(t, err)
   378  	th.CheckDeepEquals(t, expected, actual)
   379  }
   380  
   381  func TestFlavorExtraSpecUpdate(t *testing.T) {
   382  	th.SetupHTTP()
   383  	defer th.TeardownHTTP()
   384  	HandleExtraSpecUpdateSuccessfully(t)
   385  
   386  	updateOpts := flavors.ExtraSpecsOpts{
   387  		"hw:cpu_policy": "CPU-POLICY-2",
   388  	}
   389  	expected := UpdatedExtraSpec
   390  	actual, err := flavors.UpdateExtraSpec(fake.ServiceClient(), "1", updateOpts).Extract()
   391  	th.AssertNoErr(t, err)
   392  	th.CheckDeepEquals(t, expected, actual)
   393  }
   394  
   395  func TestFlavorExtraSpecDelete(t *testing.T) {
   396  	th.SetupHTTP()
   397  	defer th.TeardownHTTP()
   398  	HandleExtraSpecDeleteSuccessfully(t)
   399  
   400  	res := flavors.DeleteExtraSpec(fake.ServiceClient(), "1", "hw:cpu_policy")
   401  	th.AssertNoErr(t, res.Err)
   402  }