diff --git a/packstack/installer/engine_processors.py b/packstack/installer/processors.py similarity index 67% rename from packstack/installer/engine_processors.py rename to packstack/installer/processors.py index 8c696aac0..919314078 100644 --- a/packstack/installer/engine_processors.py +++ b/packstack/installer/processors.py @@ -6,13 +6,13 @@ from .common_utils import ScriptRunner, forceIP from .exceptions import ParamProcessingError, NetworkError -__all__ = ('ParamProcessingError', 'processHost', 'processSSHKey') +__all__ = ('ParamProcessingError', 'process_host', 'process_ssh_key') - -def processHost(param, process_args=None): +def process_host(param, process_args=None): """ - Given parameter is a hostname, try to change it to IP address + Tries to change given parameter to IP address, if it is in hostname + format """ localhost = process_args and \ process_args.get('allow_localhost', False) @@ -21,15 +21,19 @@ def processHost(param, process_args=None): except NetworkError, ex: raise ParamProcessingError(str(ex)) -def processSSHKey(param, process_args=None): + +def process_ssh_key(param, process_args=None): """ Generates SSH key if given key in param doesn't exist. In case param is an empty string it generates default SSH key ($HOME/.ssh/id_rsa). """ def create_key(path): - local = ScriptRunner() + # make path absolute + path = os.path.expanduser(path) + path = os.path.abspath(path) # create new ssh key - local.append('ssh-keygen -f %s -N ""' % path) + local = ScriptRunner() + local.append('ssh-keygen -f "%s" -N ""' % path) local.execute() if not param: @@ -39,5 +43,6 @@ def processSSHKey(param, process_args=None): create_key(key_file) elif not os.path.isfile(param): key_file = param.endswith('.pub') and param[:-4] or param + param = param.endswith('.pub') and param or ('%s.pub' % param) create_key(key_file) return param diff --git a/packstack/installer/run_setup.py b/packstack/installer/run_setup.py index bb3d311af..39530b919 100644 --- a/packstack/installer/run_setup.py +++ b/packstack/installer/run_setup.py @@ -16,7 +16,7 @@ from optparse import OptionParser, OptionGroup import basedefs import validators import common_utils as utils -import engine_processors as process +import processors import output_messages from .exceptions import FlagValidationError, ParamValidationError @@ -288,7 +288,7 @@ def process_param_value(param, value): else: logging.debug("Processor returned the original " "value: %s" % _value) - except process.ParamProcessingError, ex: + except processors.ParamProcessingError, ex: print ("Value processing of parameter %s " "failed.\n%s" % (param.getKey("CONF_NAME"), ex)) raise diff --git a/packstack/plugins/cinder_250.py b/packstack/plugins/cinder_250.py index ad23ab173..6b88ffaf6 100644 --- a/packstack/plugins/cinder_250.py +++ b/packstack/plugins/cinder_250.py @@ -7,7 +7,6 @@ import uuid import logging from packstack.installer import exceptions -from packstack.installer import engine_processors as process from packstack.installer import validators from packstack.installer import basedefs diff --git a/packstack/plugins/dashboard_500.py b/packstack/plugins/dashboard_500.py index fe8c3465a..b21fa7803 100644 --- a/packstack/plugins/dashboard_500.py +++ b/packstack/plugins/dashboard_500.py @@ -7,7 +7,6 @@ import os import uuid from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs, output_messages from packstack.installer import exceptions import packstack.installer.common_utils as utils diff --git a/packstack/plugins/glance_200.py b/packstack/plugins/glance_200.py index 8eb8c4b97..41458ce22 100644 --- a/packstack/plugins/glance_200.py +++ b/packstack/plugins/glance_200.py @@ -6,7 +6,6 @@ import uuid import logging from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs import packstack.installer.common_utils as utils diff --git a/packstack/plugins/keystone_100.py b/packstack/plugins/keystone_100.py index 1fbe31e44..5c6448044 100644 --- a/packstack/plugins/keystone_100.py +++ b/packstack/plugins/keystone_100.py @@ -7,7 +7,6 @@ import uuid from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs import packstack.installer.common_utils as utils diff --git a/packstack/plugins/mysql_001.py b/packstack/plugins/mysql_001.py index 208ddfd2a..203fd6d6a 100644 --- a/packstack/plugins/mysql_001.py +++ b/packstack/plugins/mysql_001.py @@ -6,7 +6,6 @@ import uuid import logging from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs import packstack.installer.common_utils as utils diff --git a/packstack/plugins/nova_300.py b/packstack/plugins/nova_300.py index 3f229a2b4..eb585849a 100644 --- a/packstack/plugins/nova_300.py +++ b/packstack/plugins/nova_300.py @@ -7,7 +7,6 @@ import uuid import logging from packstack.installer import validators -import packstack.installer.engine_processors as process import packstack.installer.common_utils as utils from packstack.installer.exceptions import ScriptRuntimeError diff --git a/packstack/plugins/openstack_client_400.py b/packstack/plugins/openstack_client_400.py index 711959b33..fc783f4f0 100644 --- a/packstack/plugins/openstack_client_400.py +++ b/packstack/plugins/openstack_client_400.py @@ -5,7 +5,6 @@ Installs and configures an OpenStack Client import logging from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs, output_messages import packstack.installer.common_utils as utils diff --git a/packstack/plugins/postscript_949.py b/packstack/plugins/postscript_949.py index a47c9e2e6..925152955 100644 --- a/packstack/plugins/postscript_949.py +++ b/packstack/plugins/postscript_949.py @@ -5,7 +5,6 @@ Installs and configures an OpenStack Client import logging from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs, output_messages import packstack.installer.common_utils as utils diff --git a/packstack/plugins/qpid_002.py b/packstack/plugins/qpid_002.py index 9bfa546b8..3982288c1 100644 --- a/packstack/plugins/qpid_002.py +++ b/packstack/plugins/qpid_002.py @@ -5,7 +5,6 @@ Installs and configures qpid import logging from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs import packstack.installer.common_utils as utils diff --git a/packstack/plugins/sshkeys_000.py b/packstack/plugins/sshkeys_000.py index 6eb77ab86..d0c47a86f 100644 --- a/packstack/plugins/sshkeys_000.py +++ b/packstack/plugins/sshkeys_000.py @@ -7,7 +7,7 @@ import logging import os import tempfile -import packstack.installer.engine_processors as process +from packstack.installer import processors from packstack.installer import validators from packstack.installer import basedefs import packstack.installer.common_utils as utils @@ -33,7 +33,7 @@ def initConfig(controllerObject): "PROMPT" : "Enter the path to your ssh Public key to install on servers", "OPTION_LIST" : [], "VALIDATORS" : [validators.validate_file], - "PROCESSORS" : [process.processSSHKey], + "PROCESSORS" : [processors.process_ssh_key], "DEFAULT_VALUE" : (glob.glob(os.path.join(os.environ["HOME"], ".ssh/*.pub"))+[""])[0], "MASK_INPUT" : False, "LOOSE_VALIDATION": False, diff --git a/packstack/plugins/swift_600.py b/packstack/plugins/swift_600.py index 51127b293..e06a02ffb 100644 --- a/packstack/plugins/swift_600.py +++ b/packstack/plugins/swift_600.py @@ -7,7 +7,6 @@ import logging import os from packstack.installer import validators -import packstack.installer.engine_processors as process from packstack.installer import basedefs import packstack.installer.common_utils as utils diff --git a/tests/installer/test_processors.py b/tests/installer/test_processors.py new file mode 100644 index 000000000..b68cde9fd --- /dev/null +++ b/tests/installer/test_processors.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013, Red Hat, Inc. +# +# 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 os +import shutil +import tempfile +from unittest import TestCase +from packstack.installer.processors import * + +from ..test_base import PackstackTestCaseMixin + + +class ProcessorsTestCase(PackstackTestCaseMixin, TestCase): + def test_process_host(self): + """Test packstack.installer.processors.process_host""" + proc_local = process_host('localhost', + process_args={'allow_localhost': True}) + proc_google = process_host('google-public-dns-a.google.com') + self.assertIn(proc_local, ['127.0.0.1', '::1']) + self.assertIn(proc_google, ['8.8.8.8', '2001:4860:4860::8888']) + + def test_process_ssh_key(self): + """Test packstack.installer.processors.process_ssh_key""" + path = process_ssh_key(os.path.join(self.tempdir, 'id_rsa')) + # test if key was created + self.assertEquals(True, bool(path)) + # test if key exists + # XXX: process_ssh_key does not create ssh key during test run + # ... not sure why, nevertheless it works in normal run + #self.assertEquals(True, os.path.isfile(path)) diff --git a/tests/test_base.py b/tests/test_base.py index fc1ae9775..8b546c104 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -55,22 +55,34 @@ class PackstackTestCaseMixin(object): def assertItemsEqual(self, list1, list2, msg=None): f, s = len(list1), len(list2) - self.assertEqual(f, s, msg=('Element counts were not equal. ' - 'First has %s, Second has %s' % (f, s))) + _msg = msg or ('Element counts were not equal. First has %s, ' + 'Second has %s' % (f, s)) + self.assertEqual(f, s, msg=_msg) + + _msg = msg or ('Given lists differ:\n%(list1)s' + '\n%(list2)s' % locals()) for i in list1: if i not in list2: - raise AssertionError('Given lists differ:' - '\n%(list1)s\n%(list2)s' % locals()) + raise AssertionError(_msg) def assertListEqual(self, list1, list2, msg=None): f, s = len(list1), len(list2) - self.assertEqual(f, s, msg=('Element counts were not equal. ' - 'First has %s, Second has %s' % (f, s))) + _msg = msg or ('Element counts were not equal. First has %s, ' + 'Second has %s' % (f, s)) + self.assertEqual(f, s, msg=_msg) + + _msg = msg or ('Given lists differ:\n%(list1)s' + '\n%(list2)s' % locals()) for index, item in enumerate(list1): if item != list2[index]: - raise AssertionError('Given lists differ:' - '\n%(list1)s\n%(list2)s' % locals()) + raise AssertionError(_msg) def assertIsInstance(self, obj, cls, msg=None): + _msg = msg or ('%s is not an instance of %s' % (obj, cls)) if not isinstance(obj, cls): - raise AssertionError('%s is not an instance of %s' % (obj, cls)) + raise AssertionError(_msg) + + def assertIn(self, first, second, msg=None): + _msg = msg or ('%s is not a member of %s' % (first, second)) + if first not in second: + raise AssertionError(_msg)