Exemplo n.º 1
0
def test_armor_raw():
    raw_encoded = sh('python -m saltpack armor --raw', input=inputstr).read()
    print(raw_encoded)
    raw_decoded = sh('python -m saltpack dearmor --raw',
                     input=raw_encoded).read()
    print(raw_decoded)
    assert inputstr == raw_decoded
Exemplo n.º 2
0
def test_local_path_doesnt_match_PATH():
    echo_path = Path('echo')
    assert not echo_path.exists(), 'This path is supposed to be nonexistent.'
    with raises(PROGRAM_NOT_FOUND_ERROR):
        cmd(echo_path).run()
    with raises(duct.StatusError):
        sh(echo_path).run()
Exemplo n.º 3
0
def test_local_path_doesnt_match_PATH():
    echo_path = Path('echo')
    assert not echo_path.exists(), 'This path is supposed to be nonexistent.'
    with raises(PROGRAM_NOT_FOUND_ERROR):
        cmd(echo_path).run()
    with raises(duct.StatusError):
        sh(echo_path).run()
Exemplo n.º 4
0
def test_armor_raw():
    raw_encoded = sh('python -m saltpack armor --raw', input=inputstr).read()
    print(raw_encoded)
    raw_decoded = sh('python -m saltpack dearmor --raw',
                     input=raw_encoded).read()
    print(raw_decoded)
    assert inputstr == raw_decoded
def test_encryption():
    encrypted = sh("python -m saltpack encrypt") \
        .input(message) \
        .read()
    print(encrypted)
    decrypted = sh("python -m saltpack decrypt --debug") \
        .input(encrypted) \
        .read()
    assert message == decrypted, repr(message) + " != " + repr(decrypted)
Exemplo n.º 6
0
def test_start():
    handle1 = sh('echo one').stdout_capture().start()
    handle2 = sh('echo two').stdout_capture().start()
    result1 = handle1.wait()
    result2 = handle2.wait()
    assert b"one" + NEWLINE == result1.stdout
    assert b"" == result1.stderr
    assert 0 == result1.status
    assert b"two" + NEWLINE == result2.stdout
    assert b"" == result2.stderr
    assert 0 == result2.status
Exemplo n.º 7
0
def test_stderr():
    # with a file path
    temp = mktemp()
    sh('echo hi').stdout_to_stderr().stderr(temp).run()
    with open(temp) as f:
        assert 'hi\n' == f.read()
    # with a Path path
    if has_pathlib:
        temp = mktemp()
        sh('echo hi').stdout_to_stderr().stderr(Path(temp)).run()
        with open(temp) as f:
            assert 'hi\n' == f.read()
    # with an open file
    temp = mktemp()
    with open(temp, 'w') as f:
        sh('echo hi').stdout_to_stderr().stderr_file(f).run()
    with open(temp) as f:
        assert 'hi\n' == f.read()
    # to /dev/null
    out = sh('echo hi').stdout_to_stderr().stderr_null().read()
    assert '' == out
    # to stdout
    result = (sh('echo hi')
              .stdout_to_stderr()
              .stderr_to_stdout()
              .stdout_capture()
              .stderr_capture()
              .run())
    assert b'hi' + NEWLINE == result.stdout
    assert b'' == result.stderr
Exemplo n.º 8
0
def test_stderr():
    # with a file path
    temp = mktemp()
    sh('echo hi').stdout_to_stderr().stderr(temp).run()
    with open(temp) as f:
        assert 'hi\n' == f.read()
    # with a Path path
    if has_pathlib:
        temp = mktemp()
        sh('echo hi').stdout_to_stderr().stderr(Path(temp)).run()
        with open(temp) as f:
            assert 'hi\n' == f.read()
    # with an open file
    temp = mktemp()
    with open(temp, 'w') as f:
        sh('echo hi').stdout_to_stderr().stderr_file(f).run()
    with open(temp) as f:
        assert 'hi\n' == f.read()
    # to /dev/null
    out = sh('echo hi').stdout_to_stderr().stderr_null().read()
    assert '' == out
    # to stdout
    result = (sh('echo hi').stdout_to_stderr().stderr_to_stdout().
              stdout_capture().stderr_capture().run())
    assert b'hi' + NEWLINE == result.stdout
    assert b'' == result.stderr
Exemplo n.º 9
0
def test_run_local_path():
    '''Trying to execute 'test.sh' without the leading dot fails in bash and
    subprocess.py. But it needs to succeed with Path('test.sh'), because
    there's no difference between that and Path('./test.sh').'''
    if os.name == 'nt':
        extension = '.bat'
        code = textwrap.dedent(u'''\
            @echo off
            echo foo
            ''')
    else:
        extension = '.sh'
        code = textwrap.dedent(u'''\
            #! /bin/sh
            echo foo
            ''')
    # Use a random name just in case.
    random_letters = binascii.hexlify(os.urandom(4)).decode()
    local_script = 'test_' + random_letters + extension
    script_path = Path(local_script)
    try:
        with script_path.open('w') as f:
            f.write(code)
        script_path.chmod(0o755)
        assert 'foo' == cmd(script_path).read()
        assert 'foo' == sh(script_path).read()
    finally:
        script_path.unlink()
