Beispiel #1
0
def test_ensure_latest_bundle_to_update_http_error():
    """
    If an HTTP error happens during a bundle update, print a friendly
    error message and exit 1.
    """
    tags_data = {TEST_BUNDLE_NAME: "12345"}
    with mock.patch("circup.Bundle.latest_tag", "54321"), mock.patch(
        #         "circup.tags_data_load", return_value=tags_data
        #     ), mock.patch(
        "circup.os.path.isfile",
        return_value=True,
    ), mock.patch("circup.open"), mock.patch(
        "circup.get_bundle", side_effect=requests.exceptions.HTTPError("404")
    ) as mock_gb, mock.patch(
        "circup.json"
    ) as mock_json, mock.patch(
        "circup.click.secho"
    ) as mock_click, mock.patch(
        "circup.sys.exit"
    ) as mock_exit:
        circup.Bundle.tags_data = dict()
        mock_json.load.return_value = tags_data
        bundle = circup.Bundle(TEST_BUNDLE_NAME)
        circup.ensure_latest_bundle(bundle)
        mock_gb.assert_called_once_with(bundle, "54321")
        assert mock_json.dump.call_count == 0  # not saved.
        assert mock_click.call_count == 1  # friendly message.
        mock_exit.assert_called_once_with(1)  # exit 1.
Beispiel #2
0
def test_ensure_latest_bundle_no_bundle_data():
    """
    If there's no BUNDLE_DATA file (containing previous current version of the
    bundle) then default to update.
    """
    with mock.patch("circup.get_latest_tag", return_value="12345"), mock.patch(
            "circup.os.path.isfile", return_value=False), mock.patch(
                "circup.get_bundle") as mock_gb, mock.patch(
                    "circup.json") as mock_json:
        circup.ensure_latest_bundle()
        mock_gb.assert_called_once_with("12345")
        assert mock_json.dump.call_count == 1  # Current version saved to file.
Beispiel #3
0
def test_ensure_latest_bundle_to_update():
    """
    If the version found in the BUNDLE_DATA is out of date, then cause an
    update to the bundle.
    """
    with mock.patch("circup.get_latest_tag", return_value="54321"), mock.patch(
            "circup.os.path.isfile",
            return_value=True), mock.patch("circup.open"), mock.patch(
                "circup.get_bundle") as mock_gb, mock.patch(
                    "circup.json") as mock_json:
        mock_json.load.return_value = {"tag": "12345"}
        circup.ensure_latest_bundle()
        mock_gb.assert_called_once_with("54321")
        assert mock_json.dump.call_count == 1  # Current version saved to file.
Beispiel #4
0
def test_ensure_latest_bundle_no_update():
    """
    If the version found in the BUNDLE_DATA is NOT out of date, just log the
    fact and don't update.
    """
    with mock.patch("circup.get_latest_tag", return_value="12345"), mock.patch(
            "circup.os.path.isfile",
            return_value=True), mock.patch("circup.open"), mock.patch(
                "circup.get_bundle") as mock_gb, mock.patch(
                    "circup.json") as mock_json, mock.patch(
                        "circup.logger") as mock_logger:
        mock_json.load.return_value = {"tag": "12345"}
        circup.ensure_latest_bundle()
        assert mock_gb.call_count == 0
        assert mock_logger.info.call_count == 2
Beispiel #5
0
def test_ensure_latest_bundle_to_update_http_error():
    """
    If an HTTP error happens during a bundle update, print a friendly
    error message and exit 1.
    """
    with mock.patch("circup.get_latest_tag", return_value="54321"), mock.patch(
            "circup.os.path.isfile",
            return_value=True), mock.patch("circup.open"), mock.patch(
                "circup.get_bundle",
                side_effect=requests.exceptions.HTTPError("404")
            ) as mock_gb, mock.patch("circup.json") as mock_json, mock.patch(
                "circup.click.secho") as mock_click, mock.patch(
                    "circup.sys.exit") as mock_exit:
        mock_json.load.return_value = {"tag": "12345"}
        circup.ensure_latest_bundle()
        mock_gb.assert_called_once_with("54321")
        assert mock_json.dump.call_count == 0  # not saved.
        mock_click.call_count == 1  # friendly message.
        mock_exit.assert_called_once_with(1)  # exit 1.
Beispiel #6
0
def test_ensure_latest_bundle_bad_bundle_data():
    """
    If there's a BUNDLE_DATA file (containing previous current version of the
    bundle) but it has been corrupted (which has sometimes happened during
    manual testing) then default to update.
    """
    with mock.patch("circup.get_latest_tag", return_value="12345"), mock.patch(
            "circup.os.path.isfile",
            return_value=True), mock.patch("circup.open"), mock.patch(
                "circup.get_bundle") as mock_gb, mock.patch(
                    "circup.json.load",
                    side_effect=json.decoder.JSONDecodeError(
                        "BANG!", "doc", 1),
                ), mock.patch("circup.json.dump"), mock.patch(
                    "circup.logger") as mock_logger:
        circup.ensure_latest_bundle()
        mock_gb.assert_called_once_with("12345")
        assert mock_logger.error.call_count == 1
        assert mock_logger.exception.call_count == 1
Beispiel #7
0
def test_ensure_latest_bundle_to_update_http_error():
    # pylint: disable=pointless-statement
    # 2nd to last line is deemed pointless.
    # Don't understand but for now this is an accpetable workaround
    # Tracking issue will be opened.
    """
    If an HTTP error happens during a bundle update, print a friendly
    error message and exit 1.
    """
    with mock.patch("circup.get_latest_tag", return_value="54321"), mock.patch(
            "circup.os.path.isfile",
            return_value=True), mock.patch("circup.open"), mock.patch(
                "circup.get_bundle",
                side_effect=requests.exceptions.HTTPError("404")
            ) as mock_gb, mock.patch("circup.json") as mock_json, mock.patch(
                "circup.click.secho") as mock_click, mock.patch(
                    "circup.sys.exit") as mock_exit:
        mock_json.load.return_value = {"tag": "12345"}
        circup.ensure_latest_bundle()
        mock_gb.assert_called_once_with("54321")
        assert mock_json.dump.call_count == 0  # not saved.
        mock_click.call_count == 1  # friendly message.
        mock_exit.assert_called_once_with(1)  # exit 1.