From 9ecab5f1fa2185b9eea6ccf21a5fcdbd4e6b0c3a Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Sat, 14 Nov 2015 13:59:57 -0600 Subject: [PATCH] Add unit and system tests for log lvl in cmd line 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. --- tests/system/settings_test.py | 21 +++++- tests/unit/cli/cli_parser_test.py | 108 ++++++++++++++++-------------- 2 files changed, 79 insertions(+), 50 deletions(-) diff --git a/tests/system/settings_test.py b/tests/system/settings_test.py index e74a971..28c7841 100644 --- a/tests/system/settings_test.py +++ b/tests/system/settings_test.py @@ -1,7 +1,9 @@ import os import textwrap +import logging from nose.tools import istest +import salve from tests import system @@ -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 diff --git a/tests/unit/cli/cli_parser_test.py b/tests/unit/cli/cli_parser_test.py index 9fa1316..2af123f 100644 --- a/tests/unit/cli/cli_parser_test.py +++ b/tests/unit/cli/cli_parser_test.py @@ -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