<commit_before>#!/bin/bash
#
# Functions for tests for the MariaDB image in OpenShift.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#

THISDIR=$(dirname ${BASH_SOURCE[0]})

source ${THISDIR}/test-lib.sh
source ${THISDIR}/test-lib-openshift.sh
source ${THISDIR}/test-lib-remote-openshift.sh

function check_mysql_os_service_connection() {
  local util_image_name="${1}" ; shift
  local service_name="${1}" ; shift
  local user="${1}" ; shift
  local pass="${1}" ; shift
  local timeout="${1:-60}" ; shift || :
  local pod_ip=$(ct_os_get_service_ip ${service_name})

  : "  Service ${service_name} check ..."

  local cmd="echo 'SELECT 42 as testval\g' | mysql --connect-timeout=15 -h ${pod_ip} -u${user} -p${pass}"
  local expected_value='^42'
  local output
  local ret
  SECONDS=0

  echo -n "Waiting for ${service_name} service becoming ready ..."
  while true ; do
    output=$(docker run --rm ${util_image_name} bash -c "${cmd}" || :)
    echo "${output}" | grep -qe "${expected_value}" && ret=0 || ret=1
    if [ ${ret} -eq 0 ] ; then
      echo " PASS"
      return 0
    fi
    echo -n "."
    [ ${SECONDS} -gt ${timeout} ] && break
    sleep 3
  done
  echo " FAIL"
  return 1
}

