From 7bb2e88c24378136798da8faadb236b4f720d6c6 Mon Sep 17 00:00:00 2001 From: Rohan Kanade Date: Fri, 24 Jan 2014 19:10:55 +0530 Subject: [PATCH] Add functional tests for server, pool, volumes --- seamicroclient/base.py | 5 ++ .../tests/functional/v2/test_pools.py | 44 ++++++++++++++++ .../tests/functional/v2/test_servers.py | 51 +++++++++++++++++++ .../tests/functional/v2/test_volumes.py | 50 ++++++++++++++++++ seamicroclient/v2/pools.py | 7 +-- seamicroclient/v2/volumes.py | 6 ++- 6 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 seamicroclient/tests/functional/v2/test_pools.py create mode 100644 seamicroclient/tests/functional/v2/test_volumes.py diff --git a/seamicroclient/base.py b/seamicroclient/base.py index f490e16..fd3bece 100644 --- a/seamicroclient/base.py +++ b/seamicroclient/base.py @@ -71,6 +71,8 @@ class Manager(utils.HookableMixin): def _create(self, url, body, return_raw=False, **kwargs): self.run_hooks('modify_body_for_create', body, **kwargs) _resp, body = self.api.client.post(url, body=body) + if isinstance(body, basestring): + return self.get(body.partition('/')[-1]) if return_raw: return body for k, v in body.iteritems(): @@ -84,6 +86,9 @@ class Manager(utils.HookableMixin): self.run_hooks('modify_body_for_update', body, **kwargs) _resp, body = self.api.client.put(url, body=body) if body: + if isinstance(body, basestring): + return self.get(body.partition('/')[-1]) + if body == kwargs.get('action'): return for k, v in body.iteritems(): diff --git a/seamicroclient/tests/functional/v2/test_pools.py b/seamicroclient/tests/functional/v2/test_pools.py new file mode 100644 index 0000000..a425333 --- /dev/null +++ b/seamicroclient/tests/functional/v2/test_pools.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# +# 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 uuid + +from seamicroclient.tests import utils +from seamicroclient.v2 import Client + + +cs = Client("admin", "seamicro", "http://chassis/v2.0") + + +class PoolsTest(utils.TestCase): + + def test_list_pool(self): + pool_list = cs.pools.list() + self.assertTrue(len(pool_list) > 0) + + def test_list_pool_with_filter(self): + filters = {'id': 'p6-'} + for pool in cs.pools.list(filters): + for k, v in filters.iteritems(): + self.assertIn(v, getattr(pool, k)) + + def test_list_pool_with_filter_no_match(self): + filters = {'id': str(uuid.uuid4())} + for pool in cs.pools.list(filters): + for k, v in filters.iteritems(): + self.assertNotIn(getattr(pool, k), v) + + def test_get_pool(self): + pool_id = cs.pools.list()[0].id + pool = cs.pools.get(pool_id) + self.assertEqual(pool.id, pool_id) diff --git a/seamicroclient/tests/functional/v2/test_servers.py b/seamicroclient/tests/functional/v2/test_servers.py index f4b274d..ef4ee4b 100644 --- a/seamicroclient/tests/functional/v2/test_servers.py +++ b/seamicroclient/tests/functional/v2/test_servers.py @@ -11,15 +11,66 @@ # 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 time from seamicroclient.tests import utils from seamicroclient.v2 import Client +STATUS_WAIT_TIMEOUT = 30 +BUILD_INTERVAL = 10 + +SERVER_ID = '0/0' +VLAN_ID = '7' cs = Client("admin", "seamicro", "http://chassis/v2.0") + +class FunctionalException(Exception): + pass + + class ServersTest(utils.TestCase): + @staticmethod + def wait_for_server_status(s, active): + start_time = int(time.time()) + while True: + timed_out = int(time.time()) - start_time > STATUS_WAIT_TIMEOUT + if timed_out: + raise FunctionalException() + time.sleep(BUILD_INTERVAL) + s = cs.servers.get(s.id) + if s.active == active: + return + def test_list_servers(self): sl = cs.servers.list() self.assertTrue(len(sl) > 0) + + def test_get_server(self): + s = cs.servers.get(SERVER_ID) + self.assertEqual(s.id, SERVER_ID) + + def test_power_on_power_off(self): + s = cs.servers.get(SERVER_ID) + self.assertEqual(s.id, SERVER_ID) + if s.active: + s.power_off() + self.wait_for_server_status(s, active=False) + s = cs.servers.get(s.id) + self.assertEqual(s.active, False) + s.power_on() + else: + s.power_on() + self.wait_for_server_status(s, active=True) + s = cs.servers.get(s.id) + self.assertEqual(s.active, True) + s.power_off() + + def test_reset(self): + s = cs.servers.get(SERVER_ID) + self.assertEqual(s.id, SERVER_ID) + s.reset() + self.wait_for_server_status(s, active=True) + s = cs.servers.get(SERVER_ID) + self.assertEqual(s.active, True) diff --git a/seamicroclient/tests/functional/v2/test_volumes.py b/seamicroclient/tests/functional/v2/test_volumes.py new file mode 100644 index 0000000..5a0445e --- /dev/null +++ b/seamicroclient/tests/functional/v2/test_volumes.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# +# 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. + +from seamicroclient import exceptions +from seamicroclient.tests import utils +from seamicroclient.v2 import Client + + +cs = Client("admin", "seamicro", "http://chassis/v2.0") + + +class VolumesTest(utils.TestCase): + + @staticmethod + def create_volume(volume_size=2, pool=None): + return cs.volumes.create(volume_size, pool) + + def test_list_volume(self): + volume_list = cs.volumes.list() + self.assertTrue(len(volume_list) > 0) + + def test_get_volume(self): + volume_id = cs.volumes.list()[0].id + volume = cs.volumes.get(volume_id) + self.assertEqual(volume.id, volume_id) + + def test_create_volume(self): + pool = cs.pools.list()[0] + volume_size = 2 + volume = self.create_volume(volume_size, pool) + self.assertIn(pool, volume.id) + self.assertEqual(volume.size, volume_size) + volume.delete() + + def test_delete_volume(self): + volume = self.create_volume() + volume.delete() + self.assertRaises(exceptions.NotFound, cs.volumes.get, + volume.id) diff --git a/seamicroclient/v2/pools.py b/seamicroclient/v2/pools.py index fb7a89b..1da40f3 100644 --- a/seamicroclient/v2/pools.py +++ b/seamicroclient/v2/pools.py @@ -50,20 +50,21 @@ class PoolManager(base.ManagerWithFind): for k, v in filters.iteritems(): for pool in pools: if isinstance(v, basestring): - if v in pool[k]: + if v in getattr(pool, k): output.add(pool) else: if pool in output: output.remove(pool) elif isinstance(v, int): - if v == pool[k]: + if v == getattr(pool, k): output.add(pool) else: if pool in output: output.remove(pool) else: continue - return output + return output + return pools def _action(self, action, pool, info=None, **kwargs): """ diff --git a/seamicroclient/v2/volumes.py b/seamicroclient/v2/volumes.py index 8e64ff1..3e7b9d4 100644 --- a/seamicroclient/v2/volumes.py +++ b/seamicroclient/v2/volumes.py @@ -14,7 +14,9 @@ Volume interface. """ -from seamicroclient.openstack.common import uuidutils +import binascii +import os + from seamicroclient import base @@ -60,7 +62,7 @@ class VolumeManager(base.ManagerWithFind): """ create_params = {} if volume_id is None: - volume_id = uuidutils.generate_uuid() + volume_id = str(binascii.b2a_hex(os.urandom(6))) if pool and volume_id and size: create_params = {'volume-size': str(size)} resource_url = "%s/%s" % (base.getid(pool), volume_id)