Kenji Yasui | 1c695ee | 2015-07-15 08:10:19 +0000 | [diff] [blame] | 1 | # Copyright 2015 NEC Corporation. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import httplib2 |
| 16 | |
Kenji Yasui | c03e13b | 2015-08-03 05:47:42 +0000 | [diff] [blame] | 17 | from oslo_serialization import jsonutils as json |
Kenji Yasui | 1c695ee | 2015-07-15 08:10:19 +0000 | [diff] [blame] | 18 | from oslotest import mockpatch |
| 19 | |
| 20 | from tempest.services.compute.json import aggregates_client |
| 21 | from tempest.tests import base |
| 22 | from tempest.tests import fake_auth_provider |
| 23 | |
| 24 | |
| 25 | class TestAggregatesClient(base.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | super(TestAggregatesClient, self).setUp() |
| 29 | fake_auth = fake_auth_provider.FakeAuthProvider() |
| 30 | self.client = aggregates_client.AggregatesClient( |
| 31 | fake_auth, 'compute', 'regionOne') |
| 32 | |
| 33 | def _test_list_aggregates(self, bytes_body=False): |
| 34 | body = '{"aggregates": []}' |
| 35 | if bytes_body: |
| 36 | body = body.encode('utf-8') |
| 37 | expected = [] |
| 38 | response = (httplib2.Response({'status': 200}), body) |
| 39 | self.useFixture(mockpatch.Patch( |
| 40 | 'tempest.common.service_client.ServiceClient.get', |
| 41 | return_value=response)) |
| 42 | self.assertEqual(expected, self.client.list_aggregates()) |
| 43 | |
| 44 | def test_list_aggregates_with_str_body(self): |
| 45 | self._test_list_aggregates() |
| 46 | |
| 47 | def test_list_aggregates_with_bytes_body(self): |
| 48 | self._test_list_aggregates(bytes_body=True) |
Kenji Yasui | c03e13b | 2015-08-03 05:47:42 +0000 | [diff] [blame] | 49 | |
| 50 | def _test_show_aggregate(self, bytes_body=False): |
| 51 | expected = {"name": "hoge", |
| 52 | "availability_zone": None, |
| 53 | "deleted": False, |
| 54 | "created_at": |
| 55 | "2015-07-16T03:07:32.000000", |
| 56 | "updated_at": None, |
| 57 | "hosts": [], |
| 58 | "deleted_at": None, |
| 59 | "id": 1, |
| 60 | "metadata": {}} |
| 61 | serialized_body = json.dumps({"aggregate": expected}) |
| 62 | if bytes_body: |
| 63 | serialized_body = serialized_body.encode('utf-8') |
| 64 | |
| 65 | mocked_resp = (httplib2.Response({'status': 200}), serialized_body) |
| 66 | self.useFixture(mockpatch.Patch( |
| 67 | 'tempest.common.service_client.ServiceClient.get', |
| 68 | return_value=mocked_resp)) |
| 69 | resp = self.client.show_aggregate(1) |
| 70 | self.assertEqual(expected, resp) |
| 71 | |
| 72 | def test_show_aggregate_with_str_body(self): |
| 73 | self._test_show_aggregate() |
| 74 | |
| 75 | def test_show_aggregate_with_bytes_body(self): |
| 76 | self._test_show_aggregate(bytes_body=True) |
| 77 | |
| 78 | def _test_create_aggregate(self, bytes_body=False): |
| 79 | expected = {"name": u'\xf4', |
| 80 | "availability_zone": None, |
| 81 | "deleted": False, |
| 82 | "created_at": "2015-07-21T04:11:18.000000", |
| 83 | "updated_at": None, |
| 84 | "deleted_at": None, |
| 85 | "id": 1} |
| 86 | serialized_body = json.dumps({"aggregate": expected}) |
| 87 | if bytes_body: |
| 88 | serialized_body = serialized_body.encode('utf-8') |
| 89 | |
| 90 | mocked_resp = (httplib2.Response({'status': 200}), serialized_body) |
| 91 | self.useFixture(mockpatch.Patch( |
| 92 | 'tempest.common.service_client.ServiceClient.post', |
| 93 | return_value=mocked_resp)) |
| 94 | resp = self.client.create_aggregate(name='hoge') |
| 95 | self.assertEqual(expected, resp) |
| 96 | |
| 97 | def test_create_aggregate_with_str_body(self): |
| 98 | self._test_create_aggregate() |
| 99 | |
| 100 | def test_create_aggregate_with_bytes_body(self): |
| 101 | self._test_create_aggregate(bytes_body=True) |
| 102 | |
| 103 | def test_delete_aggregate(self): |
| 104 | expected = {} |
| 105 | mocked_resp = (httplib2.Response({'status': 200}), None) |
| 106 | self.useFixture(mockpatch.Patch( |
| 107 | 'tempest.common.service_client.ServiceClient.delete', |
| 108 | return_value=mocked_resp)) |
| 109 | resp = self.client.delete_aggregate("1") |
| 110 | self.assertEqual(expected, resp) |
| 111 | |
| 112 | def _test_update_aggregate(self, bytes_body=False): |
| 113 | expected = {"name": u'\xe9', |
| 114 | "availability_zone": None, |
| 115 | "deleted": False, |
| 116 | "created_at": "2015-07-16T03:07:32.000000", |
| 117 | "updated_at": "2015-07-23T05:16:29.000000", |
| 118 | "hosts": [], |
| 119 | "deleted_at": None, |
| 120 | "id": 1, |
| 121 | "metadata": {}} |
| 122 | serialized_body = json.dumps({"aggregate": expected}) |
| 123 | if bytes_body: |
| 124 | serialized_body = serialized_body.encode('utf-8') |
| 125 | |
| 126 | mocked_resp = (httplib2.Response({'status': 200}), serialized_body) |
| 127 | self.useFixture(mockpatch.Patch( |
| 128 | 'tempest.common.service_client.ServiceClient.put', |
| 129 | return_value=mocked_resp)) |
| 130 | resp = self.client.update_aggregate(1) |
| 131 | self.assertEqual(expected, resp) |
| 132 | |
| 133 | def test_update_aggregate_with_str_body(self): |
| 134 | self._test_update_aggregate() |
| 135 | |
| 136 | def test_update_aggregate_with_bytes_body(self): |
| 137 | self._test_update_aggregate(bytes_body=True) |