github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/qos/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/qos"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	"github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  func TestCreate(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	MockCreateResponse(t)
    18  
    19  	options := qos.CreateOpts{
    20  		Name:     "qos-001",
    21  		Consumer: qos.ConsumerFront,
    22  		Specs: map[string]string{
    23  			"read_iops_sec": "20000",
    24  		},
    25  	}
    26  	actual, err := qos.Create(client.ServiceClient(), options).Extract()
    27  	th.AssertNoErr(t, err)
    28  	th.CheckDeepEquals(t, &createQoSExpected, actual)
    29  }
    30  
    31  func TestDelete(t *testing.T) {
    32  	th.SetupHTTP()
    33  	defer th.TeardownHTTP()
    34  
    35  	MockDeleteResponse(t)
    36  
    37  	res := qos.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", qos.DeleteOpts{})
    38  	th.AssertNoErr(t, res.Err)
    39  }
    40  
    41  func TestList(t *testing.T) {
    42  	th.SetupHTTP()
    43  	defer th.TeardownHTTP()
    44  
    45  	MockListResponse(t)
    46  
    47  	pages := 0
    48  	err := qos.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
    49  		pages++
    50  		actual, err := qos.ExtractQoS(page)
    51  		if err != nil {
    52  			return false, err
    53  		}
    54  
    55  		expected := []qos.QoS{
    56  			{ID: "1", Consumer: "back-end", Name: "foo", Specs: map[string]string{}},
    57  			{ID: "2", Consumer: "front-end", Name: "bar", Specs: map[string]string{
    58  				"read_iops_sec": "20000",
    59  			},
    60  			},
    61  		}
    62  
    63  		if !reflect.DeepEqual(expected, actual) {
    64  			t.Errorf("Expected %#v, but was %#v", expected, actual)
    65  		}
    66  
    67  		return true, nil
    68  	})
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if pages != 1 {
    73  		t.Errorf("Expected one page, got %d", pages)
    74  	}
    75  }
    76  
    77  func TestGet(t *testing.T) {
    78  	th.SetupHTTP()
    79  	defer th.TeardownHTTP()
    80  
    81  	MockGetResponse(t)
    82  
    83  	actual, err := qos.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
    84  	th.AssertNoErr(t, err)
    85  	th.CheckDeepEquals(t, &getQoSExpected, actual)
    86  }
    87  
    88  func TestUpdate(t *testing.T) {
    89  	th.SetupHTTP()
    90  	defer th.TeardownHTTP()
    91  	MockUpdateResponse(t)
    92  
    93  	updateOpts := qos.UpdateOpts{
    94  		Consumer: qos.ConsumerBack,
    95  		Specs: map[string]string{
    96  			"read_iops_sec":  "40000",
    97  			"write_iops_sec": "40000",
    98  		},
    99  	}
   100  
   101  	expected := UpdateQos
   102  	actual, err := qos.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", updateOpts).Extract()
   103  	th.AssertNoErr(t, err)
   104  	th.CheckDeepEquals(t, expected, actual)
   105  }
   106  
   107  func TestDeleteKeys(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  
   111  	MockDeleteKeysResponse(t)
   112  
   113  	res := qos.DeleteKeys(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", qos.DeleteKeysOpts{"read_iops_sec"})
   114  	th.AssertNoErr(t, res.Err)
   115  }
   116  
   117  func TestAssociate(t *testing.T) {
   118  	th.SetupHTTP()
   119  	defer th.TeardownHTTP()
   120  
   121  	MockAssociateResponse(t)
   122  
   123  	associateOpts := qos.AssociateOpts{
   124  		VolumeTypeID: "b596be6a-0ce9-43fa-804a-5c5e181ede76",
   125  	}
   126  
   127  	res := qos.Associate(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", associateOpts)
   128  	th.AssertNoErr(t, res.Err)
   129  }
   130  
   131  func TestDisssociate(t *testing.T) {
   132  	th.SetupHTTP()
   133  	defer th.TeardownHTTP()
   134  
   135  	MockDisassociateResponse(t)
   136  
   137  	disassociateOpts := qos.DisassociateOpts{
   138  		VolumeTypeID: "b596be6a-0ce9-43fa-804a-5c5e181ede76",
   139  	}
   140  
   141  	res := qos.Disassociate(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", disassociateOpts)
   142  	th.AssertNoErr(t, res.Err)
   143  }
   144  
   145  func TestDissasociateAll(t *testing.T) {
   146  	th.SetupHTTP()
   147  	defer th.TeardownHTTP()
   148  
   149  	MockDisassociateAllResponse(t)
   150  
   151  	res := qos.DisassociateAll(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   152  	th.AssertNoErr(t, res.Err)
   153  }
   154  
   155  func TestQosAssociationsList(t *testing.T) {
   156  	th.SetupHTTP()
   157  	defer th.TeardownHTTP()
   158  
   159  	MockListAssociationsResponse(t)
   160  
   161  	expected := []qos.QosAssociation{
   162  		{
   163  			Name:            "foo",
   164  			ID:              "2f954bcf047c4ee9b09a37d49ae6db54",
   165  			AssociationType: "volume_type",
   166  		},
   167  	}
   168  
   169  	allPages, err := qos.ListAssociations(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").AllPages()
   170  	th.AssertNoErr(t, err)
   171  
   172  	actual, err := qos.ExtractAssociations(allPages)
   173  	th.AssertNoErr(t, err)
   174  
   175  	if !reflect.DeepEqual(expected, actual) {
   176  		t.Errorf("Expected %#v, but was %#v", expected, actual)
   177  	}
   178  }