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