function test_mysql_pure_image() {
  local image_name=${1:-quay.io/centos7/mariadb-103-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  # Create a specific imagestream tag for the image so that oc cannot use anything else
  ct_os_upload_image "${image_name}" "$image_name_no_namespace"

  ct_os_deploy_pure_image "$image_name_no_namespace" \
                          --name "${service_name}" \
                          --env MYSQL_ROOT_PASSWORD=<PASSWORD>

  ct_os_wait_pod_ready "${service_name}" 60
  check_mysql_os_service_connection "${image_name}" "${service_name}" root test

  ct_os_delete_project
}

function test_mysql_template() {
  local image_name=${1:-quay.io/centos7/mariadb-103-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  ct_os_upload_image "${image_name}" "mariadb:$VERSION"

  ct_os_deploy_template_image ${THISDIR}/mariadb-ephemeral-template.json \
                              NAMESPACE="$(oc project -q)" \
                              MARIADB_VERSION="$VERSION" \
                              DATABASE_SERVICE_NAME="${service_name}" \
                              MYSQL_USER=testu \
                              MYSQL_PASSWORD=<PASSWORD> \
                              MYSQL_DATABASE=testdb

  ct_os_wait_pod_ready "${service_name}" 60
  check_mysql_os_service_connection "${image_name}" "${service_name}" testu testp

  ct_os_delete_project
}

function test_mysql_s2i() {
  local image_name=${1:-quay.io/centos7/mariadb-103-centos7}
  local app=${2:-https://github.com/sclorg/mariadb-container.git}
  local context_dir=${3:-test/test-app}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  # Create a specific imagestream tag for the image so that oc cannot use anything else
  ct_os_upload_image "${image_name}" "$image_name_no_namespace"

  ct_os_deploy_s2i_image "$image_name_no_namespace" "${app}" \
                          --context-dir="${context_dir}" \
                          --name "${service_name}" \
                          --env MYSQL_ROOT_PASSWORD=<PASSWORD> \
                          --env MYSQL_OPERATIONS_USER=testo \
                          --env MYSQL_OPERATIONS_PASSWORD=<PASSWORD> \
                          --env MYSQL_DATABASE=testopdb \
                          --env MYSQL_USER=testnormal \
                          --env MYSQL_PASSWORD=<PASSWORD>

  ct_os_wait_pod_ready "${service_name}" 60
  check_mysql_os_service_connection "${image_name}" "${service_name}" testo testo 120

  ct_os_delete_project
}

function test_mariadb_integration() {
  local service_name=mariadb
  ct_os_template_exists mariadb-ephemeral && t=mariadb-ephemeral || t=mariadb-persistent
  ct_os_test_template_app_func "${IMAGE_NAME}" \
                               "${t}" \
                               "${service_name}" \
                               "ct_os_check_cmd_internal '<SAME_IMAGE>' '${service_name}-testing' \"echo 'SELECT 42 as testval\g' | mysql --connect-timeout=15 -h <IP> testdb -utestu -ptestp\" '^42' 120" \
                               "-p MARIADB_VERSION=${VERSION} \
                                -p DATABASE_SERVICE_NAME="${service_name}-testing" \
                                -p MYSQL_USER=testu \
                                -p MYSQL_PASSWORD=<PASSWORD> \
                                -p MYSQL_DATABASE=testdb"
}


# Check the imagestream
function test_mariadb_imagestream() {
  case ${OS} in
    rhel7|centos7) ;;
    *) echo "Imagestream testing not supported for $OS environment." ; return 0 ;;
  esac
  local tag="-el7"
  if [ "${OS}" == "rhel8" ]; then
    tag="-el8"
  fi
  # Try pulling the image first to see if it is accessible
  PUBLIC_IMAGE_NAME=${PUBLIC_IMAGE_NAME:-$(ct_get_public_image_name "${OS}" "${BASE_IMAGE_NAME}" "${VERSION}")}
  if docker pull "${PUBLIC_IMAGE_NAME}"; then
    ct_os_test_image_stream_template "${THISDIR}/../imagestreams/mariadb-${OS%[0-9]*}.json" "${THISDIR}/../examples/mariadb-ephemeral-template.json" mariadb "-p MARIADB_VERSION=${VERSION}${tag}"
  else
    echo "Warning: ${PUBLIC_IMAGE_NAME} could not be downloaded via 'docker'"
    # ignore possible failure of this test for centos images
    [ "${OS}" == "rhel7" ] && false "ERROR: Failed to pull image"
  fi
}
function test_mariadb_template() {
  if [[ "${OS}" =~ rhel7 ]] || [[ "${OS}" =~ centos7 ]] ; then
    ct_os_test_image_stream_template "${THISDIR}/imagestreams/mariadb-${OS%[0-9]*}.json" "${THISDIR}/mariadb-ephemer
  al-template.json" mariadb
  fi
}

# Check the latest imagestreams
function run_latest_imagestreams() {
  local result=1
  # Switch to root directory of a container
  echo "Testing the latest version in imagestreams"
  pushd "${THISDIR}/../.." >/dev/null || return 1
  ct_check_latest_imagestreams
  result=$?
  popd >/dev/null || return 1
  return $result
}
# vim: set tabstop=2:shiftwidth=2:expandtab:<commit_msg>test os templates with rhel8<commit_after>#!/bin/bash
#
# Functions for tests for the MariaDB image in OpenShift.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#

THISDIR=$(dirname ${BASH_SOURCE[0]})

source ${THISDIR}/test-lib.sh
source ${THISDIR}/test-lib-openshift.sh
source ${THISDIR}/test-lib-remote-openshift.sh

function check_mysql_os_service_connection() {
  local util_image_name="${1}" ; shift
  local service_name="${1}" ; shift
  local user="${1}" ; shift
  local pass="${1}" ; shift
  local timeout="${1:-60}" ; shift || :
  local pod_ip=$(ct_os_get_service_ip ${service_name})

  : "  Service ${service_name} check ..."

  local cmd="echo 'SELECT 42 as testval\g' | mysql --connect-timeout=15 -h ${pod_ip} -u${user} -p${pass}"
  local expected_value='^42'
  local output
  local ret
  SECONDS=0

  echo -n "Waiting for ${service_name} service becoming ready ..."
  while true ; do
    output=$(docker run --rm ${util_image_name} bash -c "${cmd}" || :)
    echo "${output}" | grep -qe "${expected_value}" && ret=0 || ret=1
    if [ ${ret} -eq 0 ] ; then
      echo " PASS"
      return 0
    fi
    echo -n "."
    [ ${SECONDS} -gt ${timeout} ] && break
    sleep 3
  done
  echo " FAIL"
  return 1
}

function test_mysql_pure_image() {
  local image_name=${1:-quay.io/centos7/mariadb-103-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  # Create a specific imagestream tag for the image so that oc cannot use anything else
  ct_os_upload_image "${image_name}" "$image_name_no_namespace"

  ct_os_deploy_pure_image "$image_name_no_namespace" \
                          --name "${service_name}" \
                          --env MYSQL_ROOT_PASSWORD=<PASSWORD>

  ct_os_wait_pod_ready "${service_name}" 60
  check_mysql_os_service_connection "${image_name}" "${service_name}" root test

  ct_os_delete_project
}

function test_mysql_template() {
  local image_name=${1:-quay.io/centos7/mariadb-103-centos7}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  ct_os_upload_image "${image_name}" "mariadb:$VERSION"

  ct_os_deploy_template_image ${THISDIR}/mariadb-ephemeral-template.json \
                              NAMESPACE="$(oc project -q)" \
                              MARIADB_VERSION="$VERSION" \
                              DATABASE_SERVICE_NAME="${service_name}" \
                              MYSQL_USER=testu \
                              MYSQL_PASSWORD=<PASSWORD> \
                              MYSQL_DATABASE=testdb

  ct_os_wait_pod_ready "${service_name}" 60
  check_mysql_os_service_connection "${image_name}" "${service_name}" testu testp

  ct_os_delete_project
}

function test_mysql_s2i() {
  local image_name=${1:-quay.io/centos7/mariadb-103-centos7}
  local app=${2:-https://github.com/sclorg/mariadb-container.git}
  local context_dir=${3:-test/test-app}
  local image_name_no_namespace=${image_name##*/}
  local service_name="${image_name_no_namespace%%:*}-testing"

  ct_os_new_project
  # Create a specific imagestream tag for the image so that oc cannot use anything else
  ct_os_upload_image "${image_name}" "$image_name_no_namespace"

  ct_os_deploy_s2i_image "$image_name_no_namespace" "${app}" \
                          --context-dir="${context_dir}" \
                          --name "${service_name}" \
                          --env MYSQL_ROOT_PASSWORD=<PASSWORD> \
                          --env MYSQL_OPERATIONS_USER=testo \
                          --env MYSQL_OPERATIONS_PASSWORD=<PASSWORD> \
                          --env MYSQL_DATABASE=testopdb \
                          --env MYSQL_USER=testnormal \
                          --env MYSQL_PASSWORD=<PASSWORD>

  ct_os_wait_pod_ready "${service_name}" 60
  check_mysql_os_service_connection "${image_name}" "${service_name}" testo testo 120

  ct_os_delete_project
}

function test_mariadb_integration() {
  local service_name=mariadb
  ct_os_template_exists mariadb-ephemeral && t=mariadb-ephemeral || t=mariadb-persistent
  ct_os_test_template_app_func "${IMAGE_NAME}" \
                               "${t}" \
                               "${service_name}" \
                               "ct_os_check_cmd_internal '<SAME_IMAGE>' '${service_name}-testing' \"echo 'SELECT 42 as testval\g' | mysql --connect-timeout=15 -h <IP> testdb -utestu -ptestp\" '^42' 120" \
                               "-p MARIADB_VERSION=${VERSION} \
                                -p DATABASE_SERVICE_NAME="${service_name}-testing" \
                                -p MYSQL_USER=testu \
                                -p MYSQL_PASSWORD=<PASSWORD> \
                                -p MYSQL_DATABASE=testdb"
}

# Check the imagestream
function test_mariadb_imagestream() {
  case ${OS} in
    rhel7|centos7) ;;
    *) echo "Imagestream testing not supported for $OS environment." ; return 0 ;;
  esac
  local tag="-el7"
  if [ "${OS}" == "rhel8" ]; then
    tag="-el8"
  fi
  # Try pulling the image first to see if it is accessible
  PUBLIC_IMAGE_NAME=${PUBLIC_IMAGE_NAME:-$(ct_get_public_image_name "${OS}" "${BASE_IMAGE_NAME}" "${VERSION}")}
  if docker pull "${PUBLIC_IMAGE_NAME}"; then
    ct_os_test_image_stream_template "${THISDIR}/../imagestreams/mariadb-${OS%[0-9]*}.json" "${THISDIR}/../examples/mariadb-ephemeral-template.json" mariadb "-p MARIADB_VERSION=${VERSION}${tag}"
  else
    echo "Warning: ${PUBLIC_IMAGE_NAME} could not be downloaded via 'docker'"
    # ignore possible failure of this test for centos images
    [ "${OS}" == "rhel7" ] && false "ERROR: Failed to pull image"
  fi
}

function test_mariadb_template() {
  if [[ "${OS}" =~ rhel7 ]] || [[ "${OS}" =~ centos7 ]] || [[ "${OS}" =~ rhel8 ]] ; then
    ct_os_test_image_stream_template "${THISDIR}/imagestreams/mariadb-${OS%[0-9]*}.json" "${THISDIR}/mariadb-ephemer
  al-template.json" mariadb
  fi
}

# Check the latest imagestreams
function run_latest_imagestreams() {
  local result=1
  # Switch to root directory of a container
  echo "Testing the latest version in imagestreams"
  pushd "${THISDIR}/../.." >/dev/null || return 1
  ct_check_latest_imagestreams
  result=$?
  popd >/dev/null || return 1
  return $result
}
# vim: set tabstop=2:shiftwidth=2:expandtab:
{'fraction_non_alphanumeric': 0.13179867342957471, 'fraction_numerical': 0.012485368708544674, 'mean_word_length': 2.854436090225564, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3674132', 'n_tokens_mistral': 4441, 'n_tokens_neox': 4207, 'n_words': 879}