示例#1
0
def test_database():
    # type: () -> None
    settings = Settings()

    # The default is uninitialized and should throw an error until we load a configuration.
    with pytest.raises(InvalidSettingsError):
        assert settings.database

    # If database is set, it should be used.
    settings.database = "sqlite:///grouper.sqlite"
    assert settings.database == "sqlite:///grouper.sqlite"
    settings.database_source = "/bin/false"
    assert settings.database == "sqlite:///grouper.sqlite"

    # If only database_source is set, it should be run to get a URL.
    settings.database = ""
    settings.database_source = "/path/to/program"
    with patch("subprocess.check_output") as mock_subprocess:
        mock_subprocess.return_value = b"sqlite:///other.sqlite\n"
        assert settings.database == "sqlite:///other.sqlite"
        assert mock_subprocess.call_args_list == [
            call(["/path/to/program"], stderr=subprocess.STDOUT)
        ]

    # If the command fails, it should be retried.  Disable the delay to not make the test slow.
    settings = Settings()
    settings.database_source = "/path/to/program"
    with patch.object(Settings, "DB_URL_RETRY_DELAY", new=0):
        with patch("subprocess.check_output") as mock_subprocess:
            exception = subprocess.CalledProcessError(1, "/path/to/program")
            mock_subprocess.side_effect = [exception, b"sqlite:///third.sqlite"]
            assert settings.database == "sqlite:///third.sqlite"
            assert mock_subprocess.call_count == 2

    # Commands that return an empty URL should also be retried.
    settings = Settings()
    settings.database_source = "/path/to/program"
    with patch.object(Settings, "DB_URL_RETRY_DELAY", new=0):
        with patch("subprocess.check_output") as mock_subprocess:
            mock_subprocess.side_effect = [b"", b"sqlite:///notempty.sqlite"]
            assert settings.database == "sqlite:///notempty.sqlite"
            assert mock_subprocess.call_count == 2

    # Too many retries should raise an exception.
    settings = Settings()
    settings.database_source = "/path/to/program"
    with patch.object(Settings, "DB_URL_RETRY_DELAY", new=0):
        with patch("subprocess.check_output") as mock_subprocess:
            mock_subprocess.return_value = b""
            with pytest.raises(DatabaseSourceException):
                assert settings.database
示例#2
0
def test_database():
    # type: () -> None
    settings = Settings()

    # The default is uninitialized and should throw an error until we load a configuration.
    with pytest.raises(InvalidSettingsError):
        assert settings.database

    # If database is set, it should be used.
    settings.database = "sqlite:///grouper.sqlite"
    assert settings.database == "sqlite:///grouper.sqlite"
    settings.database_source = "/bin/false"
    assert settings.database == "sqlite:///grouper.sqlite"

    # If only database_source is set, it should be run to get a URL.
    settings.database = ""
    settings.database_source = "/path/to/program"
    with patch("subprocess.check_output") as mock_subprocess:
        mock_subprocess.return_value = "sqlite:///other.sqlite\n"
        assert settings.database == "sqlite:///other.sqlite"
        assert mock_subprocess.call_args_list == [
            call(["/path/to/program"], stderr=subprocess.STDOUT)
        ]

    # If the command fails, it should be retried.  Disable the delay to not make the test slow.
    settings = Settings()
    settings.database_source = "/path/to/program"
    with patch.object(Settings, "DB_URL_RETRY_DELAY", new=0):
        with patch("subprocess.check_output") as mock_subprocess:
            exception = subprocess.CalledProcessError(1, "/path/to/program")
            mock_subprocess.side_effect = [exception, "sqlite:///third.sqlite"]
            assert settings.database == "sqlite:///third.sqlite"
            assert mock_subprocess.call_count == 2

    # Commands that return an empty URL should also be retried.
    settings = Settings()
    settings.database_source = "/path/to/program"
    with patch.object(Settings, "DB_URL_RETRY_DELAY", new=0):
        with patch("subprocess.check_output") as mock_subprocess:
            mock_subprocess.side_effect = ["", "sqlite:///notempty.sqlite"]
            assert settings.database == "sqlite:///notempty.sqlite"
            assert mock_subprocess.call_count == 2

    # Too many retries should raise an exception.
    settings = Settings()
    settings.database_source = "/path/to/program"
    with patch.object(Settings, "DB_URL_RETRY_DELAY", new=0):
        with patch("subprocess.check_output") as mock_subprocess:
            mock_subprocess.return_value = ""
            with pytest.raises(DatabaseSourceException):
                assert settings.database
示例#3
0
def test_mask_passsword_in_logs(caplog):
    # type: (LogCaptureFixture) -> None
    settings = Settings()
    settings._logger.setLevel(logging.DEBUG)
    settings.database_source = "/path/to/program"
    test_url = "mysql://*****:*****@example.com:8888/merou"
    expected_url = "mysql://*****:*****@example.com:8888/merou"

    # Reading settings.database will run the external program and trigger the logging.
    with patch("subprocess.check_output") as mock_subprocess:
        mock_subprocess.return_value = b"mysql://*****:*****@example.com:8888/merou"
        assert settings.database == test_url

    assert test_url not in caplog.text
    assert expected_url in caplog.text
示例#4
0
def test_mask_passsword_in_logs(caplog):
    # type: (LogCaptureFixture) -> None
    settings = Settings()
    settings._logger.setLevel(logging.DEBUG)
    settings.database_source = "/path/to/program"
    test_url = "mysql://*****:*****@example.com:8888/merou"
    expected_url = "mysql://*****:*****@example.com:8888/merou"

    # Reading settings.database will run the external program and trigger the logging.
    with patch("subprocess.check_output") as mock_subprocess:
        mock_subprocess.return_value = "mysql://*****:*****@example.com:8888/merou"
        assert settings.database == test_url

    assert test_url not in caplog.text
    assert expected_url in caplog.text
示例#5
0
def test_mask_passsword_in_logs():
    # type: () -> None
    settings = Settings()
    settings.database_source = "/path/to/program"
    test_url = "mysql://*****:*****@example.com:8888/merou"
    expected_url = "mysql://*****:*****@example.com:8888/merou"

    log_output = StringIO()
    log_handler = logging.StreamHandler(log_output)
    settings._logger.addHandler(log_handler)
    settings._logger.setLevel(logging.DEBUG)

    # Reading settings.database will run the external program and trigger the logging.
    with patch("subprocess.check_output") as mock_subprocess:
        mock_subprocess.return_value = "mysql://*****:*****@example.com:8888/merou"
        assert settings.database == test_url

    output = log_output.getvalue()
    assert test_url not in output
    assert expected_url in output