Commit a7cb7a8c authored by heemayl's avatar heemayl

Added client tests, modified installer

parent 4a770983
......@@ -6,6 +6,5 @@ logs/*
*/__pycache__/*
*.pyc
*.lock*
test*
*.pkl
hatd.pid
......@@ -24,7 +24,9 @@ class DateTime:
to Python datetime object and converts that into Epoch eventually.
'''
def __init__(self, str_dt):
self.str_dt = str_dt.lower().strip().strip(':')
self.str_dt = str_dt.lower().strip().strip(':')\
.replace('+', ' + ')\
.replace('-', ' - ')
self.list_dt = self.str_dt.split()
def check_get(self):
......
......@@ -34,16 +34,16 @@ mkdir -p /var/run/hatd/locks
chown :hatd /var/run/hatd/locks
chmod g+s /var/run/hatd/locks
# Start the daemon
systemctl daemon-reload
systemctl enable hat-daemon.service
systemctl start hatd.service
# Print done msg
msgs=($'\nInstallation Done and daemon started!\n'
'Now, do the following:'
$'\t1. Add the user(s) who can schedule jobs to the `hatd` group e.g. for user `foobar`: usermod -a -G hatd foobar'
$'\t2. Read `hatc --help`'
$'\t2. For group changes to take effect on any live session of the user, the user needs to logout of that session, and then login Or simply start a new session'
$'\t3. Read `hatc --help`'
)
printf '%s\n' "${msgs[@]}"
# Start the daemon
systemctl daemon-reload
systemctl enable hat-daemon.service && \
systemctl start hatd.service && { printf '%s\n' "${msgs[@]}" || printf '%s\n' "Installation Failed!" ;}
import datetime
import os
# import multiprocessing
import time
import json
# from lib.scheduler import Job
from lib.runner import BaseRunner
# from daemon import HatDaemon
DAEMON_IN = '/var/run/hatd/ipc/daemon_in.fifo'
DAEMON_OUT = '/var/run/hatd/ipc/daemon_out.fifo'
# These are server daemon and client codes...
# daemon = HatDaemon()
# daemon.start()
# print(daemon.status_verbose)
# print(daemon.pid)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'add_job': {
'euid': os.geteuid(),
'command': 'cat /etc/apg.conf',
'time_': (datetime.datetime.now() + datetime.timedelta(minutes=1))
.strftime('%Y-%m-%d %H:%M:%S'),
# 'use_shell': True
}
}), 'wt', True)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'add_job': {
'euid': os.geteuid(),
'command': 'caasdasdasd',
'time_': (datetime.datetime.now() + datetime.timedelta(minutes=1))
.strftime('%Y-%m-%d %H:%M:%S'),
'use_shell': True
}
}), 'wt', True)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'add_job': {
'euid': os.geteuid(),
'command': 'cat /etc/shadow',
'time_': (datetime.datetime.now() + datetime.timedelta(minutes=2))
.strftime('%Y-%m-%d %H:%M:%S'),
# 'use_shell': True
}
}), 'wt', True)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'add_job': {
'euid': os.geteuid(),
'command': 'bash -c "foo(){ echo HAT ;}; foo"',
'time_': (datetime.datetime.now() + datetime.timedelta(minutes=4))
.strftime('%Y-%m-%d %H:%M:%S'),
'use_shell': True
}
}), 'wt', True)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'remove_job': (1000, 4)
}), 'wt', True)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'jobcount': 1000
}), 'wt', True)
BaseRunner.write_to_file(DAEMON_IN, json.dumps({
'joblist': 1000
}), 'wt', True)
# daemon.remove_job(1000, 8, 1)
# daemon.remove_job(1)
with open(DAEMON_OUT) as f:
while True:
for line in f:
print(json.loads(line))
# time.sleep(120)
# daemon.stop()
# The runner object
# runner_ = BaseRunner()
# queue = runner_.get_queue()
# # Starting the runner
# # def runner_wraper(queue):
# # while True:
# # if runner_._running:
# # item = queue.get()
# # with open('/home/chayan/checking', 'a') as f:
# # f.write(str(queue) + 'dads')
# # if isinstance(item, dict):
# # command = item['command']
# # time_ = item['time_']
# # Job(command, time_)
# # else:
# # with open('/home/chayan/checking', 'a') as f:
# # f.write('start')
# # runner_.start()
# # time.sleep(0.1)
# print(runner_.jobcount)
# proc = multiprocessing.Process(target=runner_.start)
# proc.start()
# print(proc.is_alive())
# #runner_.start()
# # Creating test jobs
# Job('cat /etc/shadow',
# (datetime.datetime.now() + datetime.timedelta(minutes=1))
# .strftime('%Y-%m-%d %H:%M:%S'))
# Job('cat /proc/uptime',
# (datetime.datetime.now() + datetime.timedelta(minutes=1))
# .strftime('%Y-%m-%d %H:%M:%S'))
# Job('no_such_command',
# (datetime.datetime.now() + datetime.timedelta(minutes=5))
# .strftime('%Y-%m-%d %H:%M:%S'))
# Job('whoami',
# (datetime.datetime.now() + datetime.timedelta(minutes=10))
# .strftime('%Y-%m-%d %H:%M:%S'))
# print(runner_.jobcount)
# # Creating test jobs
# queue.put_nowait({'command': 'cat /proc/meminfo',
# 'time_': (
# datetime.datetime.now() + datetime.timedelta(minutes=1))
# .strftime('%Y-%m-%d %H:%M:%S')})
# queue.put_nowait({'command': 'cat /proc/irq/default_smp_affinity',
# 'time_': (
# datetime.datetime.now() + datetime.timedelta(minutes=2))
# .strftime('%Y-%m-%d %H:%M:%S'),
# 'use_shell': True})
# # print(runner_.jobcount)
# time.sleep(140)
# print(runner_.jobcount)
# runner_.stop()
# time.sleep(2)
# print(runner_._running)
# print(proc.is_alive())
# # q.put('oooopsie poopsie',
# # (datetime.datetime.now() + datetime.timedelta(minutes=1))
# # .strftime('%Y-%m-%d %H:%M:%S'))
# # q.put('no_such_command',
# # (datetime.datetime.now() + datetime.timedelta(minutes=5))
# # .strftime('%Y-%m-%d %H:%M:%S'))
# # q.put('whoami',
# # (datetime.datetime.now() + datetime.timedelta(minutes=10))
# # .strftime('%Y-%m-%d %H:%M:%S'))
#!/usr/bin/env python3
# Test case(s) for the hat client -- `client.py`
import os
import sys
import unittest
# Inserting the dir in `sys.path` at index 0
sys.path.insert(0, os.path.join(os.path.dirname(
os.path.dirname(os.path.abspath(__file__))), 'hat'))
import client
class ClientTest(unittest.TestCase):
'''Testing various functionalities of the hat client.'''
def test_check_daemon_process(self):
'''Checking the check_daemon_process func.'''
pid_file = '/var/run/hatd/hatd.pid'
if os.path.isfile(pid_file):
self.assertTrue(client.check_daemon_process(pid_file))
else:
self.assertFalse(client.check_daemon_process(pid_file))
def test_argument_daemon_response(self):
'''Checking argument_serializer func,
and daemon response after sending data.
'''
# Argument input-output map for argument_serializer
args_dict_all = {
'joblist': {
'input': {
'joblist': True
},
'output': 1 # length
},
'jobcount': {
'input': {
'jobcount': True
},
'output': 1
},
'add_job': {
'input': {
'add_job': ['whoami', 'now + 3 mins', 'bash']
},
'output': 4
},
'remove_job': {
'input': {
'remove_job': [1, 2]
},
'output': 3
}
}
for arg, val in args_dict_all.items():
out = client.argument_serializer(val['input'])
# Check args serializer
self.assertTrue((out[0] == arg) and (len(out) == val['output']),
'Error on arg: {}'.format(arg))
data = client.SendReceiveData(out)
data.check_get_send()
received = data.receive_from_daemon()
# daemon returned something or not
self.assertIsNotNone(received, 'No data for: {}'.format(arg))
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment