Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +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 | |
Ken'ichi Ohmichi | 14bf424 | 2015-07-17 02:28:21 +0000 | [diff] [blame] | 17 | from oslo_serialization import jsonutils as json |
Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +0000 | [diff] [blame] | 18 | from oslotest import mockpatch |
| 19 | |
| 20 | from tempest.services.compute.json import agents_client |
| 21 | from tempest.tests import base |
| 22 | from tempest.tests import fake_auth_provider |
Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class TestAgentsClient(base.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | super(TestAgentsClient, self).setUp() |
Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +0000 | [diff] [blame] | 29 | fake_auth = fake_auth_provider.FakeAuthProvider() |
| 30 | self.client = agents_client.AgentsClient(fake_auth, |
| 31 | 'compute', 'regionOne') |
| 32 | |
| 33 | def _test_list_agents(self, bytes_body=False): |
Ken'ichi Ohmichi | eb23aef | 2015-07-17 02:02:09 +0000 | [diff] [blame] | 34 | body = '{"agents": []}' |
Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +0000 | [diff] [blame] | 35 | if bytes_body: |
Ken'ichi Ohmichi | 14bf424 | 2015-07-17 02:28:21 +0000 | [diff] [blame] | 36 | body = body.encode('utf-8') |
ghanshyam | 5805ccc | 2015-08-04 14:06:20 +0900 | [diff] [blame] | 37 | expected = {"agents": []} |
Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +0000 | [diff] [blame] | 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_agents()) |
| 43 | |
Ken'ichi Ohmichi | 14bf424 | 2015-07-17 02:28:21 +0000 | [diff] [blame] | 44 | def _test_create_agent(self, bytes_body=False): |
ghanshyam | 5805ccc | 2015-08-04 14:06:20 +0900 | [diff] [blame] | 45 | expected = {"agent": {"url": "http://foo.com", "hypervisor": "kvm", |
| 46 | "md5hash": "md5", "version": "2", |
| 47 | "architecture": "x86_64", |
| 48 | "os": "linux", "agent_id": 1}} |
| 49 | serialized_body = json.dumps(expected) |
Ken'ichi Ohmichi | 14bf424 | 2015-07-17 02:28:21 +0000 | [diff] [blame] | 50 | if bytes_body: |
| 51 | serialized_body = serialized_body.encode('utf-8') |
| 52 | |
| 53 | mocked_resp = (httplib2.Response({'status': 200}), serialized_body) |
| 54 | self.useFixture(mockpatch.Patch( |
| 55 | 'tempest.common.service_client.ServiceClient.post', |
| 56 | return_value=mocked_resp)) |
| 57 | resp = self.client.create_agent( |
| 58 | url="http://foo.com", hypervisor="kvm", md5hash="md5", |
| 59 | version="2", architecture="x86_64", os="linux" |
| 60 | ) |
| 61 | self.assertEqual(expected, resp) |
| 62 | |
| 63 | def _test_delete_agent(self): |
| 64 | mocked_resp = (httplib2.Response({'status': 200}), None) |
| 65 | self.useFixture(mockpatch.Patch( |
| 66 | 'tempest.common.service_client.ServiceClient.delete', |
| 67 | return_value=mocked_resp)) |
| 68 | self.client.delete_agent("1") |
| 69 | |
| 70 | def _test_update_agent(self, bytes_body=False): |
ghanshyam | 5805ccc | 2015-08-04 14:06:20 +0900 | [diff] [blame] | 71 | expected = {"agent": {"url": "http://foo.com", "md5hash": "md5", |
| 72 | "version": "2", "agent_id": 1}} |
| 73 | serialized_body = json.dumps(expected) |
Ken'ichi Ohmichi | 14bf424 | 2015-07-17 02:28:21 +0000 | [diff] [blame] | 74 | if bytes_body: |
| 75 | serialized_body = serialized_body.encode('utf-8') |
| 76 | |
| 77 | mocked_resp = (httplib2.Response({'status': 200}), serialized_body) |
| 78 | self.useFixture(mockpatch.Patch( |
| 79 | 'tempest.common.service_client.ServiceClient.put', |
| 80 | return_value=mocked_resp)) |
| 81 | resp = self.client.update_agent( |
| 82 | "1", url="http://foo.com", md5hash="md5", version="2" |
| 83 | ) |
| 84 | self.assertEqual(expected, resp) |
| 85 | |
Ken'ichi Ohmichi | ad0dfb4 | 2015-07-14 08:52:25 +0000 | [diff] [blame] | 86 | def test_list_agents_with_str_body(self): |
| 87 | self._test_list_agents() |
| 88 | |
| 89 | def test_list_agents_with_bytes_body(self): |
| 90 | self._test_list_agents(bytes_body=True) |
Ken'ichi Ohmichi | 14bf424 | 2015-07-17 02:28:21 +0000 | [diff] [blame] | 91 | |
| 92 | def test_create_agent_with_str_body(self): |
| 93 | self._test_create_agent() |
| 94 | |
| 95 | def test_create_agent_with_bytes_body(self): |
| 96 | self._test_create_agent(bytes_body=True) |
| 97 | |
| 98 | def test_delete_agent(self): |
| 99 | self._test_delete_agent() |
| 100 | |
| 101 | def test_update_agent_with_str_body(self): |
| 102 | self._test_update_agent() |
| 103 | |
| 104 | def test_update_agent_with_bytes_body(self): |
| 105 | self._test_update_agent(bytes_body=True) |