<commit_before>#!/usr/bin/env bash
# -*- sh-basic-offset: 4 ; indent-tabs-mode: nil -*-

# This is a script for more-or-less automating our smoke tests
# given the scenarios we have currently.
# This should move to our go tests eventually since there is some overlap
# and it's nice to have tests in one place.

# Note that the given step has passsed and bumps global step counter
finish_step() {
    echo "Step ${STEP} success!"
    echo
    ((STEP+=1))
}

# Verify that the output is empty or not depending on args
verify_output_empty() {
    local output=${1}
    local is_empty=${2}
    if $is_empty; then
        if [ -n "$output" ]; then
            echo "Failed in step: ${STEP}"
            echo '> Output was supposed to be empty and it was not'
            exit 1
        fi
    else
        if [ -z "$output" ]; then
            echo "Failed in step: ${STEP}"
            echo "> Output wasn't supposed to be empty and it was"
            exit 1
        fi
    fi
}

# Run telepresence login command + reports output
login() {
    local output
    output=$($TELEPRESENCE login)
    if [[ $output != *"Login successful"* ]]; then
        echo "Login Failed in step: ${STEP}"
        exit 1
    fi
}

# Verify that the user logged out and was logged in prior.
verify_logout() {
    # We care about the error here, so we redirect stderr to stdout
    local output
    output=$($TELEPRESENCE logout 2>&1)
    if [[ $output != *"not logged in"* ]]; then
        echo "Logout Failed in step: ${STEP}"
        exit 1
    fi
}

# Verifies that a Preview URL is or is not in the output
has_preview_url() {
    local present=${1}
    if $present; then
        if [[ "$output" != *"Preview URL"* ]]; then
            echo "Preview URL wasn't present and it should be. Failed step: ${STEP}"
            exit 1
        fi
    else
        if [[ "$output" == *"Preview URL"* ]]; then
            echo "Preview URL was present and it shouldn't be. Failed step: ${STEP}"
            exit 1
        fi
    fi
}

# Verifies that an Intercept ID is or is not in the output
has_intercept_id() {
    local present=${1}
    if $present; then
        if [[ "$output" != *"x-telepresence-intercept-id"* ]]; then
            echo "Intercept id wasn't present and it should be. Failed step: ${STEP}"
            exit 1
        fi
    else
        if [[ "$output" == *"x-telepresence-intercept-id"* ]]; then
            echo "Intercept id was present and it shouldn't be. Failed step: ${STEP}"
            exit 1
        fi
    fi
}

# Puts preview url in a variable
get_preview_url() {
    preview_url=$(echo "$output" | grep -Eo 'https://[^ >]+')
    if [[ -z $preview_url ]]; then
        echo "No Preview URL found"
        exit 1
    fi
}

# Puts workstation api key in a variable
get_workstation_apikey() {
    local cache_file
    case "$(uname)" in
	Darwin)
		cache_file="$HOME/Library/Caches/telepresence/apikeys.json"
		;;
	Linux)
		cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/telepresence/apikeys.json"
		;;
    *)
        echo "OS is unknown by smoke-tests. Update get_workstation_apikey to include default config location for your OS"
        exit 1
        ;;
    esac
    apikey=$(jq -r '.[]|.["telepresence:agent-http"]|strings' "$cache_file")
    if [[ -z $apikey ]]; then
        echo "No apikey found"
        exit 1
    fi
}

# Puts intercept id in a variable
get_intercept_id() {
    local header
    header=$(grep 'x-telepresence-intercept-id' <<<"$output")
    #local header=$(echo $output | grep 'x-telepresence-intercept-id')
    local regex=" ([a-zA-Z0-9-]+:dataprocessingservice)'"
    if [[ $header =~ $regex ]]; then
        interceptid="${BASH_REMATCH[1]}"
    else
        echo "No Intercept ID"
        exit 1
    fi
}

