From 4f3e418785aaa1b55ef2a401c2e517edeffbd833 Mon Sep 17 00:00:00 2001 From: Sumanth Nagadavalli Date: Tue, 21 May 2013 16:26:51 +0530 Subject: [PATCH] Integration tests for hosts client 1.Added a mock library called httpretty. 2.Added tests to list and get hosts. Change-Id: Id8936daa15cd646c800a530e2773914a42eeb6ca --- cloudcafe/compute/{unit => tests}/__init__.py | 0 .../models => tests/integration}/__init__.py | 0 .../tests/integration/hosts/__init__.py | 15 +++++ .../tests/integration/hosts/responses.py | 43 +++++++++++++ .../integration/hosts/test_hosts_client.py | 61 +++++++++++++++++++ cloudcafe/compute/tests/unit/__init__.py | 15 +++++ .../compute/tests/unit/hosts/__init__.py | 15 +++++ .../models => tests/unit/hosts}/test_hosts.py | 16 +++++ pip-requires | 1 + 9 files changed, 166 insertions(+) rename cloudcafe/compute/{unit => tests}/__init__.py (100%) rename cloudcafe/compute/{unit/models => tests/integration}/__init__.py (100%) create mode 100644 cloudcafe/compute/tests/integration/hosts/__init__.py create mode 100644 cloudcafe/compute/tests/integration/hosts/responses.py create mode 100644 cloudcafe/compute/tests/integration/hosts/test_hosts_client.py create mode 100644 cloudcafe/compute/tests/unit/__init__.py create mode 100644 cloudcafe/compute/tests/unit/hosts/__init__.py rename cloudcafe/compute/{unit/models => tests/unit/hosts}/test_hosts.py (86%) diff --git a/cloudcafe/compute/unit/__init__.py b/cloudcafe/compute/tests/__init__.py similarity index 100% rename from cloudcafe/compute/unit/__init__.py rename to cloudcafe/compute/tests/__init__.py diff --git a/cloudcafe/compute/unit/models/__init__.py b/cloudcafe/compute/tests/integration/__init__.py similarity index 100% rename from cloudcafe/compute/unit/models/__init__.py rename to cloudcafe/compute/tests/integration/__init__.py diff --git a/cloudcafe/compute/tests/integration/hosts/__init__.py b/cloudcafe/compute/tests/integration/hosts/__init__.py new file mode 100644 index 00000000..59ab77fa --- /dev/null +++ b/cloudcafe/compute/tests/integration/hosts/__init__.py @@ -0,0 +1,15 @@ +""" +Copyright 2013 Rackspace + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" diff --git a/cloudcafe/compute/tests/integration/hosts/responses.py b/cloudcafe/compute/tests/integration/hosts/responses.py new file mode 100644 index 00000000..8333789a --- /dev/null +++ b/cloudcafe/compute/tests/integration/hosts/responses.py @@ -0,0 +1,43 @@ +""" +Copyright 2013 Rackspace + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + + +class HostsClientMockResponse(object): + + @classmethod + def list_hosts(cls): + return '{"hosts":[{' \ + '"host_name": "787f4f6dda1b409bb8b2f9082349690e",' \ + '"service": "compute",' \ + '"zone": "nova"},' \ + '"{host_name": "a98b433151084aee8b1a986e28823b36",' \ + '"service": "cert",' \ + '"zone": "internal"}]}' + + @classmethod + def get_host(cls): + return '{"host":' \ + ' [{"resource":' \ + ' {"cpu": 1,' \ + '"disk_gb": 1028,' \ + '"host": "787f4f6dda1b409bb8b2f9082349690e",' \ + '"memory_mb": 8192,"project": "(total)"}},' \ + '{"resource":' \ + ' {"cpu": 0,' \ + '"disk_gb": 0,' \ + '"host": "787f4f6dda1b409bb8b2f9082349690e",' \ + '"memory_mb": 512,' \ + '"project": "(used_now)"}}]}' diff --git a/cloudcafe/compute/tests/integration/hosts/test_hosts_client.py b/cloudcafe/compute/tests/integration/hosts/test_hosts_client.py new file mode 100644 index 00000000..b1981be1 --- /dev/null +++ b/cloudcafe/compute/tests/integration/hosts/test_hosts_client.py @@ -0,0 +1,61 @@ +""" +Copyright 2013 Rackspace + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +import unittest2 as unittest +import httpretty + +from cloudcafe.compute.hosts_api.client import HostsClient +from cloudcafe.compute.tests.integration.hosts.responses\ + import HostsClientMockResponse + +AUTH_TOKEN = "dda0e9d0a1084f67bb9ea4e91abcd4ec" +HOSTS_API_ENDPOINT = "http://localhost:5000/v1" +HOST_NAME = "787f4f6dda1b409bb8b2f9082349690e" + + +class HostsClientTest(unittest.TestCase): + + @classmethod + def setUp(cls): + cls.hosts_client = HostsClient( + url=HOSTS_API_ENDPOINT, + auth_token=AUTH_TOKEN, + serialize_format="json", + deserialize_format="json" + ) + cls.hosts_uri = "{0}/os-hosts".format(HOSTS_API_ENDPOINT) + cls.host_uri = "{0}/{1}".format(cls.hosts_uri, HOST_NAME) + + @httpretty.activate + def test_list_hosts(self): + httpretty.register_uri(httpretty.GET, self.hosts_uri, + body=HostsClientMockResponse.list_hosts()) + response = self.hosts_client.list_hosts() + self.assertEqual(200, response.status_code) + self.assertEqual(HostsClientMockResponse.list_hosts(), + response.content) + + @httpretty.activate + def test_get_host(self): + httpretty.register_uri(httpretty.GET, self.host_uri, + body=HostsClientMockResponse.get_host()) + response = self.hosts_client.get_host(HOST_NAME) + self.assertEqual(200, response.status_code) + self.assertEqual(HostsClientMockResponse.get_host(), response.content) + + +if __name__ == '__main__': + unittest.main() diff --git a/cloudcafe/compute/tests/unit/__init__.py b/cloudcafe/compute/tests/unit/__init__.py new file mode 100644 index 00000000..59ab77fa --- /dev/null +++ b/cloudcafe/compute/tests/unit/__init__.py @@ -0,0 +1,15 @@ +""" +Copyright 2013 Rackspace + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" diff --git a/cloudcafe/compute/tests/unit/hosts/__init__.py b/cloudcafe/compute/tests/unit/hosts/__init__.py new file mode 100644 index 00000000..59ab77fa --- /dev/null +++ b/cloudcafe/compute/tests/unit/hosts/__init__.py @@ -0,0 +1,15 @@ +""" +Copyright 2013 Rackspace + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" diff --git a/cloudcafe/compute/unit/models/test_hosts.py b/cloudcafe/compute/tests/unit/hosts/test_hosts.py similarity index 86% rename from cloudcafe/compute/unit/models/test_hosts.py rename to cloudcafe/compute/tests/unit/hosts/test_hosts.py index c94ecd38..1b361c69 100644 --- a/cloudcafe/compute/unit/models/test_hosts.py +++ b/cloudcafe/compute/tests/unit/hosts/test_hosts.py @@ -1,3 +1,19 @@ +""" +Copyright 2013 Rackspace + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + import unittest2 as unittest from cloudcafe.compute.hosts_api.models.hosts import Host diff --git a/pip-requires b/pip-requires index e69de29b..b0284b10 100644 --- a/pip-requires +++ b/pip-requires @@ -0,0 +1 @@ +httpretty