Skip to content

Commit

Permalink
New helper context manager test_resource(...).
Browse files Browse the repository at this point in the history
Should help writing tests where a single configured resource is
wanted::

    with test_resource(max_cores_per_job=7) as cfg:
         core = Core(cfg)
         # ... do something

The parameters of the test resource are willingly set to some unlikely
value; tests of `Core` and `Engine` need to be updated after these new
settings.
  • Loading branch information
riccardomurri committed Aug 29, 2018
1 parent d607bcc commit 459d3e2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
77 changes: 50 additions & 27 deletions gc3libs/testing/helpers.py
Expand Up @@ -4,7 +4,7 @@
Utility functions for use in unit test code.
"""
#
# Copyright (C) 2015, 2016 S3IT, Zentrale Informatik, University of Zurich
# Copyright (C) 2015, 2016, 2018 S3IT, Zentrale Informatik, University of Zurich
#
#
# This program is free software; you can redistribute it and/or modify it
Expand All @@ -23,57 +23,80 @@

# stdlib imports
from contextlib import contextmanager
import sys
from tempfile import NamedTemporaryFile, mkdtemp
import shutil

# GC3Pie imports
from gc3libs import Application, Run
from gc3libs.config import Configuration
from gc3libs.core import Core, Engine
from gc3libs.quantity import GB, hours
from gc3libs.quantity import GB, MB, hours
from gc3libs.workflow import ParallelTaskCollection, SequentialTaskCollection


@contextmanager
def temporary_core(
transition_graph=None,
max_cores_per_job=1,
max_memory_per_core=1*GB,
max_walltime=8*hours,
max_cores=2,
architecture=Run.Arch.X86_64
):
def test_resource(name='test', **params):
"""
Yield a GC3Pie configuration containing a single resource,
built using the given parameters.
The only resource is named ``test`` (can be changed by passing
keyword argument ``name``).
.. note::
The parameters *must* be given in the internal format expected
by the backend "LRMS" constructors, not in the string format
expected by the configuration file parser.
"""
cfg = Configuration()
cfg.TYPE_CONSTRUCTOR_MAP['noop'] = ('gc3libs.backends.noop', 'NoOpLrms')
name = 'test'
cfg.resources[name].update(
rsc = cfg.resources[name]
# defaults (1) -- general
rsc.update(
name=name,
type='noop',
auth='none',
transport='local',
max_cores_per_job=max_cores_per_job,
max_memory_per_core=max_memory_per_core,
max_walltime=max_walltime,
max_cores=max_cores,
architecture=architecture,
auth='none',
architecture=set([Run.Arch.X86_64]),
enabled=True,
# Use unusual values so that we can easily spot if the `override` option works
large_file_chunk_size=1.78*MB,
large_file_threshold=1.414*GB,
max_cores=123,
max_cores_per_job=123,
max_memory_per_core=999*GB,
max_walltime=7*hours,
)
# update with given parameters
rsc.update(**params)
# defaults (2) -- type-specific
if rsc.type == 'shellcmd':
rsc.setdefault('override', False)
rsc.setdefault('time_cmd', '/usr/bin/time')

core = Core(cfg)
rsc = core.get_backend(name)
# give each job a 50% chance of moving from one state to the
# next one
if transition_graph:
rsc.transition_graph = transition_graph

yield core
yield cfg

# since TYPE_CONSTRUCTOR_MAP is a class-level variable, we
# need to clean up otherwise other tests will see the No-Op
# backend
del cfg.TYPE_CONSTRUCTOR_MAP['noop']


@contextmanager
def temporary_core(transition_graph=None, **params):
with test_resource(**params) as cfg:
name = params.get('name', 'test')
core = Core(cfg)
rsc = core.get_backend(name)
# give each job a 50% chance of moving from one state to the
# next one
if transition_graph:
rsc.transition_graph = transition_graph

yield core


@contextmanager
def temporary_directory(*args, **kwargs):
tmpdir = mkdtemp(*args, **kwargs)
Expand Down
8 changes: 4 additions & 4 deletions gc3libs/tests/test_core.py
@@ -1,7 +1,7 @@
# test_core.py
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 S3IT, Zentrale Informatik, University of Zurich
# Copyright (C) 2015-2018 S3IT, Zentrale Informatik, University of Zurich
#
#
# This program is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -43,9 +43,9 @@ def test_core_resources():
assert 'test' in resources
test_rsc = resources['test']
# these should match the resource definition in `gc3libs.testing.helpers.temporary_core`
assert test_rsc.max_cores_per_job == 1
assert test_rsc.max_memory_per_core == 1*GB
assert test_rsc.max_walltime == 8*hours
assert test_rsc.max_cores_per_job == 123
assert test_rsc.max_memory_per_core == 999*GB
assert test_rsc.max_walltime == 7*hours


@pytest.mark.skipif(
Expand Down
6 changes: 3 additions & 3 deletions gc3libs/tests/test_engine.py
Expand Up @@ -48,9 +48,9 @@ def test_engine_resources():
assert 'test' in resources
test_rsc = resources['test']
# these should match the resource definition in `gc3libs.testing.helpers.temporary_core`
assert test_rsc.max_cores_per_job == 1
assert test_rsc.max_memory_per_core == 1*GB
assert test_rsc.max_walltime == 8*hours
assert test_rsc.max_cores_per_job == 123
assert test_rsc.max_memory_per_core == 999*GB
assert test_rsc.max_walltime == 7*hours


def test_engine_progress(num_jobs=5, transition_graph=None, max_iter=100):
Expand Down

0 comments on commit 459d3e2

Please sign in to comment.