Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from TomRicardoBola/master
Browse files Browse the repository at this point in the history
mock SSHClient in hostvars and inventory tests
  • Loading branch information
mwperina committed Jul 13, 2020
2 parents c5e678b + 202c4d7 commit 9bf47dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 10 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil

import unittest
from unittest.mock import patch

sys.path.extend(["../", "./"])
from ansible_runner_service import main # noqa E402
Expand Down Expand Up @@ -35,6 +36,15 @@ def seed_dirs(seed_list):
else:
shutil.copyfile(src, dest)

def fake_ssh_client(func):
def wrapper(self, *args, **kwargs):
with patch('runner_service.utils.SSHClient') as MockSSHCLient:
instance = MockSSHCLient.return_value
instance.connect.return_value = True

func(self, *args, **kwargs)
return wrapper

class APITestCase(unittest.TestCase):
app = None

Expand Down
6 changes: 3 additions & 3 deletions tests/test_api_hostvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import unittest

sys.path.extend(["../", "./"])
from common import APITestCase # noqa
from common import APITestCase, fake_ssh_client # noqa
from runner_service.utils import fread # noqa


class TestHostVars(APITestCase):

def test_get_invalid_group(self):
Expand Down Expand Up @@ -78,7 +77,7 @@ def test_add_hostvars_request_invalid(self):
content_type="text/html")
self.assertEqual(response.status_code,
415)

@fake_ssh_client
def test_add_hostvars_inventory(self):
"""- create hostvars (file), group and host valid"""
response = self.app.post('api/v1/groups/tahi')
Expand Down Expand Up @@ -120,6 +119,7 @@ def test_delete_hostsvars_invalid(self):
response = self.app.delete('api/v1/hostvars/deleteme/group/unknown')
self.assertEqual(response.status_code, 404)

@fake_ssh_client
def test_delete_hostvars(self):
"""- delete valid hostvars (inventory or file removed)"""
response = self.app.post('api/v1/groups/tdh')
Expand Down
13 changes: 10 additions & 3 deletions tests/test_api_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import unittest

sys.path.extend(["../", "./"])
from common import APITestCase # noqa
from runner_service.utils import fread # noqa
from common import APITestCase, fake_ssh_client # noqa
from runner_service.utils import fread # noqa

# turn of normal logging that the ansible_runner_service will generate
nh = logging.NullHandler()
Expand Down Expand Up @@ -102,6 +102,7 @@ def test_host_add(self):
payload = json.loads(response.data)
self.assertTrue(payload['msg'].upper().startswith('SKIPPED'))

@fake_ssh_client
def test_host_add_localhost(self):
"""- Add a localhost to a group"""

Expand All @@ -124,6 +125,7 @@ def test_host_add_nogroup(self):
self.assertEqual(response.status_code,
400)

@fake_ssh_client
def test_host_add_duplicate(self):
"""- Attempt to add a host multiple times to a group"""
dupe_count = 2
Expand Down Expand Up @@ -154,6 +156,7 @@ def test_hosts(self):
payload = json.loads(response.data)
self.assertTrue(isinstance(payload['data']['hosts'], list))

@fake_ssh_client
def test_remove_valid_host(self):
"""- remove a host from a group"""
response = self.app.post('api/v1/groups/temphost')
Expand All @@ -177,7 +180,7 @@ def test_remove_invalid_host(self):
response = self.app.delete('/api/v1/hosts/notthere/groups/invalidhost')
self.assertEqual(response.status_code,
400)

@fake_ssh_client
def test_check_membership(self):
"""- show groups host is a member of"""
response = self.app.post('api/v1/groups/groupone')
Expand All @@ -192,6 +195,7 @@ def test_check_membership(self):
payload = json.loads(response.data)
self.assertIn("groupone", payload['data']['groups'])

@fake_ssh_client
def test_show_group_members(self):
"""- show hosts that are members of a specific group"""
response = self.app.post('api/v1/groups/grouptwo')
Expand All @@ -215,6 +219,7 @@ def test_show_group_members_missing(self):
self.assertEqual(response.status_code,
404)

@fake_ssh_client
def test_add_host_multiple_groups(self):
"""- add a host to multiple groups"""

Expand All @@ -234,6 +239,7 @@ def test_add_host_multiple_groups(self):
self.assertEqual(response.status_code,
200)

@fake_ssh_client
def test_hosts_with_invalid_parms(self):
"""- issue a host/group request with an invalid parameter"""
response = self.app.post('api/v1/groups/parmcheck') # noqa
Expand All @@ -243,6 +249,7 @@ def test_hosts_with_invalid_parms(self):
self.assertEqual(response.status_code,
400)

@fake_ssh_client
def test_remove_host(self):
"""- remove a host from all groups"""
response = self.app.post('api/v1/groups/removehost1')
Expand Down

0 comments on commit 9bf47dc

Please sign in to comment.