Commit f77d0cc2 authored by Łukasz Nowak's avatar Łukasz Nowak

software/kvm: dcron mock

Make it possible to mock any specific cron entry.
parent a92e2d03
...@@ -19,7 +19,7 @@ md5sum = 9ae66fb63a3cdd8072582622aa1bb36c ...@@ -19,7 +19,7 @@ md5sum = 9ae66fb63a3cdd8072582622aa1bb36c
[template-kvm] [template-kvm]
filename = instance-kvm.cfg.jinja2 filename = instance-kvm.cfg.jinja2
md5sum = bd3a7229e4fdfa9372ee61b6054acf78 md5sum = 20554e40f9fe666839ef86ff8216cebf
[template-kvm-cluster] [template-kvm-cluster]
filename = instance-kvm-cluster.cfg.jinja2.in filename = instance-kvm-cluster.cfg.jinja2.in
......
...@@ -715,7 +715,6 @@ cron-entries = ${directory:cron-entries} ...@@ -715,7 +715,6 @@ cron-entries = ${directory:cron-entries}
crontabs = ${directory:crontabs} crontabs = ${directory:crontabs}
cronstamps = ${directory:cronstamps} cronstamps = ${directory:cronstamps}
catcher = ${cron-simplelogger:wrapper} catcher = ${cron-simplelogger:wrapper}
binary = ${directory:bin}/crond_raw
[cron-service] [cron-service]
recipe = slapos.cookbook:wrapper recipe = slapos.cookbook:wrapper
......
# THIS IS NOT A BUILDOUT FILE, despite purposedly using a compatible syntax.
# The only allowed lines here are (regexes):
# - "^#" comments, copied verbatim
# - "^[" section beginings, copied verbatim
# - lines containing an "=" sign which must fit in the following categorie.
# - "^\s*filename\s*=\s*path\s*$" where "path" is relative to this file
# Copied verbatim.
# - "^\s*hashtype\s*=.*" where "hashtype" is one of the values supported
# by the re-generation script.
# Re-generated.
# - other lines are copied verbatim
# Substitution (${...:...}), extension ([buildout] extends = ...) and
# section inheritance (< = ...) are NOT supported (but you should really
# not need these here).
[template-test]
filename = test-instance.cfg.in
md5sum = 466c7324a44e0cd8ae688699aab62d5f
[template-kvm-export-mock]
filename = test-instance-kvm-export-mock.cfg.jinja2
md5sum = acf0b9a916e1411f184eb4f94c7c2abb
[buildout]
extends = {{ template_kvm_export }}
parts +=
cron-entry-environment
[directory]
cron-d-mock = ${:var}/cron-d-mock
[cron-entry-environment]
recipe = slapos.cookbook:cron.d
cron-entries = ${cron:cron-entries}
name = environment
frequency = * * * * *
command = ${buildout:executable} -c 'import os ; import json ; print(json.dumps(dict(os.environ)))' > ${directory:var}/cron-environment.json
[cron-entry-backup]
recipe = slapos.recipe.template:jinja2
inline =
${:command}
output = ${directory:cron-d-mock}/${:name}
[buildout]
extends = ${template:output}
[dynamic-template-kvm-export-mock]
recipe = slapos.recipe.template:jinja2
url = ${template-kvm-export-mock:location}/${template-kvm-export-mock:filename}
output = $${buildout:directory}/template-kvm-export-mock.cfg
context =
key template_kvm_export dynamic-template-kvm-export:output
[switch_softwaretype]
kvm-export = dynamic-template-kvm-export-mock:output
[buildout]
extends =
../software.cfg
buildout.hash.cfg
parts +=
template-test
[template]
output = ${buildout:directory}/template-original.cfg
[template-test]
recipe = slapos.recipe.template
url = ${:_profile_base_location_}/${:filename}
output = ${buildout:directory}/template.cfg
[template-kvm-export-mock]
recipe = slapos.recipe.build:download
url = ${:_profile_base_location_}/${:filename}
...@@ -59,8 +59,7 @@ skipUnlessKvm = unittest.skipUnless(has_kvm, 'kvm not loaded or not allowed') ...@@ -59,8 +59,7 @@ skipUnlessKvm = unittest.skipUnless(has_kvm, 'kvm not loaded or not allowed')
if has_kvm: if has_kvm:
setUpModule, InstanceTestCase = makeModuleSetUpAndTestCaseClass( setUpModule, InstanceTestCase = makeModuleSetUpAndTestCaseClass(
os.path.abspath( os.path.join(os.path.dirname(__file__), 'test-software.cfg'))
os.path.join(os.path.dirname(__file__), '..', 'software.cfg')))
# XXX Keep using slapos node instance --all, because of missing promises # XXX Keep using slapos node instance --all, because of missing promises
InstanceTestCase.slap._force_slapos_node_instance_all = True InstanceTestCase.slap._force_slapos_node_instance_all = True
else: else:
...@@ -767,6 +766,45 @@ class TestAccessKvmClusterBootstrap(MonitorAccessMixin, KVMTestCase): ...@@ -767,6 +766,45 @@ class TestAccessKvmClusterBootstrap(MonitorAccessMixin, KVMTestCase):
self.assertIn('<title>noVNC</title>', result.text) self.assertIn('<title>noVNC</title>', result.text)
class CronMixin(object):
def setUp(self):
super().setUp()
# wait until all mocked partition have var/cron-environment.json
for i in range(20):
missing_list = []
for mocked in glob.glob(os.path.join(
self.slap._instance_root, '*', 'var', 'cron-d-mock')):
cron_environment = os.path.join(
'/', *mocked.split('/')[:-2], 'var', 'cron-environment.json')
if not os.path.exists(cron_environment):
missing_list.append(cron_environment)
if len(missing_list) == 0:
break
time.sleep(1)
else:
raise ValueError('Missing cron environment', ' '.join(missing_list))
@classmethod
def executeCronDMockJob(cls, instance_type, cron):
jobpath = cls.getPartitionPath(
instance_type, 'var', 'cron-d-mock', cron)
with open(
cls.getPartitionPath(
'kvm-export', 'var', 'cron-environment.json')) as fh:
cron_environment = json.load(fh)
job_list = []
with open(jobpath, 'r') as fh:
for job in fh.readlines():
job = job.strip()
job_list.append(job)
job_list_output = []
for job in job_list:
job_list_output.append(subprocess.run(
job, env=cron_environment, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT))
return job_list_output
@skipUnlessKvm @skipUnlessKvm
class TestInstanceResilient(KVMTestCase, KvmMixin): class TestInstanceResilient(KVMTestCase, KvmMixin):
__partition_reference__ = 'ir' __partition_reference__ = 'ir'
......
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