Exemplo n.º 10
0
def test_run_local_path():
    '''Trying to execute 'test.sh' without the leading dot fails in bash and
    subprocess.py. But it needs to succeed with Path('test.sh'), because
    there's no difference between that and Path('./test.sh').'''
    if os.name == 'nt':
        extension = '.bat'
        code = textwrap.dedent(u'''\
            @echo off
            echo foo
            ''')
    else:
        extension = '.sh'
        code = textwrap.dedent(u'''\
            #! /bin/sh
            echo foo
            ''')
    # Use a random name just in case.
    random_letters = binascii.hexlify(os.urandom(4)).decode()
    local_script = 'test_' + random_letters + extension
    script_path = Path(local_script)
    try:
        with script_path.open('w') as f:
            f.write(code)
        script_path.chmod(0o755)
        assert 'foo' == cmd(script_path).read()
        assert 'foo' == sh(script_path).read()
    finally:
        script_path.unlink()
Exemplo n.º 11
0
def test_unchecked():
    assert 1 == false().unchecked().run().status
    with raises(StatusError) as e:
        false().run()
    assert e.value.result.status == 1

    # Make sure unchecked errors don't short-circuit a `then`.
    output = false().unchecked().then(sh("echo hi")).read()
    assert output == "hi"
Exemplo n.º 12
0
def test_sign_detached():
    detached = sh("python -m saltpack sign --binary --detached").read(
        input=message, decode=False)
    print(detached)
    _, temp = tempfile.mkstemp()
    with open(temp, 'wb') as f:
        f.write(detached)
    command = ["python", "-m", "saltpack", "verify", "--signature", temp,
               "--binary", "--debug"]
    cmd(*command).read(input=message)
Exemplo n.º 13
0
def test_sign_detached():
    detached = sh("python -m saltpack sign --binary --detached").read(
        input=message, decode=False)
    print(detached)
    _, temp = tempfile.mkstemp()
    with open(temp, 'wb') as f:
        f.write(detached)
    command = [
        "python", "-m", "saltpack", "verify", "--signature", temp, "--binary",
        "--debug"
    ]
    cmd(*command).read(input=message)
Exemplo n.º 14
0
def test_commands_can_be_paths():
    tempdir = tempfile.mkdtemp()
    path = Path(tempdir, "script.bat")
    # Note that Path.open() rejects Python 2 non-unicode strings.
    with open(str(path), 'w') as f:
        if os.name == 'nt':
            f.write('@echo off\n')
        else:
            f.write('#! /bin/sh\n')
        f.write('echo some stuff\n')
    path.chmod(0o755)
    assert 'some stuff' == cmd(path).read()
    assert 'some stuff' == sh(path).read()
Exemplo n.º 15
0
def test_commands_can_be_paths():
    tempdir = tempfile.mkdtemp()
    path = Path(tempdir, "script.bat")
    # Note that Path.open() rejects Python 2 non-unicode strings.
    with open(str(path), 'w') as f:
        if os.name == 'nt':
            f.write('@echo off\n')
        else:
            f.write('#! /bin/sh\n')
        f.write('echo some stuff\n')
    path.chmod(0o755)
    assert 'some stuff' == cmd(path).read()
    assert 'some stuff' == sh(path).read()
Exemplo n.º 16
0
def main():
    os.chdir(str(here))

    status = sh("git status --porcelain").read()
    if status:
        print("repo isn't clean")
        return 1

    cmd("git", "push", "origin", "master").run()

    cmd(
        "ssh",
        "*****@*****.**",
        "cd /srv/jacko.io && git pull --ff-only && peru sync --no-cache",
    ).run()
def test_sign_detached():
    detached = sh("python -m saltpack sign --binary --detached") \
            .input(message) \
            .stdout_capture() \
            .run() \
            .stdout
    print(detached)
    _, temp = tempfile.mkstemp()
    with open(temp, 'wb') as f:
        f.write(detached)
    command = [
        "python", "-m", "saltpack", "verify", "--signature", temp, "--binary",
        "--debug"
    ]
    cmd(*command).input(message).read()
Exemplo n.º 18
0
def test_block():
    blocked = sh('python -m saltpack block', input=inputstr).read()
    print(blocked)
    unblocked = sh('python -m saltpack unblock', input=blocked).read()
    print(unblocked)
    assert inputstr == unblocked
Exemplo n.º 19
0
def test_sign_attached():
    signed = sh("python -m saltpack sign").read(input=message)
    print(signed)
    verified = sh("python -m saltpack verify --debug").read(input=signed)
    assert message == verified, repr(message) + " != " + repr(verified)
Exemplo n.º 20
0
def test_encryption():
    encrypted = sh("python -m saltpack encrypt") \
                .read(input=message, decode=False)
    print(encrypted)
    decrypted = sh("python -m saltpack decrypt --debug").read(input=encrypted)
    assert message == decrypted, repr(message) + " != " + repr(decrypted)
Exemplo n.º 21
0
def test_efficient():
    efficient = sh('python -m saltpack efficient 64').read()
    print(efficient)
    assert re.search('3 bytes.*4 chars', efficient)
Exemplo n.º 22
0
def test_swap_and_redirect_at_same_time():
    '''We need to make sure that doing e.g. stderr_to_stdout while also doing
    stdout_capture means that stderr joins the redirected stdout, rather than
    joining what stdout used to be.'''
    err_out = sh('echo hi>&2').stderr_to_stdout().read()
    assert err_out == 'hi'
Exemplo n.º 23
0
def test_encryption():
    encrypted = sh("python -m saltpack encrypt") \
                .read(input=message, decode=False)
    print(encrypted)
    decrypted = sh("python -m saltpack decrypt --debug").read(input=encrypted)
    assert message == decrypted, repr(message) + " != " + repr(decrypted)
Exemplo n.º 24
0
def test_nesting():
    innermost = true().then(replace('i', 'o'))
    middle = true().then(innermost)
    out = sh('echo hi').pipe(middle).read()
    assert 'ho' == out
Exemplo n.º 25
0
def test_block():
    blocked = sh('python -m saltpack block', input=inputstr).read()
    print(blocked)
    unblocked = sh('python -m saltpack unblock', input=blocked).read()
    print(unblocked)
    assert inputstr == unblocked
Exemplo n.º 26
0
def test_sign_attached():
    signed = sh("python -m saltpack sign").read(input=message)
    print(signed)
    verified = sh("python -m saltpack verify --debug").read(input=signed)
    assert message == verified, repr(message) + " != " + repr(verified)
Exemplo n.º 27
0
def test_result():
    result = sh('echo more stuff').stdout_capture().run()
    assert b"more stuff" + NEWLINE == result.stdout
    assert b"" == result.stderr
    assert 0 == result.status
Exemplo n.º 28
0
def test_hello_world():
    out = sh('echo hello world').read()
    assert "hello world" == out
Exemplo n.º 29
0
def test_result():
    result = sh('echo more stuff').stdout_capture().run()
    assert b"more stuff" + NEWLINE == result.stdout
    assert b"" == result.stderr
    assert 0 == result.status
Exemplo n.º 30
0
def test_nesting():
    innermost = true().then(replace('i', 'o'))
    middle = true().then(innermost)
    out = sh('echo hi').pipe(middle).read()
    assert 'ho' == out
Exemplo n.º 31
0
def test_string_mode_returns_unicode():
    '''In Python 2, reading a file in text mode still returns a raw string,
    instead of a unicode string. Make sure we convert.'''
    out = sh('echo hi').read()
    assert isinstance(out, type(u''))
Exemplo n.º 32
0
def test_armor():
    encoded = sh('python -m saltpack armor', input=inputstr).read()
    print(encoded)
    decoded = sh('python -m saltpack dearmor', input=encoded).read()
    print(decoded)
    assert inputstr == decoded
Exemplo n.º 33
0
def test_hello_world():
    out = sh('echo hello world').read()
    assert "hello world" == out
Exemplo n.º 34
0
def test_efficient():
    efficient = sh('python -m saltpack efficient 64').read()
    print(efficient)
    assert re.search('3 bytes.*4 chars', efficient)
Exemplo n.º 35
0
def test_armor():
    encoded = sh('python -m saltpack armor', input=inputstr).read()
    print(encoded)
    decoded = sh('python -m saltpack dearmor', input=encoded).read()
    print(decoded)
    assert inputstr == decoded
Exemplo n.º 36
0
def test_swap_and_redirect_at_same_time():
    '''We need to make sure that doing e.g. stderr_to_stdout while also doing
    stdout_capture means that stderr joins the redirected stdout, rather than
    joining what stdout used to be.'''
    err_out = sh('echo hi>&2').stderr_to_stdout().read()
    assert err_out == 'hi'
Exemplo n.º 37
0
def test_string_mode_returns_unicode():
    '''In Python 2, reading a file in text mode still returns a raw string,
    instead of a unicode string. Make sure we convert.'''
    out = sh('echo hi').read()
    assert isinstance(out, type(u''))