-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Covered "ssh-command" with tests and fixed all spotted bugs
- Loading branch information
1 parent
7c9994d
commit 5f59478
Showing
10 changed files
with
129 additions
and
6 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
File renamed without changes.
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
File renamed without changes.
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,56 @@ | ||
import unittest | ||
import docker | ||
|
||
from .utils import run_check | ||
from .utils.ssh_test import TestThatRequiresSshServer | ||
|
||
|
||
class SshCommandCheckTest(TestThatRequiresSshServer, unittest.TestCase): | ||
docker_client: docker.DockerClient | ||
|
||
def test_not_passed_host_raises_human_readable_message(self): | ||
stdout: str | ||
result: int | ||
|
||
stdout, result, hooks_output = run_check('ssh-command', {}, {}) | ||
|
||
self.assertIn('HOST is mandatory', stdout) | ||
self.assertFalse(result) | ||
|
||
def test_success_case(self): | ||
""" | ||
Simple success case with expected keywords usage | ||
:return: | ||
""" | ||
|
||
stdout: str | ||
result: int | ||
|
||
stdout, result, hooks_output = run_check('ssh-command', { | ||
'HOST': 'localhost', | ||
'PORT': 3222, | ||
'USER': 'root', | ||
'PASSWORD': 'root', | ||
'SSH_OPTS': '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null', | ||
'COMMAND': 'uname -a', | ||
'EXPECTED_KEYWORDS': 'Linux', | ||
'UNEXPECTED_KEYWORDS': 'Darwin' | ||
}, {}) | ||
|
||
self.assertEqual('OK', stdout.strip()) | ||
self.assertTrue(result) | ||
|
||
def test_invalid_password(self): | ||
stdout: str | ||
result: int | ||
|
||
stdout, result, hooks_output = run_check('ssh-command', { | ||
'HOST': 'localhost', | ||
'PORT': 3222, | ||
'USER': 'root', | ||
'PASSWORD': 'invalid-password', | ||
'SSH_OPTS': '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' | ||
}, {}) | ||
|
||
self.assertIn('Permission denied, please try again.', stdout) | ||
self.assertFalse(result) |
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,17 @@ | ||
|
||
import inspect | ||
import sys | ||
import os | ||
|
||
path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + '/../' | ||
sys.path.append(path) | ||
|
||
try: | ||
from .infracheck.infracheck.runner import Runner | ||
except ImportError as e: | ||
from infracheck.infracheck.runner import Runner | ||
|
||
|
||
def run_check(check_type: str, input_data: dict, hooks: dict): | ||
runner = Runner([path + '/../example/healthchecks', path]) | ||
return runner.run(check_type, input_data, hooks) |
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,46 @@ | ||
|
||
import docker | ||
import docker.errors | ||
import subprocess | ||
import time | ||
|
||
|
||
class TestThatRequiresSshServer: | ||
docker_client: docker.DockerClient | ||
|
||
def setUp(self) -> None: | ||
self.docker_client = docker.from_env() | ||
self._remove_ssh_container() | ||
self.docker_client.containers.run('sickp/alpine-sshd:7.5', name='ssh', ports={'22/tcp': 3222}, detach=True) | ||
self._wait_for_ssh_to_be_ready() | ||
|
||
def tearDown(self) -> None: | ||
self._remove_ssh_container() | ||
|
||
def _remove_ssh_container(self): | ||
try: | ||
container = self.docker_client.containers.get('ssh') | ||
|
||
try: | ||
container.kill() | ||
except docker.errors.APIError: | ||
pass | ||
|
||
container.remove() | ||
|
||
except docker.errors.NotFound: | ||
pass | ||
|
||
@staticmethod | ||
def _wait_for_ssh_to_be_ready(): | ||
for i in range(1, 30): | ||
try: | ||
out = subprocess.check_output('echo "ttttt\n\n" | nc -w 1 "localhost" "3222"', | ||
shell=True, stderr=subprocess.STDOUT) | ||
|
||
if "SSH-2.0-OpenSSH" in str(out): | ||
return | ||
except subprocess.CalledProcessError: | ||
time.sleep(0.5) | ||
|
||
raise Exception('SSH container did not get up properly') |