Added processors tests and made processors module pep8 compliant
Change-Id: Ifd7ba903b24c441f9f1a36c7ebf05fdba193ff11
This commit is contained in:
@@ -6,13 +6,13 @@ from .common_utils import ScriptRunner, forceIP
|
|||||||
from .exceptions import ParamProcessingError, NetworkError
|
from .exceptions import ParamProcessingError, NetworkError
|
||||||
|
|
||||||
|
|
||||||
__all__ = ('ParamProcessingError', 'processHost', 'processSSHKey')
|
__all__ = ('ParamProcessingError', 'process_host', 'process_ssh_key')
|
||||||
|
|
||||||
|
|
||||||
|
def process_host(param, process_args=None):
|
||||||
def processHost(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 \
|
localhost = process_args and \
|
||||||
process_args.get('allow_localhost', False)
|
process_args.get('allow_localhost', False)
|
||||||
@@ -21,15 +21,19 @@ def processHost(param, process_args=None):
|
|||||||
except NetworkError, ex:
|
except NetworkError, ex:
|
||||||
raise ParamProcessingError(str(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
|
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).
|
is an empty string it generates default SSH key ($HOME/.ssh/id_rsa).
|
||||||
"""
|
"""
|
||||||
def create_key(path):
|
def create_key(path):
|
||||||
local = ScriptRunner()
|
# make path absolute
|
||||||
|
path = os.path.expanduser(path)
|
||||||
|
path = os.path.abspath(path)
|
||||||
# create new ssh key
|
# create new ssh key
|
||||||
local.append('ssh-keygen -f %s -N ""' % path)
|
local = ScriptRunner()
|
||||||
|
local.append('ssh-keygen -f "%s" -N ""' % path)
|
||||||
local.execute()
|
local.execute()
|
||||||
|
|
||||||
if not param:
|
if not param:
|
||||||
@@ -39,5 +43,6 @@ def processSSHKey(param, process_args=None):
|
|||||||
create_key(key_file)
|
create_key(key_file)
|
||||||
elif not os.path.isfile(param):
|
elif not os.path.isfile(param):
|
||||||
key_file = param.endswith('.pub') and param[:-4] or 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)
|
create_key(key_file)
|
||||||
return param
|
return param
|
||||||
@@ -16,7 +16,7 @@ from optparse import OptionParser, OptionGroup
|
|||||||
import basedefs
|
import basedefs
|
||||||
import validators
|
import validators
|
||||||
import common_utils as utils
|
import common_utils as utils
|
||||||
import engine_processors as process
|
import processors
|
||||||
import output_messages
|
import output_messages
|
||||||
from .exceptions import FlagValidationError, ParamValidationError
|
from .exceptions import FlagValidationError, ParamValidationError
|
||||||
|
|
||||||
@@ -288,7 +288,7 @@ def process_param_value(param, value):
|
|||||||
else:
|
else:
|
||||||
logging.debug("Processor returned the original "
|
logging.debug("Processor returned the original "
|
||||||
"value: %s" % _value)
|
"value: %s" % _value)
|
||||||
except process.ParamProcessingError, ex:
|
except processors.ParamProcessingError, ex:
|
||||||
print ("Value processing of parameter %s "
|
print ("Value processing of parameter %s "
|
||||||
"failed.\n%s" % (param.getKey("CONF_NAME"), ex))
|
"failed.\n%s" % (param.getKey("CONF_NAME"), ex))
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import uuid
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import exceptions
|
from packstack.installer import exceptions
|
||||||
from packstack.installer import engine_processors as process
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
|
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import os
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs, output_messages
|
from packstack.installer import basedefs, output_messages
|
||||||
from packstack.installer import exceptions
|
from packstack.installer import exceptions
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import uuid
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import uuid
|
|||||||
|
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import uuid
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import uuid
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
from packstack.installer.exceptions import ScriptRuntimeError
|
from packstack.installer.exceptions import ScriptRuntimeError
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ Installs and configures an OpenStack Client
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs, output_messages
|
from packstack.installer import basedefs, output_messages
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ Installs and configures an OpenStack Client
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs, output_messages
|
from packstack.installer import basedefs, output_messages
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ Installs and configures qpid
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import packstack.installer.engine_processors as process
|
from packstack.installer import processors
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
import packstack.installer.common_utils as utils
|
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",
|
"PROMPT" : "Enter the path to your ssh Public key to install on servers",
|
||||||
"OPTION_LIST" : [],
|
"OPTION_LIST" : [],
|
||||||
"VALIDATORS" : [validators.validate_file],
|
"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],
|
"DEFAULT_VALUE" : (glob.glob(os.path.join(os.environ["HOME"], ".ssh/*.pub"))+[""])[0],
|
||||||
"MASK_INPUT" : False,
|
"MASK_INPUT" : False,
|
||||||
"LOOSE_VALIDATION": False,
|
"LOOSE_VALIDATION": False,
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import logging
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from packstack.installer import validators
|
from packstack.installer import validators
|
||||||
import packstack.installer.engine_processors as process
|
|
||||||
from packstack.installer import basedefs
|
from packstack.installer import basedefs
|
||||||
import packstack.installer.common_utils as utils
|
import packstack.installer.common_utils as utils
|
||||||
|
|
||||||
|
|||||||
44
tests/installer/test_processors.py
Normal file
44
tests/installer/test_processors.py
Normal file
@@ -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))
|
||||||
@@ -55,22 +55,34 @@ class PackstackTestCaseMixin(object):
|
|||||||
|
|
||||||
def assertItemsEqual(self, list1, list2, msg=None):
|
def assertItemsEqual(self, list1, list2, msg=None):
|
||||||
f, s = len(list1), len(list2)
|
f, s = len(list1), len(list2)
|
||||||
self.assertEqual(f, s, msg=('Element counts were not equal. '
|
_msg = msg or ('Element counts were not equal. First has %s, '
|
||||||
'First has %s, Second has %s' % (f, 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:
|
for i in list1:
|
||||||
if i not in list2:
|
if i not in list2:
|
||||||
raise AssertionError('Given lists differ:'
|
raise AssertionError(_msg)
|
||||||
'\n%(list1)s\n%(list2)s' % locals())
|
|
||||||
|
|
||||||
def assertListEqual(self, list1, list2, msg=None):
|
def assertListEqual(self, list1, list2, msg=None):
|
||||||
f, s = len(list1), len(list2)
|
f, s = len(list1), len(list2)
|
||||||
self.assertEqual(f, s, msg=('Element counts were not equal. '
|
_msg = msg or ('Element counts were not equal. First has %s, '
|
||||||
'First has %s, Second has %s' % (f, 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):
|
for index, item in enumerate(list1):
|
||||||
if item != list2[index]:
|
if item != list2[index]:
|
||||||
raise AssertionError('Given lists differ:'
|
raise AssertionError(_msg)
|
||||||
'\n%(list1)s\n%(list2)s' % locals())
|
|
||||||
|
|
||||||
def assertIsInstance(self, obj, cls, msg=None):
|
def assertIsInstance(self, obj, cls, msg=None):
|
||||||
|
_msg = msg or ('%s is not an instance of %s' % (obj, cls))
|
||||||
if not isinstance(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)
|
||||||
|
|||||||
Reference in New Issue
Block a user