Пример #1
0
def test_00_run(monkeypatch):
    EXPECTED = {
        'command': 'uptime',
        'command_output': "0:40  up 1 day,  8:19, 4 users, load averages: 0.00 0.50 1.00",
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(command = [ EXPECTED['command'] ])
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    o = stdout.stop().value()

    assert status == 0
    s = \
"""tomahawk@localhost %% %(command)s
%(command_output)s

""" % EXPECTED
    assert o == s
Пример #2
0
def test_00_run(monkeypatch):
    EXPECTED = {
        'command': 'uptime',
        'command_output':
        "0:40  up 1 day,  8:19, 4 users, load averages: 0.00 0.50 1.00",
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(command=[EXPECTED['command']])

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    o = stdout.stop().value()

    assert status == 0
    s = \
"""tomahawk@localhost %% %(command)s
%(command_output)s

""" % EXPECTED
    assert o == s
Пример #3
0
def test_60_verify_output_ok(monkeypatch):
    EXPECTED = {
        'command': 'echo "hello world"',
        'command_output': r'hello world',
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            hosts = 'localhost,127.0.0.1',
            verify_output = True,
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    out = stdout.stop().value().strip()
    assert status == 0
    assert out == """
tomahawk@localhost % echo "hello world"
hello world

[email protected] % echo "hello world"
hello world

Verified output of all hosts.
""".strip()
Пример #4
0
def test_11_run_option_mirror_mode_pull(monkeypatch):
    EXPECTED = {
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()
    target_files = ( 'localhost__hello', '127.0.0.1__hello' )
    # remove target_files
    for f in target_files:
        path = os.path.join(TMP_DIR, f)
        if os.path.exists(path):
            os.remove(path)

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source = hello_file,
            destination = TMP_DIR,
            hosts = 'localhost,127.0.0.1',
            mirror_mode = 'pull',
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        for f in target_files:
            shutil.copyfile(hello_file, os.path.join(TMP_DIR, f))
        return EXPECTED['exit_status'], ''
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == EXPECTED['exit_status']
    for f in target_files:
        assert os.path.exists(os.path.join(TMP_DIR, f))
Пример #5
0
def test_03_run_without_user(monkeypatch):
    EXPECTED = {
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(source=hello_file,
                                            destination=hello_file_copied,
                                            rsync_user=None)

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        shutil.copyfile(hello_file, hello_file_copied)
        return EXPECTED['exit_status'], ''

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == EXPECTED['exit_status']
    assert os.path.exists(hello_file_copied)
    assert not re.search(r'rsync -av.*@',
                         stdout.stop().value())  # test no user on output
Пример #6
0
def test_03_run_escape_shell_chars(monkeypatch):
    EXPECTED = {
        'command': 'echo \\\\',
        'command_output': "\\",
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            debug_enabled=True,
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    main.run()
    o = stdout.stop().value().strip()
    assert o == "tomahawk@localhost % echo \\\\\n\\"
Пример #7
0
def test_11_run_option_mirror_mode_pull(monkeypatch):
    EXPECTED = {
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()
    target_files = ('localhost__hello', '127.0.0.1__hello')
    # remove target_files
    for f in target_files:
        path = os.path.join(TMP_DIR, f)
        if os.path.exists(path):
            os.remove(path)

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source=hello_file,
            destination=TMP_DIR,
            hosts='localhost,127.0.0.1',
            mirror_mode='pull',
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        for f in target_files:
            shutil.copyfile(hello_file, os.path.join(TMP_DIR, f))
        return EXPECTED['exit_status'], ''

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == EXPECTED['exit_status']
    for f in target_files:
        assert os.path.exists(os.path.join(TMP_DIR, f))
Пример #8
0
def test_10_run_option_rsync_options(monkeypatch):
    EXPECTED = {
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()
    hello_file_dry_run = os.path.join(TMP_DIR, 'hello.dry-run')

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source=hello_file,
            destination=hello_file_copied,
            rsync_options='-av --dry-run',
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], ''

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == EXPECTED['exit_status']
    assert not os.path.exists(hello_file_dry_run)
Пример #9
0
def test_60_verify_output_ok(monkeypatch):
    EXPECTED = {
        'command': 'echo "hello world"',
        'command_output': r'hello world',
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            hosts='localhost,127.0.0.1',
            verify_output=True,
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    out = stdout.stop().value().strip()
    assert status == 0
    assert out == """
tomahawk@localhost % echo "hello world"
hello world

[email protected] % echo "hello world"
hello world

Verified output of all hosts.
""".strip()
Пример #10
0
def test_21_run_option_continue_on_error(monkeypatch):
    EXPECTED = {
        'exit_status': 1,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source=hello_file,
            destination=TMP_DIR,
            hosts='localhost,127.0.0.1',
            continue_on_error=True,
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 127, 'error when rsync'

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    err = stderr.stop().value()
    assert status == EXPECTED['exit_status']
    assert len(err.split('\n')) == 4
Пример #11
0
def test_42_output_format_no_newline(monkeypatch):
    """\\n no new line test"""
    EXPECTED = {
        'command': 'uptime',
        'command_output': r"localhost \\n uptime",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            output_format=r'${host} \\n ${command}',
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 0
    assert stdout.stop().value().strip() == EXPECTED['command_output']
Пример #12
0
def test_20_run_option_continue_on_error(monkeypatch):
    EXPECTED = {
        'command': 'failure_command',
        'command_output': "hello world",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            continue_on_error=True,
            hosts='localhost,127.0.0.1',
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 127, EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    err = stderr.stop().value().strip()
    assert status == 1
    assert len(err.split('\n')) == 3
Пример #13
0
def test_05_run_utf8_command(monkeypatch):
    EXPECTED = {
        'command': 'echo -n "あ"',
        'command_output': six.u("あ"),
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            ssh_user=None,
            ssh_options='-o User=tomahawk')

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    o = stdout.stop().value()
    #print(o)
    if six.PY2:
        EXPECTED['command_output'] = EXPECTED['command_output'].encode('utf-8')
    assert status == 0
    s = \
        """[user]@localhost %% %(command)s
%(command_output)s

""" % EXPECTED
    assert o == s
Пример #14
0
def test_21_run_option_parallel_continue_on_error(monkeypatch):
    EXPECTED = {
        'command': 'failure_command',
        'command_output': "hello world",
    }
    stdout, stderr = utils.capture_stdout_stderr()
    target_hosts = [
        'localhost',
        'localhost',
        'localhost',
        'localhost',
        '127.0.0.1',
        '127.0.0.1',
        '127.0.0.1',
        '127.0.0.1',
    ]

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            continue_on_error=True,
            parallel=2,
            hosts=','.join(target_hosts),
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 127, EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 1

    # parse output to collect failure hosts.
    hosts = []
    hosts_start = False
    for line in stderr.stop().value().split('\n'):
        if re.search(r'failed on following hosts', line, re.I):
            hosts_start = True
            continue
        if hosts_start:
            h = line.strip()
            if h != '':
                hosts.append(h)
    assert hosts == target_hosts
Пример #15
0
def test_50_parallel_adjustment(monkeypatch):
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ 'uptime' ], parallel = 10
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, "mock execute"
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    main.run()
    assert main.context.options['parallel'] == 1
Пример #16
0
def test_50_parallel_adjustment(monkeypatch):
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(command=['uptime'], parallel=10)

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, "mock execute"

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    main.run()
    assert main.context.options['parallel'] == 1
Пример #17
0
def test_01_run_error(monkeypatch):
    EXPECTED = {
        'command': 'command_not_found',
        'exit_status': 127,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(command = [ EXPECTED['command'] ])
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], "/bin/sh: command_not_found: command not found"
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 1
    assert re.search(r'failed on host', stderr.stop().value())
Пример #18
0
def test_02_run_timeout(monkeypatch):
    EXPECTED = {
        'command': 'sleep 3',
        'command_output': "/bin/sh: command_not_found: command not found",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ], timeout = 1
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        raise TimeoutError()
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 1
    assert re.search(r'timed out on host', stderr.stop().value())
Пример #19
0
def test_61_verify_output_ng(monkeypatch):
    EXPECTED = {
        'command': 'date',
        'command_output': r'hello world',
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            hosts = 'localhost,127.0.0.1',
            verify_output = True,
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, str(datetime.datetime.now())
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status != 0
Пример #20
0
def test_02_run_timeout(monkeypatch):
    EXPECTED = {
        'exit_status': 1,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source = 'file_does_not_exist',
            destination = TMP_DIR,
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        raise TimeoutError()
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    err = stderr.stop().value()
    assert status == EXPECTED['exit_status']
    assert re.search(r'timed out on host', err)
Пример #21
0
def test_30_execute_option_ssh_options(monkeypatch):
    EXPECTED = {
        'command': 'echo "hello world"',
        'command_output': "hello world",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            ssh_options = '-c arcfour',
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 0
    assert re.search(EXPECTED['command_output'], stdout.stop().value())
Пример #22
0
def test_21_run_option_parallel_continue_on_error(monkeypatch):
    EXPECTED = {
        'command': 'failure_command',
        'command_output': "hello world",
    }
    stdout, stderr = utils.capture_stdout_stderr()
    target_hosts = [
        'localhost', 'localhost', 'localhost', 'localhost',
        '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1',
    ]
    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            continue_on_error = True,
            parallel = 2,
            hosts = ','.join(target_hosts),
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 127, EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 1

    # parse output to collect failure hosts.
    hosts = []
    hosts_start = False
    for line in stderr.stop().value().split('\n'):
        if re.search(r'failed on following hosts', line, re.I):
            hosts_start = True
            continue
        if hosts_start:
            h = line.strip()
            if h != '':
                hosts.append(h)
    assert hosts == target_hosts
Пример #23
0
def test_02_run_timeout(monkeypatch):
    EXPECTED = {
        'command': 'sleep 3',
        'command_output': "/bin/sh: command_not_found: command not found",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(command=[EXPECTED['command']],
                                              timeout=1)

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        raise TimeoutError()

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 1
    assert re.search(r'timed out on host', stderr.stop().value())
Пример #24
0
def test_01_run_error(monkeypatch):
    EXPECTED = {
        'command': 'command_not_found',
        'exit_status': 127,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(command=[EXPECTED['command']])

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED[
            'exit_status'], "/bin/sh: command_not_found: command not found"

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 1
    assert re.search(r'failed on host', stderr.stop().value())
Пример #25
0
def test_01_run_error(monkeypatch):
    EXPECTED = {
        'exit_status': 1,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source = 'file_does_not_exist',
            destination = TMP_DIR,
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        shutil.copyfile(hello_file, hello_file_copied)
        return EXPECTED['exit_status'], ''
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == 1
    assert not os.path.exists(os.path.join(TMP_DIR, 'file_does_not_exist'))
Пример #26
0
def test_42_output_format_no_newline(monkeypatch):
    """\\n no new line test"""
    EXPECTED = {
        'command': 'uptime',
        'command_output': r"localhost \\n uptime",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            output_format = r'${host} \\n ${command}',
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 0
    assert stdout.stop().value().strip() == EXPECTED['command_output']
Пример #27
0
def test_10_run_option_rsync_options(monkeypatch):
    EXPECTED = {
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()
    hello_file_dry_run = os.path.join(TMP_DIR, 'hello.dry-run')

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source = hello_file,
            destination = hello_file_copied,
            rsync_options = '-av --dry-run',
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], ''
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == EXPECTED['exit_status']
    assert not os.path.exists(hello_file_dry_run)
Пример #28
0
def test_03_run_escape_shell_chars(monkeypatch):
    EXPECTED = {
        'command': 'echo \\\\',
        'command_output': "\\",
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            debug_enabled = True,
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return EXPECTED['exit_status'], EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    main.run()
    o = stdout.stop().value().strip()
    assert o == "tomahawk@localhost % echo \\\\\n\\"
Пример #29
0
def test_02_run_timeout(monkeypatch):
    EXPECTED = {
        'exit_status': 1,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source='file_does_not_exist',
            destination=TMP_DIR,
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        raise TimeoutError()

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    err = stderr.stop().value()
    assert status == EXPECTED['exit_status']
    assert re.search(r'timed out on host', err)
Пример #30
0
def test_01_run_error(monkeypatch):
    EXPECTED = {
        'exit_status': 1,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source='file_does_not_exist',
            destination=TMP_DIR,
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        shutil.copyfile(hello_file, hello_file_copied)
        return EXPECTED['exit_status'], ''

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == 1
    assert not os.path.exists(os.path.join(TMP_DIR, 'file_does_not_exist'))
Пример #31
0
def test_03_run_without_user(monkeypatch):
    EXPECTED = {
        'exit_status': 0,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source=hello_file,
            destination=hello_file_copied,
            rsync_user=None
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        shutil.copyfile(hello_file, hello_file_copied)
        return EXPECTED['exit_status'], ''
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    assert status == EXPECTED['exit_status']
    assert os.path.exists(hello_file_copied)
    assert not re.search(r'rsync -av.*@', stdout.stop().value()) # test no user on output
Пример #32
0
def test_61_verify_output_ng(monkeypatch):
    EXPECTED = {
        'command': 'date',
        'command_output': r'hello world',
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            hosts='localhost,127.0.0.1',
            verify_output=True,
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, str(datetime.datetime.now())

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status != 0
Пример #33
0
def test_21_run_option_continue_on_error(monkeypatch):
    EXPECTED = {
        'exit_status': 1,
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_rsync_namespace(
            source = hello_file,
            destination = TMP_DIR,
            hosts = 'localhost,127.0.0.1',
            continue_on_error = True,
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 127, 'error when rsync'
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = RsyncMain('tomahawk-rsync')
    status = main.run()
    err = stderr.stop().value()
    assert status == EXPECTED['exit_status']
    assert len(err.split('\n')) == 4
Пример #34
0
def test_30_execute_option_ssh_options(monkeypatch):
    EXPECTED = {
        'command': 'echo "hello world"',
        'command_output': "hello world",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command=[EXPECTED['command']],
            ssh_options='-c arcfour',
        )

    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 0, EXPECTED['command_output']

    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    assert status == 0
    assert re.search(EXPECTED['command_output'], stdout.stop().value())
Пример #35
0
def test_20_run_option_continue_on_error(monkeypatch):
    EXPECTED = {
        'command': 'failure_command',
        'command_output': "hello world",
    }
    stdout, stderr = utils.capture_stdout_stderr()

    def mock_parse_args(self, args):
        return utils.create_command_namespace(
            command = [ EXPECTED['command'] ],
            continue_on_error = True,
            hosts = 'localhost,127.0.0.1',
        )
    monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', mock_parse_args)

    def mock_execute(self):
        return 127, EXPECTED['command_output']
    monkeypatch.setattr(CommandWithExpect, 'execute', mock_execute)

    main = CommandMain('tomahawk')
    status = main.run()
    err = stderr.stop().value().strip()
    assert status == 1
    assert len(err.split('\n')) == 3