Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a login subcommand (#711)
* Cleaned up interpolation coverage When moving code over from Docker, there was uncovered code. Deleted the code, since I don't see a valid reason we would encounter the raise. * Implemented a login subcommand Ported v1 login to v2. Fixes: #704
- Loading branch information
Showing
10 changed files
with
304 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Copyright (c) 2015-2017 Cisco Systems, Inc. | ||
# | ||
# 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. | ||
|
||
import fcntl | ||
import os | ||
import pexpect | ||
import signal | ||
import struct | ||
import sys | ||
import termios | ||
|
||
import click | ||
|
||
from molecule import util | ||
from molecule.command import base | ||
|
||
|
||
class Login(base.Base): | ||
def execute(self): | ||
""" | ||
Execute the actions necessary to perform a `molecule login` and | ||
returns None. | ||
>>> molecule login --host hotname --scenario-name foo | ||
:return: None | ||
""" | ||
if not self._config.state.created: | ||
msg = 'Instances not created. Please create instances first.' | ||
util.print_error(msg) | ||
util.sysexit() | ||
|
||
hosts = [d['name'] for d in self._config.platforms_with_scenario_name] | ||
hostname = self._get_hostname(hosts) | ||
self._get_login(hostname) | ||
|
||
def _get_hostname(self, hosts): | ||
hostname = self._config.command_args.get('host') | ||
match = [x for x in hosts if x.startswith(hostname)] | ||
if len(match) == 0: | ||
msg = ("There are no hosts that match '{}'. You " | ||
'can only login to valid hosts.').format(hostname) | ||
util.print_error(msg) | ||
util.sysexit() | ||
elif len(match) != 1: | ||
# If there are multiple matches, but one of them is an exact string | ||
# match, assume this is the one they're looking for and use it. | ||
if hostname in match: | ||
match = [hostname, ] | ||
else: | ||
msg = ("There are {} hosts that match '{}'. You " | ||
'can only login to one at a time.\n\n' | ||
'Available hosts:\n{}'.format( | ||
len(match), hostname, '\n'.join(sorted(hosts)))) | ||
util.print_error(msg) | ||
util.sysexit() | ||
|
||
return match[0] | ||
|
||
def _get_login(self, hostname): # pragma: no cover | ||
login_cmd = self._config.driver.login_cmd_template | ||
login_args = self._config.driver.login_args(hostname) | ||
|
||
lines, columns = os.popen('stty size', 'r').read().split() | ||
dimensions = (int(lines), int(columns)) | ||
self._pt = pexpect.spawn( | ||
'/usr/bin/env ' + login_cmd.format(*login_args), | ||
dimensions=dimensions) | ||
signal.signal(signal.SIGWINCH, self._sigwinch_passthrough) | ||
self._pt.interact() | ||
|
||
def _sigwinch_passthrough(self, sig, data): # pragma: no cover | ||
TIOCGWINSZ = 1074295912 # assume | ||
if 'TIOCGWINSZ' in dir(termios): | ||
TIOCGWINSZ = termios.TIOCGWINSZ | ||
s = struct.pack('HHHH', 0, 0, 0, 0) | ||
a = struct.unpack('HHHH', | ||
fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)) | ||
self._pt.setwinsize(a[0], a[1]) | ||
|
||
|
||
@click.command() | ||
@click.pass_context | ||
@click.option('--host', required=True, help='Host to access.') | ||
@click.option( | ||
'--scenario-name', required=True, help='Name of the scenario to target.') | ||
def login(ctx, host, scenario_name): # pragma: no cover | ||
""" Log in to one instance. """ | ||
args = ctx.obj.get('args') | ||
command_args = { | ||
'subcommand': __name__, | ||
'host': host, | ||
'scenario_name': scenario_name | ||
} | ||
|
||
for config in base.get_configs(args, command_args): | ||
l = Login(config) | ||
l.execute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Copyright (c) 2015-2017 Cisco Systems, Inc. | ||
# | ||
# 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. | ||
|
||
import pytest | ||
|
||
from molecule import config | ||
from molecule.command import login | ||
|
||
|
||
@pytest.fixture | ||
def login_instance(molecule_file, platforms_data): | ||
configs = [platforms_data] | ||
c = config.Config( | ||
molecule_file, | ||
args={}, | ||
command_args={'host': 'instance-1'}, | ||
configs=configs) | ||
c.state.change_state('created', True) | ||
|
||
return login.Login(c) | ||
|
||
|
||
def test_execute(mocker, login_instance): | ||
m = mocker.patch('molecule.command.login.Login._get_login') | ||
login_instance.execute() | ||
|
||
m.assert_called_once_with('instance-1-default') | ||
|
||
|
||
def test_execute_raises_when_not_converged(patched_print_error, | ||
login_instance): | ||
login_instance._config.state.change_state('created', False) | ||
with pytest.raises(SystemExit) as e: | ||
login_instance.execute() | ||
|
||
assert 1 == e.value.code | ||
|
||
msg = 'Instances not created. Please create instances first.' | ||
patched_print_error.assert_called_with(msg) | ||
|
||
|
||
def test_get_hostname_does_not_match(molecule_file, patched_print_error): | ||
c = config.Config(molecule_file, command_args={'host': 'invalid'}) | ||
l = login.Login(c) | ||
hosts = ['instance-1'] | ||
|
||
with pytest.raises(SystemExit) as e: | ||
l._get_hostname(hosts) | ||
|
||
assert 1 == e.value.code | ||
|
||
msg = ("There are no hosts that match 'invalid'. You " | ||
'can only login to valid hosts.') | ||
patched_print_error.assert_called_once_with(msg) | ||
|
||
|
||
def test_get_hostname_exact_match_with_one_host(molecule_file): | ||
c = config.Config(molecule_file, command_args={'host': 'instance-1'}) | ||
l = login.Login(c) | ||
hosts = ['instance-1'] | ||
|
||
assert 'instance-1' == l._get_hostname(hosts) | ||
|
||
|
||
def test_get_hostname_partial_match_with_one_host(molecule_file): | ||
c = config.Config(molecule_file, command_args={'host': 'inst'}) | ||
l = login.Login(c) | ||
hosts = ['instance-1'] | ||
|
||
assert 'instance-1' == l._get_hostname(hosts) | ||
|
||
|
||
def test_get_hostname_exact_match_with_multiple_hosts(molecule_file): | ||
c = config.Config(molecule_file, command_args={'host': 'instance-1'}) | ||
l = login.Login(c) | ||
hosts = ['instance-1', 'instance-2'] | ||
|
||
assert 'instance-1' == l._get_hostname(hosts) | ||
|
||
|
||
def test_get_hostname_partial_match_with_multiple_hosts(molecule_file): | ||
c = config.Config(molecule_file, command_args={'host': 'foo'}) | ||
l = login.Login(c) | ||
hosts = ['foo', 'fooo'] | ||
|
||
assert 'foo' == l._get_hostname(hosts) | ||
|
||
|
||
def test_get_hostname_partial_match_with_multiple_hosts_raises( | ||
molecule_file, patched_print_error): | ||
c = config.Config(molecule_file, command_args={'host': 'inst'}) | ||
l = login.Login(c) | ||
hosts = ['instance-1', 'instance-2'] | ||
|
||
with pytest.raises(SystemExit) as e: | ||
l._get_hostname(hosts) | ||
|
||
assert 1 == e.value.code | ||
|
||
msg = ("There are 2 hosts that match 'inst'. " | ||
'You can only login to one at a time.\n\n' | ||
'Available hosts:\n' | ||
'instance-1\n' | ||
'instance-2') | ||
patched_print_error.assert_called_once_with(msg) |
Oops, something went wrong.