def test_license(capsys): itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks('license') out, _ = capsys.readouterr() expected = """ Copyright 2007 Jared Crapo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ assert out == expected
def test_requires_connection(command, capsys): itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks(command) out, err = capsys.readouterr() assert itm.exit_code == itm.exit_codes.error assert not out assert err == 'not connected\n'
def test_show_invalid_setting(command, capsys): itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks('{} bogus'.format(command)) out, err = capsys.readouterr() assert not out assert err == "unknown setting: 'bogus'\n" assert itm.exit_code == itm.exit_codes.error
def test_set_noargs(capsys): itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks('set') out, err = capsys.readouterr() assert not out assert err == 'invalid syntax: try {setting}={value}\n' assert itm.exit_code == itm.exit_codes.usage
def test_help_set(capsys): itm = tm.InteractiveTomcatManager() cmdline = 'help set' itm.onecmd_plus_hooks(cmdline) out, _ = capsys.readouterr() assert 'change the value of one of this program\'s settings' in out assert itm.exit_code == itm.exit_codes.success
def test_history_file_property(): itm = tm.InteractiveTomcatManager() # don't care where it is, just care that there is one assert itm.history_file # if appdirs doesn't exist, config_file shouldn't either itm.appdirs = None assert not itm.history_file
def test_set_with_invalid_param(): itm = tm.InteractiveTomcatManager() # this uuid won't be in itm.settable invalid_setting = str(uuid.uuid1()) with pytest.raises(ValueError): # pylint: disable=protected-access itm._change_setting(invalid_setting, 'someval')
def test_set_string(): itm = tm.InteractiveTomcatManager() prompt = str(uuid.uuid1()) command = 'set prompt={}'.format(prompt) itm.onecmd_plus_hooks(command) assert itm.prompt == prompt assert itm.exit_code == itm.exit_codes.success
def test_list_process_apps_empty(): lines = '' itm = tm.InteractiveTomcatManager() args = itm.parse_args(itm.list_parser, '') apps = parse_apps(lines) apps = itm._list_process_apps(apps, args) assert isinstance(apps, list) assert apps == []
def get_itm(tms): """ Using this as a fixture with capsys breaks capsys. So we use a function. """ itm = tm.InteractiveTomcatManager() args = 'connect {url} {user} {password}'.format(**tms) itm.onecmd_plus_hooks(args) return itm
def test_help_matches_argparser(command, capsys): itm = tm.InteractiveTomcatManager() cmdline = 'help {}'.format(command) itm.onecmd_plus_hooks(cmdline) out, _ = capsys.readouterr() parser_func = getattr(itm, '{}_parser'.format(command)) assert out == parser_func.format_help() assert itm.exit_code == itm.exit_codes.success
def test_default(capsys): cmdline = 'notacommand' itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks(cmdline) out, err = capsys.readouterr() assert itm.exit_code == itm.exit_codes.command_not_found assert not out assert err == 'unknown command: {}\n'.format(cmdline)
def test_status_prefix(tomcat_manager_server, prefix, expected, capsys): itm = tm.InteractiveTomcatManager() args = 'connect {url} {user} {password}'.format(**tomcat_manager_server) itm.status_prefix = prefix itm.onecmd_plus_hooks(args) out, err = capsys.readouterr() assert err.startswith(expected) assert itm.exit_code == itm.exit_codes.success
def test_set_noargs(capsys): itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks('set') out, err = capsys.readouterr() # not going to parse all the lines, but there # should be one per setting assert len(out.splitlines()) == len(itm.settable) assert itm.exit_code == itm.exit_codes.success
def test_exit_code(capsys): itm = tm.InteractiveTomcatManager() itm.onecmd_plus_hooks('version') out, _ = capsys.readouterr() itm.onecmd_plus_hooks('exit_code') out, _ = capsys.readouterr() assert itm.exit_code == itm.exit_codes.success assert out == '{}\n'.format(itm.exit_codes.success)
def test_help(capsys): itm = tm.InteractiveTomcatManager() cmdline = 'help' itm.onecmd_plus_hooks(cmdline) out, _ = capsys.readouterr() assert 'Connecting to a Tomcat server' in out assert 'Managing applications' in out assert 'Server information' in out assert 'Settings, configuration, and tools' in out assert itm.exit_code == itm.exit_codes.success
def test_usage_errors(cmdline, tomcat_manager_server, capsys): itm = tm.InteractiveTomcatManager() itm.quiet = True connect = 'connect {url} {user} {password}'.format(**tomcat_manager_server) itm.onecmd_plus_hooks(connect) itm.onecmd_plus_hooks(cmdline) out, err = capsys.readouterr() assert not out assert err.startswith('usage: ') assert itm.exit_code == itm.exit_codes.usage
def test_connect_with_timeout(tomcat_manager_server, capsys, mocker): connect_mock = mocker.patch('tomcatmanager.TomcatManager.connect') connect_mock.side_effect = requests.exceptions.Timeout() itm = tm.InteractiveTomcatManager() cmdline = 'connect {url} {user} {password}'.format(**tomcat_manager_server) itm.onecmd_plus_hooks(cmdline) out, err = capsys.readouterr() assert not out assert connect_mock.call_count == 1 assert err == 'connection timeout\n' assert itm.exit_code == itm.exit_codes.error
def test_timeout_property(): timeout = 10 itm = tm.InteractiveTomcatManager() # set this to a value that we know will cause it to change when we execute # the command itm.timeout = 5 assert itm.tomcat.timeout == 5 itm.onecmd_plus_hooks('set timeout={}'.format(timeout)) assert itm.exit_code == itm.exit_codes.success assert itm.timeout == timeout assert itm.tomcat.timeout == timeout
def test_connect_password_prompt(tomcat_manager_server, capsys, mocker): itm = tm.InteractiveTomcatManager() mock_getpass = mocker.patch('getpass.getpass') mock_getpass.return_value = tomcat_manager_server['password'] # this should call getpass.getpass, which is now mocked to return the password cmdline = 'connect {url} {user}'.format(**tomcat_manager_server) itm.onecmd_plus_hooks(cmdline) # make sure it got called assert mock_getpass.call_count == 1 assert itm.exit_code == itm.exit_codes.success assert_connected_to(itm, tomcat_manager_server['url'], capsys)
def test_config_file_command(mocker, capsys): fname = '/tmp/someconfig.ini' itm = tm.InteractiveTomcatManager() config_file = mocker.patch('tomcatmanager.InteractiveTomcatManager.config_file', new_callable=mock.PropertyMock) config_file.return_value = fname itm.onecmd_plus_hooks('config file') out, _ = capsys.readouterr() assert out == '{}\n'.format(fname) assert itm.exit_code == itm.exit_codes.success
def main(argv=None): """Entry point for 'tomcat-manager' command line program.""" parser = _build_parser() args = parser.parse_args(argv) if args.debug: print("--argv=" + str(argv), file=sys.stderr) print("--args=" + str(args), file=sys.stderr) itm = tm.InteractiveTomcatManager() # if we have command line switches, set those values # these override any user settings loaded from a config file if args.echo: itm.echo = True if args.quiet: itm.quiet = True if args.status_to_stdout: itm.status_to_stdout = True if args.debug: itm.debug = True if args.timeout: itm.timeout = args.timeout if args.manager_url: # try and connect server_info = {'url': args.manager_url, 'user': '', 'password': ''} server_info['user'] = (args.user or '') if args.user: server_info['password'] = (args.password or '') itm.onecmd_plus_hooks( 'connect {url} {user} {password}'.format_map(server_info)) if args.command: if itm.exit_code == itm.exit_codes.success: # we connected successfully, go run the command itm.onecmd_plus_hooks('{} {}'.format( args.command, ' '.join(args.command_args))) else: # we have no command, but we got a url, regardless of # whether the connect command worked or didn't, let's drop # into interactive mode itm.cmdloop() else: # we don't have a manager url, enter the interactive mode itm.cmdloop() return itm.exit_code
def itm_with_config(mocker, configstring): """Return an InteractiveTomcatManager object with the config set from the passed string.""" itm = tm.InteractiveTomcatManager() fd, fname = tempfile.mkstemp(prefix='', suffix='.ini') os.close(fd) with open(fname, 'w') as fobj: fobj.write(configstring) # itm aleady tried to load a config file, which it may or may not # have found, depending on if you have one or not # we are now going to patch up the config_file to point to # a known file, and the reload the config from that try: config_file = mocker.patch('tomcatmanager.InteractiveTomcatManager.config_file', new_callable=mock.PropertyMock) config_file.return_value = fname itm.load_config() # this just verifies that our patch worked assert itm.config_file == fname finally: os.remove(fname) return itm
def test_load_config(mocker): itm = tm.InteractiveTomcatManager() prompt = str(uuid.uuid1()) fd, fname = tempfile.mkstemp(prefix='', suffix='.ini') os.close(fd) with open(fname, 'w') as fobj: fobj.write('[settings]\nprompt={}\n'.format(prompt)) # itm aleady tried to load a config file, which it may or may not # have found, depending on if you have one or not # we are now going to patch up the config_file to point to # a known file, and the reload the config from that try: config_file = mocker.patch( 'tomcatmanager.InteractiveTomcatManager.config_file', new_callable=mock.PropertyMock) config_file.return_value = fname itm.load_config() # this just verifies that our patch worked assert itm.config_file == fname # now make sure that our file got loaded properly assert itm.prompt == prompt finally: os.remove(fname)
def test_list_parse_args_failure(argv): itm = tm.InteractiveTomcatManager() with pytest.raises(SystemExit): itm.parse_args(itm.list_parser, argv) assert itm.exit_code == itm.exit_codes.usage
def test_convert_to_boolean_valid(param, value): itm = tm.InteractiveTomcatManager() assert itm.convert_to_boolean(param) == value
def test_list_parse_args(raw, state, sort): itm = tm.InteractiveTomcatManager() argv = '{} {} {}'.format(raw, state, sort) itm.parse_args(itm.list_parser, argv) assert itm.exit_code == itm.exit_codes.success
def test_convert_to_boolean_invalid(param): itm = tm.InteractiveTomcatManager() with pytest.raises(ValueError): itm.convert_to_boolean(param)
def itm_nc(mocker): """Don't allow it to load a config file""" mocker.patch('tomcatmanager.InteractiveTomcatManager.load_config') itm = tm.InteractiveTomcatManager() return itm
def test_pythonize(param, value): itm = tm.InteractiveTomcatManager() assert itm._pythonize(param) == value