Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate bats-assert + fix SC issues in test #194

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Integrate bats-assert + fix SC issues in test
  • Loading branch information
timja committed Sep 4, 2021
commit 2241eab0aafdf61acb81e39ec93902d66da95e58
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "tests/test_helper/bats-assert"]
path = tests/test_helper/bats-assert
url = https://github.com/bats-core/bats-assert.git
timja marked this conversation as resolved.
Show resolved Hide resolved
[submodule "tests/test_helper/bats-support"]
path = tests/test_helper/bats-support
url = https://github.com/bats-core/bats-support.git
timja marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions tests/test_helper/bats-assert
Submodule bats-assert added at 672ad1
1 change: 1 addition & 0 deletions tests/test_helper/bats-support
Submodule bats-support added at d140a6
6 changes: 3 additions & 3 deletions tests/test_helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function retry {
}

function is_agent_container_running {
test -n "${1}"
sleep 1
retry 3 1 assert "true" docker inspect -f '{{.State.Running}}' "${1}"
test -n "${1}"
sleep 1
retry 3 1 assert "true" docker inspect -f '{{.State.Running}}' "${1}"
}
53 changes: 28 additions & 25 deletions tests/tests.bats
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/usr/bin/env bats

load test_helpers
load 'test_helper/bats-support/load' # this is required by bats-assert!
load 'test_helper/bats-assert/load'

REGEX='^([0-9]+)/(.+)$'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused variable


IMAGE=${IMAGE:-debian_jdk11}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supply a default value for when running outside of make, (like in IDE)

SUT_IMAGE=$(get_sut_image)

ARCH=${ARCH:-x86_64}

@test "[${SUT_IMAGE}] checking image metadata" {
local VOLUMES_MAP
VOLUMES_MAP="$(docker inspect -f '{{.Config.Volumes}}' ${SUT_IMAGE})"
VOLUMES_MAP="$(docker inspect -f '{{.Config.Volumes}}' "${SUT_IMAGE}")"

echo "${VOLUMES_MAP}" | grep '/home/jenkins/.jenkins'
echo "${VOLUMES_MAP}" | grep '/home/jenkins/agent'
Expand All @@ -22,7 +23,7 @@ ARCH=${ARCH:-x86_64}
else
run docker run --rm "${SUT_IMAGE}" locale charmap
fi
[ "${output}" = "UTF-8" ]
assert_equal "${output}" "UTF-8"
}

@test "[${SUT_IMAGE}] image has bash and java installed and in the PATH" {
Expand All @@ -32,19 +33,19 @@ ARCH=${ARCH:-x86_64}
is_agent_container_running "${cid}"

run docker exec "${cid}" sh -c "command -v bash"
[ "${status}" -eq 0 ]
assert_success
run docker exec "${cid}" bash --version
[ "${status}" -eq 0 ]
assert_success
run docker exec "${cid}" sh -c "command -v java"
[ "${status}" -eq 0 ]
assert_success

run docker exec "${cid}" sh -c "java -version"
[ "${status}" -eq 0 ]
assert_success

run docker exec "${cid}" sh -c "printenv | grep AGENT_WORKDIR"
[ "AGENT_WORKDIR=/home/jenkins/agent" = "${output}" ]
assert_equal "${output}" "AGENT_WORKDIR=/home/jenkins/agent"

cleanup $cid
cleanup "$cid"
}

@test "[${SUT_IMAGE}] check user access to folders" {
Expand All @@ -54,15 +55,15 @@ ARCH=${ARCH:-x86_64}
is_agent_container_running "${cid}"

run docker exec "${cid}" touch /home/jenkins/a
[ "${status}" -eq 0 ]
assert_success

run docker exec "${cid}" touch /home/jenkins/.jenkins/a
[ "${status}" -eq 0 ]
assert_success

run docker exec "${cid}" touch /home/jenkins/agent/a
[ "${status}" -eq 0 ]
assert_success

cleanup $cid
cleanup "$cid"
}

@test "[${SUT_IMAGE}] use build args correctly" {
Expand All @@ -76,6 +77,8 @@ ARCH=${ARCH:-x86_64}
local TEST_AGENT_WORKDIR="/home/test-user/something"
local sut_image="${SUT_IMAGE}-tests-${BATS_TEST_NUMBER}"

# false positive detecting platform
# shellcheck disable=SC2140
docker buildx bake \
--set "${IMAGE}".args.VERSION="${TEST_VERSION}" \
--set "${IMAGE}".args.user="${TEST_USER}" \
Expand All @@ -94,34 +97,34 @@ docker buildx bake \
is_agent_container_running "${cid}"

run docker exec "${cid}" sh -c "java -cp /usr/share/jenkins/agent.jar hudson.remoting.jnlp.Main -version"
[ "${TEST_VERSION}" = "${lines[0]}" ]
assert_line --index 0 "${TEST_VERSION}"

run docker exec "${cid}" sh -c "id -u -n ${TEST_USER}"
[ "${TEST_USER}" = "${lines[0]}" ]
assert_line --index 0 "${TEST_USER}"

run docker exec "${cid}" sh -c "id -g -n ${TEST_USER}"
[ "${TEST_GROUP}" = "${lines[0]}" ]
assert_line --index 0 "${TEST_GROUP}"

run docker exec "${cid}" sh -c "id -u ${TEST_USER}"
[ "${TEST_UID}" = "${lines[0]}" ]
assert_line --index 0 "${TEST_UID}"

run docker exec "${cid}" sh -c "id -g ${TEST_USER}"
[ "${TEST_GID}" = "${lines[0]}" ]
assert_line --index 0 "${TEST_GID}"

run docker exec "${cid}" sh -c "printenv | grep AGENT_WORKDIR"
[ "AGENT_WORKDIR=/home/${TEST_USER}/something" = "${lines[0]}" ]
assert_line --index 0 "AGENT_WORKDIR=/home/${TEST_USER}/something"

run docker exec "${cid}" sh -c 'stat -c "%U:%G" "${AGENT_WORKDIR}"'
[ "${TEST_USER}:${TEST_GROUP}" = "${lines[0]}" ]
assert_line --index 0 "${TEST_USER}:${TEST_GROUP}"

run docker exec "${cid}" touch /home/test-user/a
[ "${status}" -eq 0 ]
assert_success

run docker exec "${cid}" touch /home/test-user/.jenkins/a
[ "${status}" -eq 0 ]
assert_success

run docker exec "${cid}" touch /home/test-user/something/a
[ "${status}" -eq 0 ]
assert_success

cleanup $cid
cleanup "$cid"
}