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:
Clint Byrum 2013-04-09 10:20:46 -07:00
parent e7e4b9f0ec
commit a8f8df92d8
3 changed files with 21 additions and 14 deletions

View File

@ -48,7 +48,8 @@ def print_key(config_path, key, type_name):
try:
config = config[key]
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)
print config
@ -197,10 +198,11 @@ def main(argv=sys.argv):
logger.info("success")
except ConfigException as e:
logger.error(e)
sys.exit(1)
sys.exit(0)
return 1
return 0
# logginig
# logging
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
DATE_FORMAT = '%Y/%m/%d %I:%M:%S %p'
@ -210,6 +212,6 @@ def add_handler(logger, handler):
logger.addHandler(handler)
logger = logging.getLogger('os-config-applier')
logger.setLevel(logging.INFO)
add_handler(logger, logging.StreamHandler(sys.stdout))
add_handler(logger, logging.StreamHandler())
if os.geteuid() == 0:
add_handler(logger, logging.FileHandler('/var/log/os-config-applier.log'))

View File

@ -15,7 +15,6 @@
import json
import os
import subprocess
import tempfile
import fixtures
@ -76,34 +75,39 @@ class TestRunOSConfigApplier(testtools.TestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.stdout))
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
self.logger = self.useFixture(
fixtures.FakeLogger(name="os-config-applier"))
fd, self.path = tempfile.mkstemp()
with os.fdopen(fd, 'w') as t:
t.write(json.dumps(CONFIG))
t.flush()
def test_print_key(self):
oca.main(['os-config-applier.py', '--metadata', self.path, '--key',
'database.url', '--type', 'raw'])
self.assertEqual(0, oca.main(
['os-config-applier.py', '--metadata', self.path, '--key',
'database.url', '--type', 'raw']))
self.stdout.seek(0)
self.assertEqual(CONFIG['database']['url'],
self.stdout.read().strip())
self.assertEqual('', self.logger.output)
def test_print_key_missing(self):
self.assertRaises(
subprocess.CalledProcessError, oca.main,
self.assertEqual(1, oca.main(
['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):
self.assertRaises(
subprocess.CalledProcessError, oca.main,
self.assertEqual(1, oca.main(
['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):
oca.main(['os-config-applier', '--print-templates'])
self.stdout.seek(0)
self.assertEqual(self.stdout.read().strip(), oca.TEMPLATES_DIR)
self.assertEqual('', self.logger.output)
class OSConfigApplierTestCase(testtools.TestCase):

View File

@ -1,4 +1,5 @@
anyjson
argparse
d2to1
pbr
pystache