# Checks to see if traffic agent is present + proprietary or dummy based on inputs
is_prop_traffic_agent() {
    local present=${1}
    local image
    image=$(kubectl get deployment dataprocessingservice -o "jsonpath={.spec.template.spec.containers[?(@.name=='traffic-agent')].image}")
    if [[ -z $image ]]; then
        echo "There is no traffic-agent sidecar and there should be"
        exit 1
    fi

    if $present; then
        local image_present
        image_present=$(echo "$image" | grep 'ambassador-telepresence-agent:')
        if [[ -z $image_present ]]; then
            echo "Proprietary traffic agent image wasn't used and it should be"
            exit 1
        elif [[ -n $smart_agent && $image_present != *$smart_agent ]]; then
            echo "Propietary traffic agent was supposed to have been overridden but it wasn't"
            exit 1
        fi
    else
        local image_present
        image_present=$(echo "$image" | grep 'tel2:')
        if [[ -z $image_present ]]; then
            echo "Non-proprietary traffic agent image wasn't used and it should be"
            exit 1
        fi
    fi
}

get_config() {
    if [ -n "$TELEPRESENCE_AGENT_IMAGE" ]; then
        echo "Use images.agentImage in your config.yml to configure the Smart Agent Image to use"
        exit 1
    fi

    case "$(uname)" in
	Darwin)
        if [ test -f "$HOME/Library/ApplicationSupport/telepresence/config.yml" ]
        then
            config_file="$HOME/Library/ApplicationSupport/telepresence/config.yml"
        else
            config_file="$HOME/Library/Application Support/telepresence/config.yml"
        fi
		;;
	Linux)
		config_file="${XDG_CONFIG_HOME:-$HOME/.cache}/telepresence/config.yml"
		;;
    *)
        echo "OS is unknown by smoke-tests. Update get_workstation_apikey to include default config location for your OS"
        exit 1<commit_msg>no brackets<commit_after>#!/usr/bin/env bash
# -*- sh-basic-offset: 4 ; indent-tabs-mode: nil -*-

# This is a script for more-or-less automating our smoke tests
# given the scenarios we have currently.
# This should move to our go tests eventually since there is some overlap
# and it's nice to have tests in one place.

# Note that the given step has passsed and bumps global step counter
finish_step() {
    echo "Step ${STEP} success!"
    echo
    ((STEP+=1))
}

# Verify that the output is empty or not depending on args
verify_output_empty() {
    local output=${1}
    local is_empty=${2}
    if $is_empty; then
        if [ -n "$output" ]; then
            echo "Failed in step: ${STEP}"
            echo '> Output was supposed to be empty and it was not'
            exit 1
        fi
    else
        if [ -z "$output" ]; then
            echo "Failed in step: ${STEP}"
            echo "> Output wasn't supposed to be empty and it was"
            exit 1
        fi
    fi
}

# Run telepresence login command + reports output
login() {
    local output
    output=$($TELEPRESENCE login)
    if [[ $output != *"Login successful"* ]]; then
        echo "Login Failed in step: ${STEP}"
        exit 1
    fi
}

# Verify that the user logged out and was logged in prior.
verify_logout() {
    # We care about the error here, so we redirect stderr to stdout
    local output
    output=$($TELEPRESENCE logout 2>&1)
    if [[ $output != *"not logged in"* ]]; then
        echo "Logout Failed in step: ${STEP}"
        exit 1
    fi
}

# Verifies that a Preview URL is or is not in the output
has_preview_url() {
    local present=${1}
    if $present; then
        if [[ "$output" != *"Preview URL"* ]]; then
            echo "Preview URL wasn't present and it should be. Failed step: ${STEP}"
            exit 1
        fi
    else
        if [[ "$output" == *"Preview URL"* ]]; then
            echo "Preview URL was present and it shouldn't be. Failed step: ${STEP}"
            exit 1
        fi
    fi
}

