Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
#28: Correct node number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Nov 11, 2020
1 parent ba2a0a8 commit 5facbf4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/rkd_harbor/tasks/deployment/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run(self, context: ExecutionContext) -> bool:
self._preserve_vault_parameters_for_usage_in_inner_tasks(context)

node_group = context.get_arg('--group')
node_num = int(context.get_arg('--num'))
node_num = int(context.get_arg('--num')) - 1
should_print_password = context.get_arg('--print-password')

try:
Expand All @@ -63,8 +63,8 @@ def _get_node(self, node_group: str, node_num: int, config: dict) -> Optional[di

try:
return config['nodes'][node_group][node_num]
except KeyError:
self.io().error_msg('Node group "{}" does not have node of number #{}'.format(node_group, node_num))
except IndexError:
self.io().error_msg('Node group "{}" does not have node of number #{}'.format(node_group, node_num + 1))
return None

def _print_password(self, node: dict):
Expand Down
31 changes: 26 additions & 5 deletions test/test_deployment_sshtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_ssh_task_creates_a_valid_ssh_command_without_private_key(self):
'--ask-ssh-key-path': False,
'--ask-sudo-pass': False,
'--group': 'production',
'--num': '0',
'--num': '1',
'--print-password': False
})

Expand All @@ -64,7 +64,7 @@ def test_ssh_task_includes_private_key_if_present(self):
'--ask-ssh-key-path': False,
'--ask-sudo-pass': False,
'--group': 'production',
'--num': '0',
'--num': '1',
'--print-password': False
})

Expand All @@ -88,7 +88,7 @@ def test_ssh_prints_password_if_sudo_password_is_given(self):
'--ask-ssh-key-path': False,
'--ask-sudo-pass': False,
'--group': 'production',
'--num': '0',
'--num': '1',
'--print-password': True
})

Expand All @@ -111,11 +111,32 @@ def test_ssh_prints_user_password_if_sudo_not_present(self):
'--ask-ssh-key-path': False,
'--ask-sudo-pass': False,
'--group': 'production',
'--num': '0',
'--num': '1',
'--print-password': True
})

self.assertIn('User password is: "docker"', out)

def test_ssh_num_too_high_will_be_checked(self):
"""
Test that if we specify eg. server #2, but we have only one server, then a proper error will be shown
"""

self.prepare_valid_deployment_yml()

sh_calls, out = self._execute_task_and_get_sh_calls_and_output({
'--ask-vault-pass': False,
'--vault-passwords': '',
'--ask-ssh-login': False,
'--ask-ssh-pass': False,
'--ask-ssh-key-path': False,
'--ask-sudo-pass': False,
'--group': 'production',
'--num': '2',
'--print-password': True
})

self.assertIn('Node group "production" does not have node of number #2', out)

def test_ssh_decrypts_yaml_when_encoded(self):
"""
Expand All @@ -137,7 +158,7 @@ def test_ssh_decrypts_yaml_when_encoded(self):
'--ask-ssh-key-path': False,
'--ask-sudo-pass': False,
'--group': 'production',
'--num': '0',
'--num': '1',
'--print-password': True
}, mock_exceptions=mock_exceptions)

Expand Down

0 comments on commit 5facbf4

Please sign in to comment.