Skip to content

Commit

Permalink
Add ct_obtain_input function to get file/dir/remote content
Browse files Browse the repository at this point in the history
Other small fixes to make the code less error-prone
  • Loading branch information
hhorak committed Dec 15, 2017
1 parent 3dee4ad commit 64ba4ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
46 changes: 39 additions & 7 deletions test-lib-openshift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function ct_get_public_ip() {
fi
done
if [ -z "${public_ip}" ] ; then
echo "ERROR: public IP could not be guessed."
echo "ERROR: public IP could not be guessed." >&2
return 1
fi
echo "${public_ip}"
Expand Down Expand Up @@ -158,7 +158,7 @@ function ct_os_wait_rc_ready() {
function ct_os_deploy_pure_image() {
local image="${1}" ; shift
# ignore error exit code, because oc new-app returns error when image exists
oc new-app ${image} $@ || :
oc new-app ${image} "$@" || :
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
Expand All @@ -174,7 +174,7 @@ function ct_os_deploy_s2i_image() {
local image="${1}" ; shift
local app="${1}" ; shift
# ignore error exit code, because oc new-app returns error when image exists
oc new-app ${image}~${app} $@ || :
oc new-app "${image}~${app}" "$@" || :

# let openshift cluster to sync to avoid some race condition errors
sleep 3
Expand All @@ -194,7 +194,7 @@ function ct_os_deploy_s2i_image() {
# MYSQL_DATABASE=testdb
function ct_os_deploy_template_image() {
local template="${1}" ; shift
oc process -f "${template}" $@ | oc create -f -
oc process -f "${template}" "$@" | oc create -f -
# let openshift cluster to sync to avoid some race condition errors
sleep 3
}
Expand Down Expand Up @@ -343,15 +343,31 @@ function ct_os_test_s2i_app_func() {
local service_name="${image_name_no_namespace}-testing"
local image_tagged="${image_name_no_namespace}:testing"

if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_s2i_app_func() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi

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_tagged}"

ct_os_deploy_s2i_image "${image_tagged}" "${app}" \
local app_param="${app}"
if [ -d "${app}" ] ; then
# for local directory, we need to copy the content, otherwise too smart os command
# pulls the git remote repository instead
app_param=$(ct_obtain_input "${app}")
fi

ct_os_deploy_s2i_image "${image_tagged}" "${app_param}" \
--context-dir="${context_dir}" \
--name "${service_name}" \
${oc_args}

if [ -d "${app}" ] ; then
oc start-build "${service_name}" --from-dir="${app_param}"
fi

ct_os_wait_pod_ready "${service_name}" 300

local ip=$(ct_os_get_service_ip "${service_name}")
Expand Down Expand Up @@ -385,6 +401,11 @@ function ct_os_test_s2i_app() {
local response_code=${7:-200}
local oc_args=${8:-}

if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_s2i_app() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi

ct_os_test_s2i_app_func "${image_name}" \
"${app}" \
"${context_dir}" \
Expand All @@ -411,19 +432,25 @@ function ct_os_test_template_app_func() {
local check_command=${4}
local oc_args=${5:-}

if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_template_app_func() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi

local service_name="${name_in_template}-testing"
local image_tagged="${name_in_template}:${VERSION}"

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_tagged}"

oc new-app ${template} \
local local_template=$(ct_obtain_input "${template}")
oc new-app ${local_template} \
-p NAME="${service_name}" \
-p NAMESPACE="$(oc project -q)" \
${oc_args}

oc start-build ${service_name}
oc start-build "${service_name}"

ct_os_wait_pod_ready "${service_name}" 300

Expand Down Expand Up @@ -459,6 +486,11 @@ function ct_os_test_template_app() {
local response_code=${7:-200}
local oc_args=${8:-}

if [ $# -lt 4 ] || [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
echo "ERROR: ct_os_test_template_app() requires at least 4 arguments that cannot be emtpy." >&2
return 1
fi

ct_os_test_template_app_func "${image_name}" \
"${template}" \
"${name_in_template}" \
Expand Down
38 changes: 33 additions & 5 deletions test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,17 @@ function ct_scl_usage_old() {
: " Testing the image SCL enable"
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${command}")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${command}"] Expected '${expected}', got '${out}'"
echo "ERROR[/bin/bash -c "${command}"] Expected '${expected}', got '${out}'" >&2
return 1
fi
out=$(docker exec $(ct_get_cid $name) /bin/bash -c "${command}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${command}"] Expected '${expected}', got '${out}'"
echo "ERROR[exec /bin/bash -c "${command}"] Expected '${expected}', got '${out}'" >&2
return 1
fi
out=$(docker exec $(ct_get_cid $name) /bin/sh -ic "${command}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${command}"] Expected '${expected}', got '${out}'"
echo "ERROR[exec /bin/sh -ic "${command}"] Expected '${expected}', got '${out}'" >&2
return 1
fi
}
Expand All @@ -192,14 +192,14 @@ function ct_doc_content_old() {
# Check whether the files contain some important information
for term in $@ ; do
if ! cat ${tmpdir}/$(basename ${f}) | grep -F -q -e "${term}" ; then
echo "ERROR: File /${f} does not include '${term}'."
echo "ERROR: File /${f} does not include '${term}'." >&2
return 1
fi
done
done
# Check whether the files use the correct format
if ! file ${tmpdir}/help.1 | grep -q roff ; then
echo "ERROR: /help.1 is not in troff or groff format"
echo "ERROR: /help.1 is not in troff or groff format" >&2
return 1
fi
: " Success!"
Expand Down Expand Up @@ -267,6 +267,34 @@ ct_gen_self_signed_cert_pem() {
openssl req -new -x509 -nodes -key ${output_dir}/${base_name}-key.pem -batch > ${output_dir}/${base_name}-cert-selfsigned.pem
}

# ct_obtain_input FILE|DIR|URL
# --------------------
# Either copies a file or a directory to a tmp location for local copies, or
# downloads the file from remote location.
# Resulted file path is printed, so it can be later used by calling function.
# Arguments: input - local file, directory or remote URL
function ct_obtain_input() {
local input=$1
local extension="${input##*.}"

# Try to use same extension for the temporary file if possible
[[ "${extension}" =~ ^[a-z0-9]*$ ]] && extension=".${extension}" || extension=""

local output=$(mktemp "/var/tmp/test-input-XXXXXX$extension")
if [ -f "${input}" ] ; then
cp "${input}" "${output}"
elif [ -d "${input}" ] ; then
rm -f "${output}"
cp -r -LH "${input}" "${output}"
elif echo "${input}" | grep -qe '^http\(s\)\?://' ; then
curl "${input}" > "${output}"
else
echo "ERROR: file type not known: ${input}" >&2
return 1
fi
echo "${output}"
}

# ct_test_response
# ----------------
# Perform GET request to the application container, checks output with
Expand Down

0 comments on commit 64ba4ce

Please sign in to comment.