blob: d14d8bf5840f3e759e74112b2e9f600f6ae5fba3 [file] [log] [blame]
Ken'ichi Ohmichiad0dfb42015-07-14 08:52:25 +00001# 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
15import httplib2
16
Ken'ichi Ohmichi14bf4242015-07-17 02:28:21 +000017from oslo_serialization import jsonutils as json
Ken'ichi Ohmichiad0dfb42015-07-14 08:52:25 +000018from oslotest import mockpatch
19
20from tempest.services.compute.json import agents_client
21from tempest.tests import base
22from tempest.tests import fake_auth_provider
Ken'ichi Ohmichiad0dfb42015-07-14 08:52:25 +000023
24
25class TestAgentsClient(base.TestCase):
26
27 def setUp(self):
28 super(TestAgentsClient, self).setUp()
Ken'ichi Ohmichiad0dfb42015-07-14 08:52:25 +000029 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 Ohmichieb23aef2015-07-17 02:02:09 +000034 body = '{"agents": []}'
Ken'ichi Ohmichiad0dfb42015-07-14 08:52:25 +000035 if bytes_body:
Ken'ichi Ohmichi14bf4242015-07-17 02:28:21 +000036 body = body.encode('utf-8')
ghanshyam5805ccc2015-08-04 14:06:20 +090037 expected = {"agents": []}
Ken'ichi Ohmichiad0dfb42015-07-14 08:52:25 +000038 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 Ohmichi14bf4242015-07-17 02:28:21 +000044 def _test_create_agent(self, bytes_body=False):
ghanshyam5805ccc2015-08-04 14:06:20 +090045 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 Ohmichi14bf4242015-07-17 02:28:21 +000050 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):
ghanshyam5805ccc2015-08-04 14:06:20 +090071 expected = {"agent": {"url": "http://foo.com", "md5hash": "md5",
72 "version": "2", "agent_id": 1}}
73 serialized_body = json.dumps(expected)
Ken'ichi Ohmichi14bf4242015-07-17 02:28:21 +000074 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 Ohmichiad0dfb42015-07-14 08:52:25 +000086 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 Ohmichi14bf4242015-07-17 02:28:21 +000091
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)