Skip to content

Commit

Permalink
Add unit and system tests for log lvl in cmd line
Browse files Browse the repository at this point in the history
Setting the log level in the command line got broken. I didn't really think of
this as a fragile element until it got badly torn up by the CLI parsing
refactor. Adding multiple tests which capture this potential for breakage at
multiple levels will suffice for now.
  • Loading branch information
sirosen committed Nov 14, 2015
1 parent 1f21310 commit 9ecab5f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 50 deletions.
21 changes: 20 additions & 1 deletion tests/system/settings_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import textwrap
import logging
from nose.tools import istest

import salve
from tests import system


Expand Down Expand Up @@ -44,6 +46,23 @@ def explicit_run_log(self):
self.run_on_manifest('1.man')
assert self.exists('2.man')
s = self.read_file('2.man')
assert s == content, '%s' % s
assert s == content
s = self.read_file(os.path.join(self.userhome, 'run_log'))
assert len(s) > 0

@istest
def command_line_log_level(self):
"""
System: Settings, Set Log Level in Command Line
Runs a manifest with settings that set the log level, and verifies that
the log level set in the command line is the real value used.
"""
content = 'file { action copy source 1.man target 2.man }\n'
self.write_file('1.man', content)
self.run_on_args(['./salve.py', 'deploy', '-m', '1.man',
'-l', 'WARNING', '-d', self.scratch_dir])
assert self.exists('2.man')
s = self.read_file('2.man')
assert s == content
assert salve.logger.level == logging.WARNING
108 changes: 59 additions & 49 deletions tests/unit/cli/cli_parser_test.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,85 @@
#!/usr/bin/python

import mock
import io
from nose.tools import istest

import logging
import salve
from salve.cli import parser
from tests.util import ensure_except
from tests.util import ensure_except, MockedGlobals


@istest
def parse_cmd1():
"""
Unit: Command Line Parse Deploy Manifest File Specified
Verifies that attempting to run from the commandline successfully
parses manifest file specification in sys.argv
"""
fake_argv = ['./salve.py', 'deploy', '-m', 'a/b/c']
class TestsWithMockedIO(MockedGlobals):
@istest
@mock.patch('sys.argv', ['./salve.py', 'deploy', '-m', 'a/b/c'])
def parse_cmd1(self):
"""
Unit: Command Line Parse Deploy Manifest File Specified
Verifies that attempting to run from the commandline successfully
parses manifest file specification in sys.argv
"""
p = parser.get_parser()

p = parser.get_parser()
with mock.patch('sys.argv', fake_argv):
args = p.parse_args()
assert args.manifest == 'a/b/c'
assert args.directory is None
assert args.configfile is None

@istest
@mock.patch('sys.argv',
['./salve.py', 'deploy', '-c', 'p/q', '-m', 'root.man'])
def parse_cmd2(self):
"""
Unit: Command Line Parse Deploy Config File Other Order
Verifies that attempting to run from the commandline successfully
parses config file specification in sys.argv after the deploy
subcommand
"""
p = parser.get_parser()

@istest
def parse_cmd2():
"""
Unit: Command Line Parse Deploy Config File Other Order
Verifies that attempting to run from the commandline successfully
parses config file specification in sys.argv after the deploy subcommand
"""
fake_argv = ['./salve.py', 'deploy', '-c', 'p/q', '-m', 'root.man']

p = parser.get_parser()
with mock.patch('sys.argv', fake_argv):
args = p.parse_args()
assert args.configfile == 'p/q'
assert args.directory is None
assert args.manifest == 'root.man'

@istest
@mock.patch('sys.argv',
['./salve.py', '-c', 'a/b', 'deploy', '-c', 'p/q', '-m',
'root.man'])
def parse_cmd3(self):
"""
Unit: Command Line Parse Deploy Config Option Override
Confirms that passsing an option to a subparser overrides the value it
was given in the parent
"""
p = parser.get_parser()

@istest
def parse_cmd3():
"""
Unit: Command Line Parse Deploy Config Option Override
Confirms that passsing an option to a subparser overrides the value it was
given in the parent
"""
fake_argv = ['./salve.py', '-c', 'a/b', 'deploy', '-c', 'p/q',
'-m', 'root.man']

p = parser.get_parser()
with mock.patch('sys.argv', fake_argv):
args = p.parse_args()
assert args.configfile == 'p/q'
assert args.directory is None
assert args.manifest == 'root.man'

@istest
@mock.patch('sys.argv', ['./salve.py', 'deploy', '-c', 'p/q'])
def parse_cmd4(self):
"""
Unit: Command Line Parse Deploy No Manifest
Confirms that omitting the manifest option causes a hard abort.
"""
p = parser.get_parser()

ensure_except(SystemExit, p.parse_args)

@istest
def parse_cmd4():
"""
Unit: Command Line Parse Deploy No Manifest
Confirms that omitting the manifest option causes a hard abort.
"""
fake_argv = ['./salve.py', 'deploy', '-c', 'p/q']
stderr = io.StringIO()
@istest
@mock.patch('sys.argv',
['./salve.py', 'deploy', '-m', 'a/b/c', '-l', 'INFO'])
def parse_cmd5(self):
"""
Unit: Command Line Parse Set Log Level
Checks that the log level can be set by loading args
"""
args = parser.load_args()
assert args.manifest == 'a/b/c'
assert args.directory is None
assert args.configfile is None
assert args.log_level == 'INFO'

p = parser.get_parser()
with mock.patch('sys.argv', fake_argv):
with mock.patch('sys.stderr', stderr):
ensure_except(SystemExit, p.parse_args)
assert salve.logger.level == logging.INFO

0 comments on commit 9ecab5f

Please sign in to comment.