Skip to content

Commit

Permalink
WIP - Test 1 now mostly works, except sometimes git is finnicky...
Browse files Browse the repository at this point in the history
  • Loading branch information
JDeBo committed Dec 3, 2023
1 parent d7970b3 commit e2debc3
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 111 deletions.
4 changes: 4 additions & 0 deletions linux/instructions/challenge_6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Challenge 7:
==============================

Placeholder to keep scoring from breaking
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Step 4: Git branch management
Challenge 7: Git SSH Setup
==============================

We just got hired by a very famous movie star (who's name may not be disclosed)
Expand All @@ -8,4 +8,8 @@ dissappeared while on a golfing trip a few weeks past. We were told that the
code was about ready to deploy, just hadn't gotten the chance to merge it into
main. See if you can figure out how to get it up and running.

(Hint: the name of the app is carrot-cruncher)
The name of the app is carrot-cruncher. The last dev got the repo cloned somewhere,
but they never said where... hopefully you'll able to find it. When you do, supposedly
there was a new branch pushed to the remote repo, so you'll need to figure out how to
authenticate to that repo, clone the remote branches down, then you'll be off to
the races... Goodluck!
7 changes: 7 additions & 0 deletions linux/instructions/challenge_8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Challenge 7: Git branch management
==============================

Great, now that you've got it cloned down, you'll need to figure out which
of those remote branches have the correct changes it get it merged in ASAP.
The working version should be obvious... or so we were told... So we recommend
you just try running the the different branches and see which one fits.
35 changes: 19 additions & 16 deletions linux/scripts/linux-workshop-admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,25 @@ _check-webapp-reachable() {
}

_check-git-branch-merged-correct() {
local TEST_DIR=${wsroot}/git-check
mkdir -p "${TEST_DIR}"
pushd "${TEST_DIR}" > /dev/null
# Clone if the directory is empty
if [ ! "$(ls -A ${TEST_DIR})" ]; then
git clone "ssh://appuser@localhost:2332/home/git/repositories/carrot-cruncher.git" .
fi
git fetch
git checkout main
git pull origin main
if grep -q carrot main.go; then
score-for-step 3.1
else
log-error "feature branch not merged into main.\n"
fi
popd > /dev/null
local test_dir=${wsroot}/git-check
local repo_dir="/srv/git/repositories/carrot-cruncher.git"
if [ "$(git rev-parse master)" = "$(git rev-parse release/bunnies_v1)" ] then
log-info "commits match"
else
log-error "commits don't match"
fi
pushd "${test_dir}" > /dev/null
# Clone if the directory is empty
if [ ! "$(ls -A ${test_dir})" ]; then
su - git -c "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone 'git@localhost:${repo_dir}' ${test_dir}"
fi
su - git -c "git fetch; git checkout main; git pull origin main"
if grep -q carrot main.go; then
_score-for-challenge 7
else
log-error "feature branch not merged correctly into main.\n"
fi
popd > /dev/null
}

###
Expand Down
22 changes: 18 additions & 4 deletions linux/scripts/setup-git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ function _setup_git_user() {
# _add_to_known_hosts
echo $(which git-shell) >>/etc/shells
chsh --shell "$(command -v /bin/bash)" "${GIT_USER}"
# chsh --shell "$(command -v git-shell)" "${GIT_USER}"
}

function _init_git_repo() {
log-info "Initializing remote carrot cruncher"
rm -rf "${REPO_DIR}
mkdir -p "${REPO_DIR}"
pushd "${REPO_DIR}" >/dev/null
git config --global init.defaultBranch "${DEFAULT_BRANCH}"
git init --bare
su - ${GIT_USER} -c "git config --global init.defaultBranch ${DEFAULT_BRANCH}"
su - ${GIT_USER} -c "git config --global user.email 'bugs@bigbadbunnies.com'"
su - ${GIT_USER} -c "git config --global user.name 'Bugs Bunny'"
su - ${GIT_USER} -c "pushd "${REPO_DIR}" >/dev/null; git init --bare"
popd >/dev/null
chown -R "${GIT_USER}:${GIT_USER}" "${REPO_DIR}"
}
Expand All @@ -89,12 +89,14 @@ function _setup_local_clone() {
git config --global --add safe.directory /opt/git/carrot-cruncher
pushd "${clone_dir}"
cp -r "${APP_DIR}"/* .
sed -i 's/PrintLine/Println/g' main.go
su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'Initial commit'; git push origin"
popd >/dev/null
}
function _create_release_branch() {
local clone_dir="${WORK_DIR}/${REPO_NAME}"
local branch_2="v1.0.2-rc-tmp-bugfix-2.0.1"
pushd "${clone_dir}" >/dev/null
log-info "setting up release branch"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${BRANCH_NAME}'"
Expand All @@ -104,15 +106,27 @@ function _create_release_branch() {
rm banking.txt
su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'oops did not mean to add that...'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git push --set-upstream origin '${BRANCH_NAME}'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${DEFAULT_BRANCH}'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${branch_2}'"
sed -i -e 's/printing/uh/g' -e 's/money/oh/g' -e 's/CHA-CHING/NO-NO-NOOOOO/g' main.go
su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'I think we might be on to something...'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git push --set-upstream origin '${branch_2}'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout '${DEFAULT_BRANCH}'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git branch -D ${BRANCH_NAME} ${branch_2}"
popd >/dev/null
}
function _polish_off() {
chown -R ${APP_USER}:${APP_USER} /opt/git # git appuser ownership of git directory
chsh --shell "$(command -v git-shell)" "${GIT_USER}" # switch Git User to git-shell
}
function main() {
_setup_git_user
_init_git_repo
_setup_local_clone
_create_release_branch
_polish_off
log-info "Git server setup is complete."
}
Expand Down
205 changes: 116 additions & 89 deletions linux/scripts/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ teardown() {
# Challenge 5
ufw deny out 8000

# Challenge 7
rm -rf /home/appuser/.ssh/
_reset-score
}

Expand Down Expand Up @@ -141,122 +143,147 @@ EOF
systemctl start app-deb.service
}

solve-challenge-5() {
_solve-challenge-5() {
ufw allow out 8000
}

solve_challenge_3.1() {
_solve-challenge-7() {
local ssh_dir="/home/appuser/.ssh"
local public_key_file="${ssh_dir}/id_rsa.pub"
local private_key_file="${ssh_dir}/id_rsa"
local authorized_keys_file="/home/git/.ssh/authorized_keys"
local known_hosts_file="${ssh_dir}/known_hosts"
local user="appuser"

if [[ -d "${ssh_dir}" ]]; then
rm -rf /home/appuser/.ssh
rm -rf "${ssh_dir}"
fi
mkdir -p "${ssh_dir}"
chown "${user}:${user}" "${ssh_dir}"
chmod 700 "${ssh_dir}"
su - "${user}" -c "ssh-keygen -t rsa -f ${private_key_file} -q -N ''"
cat "${public_key_file}" >>"${authorized_keys_file}"
su - "${user}" -c "ssh-keyscan -H localhost >> ${known_hosts_file}"
}

solve_challenge_3.2() {
_solve-challenge-8() {
_solve-challenge-7
set -eux pipefail
local user="appuser"
local RELEASE_BRANCH=release/bunnies_v1
pushd "/opt/git/carrot-cruncher" >/dev/null
git merge "${RELEASE_BRANCH}"
git push origin main
su - "${user}" -c "git pull ${RELEASE_BRANCH}"
su - "${user}" -c "git merge ${RELEASE_BRANCH}"
su - "${user}" -c "git push origin main"
popd >/dev/null
}

################################################################################

@test "init steps succeeded" {
[[ -f "/home/appuser/challenge_0.md" ]]
[[ -f "/home/appuser/challenge_1.md" ]]
}

@test "challenge 1" {
# Fails before solution
[[ ! -f /opt/app/app ]]
[[ ! -x /opt/app/app ]]

# Passes after solution
_solve-challenge-1
local score="$(_get-score)"
sleep 1
printf 'DEBUG: Score from challenge 1: %s\n' "${score}"
[[ "${score}" -ge 100 ]]
[[ -f "/home/appuser/challenge_2.md" ]] # next instruction gets put in homedir
}

# This test also end ups implicitly tests two challenges' scores at once, which is
# good
@test "challenge 2" {
# Fails before solution
[[ ! -f "/home/appuser/challenge_3.md" ]]
[[ ! -L /usr/local/bin/run-app ]]

# Passes after solution
_solve-challenge-2
local score="$(_get-score)"
sleep 1
printf 'DEBUG: Score from challenge 2: %s\n' "${score}"
[[ "${score}" -ge 200 ]] # challenge 1 + 2 score
[[ -f "/home/appuser/challenge_3.md" ]]
# @test "init steps succeeded" {
# [[ -f "/home/appuser/challenge_0.md" ]]
# [[ -f "/home/appuser/challenge_1.md" ]]
# }

# @test "challenge 1" {
# # Fails before solution
# [[ ! -f /opt/app/app ]]
# [[ ! -x /opt/app/app ]]

# # Passes after solution
# _solve-challenge-1
# local score="$(_get-score)"
# sleep 1
# printf 'DEBUG: Score from challenge 1: %s\n' "${score}"
# [[ "${score}" -ge 100 ]]
# [[ -f "/home/appuser/challenge_2.md" ]] # next instruction gets put in homedir
# }

# # This test also end ups implicitly tests two challenges' scores at once, which is
# # good
# @test "challenge 2" {
# # Fails before solution
# [[ ! -f "/home/appuser/challenge_3.md" ]]
# [[ ! -L /usr/local/bin/run-app ]]

# # Passes after solution
# _solve-challenge-2
# local score="$(_get-score)"
# sleep 1
# printf 'DEBUG: Score from challenge 2: %s\n' "${score}"
# [[ "${score}" -ge 200 ]] # challenge 1 + 2 score
# [[ -f "/home/appuser/challenge_3.md" ]]
# }

# @test "challenge 3" {
# # Fails before solution
# systemctl is-active app.service && return 1
# systemctl is-enabled app.service && return 1

# # Passes after solution
# _solve-challenge-3
# local score="$(_get-score)"
# sleep 1
# printf 'DEBUG: Score from challenge 3: %s\n' "${score}"
# systemctl is-active app.service || return 1
# systemctl is-enabled app.service || return 1
# [[ -f "/home/appuser/challenge_4.md" ]]
# }

# @test "challenge 4" {
# # Fails before solution
# [[ ! -f "/home/appuser/challenge_5.md" ]]
# systemctl is-active app-deb.service && return 1
# systemctl is-enabled app-deb.service && return 1

# # Passes after solution
# _solve-challenge-4
# local score="$(_get-score)"
# sleep 1
# printf 'DEBUG: Score from challenge 4: %s\n' "${score}"
# systemctl is-active app-deb.service || return 1
# systemctl is-enabled app-deb.service || return 1
# [[ -f "/home/appuser/challenge_5.md" ]]
# }

# @test "challenge 5" {
# # Fails before solution
# # [[ ! -f "/home/appuser/challenge_6.md" ]]

# # Passes after solution
# _solve-challenge-5
# local score="$(_get-score)"
# sleep 1
# printf 'DEBUG: Score from challenge 5: %s\n' "${score}"
# counter=0
# until timeout 1s curl -fsSL "${db_addr}:8000" ; do
# printf 'Web app not reachable, trying again...\n' >&2
# counter="$((counter + 1))"
# if [[ "${counter}" -ge 30 ]] ; then
# return 1
# fi
# sleep 5
# done
# # [[ -f "/home/appuser/challenge_6.md" ]]
# }

@test "challenge 7" {
_solve-challenge-7
chmod 777 /opt/git/carrot-cruncher
su - "appuser" -c "pushd /opt/git/carrot-cruncher >/dev/null; git config --global --add safe.directory /opt/git/carrot-cruncher; git fetch"
# [[ -f "/home/appuser/challenge_8.md" ]]
}

@test "challenge 3" {
# Fails before solution
systemctl is-active app.service && return 1
systemctl is-enabled app.service && return 1

# Passes after solution
_solve-challenge-3
local score="$(_get-score)"
sleep 1
printf 'DEBUG: Score from challenge 3: %s\n' "${score}"
systemctl is-active app.service || return 1
systemctl is-enabled app.service || return 1
[[ -f "/home/appuser/challenge_4.md" ]]
}

@test "challenge 4" {
# Fails before solution
[[ ! -f "/home/appuser/challenge_5.md" ]]
systemctl is-active app-deb.service && return 1
systemctl is-enabled app-deb.service && return 1

# Passes after solution
_solve-challenge-4
local score="$(_get-score)"
sleep 1
printf 'DEBUG: Score from challenge 4: %s\n' "${score}"
systemctl is-active app-deb.service || return 1
systemctl is-enabled app-deb.service || return 1
[[ -f "/home/appuser/challenge_5.md" ]]
}

@test "challenge 5" {
# Fails before solution
# [[ ! -f "/home/appuser/challenge_6.md" ]]

# Passes after solution
_solve-challenge-5
local score="$(_get-score)"
sleep 1
printf 'DEBUG: Score from challenge 5: %s\n' "${score}"
counter=0
until timeout 1s curl -fsSL "${db_addr}:8000" ; do
printf 'Web app not reachable, trying again...\n' >&2
counter="$((counter + 1))"
if [[ "${counter}" -ge 30 ]] ; then
return 1
fi
sleep 5
done
# [[ -f "/home/appuser/challenge_6.md" ]]
@test "challenge 8" {
local git_dir=/srv/git/repositories/carrot-cruncher.git
_solve-challenge-8
pushd "${git_dir}" >/dev/null
git config --global --add safe.directory /srv/git/repositories/carrot-cruncher.git
if [ "$(git rev-parse master)" = "$(git rev-parse release/bunnies_v1)" ] ; then
return 1
fi
popd >/dev/null
# [[ -f "/home/appuser/challenge_9.md" ]]
}

@test "simulate score accumulation" {
Expand Down

0 comments on commit e2debc3

Please sign in to comment.