Skip to content

Commit

Permalink
Merge branch 'feature/centos-tests' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisa committed Nov 21, 2020
2 parents 8fd2486 + 90ed2f7 commit fbe8b7e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 36 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,24 @@ The ```extras/.githooks/pre-commit``` will run shellcheck, local tests as well a

#### With Docker

- Build the docker image (a slightly modified ubuntu by default)
- Build the docker image(s) (a slightly modified ubuntu/centos)

```bash
docker build --build-arg factorio_version=1.0.0 --tag finit:latest - < extras/Dockerfile
docker build --build-arg ubuntu_version=20.04 \
--build-arg factorio_version=1.0.0 \
--tag ubuntu-finit:latest - < extras/docker/Dockerfile.ubuntu
docker build --build-arg centos_version=centos8 \
--build-arg factorio_version=1.0.0 \
--tag centos-finit:latest - < extras/docker/Dockerfile.centos
```

Adding ```--target no-test-resources``` to the build command will avoid downloading test resources online but it will also skip tests that rely on the resources(!)

- Then run the image, mounting the current directory and removing the container once it's done

```bash
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init finit:latest extras/test
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init ubuntu-finit:latest extras/test
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init centos-finit:latest extras/test
```

#### Manually
Expand Down
72 changes: 49 additions & 23 deletions extras/.githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,59 @@
set -o pipefail
set -u

STASH_SIZE="$(git stash list |wc -l)" # remember how big the stash is before we try to stash
# remember how big the stash is before we try to stash
stash_size="$(git stash list |wc -l)"
git stash push --keep-index --include-untracked --message "pre-commit-hook-$(date +%s)" || exit 1

# try to clean up before we quit from now on
function __clean(){
if [ "$stash_size" -lt "$(git stash list |wc -l)" ]; then
# we added stuff earlier, let us restore it now
git stash pop || exit 1
fi
return 0
}
trap __clean SIGINT SIGTERM ERR EXIT

# find out where this repo is situated
root_dir="$(git rev-parse --show-toplevel)"
dockerfiles="${root_dir}/extras/docker/Dockerfile"

# setup some tags
image_id="finit"
base_tag="latest"
with_resources="${image_id}:${base_tag}-with-resources"
sans_resources="${image_id}:${base_tag}-sans-resources"
# run shellcheck
docker run --rm -v "${root_dir}:/mnt" koalaman/shellcheck:stable extras/.githooks/pre-commit &&
docker run --rm -v "${root_dir}:/mnt" koalaman/shellcheck:stable factorio || exit 1

# do shellcheck on factorio script
docker run --rm -v "$root_dir:/mnt" koalaman/shellcheck:stable factorio &&
# run local tests
"${root_dir}/extras/test/libs/bats-core/bin/bats" "${root_dir}/extras/test" &&
# build docker images
docker build --tag "$with_resources" - < "${root_dir}"/extras/Dockerfile &&
docker build --target no-test-resources --tag "$sans_resources" - < "${root_dir}"/extras/Dockerfile &&
# run tests on docker
docker run -t --rm -v "$root_dir:/opt/factorio-init" "$sans_resources" --jobs 10 extras/test &&
docker run -t --rm -v "$root_dir:/opt/factorio-init" "$with_resources" --jobs 10 extras/test
test_return=$?

if [ $STASH_SIZE -lt $(git stash list |wc -l) ]; then
# we added stuff earlier, let us restore it now
git stash pop || exit 1
fi

exit $test_return
"${root_dir}/extras/test/libs/bats-core/bin/bats" "${root_dir}/extras/test" || exit 1

# run dockerized tests
function dockertest(){
dist="$1"; shift
versions="$1"; shift
fversion="$1"; shift
targets="$1"; shift
IFS=" " read -r -a extra_args <<< "${1:-}"

for version in $versions; do
image="${dist}-${version}"
for target in $targets; do
echo
echo "Testing ${image}:${target} (factorio ${fversion})"
echo
docker build --build-arg "${dist}_version=${version}" \
--build-arg "factorio_version=${fversion}" \
--target "${target}" \
--tag "${image}:${target}" - < "${dockerfiles}.${dist}" || return 1
options=(-v "${root_dir}:/opt/factorio-init" "${image}:${target}" "${extra_args[@]}" extras/test)
docker run -t --rm "${options[@]}" || return 1
done
done
}

factorio_version="1.0.0"
targets="with-test-resources sans-test-resources"
dockertest "ubuntu" "20.04 18.04" "${factorio_version}" "${targets}" "--jobs 10" &&
dockertest "centos" "centos8" "${factorio_version}" "${targets}" || exit 1
dockertest "centos" "centos7" "${factorio_version}" "with-glibc-sidebyside" || exit 1

__clean || exit 1
46 changes: 46 additions & 0 deletions extras/docker/Dockerfile.centos
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ARG centos_version=centos8
ARG factorio_version=1.0.0
ARG factorio_user=factorio

### A base image without test resources:
FROM centos:$centos_version AS sans-test-resources
ARG factorio_user
RUN yum update -y && \
yum install -y \
wget && \
yum clean all && \
rm -rf /var/cache/yum
RUN useradd $factorio_user
USER $factorio_user
WORKDIR /opt/factorio-init
ENTRYPOINT ["bash", "/opt/factorio-init/extras/test/libs/bats-core/bin/bats"]

