예제 #1
0
def test_that_module_block_is_persisted():
    """Module should create a 'setup' action block."""
    module_config = {
        'on_setup': {
            'run': {
                'shell': 'echo first time!',
            },
        },
    }
    params = {
        'name': 'test',
        'module_config': module_config,
        'module_directory': Path(__file__).parent,
    }

    module = Module(**params)
    assert isinstance(
        module.get_action_block(name='on_setup'),
        SetupActionBlock,
    )
    assert module.execute(action='run', block='on_setup') == ((
        'echo first time!',
        'first time!',
    ), )

    # After creating this module again, the run action should not be performed.
    del module
    module = Module(**params)
    assert module.execute(action='run', block='on_setup') == tuple()
예제 #2
0
def test_defining_on_startup_block_at_root_indentation(caplog):
    """Root indentation actions should be promoted to on_startup."""
    # Test that no root actions are not affected
    module_config = {
        'on_startup': {
            'run': [{'shell': 'echo on_startup'}],
        },
    }
    assert Module.prepare_on_startup_block(
        module_name='test',
        module_config=module_config,
    ) == {
        'on_startup': {
            'run': [{'shell': 'echo on_startup'}],
        },
    }

    # Test that actions are moved into empty block
    module_config = {
        'run': [{'shell': 'touch stuff'}],
        'compile': {'source': 'some/path'},
    }
    assert Module.prepare_on_startup_block(
        module_name='test',
        module_config=module_config,
    ) == {
        'on_startup': {
            'run': [{'shell': 'touch stuff'}],
            'compile': {'source': 'some/path'},
        },
    }

    # Test that overwriting values are logged
    caplog.clear()
    module_config = {
        'run': [{'shell': 'echo overwritten'}],
        'on_startup': {
            'run': [{'shell': 'echo original'}],
        },
    }
    assert Module.prepare_on_startup_block(
        module_name='test',
        module_config=module_config,
    ) == {
        'on_startup': {
            'run': [{'shell': 'echo overwritten'}],
        },
    }
    assert caplog.record_tuples[0][1] == logging.ERROR

    # Test that we only have partial overwrites
    module_config = {
        'stow': [{'content': 'new_stow'}],
        'copy': {'content': 'new_copy'},
        'on_startup': {
            'run': [{'shell': 'run'}],
            'stow': {'content': 'old_stow'},
            'copy': {'content': 'old_copy'},
        },
        'on_exit': {},
    }
    assert Module.prepare_on_startup_block(
        module_name='test',
        module_config=module_config,
    ) == {
        'on_startup': {
            'run': [{'shell': 'run'}],
            'stow': [{'content': 'new_stow'}],
            'copy': {'content': 'new_copy'},
        },
        'on_exit': {},
    }

    # Test that prepare_on_startup_block is actually used
    module_config = {
        'run': [{'shell': 'echo overwritten'}],
        'on_startup': {
            'run': [{'shell': 'echo original'}],
        },
    }
    module = Module(
        name='test_module',
        module_config=module_config,
        module_directory=Path('/'),
    )
    assert module.execute(action='run', block='on_startup') \
        == (('echo overwritten', 'overwritten'),)