def test_fail_pipeline_invalid_template_filename(caplog, monkeypatch,
                                                 tmp_path):
    """
    GIVEN `_fail_pipeline` is called
    WHEN `FAIL_PIPELINE_CFN` env var is `enabled` but an invalid filename is passed in
    THEN exit with an error of 1
    """
    d = tmp_path / "sub"
    d.mkdir()
    template_file_path = d / "x.txt"

    # override default valid template file path
    monkeypatch.setenv("CFN_TEMPLATE_FILE_LOCATION", str(template_file_path))

    monkeypatch.setenv("FAIL_PIPELINE_CFN", "enabled")

    c = CcValidator()

    with pytest.raises(SystemExit):
        c._fail_pipeline("")

    assert "Unknown file extension for template" in caplog.text
def test_fail_pipeline_enabled():
    """
    GIVEN `_fail_pipeline` is called
    WHEN `FAIL_PIPELINE` is anything other than `disabled`
    THEN return `False` (pipeline will fail when issues are found)
    """

    c = CcValidator()

    # real template not required for testing
    fail_pipeline = c._fail_pipeline("")

    assert fail_pipeline is True
def test_fail_pipeline_disabled(monkeypatch):
    """
    GIVEN `_fail_pipeline` is called
    WHEN `FAIL_PIPELINE` is `disabled`
    THEN return `False` (pipeline won't fail even if issues are found)
    """

    monkeypatch.setenv("FAIL_PIPELINE", "disabled")

    c = CcValidator()

    # real template not required for testing
    fail_pipeline = c._fail_pipeline("")

    assert fail_pipeline is False
def test_fail_pipeline_template_files(monkeypatch,
                                      disabled_failed_pipeline_templates):
    """
    GIVEN `_fail_pipeline` is called
    WHEN `FAIL_PIPELINE_CFN` env var is `enabled` and `FailConformityPipeline` CFN parameter is `disabled`
    THEN return `False` (pipeline won't fail even if issues are found)
    """

    # override default valid template file path
    monkeypatch.setenv("CFN_TEMPLATE_FILE_LOCATION",
                       disabled_failed_pipeline_templates)

    monkeypatch.setenv("FAIL_PIPELINE_CFN", "enabled")

    with open(disabled_failed_pipeline_templates, "r") as f:
        cfn_contents = f.read()

    c = CcValidator()
    fail_pipeline = c._fail_pipeline(cfn_contents)
    assert fail_pipeline is False