### Build onto the base, add test resources:
FROM sans-test-resources AS with-test-resources
ARG factorio_version
ENV FACTORIO_INIT_WITH_TEST_RESOURCES=1
RUN wget -O /tmp/factorio_headless_x64_${factorio_version}.tar.xz \
https://factorio.com/get-download/${factorio_version}/headless/linux64

### Build onto the with-test-resources for alternate glibc (for centos7 testing)
FROM with-test-resources AS with-glibc-sidebyside
ARG factorio_user
USER root
RUN yum groupinstall -y \
"Development tools" && \
yum install -y \
glibc-devel.i686 \
glibc.i686 && \
yum clean all && \
rm -rf /var/cache/yum
WORKDIR /tmp
RUN wget -q http://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz -O - |tar -xvz
WORKDIR /tmp/glibc-2.18/glibc-build
RUN ../configure --prefix='/opt/glibc-2.18' && \
sed -i -e 's#if (/$ld_so_name/) {#if (/\Q$ld_so_name\E/) {#' \
../scripts/test-installation.pl && \
make && \
make install # these take a while (~5 minutes on a i5 3.5GHz 32GB RAM WSL2 box)
USER $factorio_user
ENV FACTORIO_INIT_ALT_GLIBC=1
WORKDIR /opt/factorio-init
4 changes: 2 additions & 2 deletions extras/Dockerfile → extras/docker/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG ubuntu_version=20.04

### A base image without test resources:
FROM ubuntu:$ubuntu_version AS no-test-resources
FROM ubuntu:$ubuntu_version AS sans-test-resources
ARG factorio_user=factorio
ARG factorio_group=factorio

Expand All @@ -18,7 +18,7 @@ WORKDIR /opt/factorio-init
ENTRYPOINT ["bash", "/opt/factorio-init/extras/test/libs/bats-core/bin/bats"]

### Build onto the base, add test resources:
FROM no-test-resources AS with-test-resources
FROM sans-test-resources AS with-test-resources
ENV FACTORIO_INIT_WITH_TEST_RESOURCES=1
ARG factorio_version=1.0.0

Expand Down
25 changes: 18 additions & 7 deletions extras/test/factorio.bats
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,22 @@ Aborting install, unable to curl '${LATEST_HEADLESS_URL}'"

@test ".install uses cached tarball" {
[ -z "${FACTORIO_INIT_WITH_TEST_RESOURCES}" ] && skip "We are not running tests with resources"

load 'tmp-helper'
load 'http-mock-helper'
source $factorio_script

mock_curl "${CURL_LATEST_STABLE_HEAD_302_200}" 0
mock_wget_fail

load_config ./config.example
FACTORIO_PATH="`create_tmp_empty_dir`"
BINARY="${FACTORIO_PATH}/bin/x64/factorio"
config_file="${BATS_TMPDIR}/config"
cp ./config.example "${config_file}"
if [ -n "${FACTORIO_INIT_ALT_GLIBC}" ]; then
sed -i -e 's/ALT_GLIBC=0/ALT_GLIBC=1/' "${config_file}"
fi
sed -i -e 's#FACTORIO_PATH=/opt/factorio#FACTORIO_PATH="`create_tmp_empty_dir`"#' "${config_file}"
load_config "${config_file}"

INSTALL_CACHE_TAR=1
USERNAME=`whoami`
USERGROUP=`whoami`
Expand All @@ -265,10 +271,15 @@ Aborting install, unable to curl '${LATEST_HEADLESS_URL}'"
mock_curl_fail
mock_wget_fail

load_config ./config.example
FACTORIO_PATH="`create_tmp_empty_dir`"
BINARY="${FACTORIO_PATH}/bin/x64/factorio"
DEBUG=1
config_file="${BATS_TMPDIR}/config"
cp ./config.example "${config_file}"
if [ -n "${FACTORIO_INIT_ALT_GLIBC}" ]; then
sed -i -e 's/ALT_GLIBC=0/ALT_GLIBC=1/' "${config_file}"
fi
sed -i -e 's#FACTORIO_PATH=/opt/factorio#FACTORIO_PATH="`create_tmp_empty_dir`"#' "${config_file}"
load_config "${config_file}"

DEBUG=1
tarball="/tmp/factorio_headless_x64_1.0.0.tar.xz"

run install "${tarball}"
Expand Down
4 changes: 3 additions & 1 deletion factorio
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ Available commands:
}

as_user() {
debug "as_user: $1}"
if [ "$ME" == "$USERNAME" ]; then # Are we the factorio user?
bash -c "$1"
elif [ "$(id -u)" == "0" ]; then # Are we root?
Expand Down Expand Up @@ -531,7 +532,8 @@ install(){
fi

# Generate default config & create a default save-game to play on
if as_user "\"${BINARY}\" --create \"${target}/saves/server-save\" ${EXE_ARGS_GLIBC}"; then
debug "EXE_ARGS_GLIBC: ${EXE_ARGS_GLIBC}"
if as_user "${BINARY} --create ${target}/saves/server-save ${EXE_ARGS_GLIBC}"; then
if ! as_user "cp \"${target}/data/server-settings.example.json\" \"${target}/data/server-settings.json\""; then
error "WARNING! Unable to copy server settings, may need to be resolved manually."
fi
Expand Down

0 comments on commit fbe8b7e

Please sign in to comment.