<commit_before>#!/usr/bin/env bash

SCRIPT_DIR_NAME="$( cd "$( dirname "$0" )" && pwd )"

test_assess_vulnerabilities_risk_dot_py_no_command_line_args() {
    DOCKER_IMAGE=${1:-}

    # :ODD: Normally you'd expect the line below to be something like
    # "STDOUT=$(mktemp)" but when that was used the error "The path /var/<something>
    # is not shared from OS X and is not known to Docker" was generated
    # and could not figure out what the problem and hence the current
    # implementation.
    STDOUT=${SCRIPT_DIR_NAME}/stdout.txt

    if ! docker run \
        --rm \
        --entrypoint assess-vulnerabilities-risk.py \
        "${DOCKER_IMAGE}" \
        >& "${STDOUT}"; then
        EXIT_CODE=0
    else
        EXIT_CODE=1

        echo ""
        echo "${FUNCNAME[0]} failed - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
        cat "${STDOUT}"
        echo "${FUNCNAME[0]} failed - <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" 
    fi

    rm -f "${STDOUT}"

    return "${EXIT_CODE}"
}

test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_high_ignore() {
    DOCKER_IMAGE=${1:-}
    VULNERABILITIES_CONTAINER=${2:-}

    # :ODD: Normally you'd expect the line below to be something like
    # "STDOUT=$(mktemp)" but when that was used the error "The path /var/<something>
    # is not shared from OS X and is not known to Docker" was generated
    # and could not figure out what the problem and hence the current
    # implementation.
    STDOUT=${SCRIPT_DIR_NAME}/stdout.txt

    if docker run \
        --rm \
        --volumes-from "${VULNERABILITIES_CONTAINER}" \
        --entrypoint assess-vulnerabilities-risk.py \
        "${DOCKER_IMAGE}" \
        "/vulnerabilities" --log info --whitelist '{"ignoreSevertiesAtOrBelow": "high"}' \
        >& "${STDOUT}"; then
        EXIT_CODE=0
    else
        EXIT_CODE=1

        echo ""
        echo "${FUNCNAME[0]} failed - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
        cat "${STDOUT}"
        echo "${FUNCNAME[0]} failed - <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" 
    fi

    rm -f "${STDOUT}"

    return "${EXIT_CODE}"
}

test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_medium_ignore() {
    DOCKER_IMAGE=${1:-}
    VULNERABILITIES_CONTAINER=${2:-}

    # :ODD: Normally you'd expect the line below to be something like
    # "STDOUT=$(mktemp)" but when that was used the error "The path /var/<something>
    # is not shared from OS X and is not known to Docker" was generated
    # and could not figure out what the problem and hence the current
    # implementation.
    STDOUT=${SCRIPT_DIR_NAME}/stdout.txt

    if ! docker run \
        --rm \
        --volumes-from "${VULNERABILITIES_CONTAINER}" \
        --entrypoint assess-vulnerabilities-risk.py \
        "${DOCKER_IMAGE}" \
        "/vulnerabilities" --log info --whitelist '{"ignoreSevertiesAtOrBelow": "medium"}' \
        >& "${STDOUT}"; then
        EXIT_CODE=0
    else
        EXIT_CODE=1

        echo ""
        echo "${FUNCNAME[0]} failed - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
        cat "${STDOUT}"
        echo "${FUNCNAME[0]} failed - <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" 
    fi

    rm -f "${STDOUT}"

    return "${EXIT_CODE}"
}

test_wrapper() {
    TEST_FUNCTION_NAME=${1:-}
    shift
    NUMBER_TESTS_RUN=$((NUMBER_TESTS_RUN+1))
    echo -n "."
    if "${TEST_FUNCTION_NAME}" "$@"; then
        NUMBER_TEST_SUCCESSES=$((NUMBER_TEST_SUCCESSES+1))
    else
        NUMBER_TEST_FAILURES=$((NUMBER_TEST_FAILURES+1))
    fi
}

