Commit 89a4334c authored by Jérome Perrin's avatar Jérome Perrin

Utilities for testing

See merge request !245
parents bbcbab07 75eafe55
......@@ -29,6 +29,10 @@
import socket
import hashlib
import unittest
import os
import subprocess
import sys
import json
from contextlib import closing
try:
......@@ -60,6 +64,56 @@ def getPortFromPath(path):
16) % (65535 - 1024)
def getPromisePluginParameterDict(filepath):
# type: (str) -> dict
"""Load the slapos monitor plugin and returns the configuration used by this plugin.
This allow to check that monitoring plugin are using a proper config.
"""
extra_config_dict_json = subprocess.check_output([
sys.executable,
"-c",
"""
import json, sys
with open(sys.argv[1]) as f:
exec(f.read())
print(json.dumps(extra_config_dict))
""",
filepath,
])
return json.loads(extra_config_dict_json)
class CrontabMixin(object):
computer_partition_root_path = None # type: str
def _getCrontabCommand(self, crontab_name):
# type: (str) -> str
"""Read a crontab and return the command that is executed.
"""
with open(
os.path.join(
self.computer_partition_root_path,
'etc',
'cron.d',
crontab_name,
)) as f:
crontab_spec = f.read()
return " ".join(crontab_spec.split()[5:])
def _executeCrontabAtDate(self, crontab_name, date):
# type: (str, str) -> None
"""Executes a crontab as if the current date was `date`.
`date` will be passed to faketime time command, it can also
be a relative time.
"""
crontab_command = self._getCrontabCommand(crontab_name)
subprocess.check_call(
"faketime {date} bash -o pipefail -e -c '{crontab_command}'".format(**locals()),
shell=True,
)
class ImageComparisonTestCase(unittest.TestCase):
"""TestCase with utility method to compare images.
......
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