Blob Blame History Raw
#!/bin/bash
#
# The 'run' performs a simple test that verifies that S2I image.
# The main focus is that the image prints out the base-usage properly.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
test -n "${IMAGE_NAME-}" || { echo 'make sure $IMAGE_NAME is defined'; exit 1; }

test_docker_run_usage() {
  echo "Testing 'docker run' usage..."
  docker run --rm ${IMAGE_NAME} &>/dev/null
}

test_cgroup_limits() {
  echo "Testing 'cgroup limits' usage..."
  if [ $EUID -eq 0 ]; then
    echo "  The test is running as root, all tests for cgroup limits will be run"

    # check memory limited (only works when running as root)
    echo "  Testing 'limited memory' usage..."
    if ! ( eval $(docker run --rm --memory=512M ${IMAGE_NAME} /usr/bin/cgroup-limits)
           echo "MEMORY_LIMIT_IN_BYTES=$MEMORY_LIMIT_IN_BYTES"
           [ "$MEMORY_LIMIT_IN_BYTES" -eq 536870912 ] ); then
      echo "MEMORY_LIMIT_IN_BYTES not set to 536870912."
      return 1
    fi

    # check cores number (only works when running as root)
    echo "  Testing 'NUMBER_OF_CORES' value..."
    if ! ( eval $(docker run --rm ${IMAGE_NAME} /usr/bin/cgroup-limits)
           echo "NUMBER_OF_CORES=$NUMBER_OF_CORES"
           [ "$NUMBER_OF_CORES" -gt 0 ] ); then
      echo "NUMBER_OF_CORES not set."
      return 1
    fi

    # check cores number (only works when running as root)
    echo "  Testing 'NUMBER_OF_CORES' value with --cpuset-cpus=0..."
    if ! ( eval $(docker run --rm --cpuset-cpus=0 ${IMAGE_NAME} /usr/bin/cgroup-limits)
           echo "NUMBER_OF_CORES=$NUMBER_OF_CORES"
           [ "$NUMBER_OF_CORES" -eq 1 ] ); then
      echo "NUMBER_OF_CORES not set to 1 when set --cpuset-cpus=0."
      return 1
    fi

  else
    echo "  The test is running as non-root, some tests for cgroup limits are skipped"
  fi

  # check NO_MEMORY_LIMIT when no limit is set
  echo "  Testing 'NO_MEMORY_LIMIT' value..."
  if ! ( eval $(docker run --rm ${IMAGE_NAME} /usr/bin/cgroup-limits)
         echo "NO_MEMORY_LIMIT=$NO_MEMORY_LIMIT"
         [ "$NO_MEMORY_LIMIT" == 'true' ] ); then
    echo "NO_MEMORY_LIMIT not set to true."
    return 1
  fi

  # check default memory in bytes
  echo "  Testing 'MEMORY_LIMIT_IN_BYTES' value..."
  if ! ( eval $(docker run --rm ${IMAGE_NAME} /usr/bin/cgroup-limits)
         echo "MEMORY_LIMIT_IN_BYTES=$MEMORY_LIMIT_IN_BYTES"
				 # This value can be different, but it must be very big (comparing to 10TB)
         [ "$MEMORY_LIMIT_IN_BYTES" -gt 10000000000000 ] ); then
    echo "MEMORY_LIMIT_IN_BYTES not greater than 10000000000000."
    return 1
  fi
}

check_result() {
  local result="$1"
  if [[ "$result" != "0" ]]; then
    echo "S2I image '${IMAGE_NAME}' test FAILED (exit code: ${result})"
    exit $result
  fi
}

# Verify the 'usage' script is working properly when running the base image with 'docker run ...'
test_docker_run_usage
check_result $?

# Verify the cgroup-limits script works as expected
test_cgroup_limits
check_result $?

echo "Tests for '${IMAGE_NAME}' succeeded."

# vim: set tabstop=2:shiftwidth=2:expandtab: