Exemplo n.º 1
0
    def test_error_dir(self, rpw_mock, tmpdir, capsys):

        with pytest.raises(SystemExit) as exc:
            walarchive.main(['a.host', 'a-server', tmpdir.strpath])

        assert exc.value.code == 2
        assert not rpw_mock.called
        out, err = capsys.readouterr()
        assert not out
        assert 'WAL_PATH cannot be a directory' in err
Exemplo n.º 2
0
    def test_connectivity_test_ok(self, popen_mock, capsys):

        popen_mock.return_value.communicate.return_value = (b"Good test!", b"")

        with pytest.raises(SystemExit) as exc:
            walarchive.main(["a.host", "a-server", "--test", "dummy_wal"])

        assert exc.value.code == 0
        out, err = capsys.readouterr()
        assert "Good test!" in out
        assert not err
Exemplo n.º 3
0
    def test_connectivity_test_ok(self, popen_mock, capsys):

        popen_mock.return_value.communicate.return_value = ('Good test!', '')

        with pytest.raises(SystemExit) as exc:
            walarchive.main(['a.host', 'a-server', '--test', 'dummy_wal'])

        assert exc.value.code == 0
        out, err = capsys.readouterr()
        assert "Good test!" in out
        assert not err
Exemplo n.º 4
0
    def test_connectivity_test_error(self, popen_mock, capsys):

        popen_mock.return_value.communicate.side_effect = subprocess.\
            CalledProcessError(255, "remote barman")

        with pytest.raises(SystemExit) as exc:
            walarchive.main(['a.host', 'a-server', '--test', 'dummy_wal'])

        assert exc.value.code == 2
        out, err = capsys.readouterr()
        assert not out
        assert ("ERROR: Impossible to invoke remote put-wal: "
                "Command 'remote barman' returned non-zero "
                "exit status 255") in err
Exemplo n.º 5
0
    def test_error_barman(self, rpw_mock, tmpdir, capsys):
        # Prepare some content
        source = tmpdir.join('wal_dir/000000080000ABFF000000C1')
        source.write('something', ensure=True)

        rpw_mock.return_value.returncode = 1

        with pytest.raises(SystemExit) as exc:
            walarchive.main(['a.host', 'a-server', source.strpath])

        assert exc.value.code == 1
        out, err = capsys.readouterr()
        assert not out
        assert "Remote 'barman put-wal' command has failed" in err
Exemplo n.º 6
0
    def test_error_ssh(self, rpw_mock, tmpdir, capsys):
        # Prepare some content
        source = tmpdir.join('wal_dir/000000080000ABFF000000C1')
        source.write('something', ensure=True)

        rpw_mock.return_value.returncode = 255

        with pytest.raises(SystemExit) as exc:
            walarchive.main(['a.host', 'a-server', source.strpath])

        assert exc.value.code == 3
        out, err = capsys.readouterr()
        assert not out
        assert 'Connection problem with ssh' in err
Exemplo n.º 7
0
    def test_error_io(self, rpw_mock, tmpdir, capsys):
        # Prepare some content
        source = tmpdir.join('wal_dir/000000080000ABFF000000C1')
        source.write('something', ensure=True)

        rpw_mock.side_effect = EnvironmentError

        with pytest.raises(SystemExit) as exc:
            walarchive.main(['a.host', 'a-server', source.strpath])

        assert exc.value.code == 2
        out, err = capsys.readouterr()
        assert not out
        assert 'Error executing ssh' in err
    def test_connectivity_test_returns_subprocess_output(self, popen_mock, capsys):

        popen_mock.return_value.communicate.return_value = (
            b"Tested subprocess return code percolation",
            b"",
        )
        popen_mock.return_value.returncode = 255

        with pytest.raises(SystemExit) as exc:
            walarchive.main(["a.host", "a-server", "--test", "dummy_wal"])

        assert exc.value.code == 255
        out, err = capsys.readouterr()
        assert "Tested subprocess return code percolation" in out
        assert not err
Exemplo n.º 9
0
    def test_ok(self, popen_mock, tmpdir):
        # Prepare some content
        source = tmpdir.join("wal_dir/000000080000ABFF000000C1")
        source.write("something", ensure=True)
        source_hash = source.computehash()

        # Prepare the fake Pipe
        input_mock, output_mock = pipe_helper()
        popen_mock.return_value.stdin = input_mock
        popen_mock.return_value.returncode = 0

        walarchive.main([
            "-c", "/etc/bwa.conf", "-U", "user", "a.host", "a-server",
            source.strpath
        ])

        popen_mock.assert_called_once_with(
            [
                "ssh",
                "-q",
                "-T",
                "*****@*****.**",
                "barman",
                "--config='/etc/bwa.conf'",
                "put-wal",
                "a-server",
            ],
            stdin=subprocess.PIPE,
        )

        # Verify the tar content
        tar = tarfile.open(mode="r|", fileobj=output_mock)
        first = tar.next()
        with closing(tar.extractfile(first)) as fp:
            first_content = fp.read().decode()
        assert first.name == "000000080000ABFF000000C1"
        assert first_content == "something"
        second = tar.next()
        with closing(tar.extractfile(second)) as fp:
            second_content = fp.read().decode()
        assert second.name == "MD5SUMS"
        assert second_content == "%s *000000080000ABFF000000C1\n" % source_hash
        assert tar.next() is None
Exemplo n.º 10
0
    def test_ok(self, popen_mock, tmpdir):
        # Prepare some content
        source = tmpdir.join('wal_dir/000000080000ABFF000000C1')
        source.write('something', ensure=True)
        source_hash = source.computehash()

        # Prepare the fake Pipe
        input_mock, output_mock = pipe_helper()
        popen_mock.return_value.stdin = input_mock
        popen_mock.return_value.returncode = 0

        walarchive.main([
            '-c', '/etc/bwa.conf', '-U', 'user', 'a.host', 'a-server',
            source.strpath
        ])

        popen_mock.assert_called_once_with([
            'ssh', '-q', '-T', '*****@*****.**', 'barman',
            "--config='/etc/bwa.conf'", 'put-wal', 'a-server'
        ],
                                           stdin=subprocess.PIPE)

        # Verify the tar content
        tar = tarfile.open(mode='r|', fileobj=output_mock)
        first = tar.next()
        with closing(tar.extractfile(first)) as fp:
            first_content = fp.read().decode()
        assert first.name == '000000080000ABFF000000C1'
        assert first_content == 'something'
        second = tar.next()
        with closing(tar.extractfile(second)) as fp:
            second_content = fp.read().decode()
        assert second.name == 'MD5SUMS'
        assert second_content == \
            '%s *000000080000ABFF000000C1\n' % source_hash
        assert tar.next() is None