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

Commit 9bf47dc

Browse files
authored
Merge pull request #67 from TomRicardoBola/master
mock SSHClient in hostvars and inventory tests
2 parents c5e678b + 202c4d7 commit 9bf47dc

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

tests/common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66

77
import unittest
8+
from unittest.mock import patch
89

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

39+
def fake_ssh_client(func):
40+
def wrapper(self, *args, **kwargs):
41+
with patch('runner_service.utils.SSHClient') as MockSSHCLient:
42+
instance = MockSSHCLient.return_value
43+
instance.connect.return_value = True
44+
45+
func(self, *args, **kwargs)
46+
return wrapper
47+
3848
class APITestCase(unittest.TestCase):
3949
app = None
4050

tests/test_api_hostvars.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import unittest
77

88
sys.path.extend(["../", "./"])
9-
from common import APITestCase # noqa
9+
from common import APITestCase, fake_ssh_client # noqa
1010
from runner_service.utils import fread # noqa
1111

12-
1312
class TestHostVars(APITestCase):
1413

1514
def test_get_invalid_group(self):
@@ -78,7 +77,7 @@ def test_add_hostvars_request_invalid(self):
7877
content_type="text/html")
7978
self.assertEqual(response.status_code,
8079
415)
81-
80+
@fake_ssh_client
8281
def test_add_hostvars_inventory(self):
8382
"""- create hostvars (file), group and host valid"""
8483
response = self.app.post('api/v1/groups/tahi')
@@ -120,6 +119,7 @@ def test_delete_hostsvars_invalid(self):
120119
response = self.app.delete('api/v1/hostvars/deleteme/group/unknown')
121120
self.assertEqual(response.status_code, 404)
122121

122+
@fake_ssh_client
123123
def test_delete_hostvars(self):
124124
"""- delete valid hostvars (inventory or file removed)"""
125125
response = self.app.post('api/v1/groups/tdh')

tests/test_api_inventory.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import unittest
88

99
sys.path.extend(["../", "./"])
10-
from common import APITestCase # noqa
11-
from runner_service.utils import fread # noqa
10+
from common import APITestCase, fake_ssh_client # noqa
11+
from runner_service.utils import fread # noqa
1212

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

105+
@fake_ssh_client
105106
def test_host_add_localhost(self):
106107
"""- Add a localhost to a group"""
107108

@@ -124,6 +125,7 @@ def test_host_add_nogroup(self):
124125
self.assertEqual(response.status_code,
125126
400)
126127

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

159+
@fake_ssh_client
157160
def test_remove_valid_host(self):
158161
"""- remove a host from a group"""
159162
response = self.app.post('api/v1/groups/temphost')
@@ -177,7 +180,7 @@ def test_remove_invalid_host(self):
177180
response = self.app.delete('/api/v1/hosts/notthere/groups/invalidhost')
178181
self.assertEqual(response.status_code,
179182
400)
180-
183+
@fake_ssh_client
181184
def test_check_membership(self):
182185
"""- show groups host is a member of"""
183186
response = self.app.post('api/v1/groups/groupone')
@@ -192,6 +195,7 @@ def test_check_membership(self):
192195
payload = json.loads(response.data)
193196
self.assertIn("groupone", payload['data']['groups'])
194197

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

222+
@fake_ssh_client
218223
def test_add_host_multiple_groups(self):
219224
"""- add a host to multiple groups"""
220225

@@ -234,6 +239,7 @@ def test_add_host_multiple_groups(self):
234239
self.assertEqual(response.status_code,
235240
200)
236241

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

252+
@fake_ssh_client
246253
def test_remove_host(self):
247254
"""- remove a host from all groups"""
248255
response = self.app.post('api/v1/groups/removehost1')

0 commit comments

Comments
 (0)