diff --git a/senlinclient/tests/functional/base.py b/senlinclient/tests/functional/base.py index b2e23029..ed6dfe29 100644 --- a/senlinclient/tests/functional/base.py +++ b/senlinclient/tests/functional/base.py @@ -11,7 +11,9 @@ # under the License. import os +import six +from oslo_utils import uuidutils from tempest.lib.cli import base from tempest.lib.cli import output_parser @@ -37,3 +39,50 @@ class OpenStackClientTestBase(base.ClientTestBase): def openstack(self, *args, **kwargs): return self.clients.openstack(*args, **kwargs) + + def show_to_dict(self, output): + obj = {} + items = self.parser.listing(output) + for item in items: + obj[item['Field']] = six.text_type(item['Value']) + return dict((self._key_name(k), v) for k, v in obj.items()) + + def _key_name(self, key): + return key.lower().replace(' ', '_') + + def name_generate(self): + """Generate randomized name for some entity.""" + name = uuidutils.generate_uuid()[:8] + return name + + def _get_profile_path(self, profile_name): + return os.path.join(os.path.dirname(os.path.realpath(__file__)), + 'profiles/%s' % profile_name) + + def _get_policy_path(self, policy_name): + return os.path.join(os.path.dirname(os.path.realpath(__file__)), + 'policies/%s' % policy_name) + + def policy_create(self, name, profile): + pf = self._get_policy_path(profile) + cmd = ('cluster policy create --spec-file %s %s' + % (pf, name)) + policy_raw = self.openstack(cmd) + result = self.show_to_dict(policy_raw) + return result + + def policy_delete(self, name_or_id): + cmd = ('cluster policy delete %s --force' % name_or_id) + self.openstack(cmd) + + def profile_create(self, name, policy): + pf = self._get_profile_path(policy) + cmd = ('cluster profile create --spec-file %s %s' + % (pf, name)) + profile_raw = self.openstack(cmd) + result = self.show_to_dict(profile_raw) + return result + + def profile_delete(self, name_or_id): + cmd = ('cluster profile delete %s --force' % name_or_id) + self.openstack(cmd) diff --git a/senlinclient/tests/functional/policies/deletion_policy.yaml b/senlinclient/tests/functional/policies/deletion_policy.yaml new file mode 100644 index 00000000..464162e3 --- /dev/null +++ b/senlinclient/tests/functional/policies/deletion_policy.yaml @@ -0,0 +1,19 @@ +# Sample deletion policy that can be attached to a cluster. +type: senlin.policy.deletion +version: 1.0 +description: A policy for choosing victim node(s) from a cluster for deletion. +properties: + # The valid values include: + # OLDEST_FIRST, OLDEST_PROFILE_FIRST, YOUNGEST_FIRST, RANDOM + criteria: OLDEST_FIRST + + # Whether deleted node should be destroyed + destroy_after_deletion: True + + # Length in number of seconds before the actual deletion happens + # This param buys an instance some time before deletion + grace_period: 60 + + # Whether the deletion will reduce the desired capacity of + # the cluster as well + reduce_desired_capacity: False diff --git a/senlinclient/tests/functional/profiles/cirros_basic.yaml b/senlinclient/tests/functional/profiles/cirros_basic.yaml new file mode 100644 index 00000000..ad122a7a --- /dev/null +++ b/senlinclient/tests/functional/profiles/cirros_basic.yaml @@ -0,0 +1,13 @@ +type: os.nova.server +version: 1.0 +properties: + name: cirros_server + flavor: 1 + image: "cirros-0.3.5-x86_64-disk.img" + networks: + - network: private + metadata: + test_key: test_value + user_data: | + #!/bin/sh + echo 'hello, world' > /tmp/test_file diff --git a/senlinclient/tests/functional/test_policies.py b/senlinclient/tests/functional/test_policies.py index 0656afa8..1b01636f 100644 --- a/senlinclient/tests/functional/test_policies.py +++ b/senlinclient/tests/functional/test_policies.py @@ -21,3 +21,20 @@ class PolicyTest(base.OpenStackClientTestBase): policy_list = self.parser.listing(result) self.assertTableStruct(policy_list, ['id', 'name', 'type', 'created_at']) + + def test_policy_create(self): + name = self.name_generate() + result = self.policy_create(name, 'deletion_policy.yaml') + self.assertEqual(result['name'], name) + self.addCleanup(self.policy_delete, result['id']) + + def test_policy_update(self): + old_name = self.name_generate() + pc1 = self.policy_create(old_name, 'deletion_policy.yaml') + new_name = self.name_generate() + cmd = ('cluster policy update --name %s %s' % (new_name, pc1['id'])) + result = self.openstack(cmd) + pc2 = self.show_to_dict(result) + self.assertEqual(pc2['name'], new_name) + self.assertNotEqual(pc1['name'], pc2['name']) + self.addCleanup(self.policy_delete, pc2['id']) diff --git a/senlinclient/tests/functional/test_profiles.py b/senlinclient/tests/functional/test_profiles.py index 3054f64f..9d0ef9a0 100644 --- a/senlinclient/tests/functional/test_profiles.py +++ b/senlinclient/tests/functional/test_profiles.py @@ -21,3 +21,20 @@ class ProfileTest(base.OpenStackClientTestBase): profile_list = self.parser.listing(result) self.assertTableStruct(profile_list, ['id', 'name', 'type', 'created_at']) + + def test_pofile_create(self): + name = self.name_generate() + result = self.profile_create(name, 'cirros_basic.yaml') + self.assertEqual(result['name'], name) + self.addCleanup(self.profile_delete, result['id']) + + def test_profile_update(self): + old_name = self.name_generate() + pf1 = self.profile_create(old_name, 'cirros_basic.yaml') + new_name = self.name_generate() + cmd = ('cluster profile update --name %s %s' % (new_name, pf1['id'])) + result = self.openstack(cmd) + pf2 = self.show_to_dict(result) + self.assertEqual(pf2['name'], new_name) + self.assertNotEqual(pf1['name'], pf2['name']) + self.addCleanup(self.profile_delete, pf2['id'])