Skip to content

Commit fbe8b7e

Browse files
committed
Merge branch 'feature/centos-tests' into develop
2 parents 8fd2486 + 90ed2f7 commit fbe8b7e

File tree

6 files changed

+127
-36
lines changed

6 files changed

+127
-36
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,24 @@ The ```extras/.githooks/pre-commit``` will run shellcheck, local tests as well a
140140

141141
#### With Docker
142142

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

145145
```bash
146-
docker build --build-arg factorio_version=1.0.0 --tag finit:latest - < extras/Dockerfile
146+
docker build --build-arg ubuntu_version=20.04 \
147+
--build-arg factorio_version=1.0.0 \
148+
--tag ubuntu-finit:latest - < extras/docker/Dockerfile.ubuntu
149+
docker build --build-arg centos_version=centos8 \
150+
--build-arg factorio_version=1.0.0 \
151+
--tag centos-finit:latest - < extras/docker/Dockerfile.centos
147152
```
148153

149154
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(!)
150155

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

153158
```bash
154-
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init finit:latest extras/test
159+
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init ubuntu-finit:latest extras/test
160+
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init centos-finit:latest extras/test
155161
```
156162

157163
#### Manually

extras/.githooks/pre-commit

+49-23
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,59 @@
33
set -o pipefail
44
set -u
55

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

10+
# try to clean up before we quit from now on
11+
function __clean(){
12+
if [ "$stash_size" -lt "$(git stash list |wc -l)" ]; then
13+
# we added stuff earlier, let us restore it now
14+
git stash pop || exit 1
15+
fi
16+
return 0
17+
}
18+
trap __clean SIGINT SIGTERM ERR EXIT
19+
920
# find out where this repo is situated
1021
root_dir="$(git rev-parse --show-toplevel)"
22+
dockerfiles="${root_dir}/extras/docker/Dockerfile"
1123

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

18-
# do shellcheck on factorio script
19-
docker run --rm -v "$root_dir:/mnt" koalaman/shellcheck:stable factorio &&
2028
# run local tests
21-
"${root_dir}/extras/test/libs/bats-core/bin/bats" "${root_dir}/extras/test" &&
22-
# build docker images
23-
docker build --tag "$with_resources" - < "${root_dir}"/extras/Dockerfile &&
24-
docker build --target no-test-resources --tag "$sans_resources" - < "${root_dir}"/extras/Dockerfile &&
25-
# run tests on docker
26-
docker run -t --rm -v "$root_dir:/opt/factorio-init" "$sans_resources" --jobs 10 extras/test &&
27-
docker run -t --rm -v "$root_dir:/opt/factorio-init" "$with_resources" --jobs 10 extras/test
28-
test_return=$?
29-
30-
if [ $STASH_SIZE -lt $(git stash list |wc -l) ]; then
31-
# we added stuff earlier, let us restore it now
32-
git stash pop || exit 1
33-
fi
34-
35-
exit $test_return
29+
"${root_dir}/extras/test/libs/bats-core/bin/bats" "${root_dir}/extras/test" || exit 1
30+
31+
# run dockerized tests
32+
function dockertest(){
33+
dist="$1"; shift
34+
versions="$1"; shift
35+
fversion="$1"; shift
36+
targets="$1"; shift
37+
IFS=" " read -r -a extra_args <<< "${1:-}"
38+
39+
for version in $versions; do
40+
image="${dist}-${version}"
41+
for target in $targets; do
42+
echo
43+
echo "Testing ${image}:${target} (factorio ${fversion})"
44+
echo
45+
docker build --build-arg "${dist}_version=${version}" \
46+
--build-arg "factorio_version=${fversion}" \
47+
--target "${target}" \
48+
--tag "${image}:${target}" - < "${dockerfiles}.${dist}" || return 1
49+
options=(-v "${root_dir}:/opt/factorio-init" "${image}:${target}" "${extra_args[@]}" extras/test)
50+
docker run -t --rm "${options[@]}" || return 1
51+
done
52+
done
53+
}
54+
55+
factorio_version="1.0.0"
56+
targets="with-test-resources sans-test-resources"
57+
dockertest "ubuntu" "20.04 18.04" "${factorio_version}" "${targets}" "--jobs 10" &&
58+
dockertest "centos" "centos8" "${factorio_version}" "${targets}" || exit 1
59+
dockertest "centos" "centos7" "${factorio_version}" "with-glibc-sidebyside" || exit 1
60+
61+
__clean || exit 1

