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