Skip to content

Commit

Permalink
Got all the tests working for 7 (FINALLY)
Browse files Browse the repository at this point in the history
  • Loading branch information
JDeBo committed Jan 2, 2024
1 parent cffbbe7 commit 8b8f34d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 42 deletions.
4 changes: 2 additions & 2 deletions linux/scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ log-info 'Set up systemd timer(s) & service(s)'
cp "${wsroot}"/services/* /etc/systemd/system/
systemctl daemon-reload
systemctl disable linux-workshop-admin.service
systemctl enable linux-workshop-admin.timer
systemctl start linux-workshop-admin.timer
# systemctl enable linux-workshop-admin.timer
# systemctl start linux-workshop-admin.timer

###
_db_init() {
Expand Down
18 changes: 14 additions & 4 deletions linux/scripts/linux-workshop-admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,28 @@ _check-webapp-reachable() {
}

_check-ssh-setup() {
set -x
local test_dir=${wsroot}/git-checks
local git_home="/srv/git"
local repo_dir="${git_home}/repositories/carrot-cruncher.git"
cat ${git_home}/ssh-keys/id_rsa.pub >> /home/git/.ssh/authorized_keys && rm -f ${git_home}/ssh-keys/id_rsa.pub || log-warn "No key to copy"
su - appuser -c "git config --global --add safe.directory ${test_dir};"
if [[ -f ${git_home}/ssh-keys/id_rsa.pub ]]; then
cat ${git_home}/ssh-keys/id_rsa.pub >> /home/git/.ssh/authorized_keys && rm -f ${git_home}/ssh-keys/id_rsa.pub
fi
[[ -d ${test_dir} ]] || mkdir -m 777 ${test_dir}
[[ ! -d ${test_dir}/carrot-cruncher ]] || rm -rf /${test_dir}/*
[[ ! -d ${test_dir}/carrot-cruncher ]] || rm -rf /${test_dir:?}/*
if su - appuser -c "git clone 'git@localhost:${repo_dir}' ${test_dir}/carrot-cruncher"; then
rm -rf /${test_dir}/*
rm -rf /${test_dir:?}/* /${test_dir:?}/.git
set +x
_score-for-challenge 6
else
set +x
log-error "SSH Keys not setup successfully"
fi
}

_check-git-branch-merged-correct() {
set -x
local test_dir=${wsroot}/git-checks
local repo_dir="/srv/git/repositories/carrot-cruncher.git"
# pushd "${repo_dir}" > /dev/null
Expand All @@ -164,15 +171,18 @@ _check-git-branch-merged-correct() {
# log-error "commits don't match"
# fi
pushd "${test_dir}" > /dev/null
su - appuser -c "git config --global --add safe.directory ${test_dir};"
# Clone if the directory is empty
if [ ! "$(ls -A ${test_dir})" ]; then
su - appuser -c "git clone 'git@localhost:${repo_dir}' ${test_dir}"
fi
su - appuser -c "cd ${test_dir}; git fetch; git checkout main; git pull origin main"
if grep -q carrot main.go; then
set +x
_score-for-challenge 7
else
log-error "feature branch not merged correctly into main.\n"
set +x
log-error "feature branch not merged correctly into main.\n"
fi
popd > /dev/null
}
Expand Down
54 changes: 27 additions & 27 deletions linux/scripts/setup-git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function _setup_ssh_keys_for_git_user() {
fi
chmod 600 "${authorized_keys_file}"
chown "${GIT_USER}:${GIT_USER}" "${authorized_keys_file}"
[[ -d /home/${APP_USER}/.ssh ]] || mkdir /home/${APP_USER}/.ssh
cat <<EOF >/home/${APP_USER}/.ssh/config
[[ -d /home/${APP_USER}/.ssh ]] || mkdir /home/"${APP_USER}"/.ssh
cat <<EOF >/home/"${APP_USER}"/.ssh/config
HOST localhost
USER ${GIT_USER}
PORT ${SSH_PORT}
Expand All @@ -63,7 +63,7 @@ function _setup_git_user() {
fi
_setup_ssh_keys_for_git_user
# _add_to_known_hosts
echo $(which git-shell) >>/etc/shells
which git-shell >>/etc/shells
chsh --shell "$(command -v /bin/bash)" "${GIT_USER}"
}

Expand All @@ -73,12 +73,12 @@ function _init_git_repo() {
mkdir -p "${REPO_DIR}"
[[ -d "${GIT_HOME}/ssh-keys" ]] || mkdir "${GIT_HOME}/ssh-keys"
chown -R "${GIT_USER}:${GIT_USER}" "${GIT_HOME}"
pushd "${REPO_DIR}" >/dev/null
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
pushd "${REPO_DIR}" >/dev/null || exit
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 || exit
}

function _setup_local_clone() {
Expand All @@ -89,36 +89,36 @@ function _setup_local_clone() {
fi
mkdir "${WORK_DIR}"
chmod 777 "${WORK_DIR}"
pushd "${WORK_DIR}" >/dev/null
su - ${GIT_USER} -c "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone '${GIT_USER}@localhost:${REPO_DIR}' ${clone_dir}"
pushd "${WORK_DIR}" >/dev/null || exit
su - "${GIT_USER}" -c "GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=accept-new' git clone '${GIT_USER}@localhost:${REPO_DIR}' ${clone_dir}"
git config --global --add safe.directory /opt/git/carrot-cruncher
pushd "${clone_dir}"
pushd "${clone_dir}" >/dev/null || exit
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
su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'Initial commit'; git push origin"
popd >/dev/null || exit
}

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
pushd "${clone_dir}" >/dev/null || exit
log-info "setting up release branch"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${BRANCH_NAME}'"
su - "${GIT_USER}" -c "pushd ${clone_dir}; git checkout -b '${BRANCH_NAME}'"
sed -i -e 's/printing/picking/g' -e 's/money/carrots/g' -e 's/CHA-CHING/CRUNCH/g' main.go
echo -e "Name: Bugs Bunny\nSecurity Question Answer: 'Crunchy King'\nSSN: 1234-BUNNY" >banking.txt
su - ${GIT_USER} -c "pushd ${clone_dir}; git add .; git commit -m 'Prepare release branch'"
su - "${GIT_USER}" -c "pushd ${clone_dir}; git add .; git commit -m 'Prepare 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 '${DEFAULT_BRANCH}'"
su - ${GIT_USER} -c "pushd ${clone_dir}; git checkout -b '${branch_2}'"
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 '${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
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 || exit
}

function _polish_off() {
Expand All @@ -131,7 +131,7 @@ printf '%s\n' "provide interactive shell access."
exit 128
EOF
chmod 777 /home/git/git-shell-commands/no-interactive-login
chown -R ${APP_USER}:${APP_USER} /opt/git # git appuser ownership of git directory
chown -R "${APP_USER}":"${APP_USER}" /opt/git # git appuser ownership of git directory
}

function main() {
Expand Down
26 changes: 17 additions & 9 deletions linux/scripts/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ teardown() {
ufw deny out 8000

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

teardown_file() {
teardown
rm -f /home/appuser/challenge_{2..200}.md # just to be sure to catch any non-0 or 1 challenges
rm -f /home/appuser/congrats.md
rm -r "${wsroot}"/team_has_been_congratulated
systemctl start linux-workshop-admin.timer
rm -rf "${wsroot}"/team_has_been_congratulated
# systemctl start linux-workshop-admin.timer
}

_reset-score() {
Expand Down Expand Up @@ -165,16 +165,19 @@ _solve-challenge-6() {

_solve-challenge-7() {
_solve-challenge-6
sleep 5 # give time for ssh key to copy
systemctl start linux-workshop-admin.service --wait # trigger admin service to copy ssh key
sleep 5
local user="appuser"
local RELEASE_BRANCH=release/bunnies_v1
[[ -d /tmp/carrot-cruncher ]] && rm -rf /tmp/carrot-cruncher
su - "${user}" -c "pushd /tmp >/dev/null; \\
git config --global --add safe.directory /tmp/;
git clone git@localhost:/srv/git/repositories/carrot-cruncher.git && \\
pushd carrot-cruncher >/dev/null && \\
git merge origin/${RELEASE_BRANCH} && \\
git push origin main && \\
popd >/dev/null && \\
rm -rf /tmp/carrot-cruncher"
popd >/dev/null"
rm -rf /tmp/carrot-cruncher
}

################################################################################
Expand Down Expand Up @@ -252,7 +255,7 @@ _solve-challenge-7() {
# Passes after solution
_solve-challenge-5
local score="$(_get-score)"
sleep 1
sleep 5
printf 'DEBUG: Score from challenge 5: %s\n' "${score}"
counter=0
until timeout 1s curl -fsSL "${db_addr}:8000" ; do
Expand All @@ -272,7 +275,9 @@ _solve-challenge-7() {

# Passes after solution
_solve-challenge-6
sleep 10
local score="$(_get-score)"
sleep 5
printf 'DEBUG: Score from challenge 6: %s\n' "${score}"
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_7.md" ]]
}
Expand All @@ -284,7 +289,9 @@ _solve-challenge-7() {
# Passes after solution
local git_dir=/srv/git/repositories/carrot-cruncher.git
local backup_dir=/tmp/git.backup/
mkdir ${backup_dir} && cp -r ${git_dir}/* "${backup_dir}/"
if [[ ! -d ${backup_dir} ]]; then
mkdir ${backup_dir} && cp -r ${git_dir}/* "${backup_dir}/"
fi
_solve-challenge-7
local score="$(_get-score)"
sleep 1
Expand All @@ -297,6 +304,7 @@ _solve-challenge-7() {
sleep 5
popd >/dev/null
rm -rf ${git_dir}/* && cp -r ${backup_dir}/* ${git_dir}/
chown -R git:git ${git_dir}
[[ -f "/home/appuser/challenge_8.md" ]]
}

Expand Down

0 comments on commit 8b8f34d

Please sign in to comment.