Skip to content

Commit

Permalink
Merge pull request #62 from kushaldas/hosttest
Browse files Browse the repository at this point in the history
Fixes #59 now we have HOSTTEST
  • Loading branch information
kushaldas committed Jul 24, 2017
2 parents 0f924ed + 4e9d832 commit 36ca0fa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
7 changes: 7 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@ host system itself. One major usecase for this directive wil be for generating
ansible inventory file using a simple script (local). The tests will continue
even if this command fails to execute properly.

HOSTTEST directive
-------------------

.. versionadded:: 0.18

Now we also have the *HOSTTEST* directive, which will allow us to execute a command
in the host, and count that as a part of the tests. Ansible usage is the best example
for this directive.


For Multi-VM configurations
Expand Down
17 changes: 12 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,40 @@ def test_multihost(self, p_br, p_sc, p_kill, p_exit, p_sleep, p_system, p_usyste
r2.return_code = 0
r3 = Result("result3")
r3.return_code = 0
r4 = Result("/usr/bin/ls")
r4.return_code = 0
values = [r1, r2, r3]
p_run.side_effect = values

c1 = Mock()
c1.communicate.return_value = ('192.168.1.100', '')
c1.return_code = 0
c1.returncode = 0
c2 = Mock()
c2.communicate.return_value = (
'? (192.168.122.100) at ABCD [ether] on virbr0\n? (192.168.122.116) at 00:16:3e:33:ba:a2 [ether] on virbr0',
'')
c2.return_code = 0
c2.returncode = 0
c3 = Mock()
c3.communicate.return_value = ('h', 'h')
c3.return_code = 0
c3.returncode = 0
c4 = Mock()
c4.communicate.return_value = (
'? (192.168.122.102) at XYZ [ether] on virbr0\n? (192.168.122.116) at 00:16:3e:33:ba:a2 [ether] on virbr0',
'')
c4.return_code = 0
p_usystem.side_effect = [c1, c2, c3, c4]
c4.returncode = 0
c5 = Mock()
c5.communicate.return_value = ("/usr/bin/ls", "nothing")
c5.returncode = 0
p_usystem.side_effect = [c1, c2, c3, c4, c5]

with captured_output() as (out, err):
tunirmultihost.start_multihost('multihost', './testvalues/multihost.txt',
debug=False, config_dir='./testvalues/')

data = out.getvalue()
self.assertIn("Passed:1", data)
self.assertIn("Job status: True", data)
self.assertIn("bin/ls", data)
for filename in ['./current_run_info.json', ]:
self.assertTrue(os.path.exists(filename), filename)
last_call = p_system.call_args_list[-1]
Expand Down
1 change: 1 addition & 0 deletions testvalues/multihost.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vm1 sudo su -c"echo Hello > /abcd.txt"
HOSTCOMMAND: touch /tmp/hostcommand.txt
vm2 ls /
## cat /etc/os-release
HOSTTEST: which ls
56 changes: 30 additions & 26 deletions tunirlib/tunirutils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import sys
import time
import json
import shutil
Expand Down Expand Up @@ -203,8 +204,7 @@ def wrapper(*args, **kargs):


@try_again
def execute(config, command, container=None):
# type: (Dict[str, str], str, bool) -> Tuple[Result, str]
def execute(config: Dict[str, str], command: str, container: bool=False) -> Tuple[Result, str]:
"""
Executes a given command based on the system.
:param config: Configuration dictionary.
Expand Down Expand Up @@ -236,15 +236,15 @@ def execute(config, command, container=None):

return result, negative

def update_result(result, command, negative):
# type: (Result, str, str) -> bool

def update_result(result: Result, command: str, negative: str) -> bool:
"""
Updates the result based on input.
:param result: Output from the command
:param job: Job object from model.
:param command: Text command.
:param negative: If it is a negative command, which is supposed to fail.
:param negative: If it is a negative command, values (yes/no).
:return: Boolean, False if the job as whole is failed.
"""
Expand All @@ -260,7 +260,6 @@ def update_result(result, command, negative):
'ret': str(result.return_code), 'status': status} # type: Dict[str,str]
STR[command] = d


if result.return_code != 0 and negative == 'no':
# Save the error message and status as fail.
return False
Expand Down Expand Up @@ -321,6 +320,8 @@ def run_job(jobpath: str, job_name: str='', extra_config: Dict[str,str]={}, cont

try:
for command in commands:
hosttest = False
cmd = ''
negative = ''
result = Result('none') # type: Result
command = command.strip(' \n')
Expand All @@ -344,31 +345,34 @@ def run_job(jobpath: str, job_name: str='', extra_config: Dict[str,str]={}, cont
cmd = command[12:].strip()
os.system(cmd)
continue
elif command.startswith('PLAYBOOK'):
playbook_name = command.split(' ')[1]
playbook = os.path.join(ansible_path, playbook_name)
cmd = "ansible-playbook {0} -i {1} --private-key={2} --ssh-extra-args='-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'".format(playbook,\
ansible_inventory_path, private_key_path)
print(cmd)
os.system(cmd)
continue
elif command.startswith('HOSTTEST:'):
cmd = command[10:].strip()
hosttest = True


print("Executing command: %s" % command)
shell_command = command

if re.search('^vm[0-9] ', command):
# We have a command for multihost
index = command.find(' ')
vm_name = command[:index]
shell_command = command[index+1:]
localconfig = config.vms[vm_name]
else: #At this case, all special keywords checked, now it will run on vm1
vm_name = 'vm1'
shell_command = command
localconfig = config.vms[vm_name]
if not hosttest:
if re.search('^vm[0-9] ', command):
# We have a command for multihost
index = command.find(' ')
vm_name = command[:index]
shell_command = command[index+1:]
localconfig = config.vms[vm_name]
else: #At this case, all special keywords checked, now it will run on vm1
vm_name = 'vm1'
shell_command = command
localconfig = config.vms[vm_name]

try:
result, negative = execute(localconfig, shell_command)
if not hosttest:
result, negative = execute(localconfig, shell_command)
else: # This is only for HOSTTEST directive
out, err, eid = system(cmd)
result = Result(out+err)
result.return_code = eid
negative = "no"
# From here we are following the normal flow
status = update_result(result, command, negative)
if not status:
break
Expand Down

0 comments on commit 36ca0fa

Please sign in to comment.