Skip to content

Commit 0aa0e98

Browse files
authored
Merge pull request #155 from ProblemSetters/tests-implementation
Tests implementation + Ubuntu 24.04 bugfixes
2 parents c31560b + adc488e commit 0aa0e98

125 files changed

Lines changed: 7542 additions & 14 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

blackbox

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@
3838
blackbox() {
3939
export BLACKBOX_DIR=/blackbox
4040
# shellcheck disable=SC2155
41-
export BLACKBOX_SPAWN=$(awk -v master="$(</proc/sys/kernel/random/uuid)" -v user="$BLACKBOX_SPAWN" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
41+
# export BLACKBOX_SPAWN=$(awk -v master="$(awk -v master="$(</proc/sys/kernel/random/uuid)" -v user="blackbox-spawn" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))" -v user="" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
42+
export BLACKBOX_SPAWN=$(awk -v master="$(</proc/sys/kernel/random/uuid)" -v user="blackbox-spawn" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
4243
# shellcheck disable=SC2155
43-
export BLACKBOX_USER=$(awk -v master="1000:1000" -v user="$BLACKBOX_USER" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
44+
export BLACKBOX_USER="1000:1000"
4445
# shellcheck disable=SC2155
45-
export BLACKBOX_USER_NAME=$(awk -v master="ubuntu" -v user="$BLACKBOX_USER_NAME" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
46+
export BLACKBOX_USER_NAME="ubuntu"
4647
# shellcheck disable=SC2155
47-
export BLACKBOX_USER_HOME=$(awk -v master="/home/${BLACKBOX_USER_NAME}" -v user="$BLACKBOX_USER_HOME" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
48+
export BLACKBOX_USER_HOME="/home/${BLACKBOX_USER_NAME}"
4849
# shellcheck disable=SC2155
49-
export BLACKBOX_USER_QUESTION_DIR=$(awk -v master="${BLACKBOX_USER_HOME}/${3}" -v user="$BLACKBOX_USER_QUESTION_DIR" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
50+
export BLACKBOX_USER_QUESTION_DIR=$(awk -v master="${BLACKBOX_USER_HOME}/${3}" -v user="" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
5051
# shellcheck disable=SC2155
51-
export BLACKBOX_VERSION=$(awk -v master="2404" -v user="$BLACKBOX_VERSION" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
52+
export BLACKBOX_VERSION="2404"
5253

5354
export BLACKBOX_FLAG__DEBUG_MODE=${BLACKBOX_FLAG__DEBUG_MODE:-no}
5455
# shellcheck disable=SC2155
55-
export BLACKBOX_FLAG__STEP_PROVISION=$(awk -v master="yes" -v user="$BLACKBOX_FLAG__STEP_PROVISION" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
56+
export BLACKBOX_FLAG__STEP_PROVISION=$(awk -v master="yes" -v user="${BLACKBOX_FLAG__STEP_PROVISION:-yes}" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
5657
# shellcheck disable=SC2155
57-
export BLACKBOX_FLAG__STEP_BUILD=$(awk -v master="yes" -v user="$BLACKBOX_FLAG__STEP_BUILD" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
58+
export BLACKBOX_FLAG__STEP_BUILD=$(awk -v master="yes" -v user="${BLACKBOX_FLAG__STEP_BUILD:-yes}" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
5859
# shellcheck disable=SC2155
59-
export BLACKBOX_FLAG__STEP_EVALUATE=$(awk -v master="yes" -v user="$BLACKBOX_FLAG__STEP_EVALUATE" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
60+
export BLACKBOX_FLAG__STEP_EVALUATE=$(awk -v master="yes" -v user="${BLACKBOX_FLAG__STEP_EVALUATE:-yes}" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
6061

6162
# shellcheck disable=SC2155
6263
export BLACKBOX_PROVISION_WITH_OPTS=$(sed "s/\({\);/\1/g; s/\(;\)\{2\}/\1/g" <(awk 'ORS="; "' <(sed '1,2d; $d; s/[[:space:]]$//g; s/^[[:space:]]\+//g' <(declare -f BLACKBOX_PROVISION_WITH_OPTS))))
@@ -192,7 +193,7 @@ blackbox() {
192193

193194
source_definition=$(sed '1,2d; $d' <<<"$source_definition")
194195
# shellcheck disable=SC2016
195-
destination_definition=$(sed "$(printf '1,2d; $d; /%s/d' "$destination_definition_source_placeholder")" <<<"$destination_definition")
196+
destination_definition=$(sed "$(printf '1,2d; $d; /%s/d; s/&/\\\&/g' "$destination_definition_source_placeholder")" <<<"$destination_definition")
196197

197198
blackbox.framework.trace "${FUNCNAME[0]}" "$*" "$(printf '&%s %s' "$source_namespace_name" "$destination_namespace_name")" <<<""
198199

@@ -270,7 +271,7 @@ blackbox() {
270271

271272
if [ ! -e "$BLACKBOX_DIR" ]; then
272273
wget -S --output-document="/tmp/blackbox.zip" "https://codeload.github.com/ProblemSetters/devops-blackbox/zip/refs/heads/${BLACKBOX_VERSION}" && {
273-
unzip /tmp/blackbox.zip -x "devops-blackbox-2404/.github/*" "devops-blackbox-2404/docs/*" "devops-blackbox-2404/.gitignore" "devops-blackbox-2404/Makefile.*" "devops-blackbox-2404/README.md" "devops-blackbox-2404/Vagrantfile" -d /tmp && {
274+
unzip /tmp/blackbox.zip -x "devops-blackbox-2404/.github/*" "devops-blackbox-2404/docs/*" "devops-blackbox-2404/tests/*" "devops-blackbox-2404/.gitignore" "devops-blackbox-2404/Makefile.*" "devops-blackbox-2404/README.md" "devops-blackbox-2404/Vagrantfile" -d /tmp && {
274275
mv "/tmp/devops-blackbox-${BLACKBOX_VERSION}" "$BLACKBOX_DIR"
275276
rm -rf /tmp/*blackbox*
276277
}

framework/inventory/ansible.inventory

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ blackbox.framework.inventory.ansible() {
3636
fi
3737

3838
DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends software-properties-common gpg-agent wget && {
39-
wget -S --output-document="/tmp/ansible.deb" --no-verbose https://launchpad.net/~ansible/+archive/ubuntu/ansible/+files/ansible_11.11.0-1ppa~noble_all.deb && {
39+
wget -S --output-document="/tmp/ansible.deb" --no-verbose "https://launchpad.net/~ansible/+archive/ubuntu/ansible-11/+files/ansible_11.12.0-1ppa~noble_all.deb" && {
4040
DEBIAN_FRONTEND=noninteractive apt install -y /tmp/ansible.deb "${with_packages[@]}" && {
4141
rm -f /tmp/ansible.deb
4242
}

framework/inventory/jenkins.inventory

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ blackbox.framework.inventory.jenkins() {
4141
wget -S --output-document="/usr/share/keyrings/jenkins-keyring.asc" --no-verbose https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
4242
tee /etc/apt/sources.list.d/jenkins.list <<<"deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/"
4343

44-
local version=2.492.2
44+
local version=2.528.2
4545

4646
apt update
47-
DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends openjdk-17-jre-headless jenkins="$version" "${with_packages[@]}" && {
47+
DEBIAN_FRONTEND=noninteractive apt install -y openjdk-21-jre-headless jenkins="$version" "${with_packages[@]}" && {
4848
install -m 0644 -D /blackbox/framework/inventory/jenkins/rootfs/etc/systemd/system/jenkins.service.d/override.conf /etc/systemd/system/jenkins.service.d/override.conf
4949
}
5050
}

module/aws/setup/provision.step

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ blackbox.module.aws.setup.provision() {
1111
sleep 30
1212
}
1313

14+
rm -f /usr/local/bin/solve
15+
1416
tee -a /etc/hosts < <(printf "127.0.0.1\teu-central-1.hackerrankaws.local\n")
1517
tee -a /etc/environment < <(printf "AWS_ENDPOINT_URL=%s\n" "eu-central-1.hackerrankaws.local")
1618
}

module/ceph-stdl/setup/provision.step

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ blackbox.module.ceph-stdl.setup.provision() {
3232
microceph.ceph status
3333
}
3434

35+
blackbox.framework.inventory.provision awscli
36+
3537
rm -f /usr/local/bin/solve
3638

3739
if [ -n "$BLACKBOX_PROVISION_WITH_OPTS" ]; then

module/git/setup/provision.step

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ blackbox.module.git.setup.provision() {
1010
blackbox.framework.inventory.provision git
1111
sudo -u "$BLACKBOX_USER_NAME" git init
1212
{
13+
git config --global --add safe.directory "$BLACKBOX_USER_QUESTION_DIR"
1314
sudo -u "$BLACKBOX_USER_NAME" git config --global --add safe.directory "$BLACKBOX_USER_QUESTION_DIR"
1415
sudo -u "$BLACKBOX_USER_NAME" git remote add origin "$BLACKBOX_USER_QUESTION_DIR"
1516
sudo -u "$BLACKBOX_USER_NAME" git config --global --list
1617
sudo -u "$BLACKBOX_USER_NAME" git config --list
1718
}
19+
rm -f /usr/local/bin/solve
1820
}

tests/Makefile.tests

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env make
2+
override blackbox/:=*
3+
4+
ifeq ($(origin .FEATURES),undefined)
5+
$(error `make` executable version `$(MAKE_VERSION)` is not supported. GNU Make 4.3 or above is required)
6+
endif
7+
8+
ifeq ($(words $(MAKEFILE_LIST)),1)
9+
$(error `Makefile` should be included, direct usage is not allowed)
10+
endif
11+
12+
ifeq ($(shell which docker),)
13+
$(error `docker` executable is required)
14+
endif
15+
16+
override SHELL:=/usr/bin/env bash -e +o pipefail
17+
18+
ifeq ($(shell echo $$BASH),)
19+
$(error current shell executable is not supported. GNU Bash 5.1 or above is required)
20+
endif
21+
22+
ifeq ($(origin .FEATURES),undefined)
23+
$(error `make` executable version "$(MAKE_VERSION)" is not supported. GNU Make 3.82 or above is required)
24+
endif
25+
26+
override SHELL:=/bin/bash
27+
28+
ifneq ($(value SHELL),$(shell echo $$BASH))
29+
$(error current shell executable is not supported. GNU Bash 4.3 or above is required)
30+
endif
31+
32+
override blackbox/host/tests/dir:=$(abspath $(dir $(lastword $(MAKEFILE_LIST))))
33+
override blackbox/host/test/dir:=$(abspath $(dir $(firstword $(MAKEFILE_LIST))))
34+
override blackbox/spawn/username:=ubuntu
35+
override blackbox/spawn/test/dir:=/test
36+
37+
export BLACKBOX_HOST_TEST_DIR=$(blackbox/host/test/dir)
38+
export BLACKBOX_FLAG__DEBUG_MODE=no
39+
export VAGRANT_VAGRANTFILE=$(blackbox/host/tests/dir)/Vagrantfile
40+
41+
debug: ;@:
42+
$(eval BLACKBOX_FLAG__DEBUG_MODE:=yes)
43+
$(foreach variable,BLACKBOX_FLAG__DEBUG_MODE,$(info $(shell printf "\033[0;37m%s = %s\033[0m" "$(variable)" "$($(variable))")))
44+
$(info $(shell printf "\033[0;37m%s\033[0m" \\))
45+
$(foreach variable,$(filter-out BLACKBOX_FLAG__%,$(filter BLACKBOX_%,$(sort $(.VARIABLES)))),$(info $(shell printf "\033[0;37m%s = %s\033[0m" "$(variable)" "$($(variable))")))
46+
$(if $(and $(filter-out $(words $(MAKECMDGOALS)),0),$(filter-out $(words $(MAKECMDGOALS)),1)),$(info $(shell printf "\033[0;37m%s\033[0m" --)))
47+
48+
all: install setup save check
49+
$(MAKE) restore solve check
50+
51+
install:
52+
vagrant up
53+
54+
setup:
55+
vagrant ssh --command="sudo BLACKBOX_FLAG__DEBUG_MODE=$(BLACKBOX_FLAG__DEBUG_MODE) bash $(blackbox/spawn/test/dir)/setup.sh"
56+
57+
solve:
58+
vagrant ssh --command="sudo -i -u $(blackbox/spawn/username) bash $(blackbox/spawn/test/dir)/solve.sh"
59+
60+
check:
61+
vagrant ssh --command="sudo BLACKBOX_FLAG__DEBUG_MODE=$(BLACKBOX_FLAG__DEBUG_MODE) bash $(blackbox/spawn/test/dir)/check.sh" | tee >(awk '/Partial Credit/ { if (!reset) { reset = 1; next }; total_score += $$3; ok += ($$3 > 0); failed += ($$3 == 0) } END { printf("\nSuccess: %d\nFail: %d\nTotal: %d\n--\nTotal Credit: %d\n", ok, failed, ok + failed, total_score) }')
62+
63+
check\:evaluate:
64+
vagrant ssh --command="sudo BLACKBOX_FLAG__DEBUG_MODE=no BLACKBOX_FLAG__STEP_PROVISION=no BLACKBOX_FLAG__STEP_BUILD=no BLACKBOX_FLAG__STEP_EVALUATE=yes bash $(blackbox/spawn/test/dir)/check.sh" | tee >(awk '/Partial Credit/ { if (!reset) { reset = 1; next }; total_score += $$3; ok += ($$3 > 0); failed += ($$3 == 0) } END { printf("\nSuccess: %d\nFail: %d\nTotal: %d\n--\nTotal Credit: %d\n", ok, failed, ok + failed, total_score) }')
65+
66+
shell:
67+
vagrant ssh --command="sudo su - $(blackbox/spawn/username)"
68+
69+
shell\:spawn:
70+
vagrant ssh --command="sudo docker exec -it -w /home/ubuntu/test blackbox-spawn bash"
71+
72+
save:
73+
vagrant snapshot save current
74+
75+
restore:
76+
vagrant snapshot restore current
77+
78+
prune:
79+
vagrant snapshot delete current
80+
81+
uninstall:
82+
vagrant destroy -f
83+
rm -rf .vagrant

tests/Vagrantfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Vagrant.configure("2") do |config|
2+
config.vm.box = "bento/ubuntu-24.04"
3+
config.vm.hostname = "taskserver"
4+
config.vm.synced_folder ".", "/vagrant", disabled: true
5+
if ENV["BLACKBOX_FLAG__DEBUG_MODE"] == "yes"
6+
config.vm.synced_folder ENV["BLACKBOX_DIR"], "/blackbox", mount_options: ["uid=0", "gid=0", "ro"]
7+
end
8+
config.vm.synced_folder ENV["BLACKBOX_HOST_TEST_DIR"], "/test", mount_options: ["uid=0", "gid=0", "ro"]
9+
config.vm.provision "shell", inline: <<-SHELL
10+
apt update && {
11+
DEBIAN_FRONTEND=noninteractive apt install -y unzip
12+
}
13+
{
14+
groupadd -g 1001 ubuntu
15+
useradd -u 1001 -g 1001 -s /bin/bash -m ubuntu
16+
cat >/etc/sudoers.d/ubuntu <<<"ubuntu ALL=(ALL) NOPASSWD:ALL"
17+
usermod -aG vagrant ubuntu
18+
}
19+
SHELL
20+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env make
2+
include ../../../Makefile.tests

0 commit comments

Comments
 (0)