if [ $# != 1 ]; then
    echo "usage: $(basename "$0") <docker image>" >&2
    exit 1
fi

DOCKER_IMAGE=${1:-}

VULNERABILITIES_CONTAINER=vulnerabilities-$(openssl rand -hex 8)
docker create \
    -v /vulnerabilities \
    --name "${VULNERABILITIES_CONTAINER}" \
    alpine:3.4 \
    /bin/true \
    > /dev/null
find "${SCRIPT_DIR_NAME}/vulnerabilities/contains-high-severity" -name '*.json' | while IFS='' read -r FILENAME; do
    docker cp -a "${FILENAME}" "${VULNERABILITIES_CONTAINER}:/vulnerabilities"
done

NUMBER_TESTS_RUN=0
NUMBER_TEST_SUCCESSES=0
NUMBER_TEST_FAILURES=0
test_wrapper test_assess_vulnerabilities_risk_dot_py_no_command_line_args "${DOCKER_IMAGE}"
test_wrapper test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_high_ignore "${DOCKER_IMAGE}" "${VULNERABILITIES_CONTAINER}"
test_wrapper test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_medium_ignore "${DOCKER_IMAGE}" "${VULNERABILITIES_CONTAINER}"
echo ""
echo "Successfully completed ${NUMBER_TESTS_RUN} integration tests. ${NUMBER_TEST_SUCCESSES} successes. ${NUMBER_TEST_FAILURES} failures."

docker kill "${VULNERABILITIES_CONTAINER}" >& /dev/null
docker rm "${VULNERABILITIES_CONTAINER}" >& /dev/null

if [[ "${NUMBER_TEST_FAILURES}" != "0" ]]; then
    exit 1
fi

exit 0<commit_msg>chore: clean docker-image-integration-tests.sh output<commit_after>#!/usr/bin/env bash

SCRIPT_DIR_NAME="$( cd "$( dirname "$0" )" && pwd )"

test_assess_vulnerabilities_risk_dot_py_no_command_line_args() {
    DOCKER_IMAGE=${1:-}

    # :ODD: Normally you'd expect the line below to be something like
    # "STDOUT=$(mktemp)" but when that was used the error "The path /var/<something>
    # is not shared from OS X and is not known to Docker" was generated
    # and could not figure out what the problem and hence the current
    # implementation.
    STDOUT=${SCRIPT_DIR_NAME}/stdout.txt

    if ! docker run \
        --rm \
        --entrypoint assess-vulnerabilities-risk.py \
        "${DOCKER_IMAGE}" \
        >& "${STDOUT}"; then
        EXIT_CODE=0
    else
        EXIT_CODE=1

        echo ""
        echo "${FUNCNAME[0]} failed - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
        cat "${STDOUT}"
        echo "${FUNCNAME[0]} failed - <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" 
    fi

    rm -f "${STDOUT}"

    return "${EXIT_CODE}"
}

test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_high_ignore() {
    DOCKER_IMAGE=${1:-}
    VULNERABILITIES_CONTAINER=${2:-}

    # :ODD: Normally you'd expect the line below to be something like
    # "STDOUT=$(mktemp)" but when that was used the error "The path /var/<something>
    # is not shared from OS X and is not known to Docker" was generated
    # and could not figure out what the problem and hence the current
    # implementation.
    STDOUT=${SCRIPT_DIR_NAME}/stdout.txt

    if docker run \
        --rm \
        --volumes-from "${VULNERABILITIES_CONTAINER}" \
        --entrypoint assess-vulnerabilities-risk.py \
        "${DOCKER_IMAGE}" \
        "/vulnerabilities" --log info --whitelist '{"ignoreSevertiesAtOrBelow": "high"}' \
        >& "${STDOUT}"; then
        EXIT_CODE=0
    else
        EXIT_CODE=1

        echo ""
        echo "${FUNCNAME[0]} failed - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
        cat "${STDOUT}"
        echo "${FUNCNAME[0]} failed - <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" 
    fi

    rm -f "${STDOUT}"

    return "${EXIT_CODE}"
}

test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_medium_ignore() {
    DOCKER_IMAGE=${1:-}
    VULNERABILITIES_CONTAINER=${2:-}

    # :ODD: Normally you'd expect the line below to be something like
    # "STDOUT=$(mktemp)" but when that was used the error "The path /var/<something>
    # is not shared from OS X and is not known to Docker" was generated
    # and could not figure out what the problem and hence the current
    # implementation.
    STDOUT=${SCRIPT_DIR_NAME}/stdout.txt

    if ! docker run \
        --rm \
        --volumes-from "${VULNERABILITIES_CONTAINER}" \
        --entrypoint assess-vulnerabilities-risk.py \
        "${DOCKER_IMAGE}" \
        "/vulnerabilities" --log info --whitelist '{"ignoreSevertiesAtOrBelow": "medium"}' \
        >& "${STDOUT}"; then
        EXIT_CODE=0
    else
        EXIT_CODE=1

        echo ""
        echo "${FUNCNAME[0]} failed - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" 
        cat "${STDOUT}"
        echo "${FUNCNAME[0]} failed - <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" 
    fi

    rm -f "${STDOUT}"

    return "${EXIT_CODE}"
}

test_wrapper() {
    TEST_FUNCTION_NAME=${1:-}
    shift
    NUMBER_TESTS_RUN=$((NUMBER_TESTS_RUN+1))
    echo -n "."
    if "${TEST_FUNCTION_NAME}" "$@"; then
        NUMBER_TEST_SUCCESSES=$((NUMBER_TEST_SUCCESSES+1))
    else
        NUMBER_TEST_FAILURES=$((NUMBER_TEST_FAILURES+1))
    fi
}

if [ $# != 1 ]; then
    echo "usage: $(basename "$0") <docker image>" >&2
    exit 1
fi

DOCKER_IMAGE=${1:-}

#
# :TRICKY: creating vulnerability container so the integration
# tests will work on CircleCI - see link below for pattern overview
# https://circleci.com/docs/2.0/building-docker-images/#mounting-folders
#
VULNERABILITIES_CONTAINER=vulnerabilities-$(openssl rand -hex 8)
# explict pull to create opportunity to swallow stdout
docker pull alpine:3.4 > /dev/null
docker create \
    -v /vulnerabilities \
    --name "${VULNERABILITIES_CONTAINER}" \
    alpine:3.4 \
    /bin/true \
    > /dev/null
find "${SCRIPT_DIR_NAME}/vulnerabilities/contains-high-severity" -name '*.json' | while IFS='' read -r FILENAME; do
    docker cp -a "${FILENAME}" "${VULNERABILITIES_CONTAINER}:/vulnerabilities"
done

NUMBER_TESTS_RUN=0
NUMBER_TEST_SUCCESSES=0
NUMBER_TEST_FAILURES=0
test_wrapper test_assess_vulnerabilities_risk_dot_py_no_command_line_args "${DOCKER_IMAGE}"
test_wrapper test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_high_ignore "${DOCKER_IMAGE}" "${VULNERABILITIES_CONTAINER}"
test_wrapper test_assess_vulnerabilities_risk_dot_py_high_risk_inline_whitelist_medium_ignore "${DOCKER_IMAGE}" "${VULNERABILITIES_CONTAINER}"
echo ""
echo "Successfully completed ${NUMBER_TESTS_RUN} integration tests. ${NUMBER_TEST_SUCCESSES} successes. ${NUMBER_TEST_FAILURES} failures."

# :TRICKY: see comment above
docker kill "${VULNERABILITIES_CONTAINER}" >& /dev/null
docker rm "${VULNERABILITIES_CONTAINER}" >& /dev/null

if [[ "${NUMBER_TEST_FAILURES}" != "0" ]]; then
    exit 1
fi

exit 0
{'fraction_non_alphanumeric': 0.21784134751105674, 'fraction_numerical': 0.007151594993883504, 'mean_word_length': 3.6922737306843265, 'pattern_counts': {'":': 4, '<': 563, '<?xml version=': 0, '>': 578, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4734844', 'n_tokens_mistral': 3872, 'n_tokens_neox': 3629, 'n_words': 757}