Make main() return rather than call sys.exit
Fixes tests not all being run and also makes main() more directly testable. Also adding tests for log messages when main() is called. Finally adding argparse to requirements.txt to fix python 2.6 Change-Id: I7f152c9aa1e99d0fd3d763871c96cf60840e3ff7
This commit is contained in:
parent
e7e4b9f0ec
commit
a8f8df92d8
|
@ -48,7 +48,8 @@ def print_key(config_path, key, type_name):
|
||||||
try:
|
try:
|
||||||
config = config[key]
|
config = config[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise KeyError('key %s does not exist in %s' % (key, config_path))
|
raise ConfigException(
|
||||||
|
'key %s does not exist in %s' % (key, config_path))
|
||||||
ensure_type(config, type_name)
|
ensure_type(config, type_name)
|
||||||
print config
|
print config
|
||||||
|
|
||||||
|
@ -197,10 +198,11 @@ def main(argv=sys.argv):
|
||||||
logger.info("success")
|
logger.info("success")
|
||||||
except ConfigException as e:
|
except ConfigException as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
sys.exit(1)
|
return 1
|
||||||
sys.exit(0)
|
return 0
|
||||||
|
|
||||||
# logginig
|
|
||||||
|
# logging
|
||||||
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
|
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
|
||||||
DATE_FORMAT = '%Y/%m/%d %I:%M:%S %p'
|
DATE_FORMAT = '%Y/%m/%d %I:%M:%S %p'
|
||||||
|
|
||||||
|
@ -210,6 +212,6 @@ def add_handler(logger, handler):
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
logger = logging.getLogger('os-config-applier')
|
logger = logging.getLogger('os-config-applier')
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
add_handler(logger, logging.StreamHandler(sys.stdout))
|
add_handler(logger, logging.StreamHandler())
|
||||||
if os.geteuid() == 0:
|
if os.geteuid() == 0:
|
||||||
add_handler(logger, logging.FileHandler('/var/log/os-config-applier.log'))
|
add_handler(logger, logging.FileHandler('/var/log/os-config-applier.log'))
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
|
@ -76,34 +75,39 @@ class TestRunOSConfigApplier(testtools.TestCase):
|
||||||
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.stdout))
|
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.stdout))
|
||||||
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
|
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
|
||||||
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
|
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
|
||||||
|
self.logger = self.useFixture(
|
||||||
|
fixtures.FakeLogger(name="os-config-applier"))
|
||||||
fd, self.path = tempfile.mkstemp()
|
fd, self.path = tempfile.mkstemp()
|
||||||
with os.fdopen(fd, 'w') as t:
|
with os.fdopen(fd, 'w') as t:
|
||||||
t.write(json.dumps(CONFIG))
|
t.write(json.dumps(CONFIG))
|
||||||
t.flush()
|
t.flush()
|
||||||
|
|
||||||
def test_print_key(self):
|
def test_print_key(self):
|
||||||
oca.main(['os-config-applier.py', '--metadata', self.path, '--key',
|
self.assertEqual(0, oca.main(
|
||||||
'database.url', '--type', 'raw'])
|
['os-config-applier.py', '--metadata', self.path, '--key',
|
||||||
|
'database.url', '--type', 'raw']))
|
||||||
self.stdout.seek(0)
|
self.stdout.seek(0)
|
||||||
self.assertEqual(CONFIG['database']['url'],
|
self.assertEqual(CONFIG['database']['url'],
|
||||||
self.stdout.read().strip())
|
self.stdout.read().strip())
|
||||||
|
self.assertEqual('', self.logger.output)
|
||||||
|
|
||||||
def test_print_key_missing(self):
|
def test_print_key_missing(self):
|
||||||
self.assertRaises(
|
self.assertEqual(1, oca.main(
|
||||||
subprocess.CalledProcessError, oca.main,
|
|
||||||
['os-config-applier.py', '--metadata', self.path, '--key',
|
['os-config-applier.py', '--metadata', self.path, '--key',
|
||||||
'does.not.exist'])
|
'does.not.exist']))
|
||||||
|
self.assertIn('does not exist', self.logger.output)
|
||||||
|
|
||||||
def test_print_key_wrong_type(self):
|
def test_print_key_wrong_type(self):
|
||||||
self.assertRaises(
|
self.assertEqual(1, oca.main(
|
||||||
subprocess.CalledProcessError, oca.main,
|
|
||||||
['os-config-applier.py', '--metadata', self.path, '--key',
|
['os-config-applier.py', '--metadata', self.path, '--key',
|
||||||
'x', '--type', 'int'])
|
'x', '--type', 'int']))
|
||||||
|
self.assertIn('cannot interpret value', self.logger.output)
|
||||||
|
|
||||||
def test_print_templates(self):
|
def test_print_templates(self):
|
||||||
oca.main(['os-config-applier', '--print-templates'])
|
oca.main(['os-config-applier', '--print-templates'])
|
||||||
self.stdout.seek(0)
|
self.stdout.seek(0)
|
||||||
self.assertEqual(self.stdout.read().strip(), oca.TEMPLATES_DIR)
|
self.assertEqual(self.stdout.read().strip(), oca.TEMPLATES_DIR)
|
||||||
|
self.assertEqual('', self.logger.output)
|
||||||
|
|
||||||
|
|
||||||
class OSConfigApplierTestCase(testtools.TestCase):
|
class OSConfigApplierTestCase(testtools.TestCase):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
anyjson
|
anyjson
|
||||||
|
argparse
|
||||||
d2to1
|
d2to1
|
||||||
pbr
|
pbr
|
||||||
pystache
|
pystache
|
||||||
|
|
Loading…
Reference in New Issue