extras/docker/Dockerfile.centos

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
ARG centos_version=centos8
2+
ARG factorio_version=1.0.0
3+
ARG factorio_user=factorio
4+
5+
### A base image without test resources:
6+
FROM centos:$centos_version AS sans-test-resources
7+
ARG factorio_user
8+
RUN yum update -y && \
9+
yum install -y \
10+
wget && \
11+
yum clean all && \
12+
rm -rf /var/cache/yum
13+
RUN useradd $factorio_user
14+
USER $factorio_user
15+
WORKDIR /opt/factorio-init
16+
ENTRYPOINT ["bash", "/opt/factorio-init/extras/test/libs/bats-core/bin/bats"]
17+
18+
### Build onto the base, add test resources:
19+
FROM sans-test-resources AS with-test-resources
20+
ARG factorio_version
21+
ENV FACTORIO_INIT_WITH_TEST_RESOURCES=1
22+
RUN wget -O /tmp/factorio_headless_x64_${factorio_version}.tar.xz \
23+
https://factorio.com/get-download/${factorio_version}/headless/linux64
24+
25+
### Build onto the with-test-resources for alternate glibc (for centos7 testing)
26+
FROM with-test-resources AS with-glibc-sidebyside
27+
ARG factorio_user
28+
USER root
29+
RUN yum groupinstall -y \
30+
"Development tools" && \
31+
yum install -y \
32+
glibc-devel.i686 \
33+
glibc.i686 && \
34+
yum clean all && \
35+
rm -rf /var/cache/yum
36+
WORKDIR /tmp
37+
RUN wget -q http://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz -O - |tar -xvz
38+
WORKDIR /tmp/glibc-2.18/glibc-build
39+
RUN ../configure --prefix='/opt/glibc-2.18' && \
40+
sed -i -e 's#if (/$ld_so_name/) {#if (/\Q$ld_so_name\E/) {#' \
41+
../scripts/test-installation.pl && \
42+
make && \
43+
make install # these take a while (~5 minutes on a i5 3.5GHz 32GB RAM WSL2 box)
44+
USER $factorio_user
45+
ENV FACTORIO_INIT_ALT_GLIBC=1
46+
WORKDIR /opt/factorio-init

extras/Dockerfile renamed to extras/docker/Dockerfile.ubuntu

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ARG ubuntu_version=20.04
22

33
### A base image without test resources:
4-
FROM ubuntu:$ubuntu_version AS no-test-resources
4+
FROM ubuntu:$ubuntu_version AS sans-test-resources
55
ARG factorio_user=factorio
66
ARG factorio_group=factorio
77

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

2020
### Build onto the base, add test resources:
21-
FROM no-test-resources AS with-test-resources
21+
FROM sans-test-resources AS with-test-resources
2222
ENV FACTORIO_INIT_WITH_TEST_RESOURCES=1
2323
ARG factorio_version=1.0.0
2424

extras/test/factorio.bats

+18-7
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,22 @@ Aborting install, unable to curl '${LATEST_HEADLESS_URL}'"
235235

236236
@test ".install uses cached tarball" {
237237
[ -z "${FACTORIO_INIT_WITH_TEST_RESOURCES}" ] && skip "We are not running tests with resources"
238+
238239
load 'tmp-helper'
239240
load 'http-mock-helper'
240241
source $factorio_script
241242

242243
mock_curl "${CURL_LATEST_STABLE_HEAD_302_200}" 0
243244
mock_wget_fail
244245

245-
load_config ./config.example
246-
FACTORIO_PATH="`create_tmp_empty_dir`"
247-
BINARY="${FACTORIO_PATH}/bin/x64/factorio"
246+
config_file="${BATS_TMPDIR}/config"
247+
cp ./config.example "${config_file}"
248+
if [ -n "${FACTORIO_INIT_ALT_GLIBC}" ]; then
249+
sed -i -e 's/ALT_GLIBC=0/ALT_GLIBC=1/' "${config_file}"
250+
fi
251+
sed -i -e 's#FACTORIO_PATH=/opt/factorio#FACTORIO_PATH="`create_tmp_empty_dir`"#' "${config_file}"
252+
load_config "${config_file}"
253+
248254
INSTALL_CACHE_TAR=1
249255
USERNAME=`whoami`
250256
USERGROUP=`whoami`
@@ -265,10 +271,15 @@ Aborting install, unable to curl '${LATEST_HEADLESS_URL}'"
265271
mock_curl_fail
266272
mock_wget_fail
267273

268-
load_config ./config.example
269-
FACTORIO_PATH="`create_tmp_empty_dir`"
270-
BINARY="${FACTORIO_PATH}/bin/x64/factorio"
271-
DEBUG=1
274+
config_file="${BATS_TMPDIR}/config"
275+
cp ./config.example "${config_file}"
276+
if [ -n "${FACTORIO_INIT_ALT_GLIBC}" ]; then
277+
sed -i -e 's/ALT_GLIBC=0/ALT_GLIBC=1/' "${config_file}"
278+
fi
279+
sed -i -e 's#FACTORIO_PATH=/opt/factorio#FACTORIO_PATH="`create_tmp_empty_dir`"#' "${config_file}"
280+
load_config "${config_file}"
281+
282+
DEBUG=1
272283
tarball="/tmp/factorio_headless_x64_1.0.0.tar.xz"
273284

274285
run install "${tarball}"

factorio

+3-1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Available commands:
242242
}
243243

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

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

0 commit comments

Comments
 (0)