# Verifies that an Intercept ID is or is not in the output
has_intercept_id() {
    local present=${1}
    if $present; then
        if [[ "$output" != *"x-telepresence-intercept-id"* ]]; then
            echo "Intercept id wasn't present and it should be. Failed step: ${STEP}"
            exit 1
        fi
    else
        if [[ "$output" == *"x-telepresence-intercept-id"* ]]; then
            echo "Intercept id was present and it shouldn't be. Failed step: ${STEP}"
            exit 1
        fi
    fi
}

# Puts preview url in a variable
get_preview_url() {
    preview_url=$(echo "$output" | grep -Eo 'https://[^ >]+')
    if [[ -z $preview_url ]]; then
        echo "No Preview URL found"
        exit 1
    fi
}

# Puts workstation api key in a variable
get_workstation_apikey() {
    local cache_file
    case "$(uname)" in
	Darwin)
		cache_file="$HOME/Library/Caches/telepresence/apikeys.json"
		;;
	Linux)
		cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/telepresence/apikeys.json"
		;;
    *)
        echo "OS is unknown by smoke-tests. Update get_workstation_apikey to include default config location for your OS"
        exit 1
        ;;
    esac
    apikey=$(jq -r '.[]|.["telepresence:agent-http"]|strings' "$cache_file")
    if [[ -z $apikey ]]; then
        echo "No apikey found"
        exit 1
    fi
}

# Puts intercept id in a variable
get_intercept_id() {
    local header
    header=$(grep 'x-telepresence-intercept-id' <<<"$output")
    #local header=$(echo $output | grep 'x-telepresence-intercept-id')
    local regex=" ([a-zA-Z0-9-]+:dataprocessingservice)'"
    if [[ $header =~ $regex ]]; then
        interceptid="${BASH_REMATCH[1]}"
    else
        echo "No Intercept ID"
        exit 1
    fi
}

# Checks to see if traffic agent is present + proprietary or dummy based on inputs
is_prop_traffic_agent() {
    local present=${1}
    local image
    image=$(kubectl get deployment dataprocessingservice -o "jsonpath={.spec.template.spec.containers[?(@.name=='traffic-agent')].image}")
    if [[ -z $image ]]; then
        echo "There is no traffic-agent sidecar and there should be"
        exit 1
    fi

    if $present; then
        local image_present
        image_present=$(echo "$image" | grep 'ambassador-telepresence-agent:')
        if [[ -z $image_present ]]; then
            echo "Proprietary traffic agent image wasn't used and it should be"
            exit 1
        elif [[ -n $smart_agent && $image_present != *$smart_agent ]]; then
            echo "Propietary traffic agent was supposed to have been overridden but it wasn't"
            exit 1
        fi
    else
        local image_present
        image_present=$(echo "$image" | grep 'tel2:')
        if [[ -z $image_present ]]; then
            echo "Non-proprietary traffic agent image wasn't used and it should be"
            exit 1
        fi
    fi
}

get_config() {
    if [ -n "$TELEPRESENCE_AGENT_IMAGE" ]; then
        echo "Use images.agentImage in your config.yml to configure the Smart Agent Image to use"
        exit 1
    fi

    case "$(uname)" in
	Darwin)
        if test -f "$HOME/Library/ApplicationSupport/telepresence/config.yml"
        then
            config_file="$HOME/Library/ApplicationSupport/telepresence/config.yml"
        else
            config_file="$HOME/Library/Application Support/telepresence/config.yml"
        fi
		;;
	Linux)
		config_file="${XDG_CONFIG_HOME:-$HOME/.cache}/telepresence/config.yml"
		;;
    *)
        echo "OS is unknown by smoke-tests. Update get_workstation_apikey to include default config location for your OS"
        exit 1
{'fraction_non_alphanumeric': 0.11291184327693678, 'fraction_numerical': 0.005520926090828139, 'mean_word_length': 2.3645895745955663, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '1051965', 'n_tokens_mistral': 3536, 'n_tokens_neox': 3383, 'n_words': 1192}