diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..d57593d --- /dev/null +++ b/.envrc @@ -0,0 +1,2 @@ +dotenv_if_exists .mod_env + diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index dd3547b..c348e52 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,8 +7,8 @@ default: - apk --no-cache add bash stages: - - test - build + - test - release variables: diff --git a/.mod_env b/.mod_env new file mode 100644 index 0000000..3b92f05 --- /dev/null +++ b/.mod_env @@ -0,0 +1,4 @@ +CURRENT_MOD=test_harness +DEPENDENCIES=[{ "url":"https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz", "path":"/usr/local/share/minetest/games/minetest", "strip":1 }] +MINETEST_ADD_CONF= +CLIENT_NB=1 \ No newline at end of file diff --git a/.util/.mod_env.tmpl b/.util/.mod_env.tmpl new file mode 100644 index 0000000..c1f539a --- /dev/null +++ b/.util/.mod_env.tmpl @@ -0,0 +1,4 @@ +CURRENT_MOD=test_harness +CLIENT_NB=1 +DEPENDENCIES=[{ "url":"https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz", "path":"/usr/local/share/minetest/games/minetest", "strip":1 }, { "url":"https://forge.apps.education.fr/iri/minetest/docker_test_harness/-/archive/main/docker_test_harness-main.tar.gz", "path":"/usr/local/share/minetest/games/minetest/mods/test_harness", "strip":1 }] +MINETEST_ADD_CONF= \ No newline at end of file diff --git a/.util/Dockerfile b/.util/Dockerfile index bd5e215..286d155 100644 --- a/.util/Dockerfile +++ b/.util/Dockerfile @@ -1,7 +1,7 @@ -FROM curlimages/curl:latest as dl_base +# -# list of mods and game to download -RUN mkdir -p minetest_game && curl -s -L https://github.com/minetest/minetest_game/archive/refs/heads/master.tar.gz | tar zxvf - -C minetest_game --strip-components=1 +ARG DEPENDENCIES +ARG MINETEST_ADD_CONF="" FROM ghcr.io/minetest/minetest:latest as builder @@ -9,17 +9,24 @@ ENV STOP_SERVER=true ENV FAILFAST=false ENV MINETEST_GAME_PATH=/usr/local/share/minetest/games ENV SHOW_DEBUG=false - +ENV CURRENT_MOD="test_harness" +ENV ADDITIONAL_MODS="" USER root RUN mkdir -p /usr/local/share/minetest/games && \ - apk add --no-cache bash + apk add --no-cache bash curl jq # WORKDIR /config/.minetest/games/devtest/mods - -# Copy from the dl base the game and mods to their correct path in the game or mod folders -COPY --from=dl_base /home/curl_user/minetest_game /usr/local/share/minetest/games/minetest +RUN < /etc/minetest/minetest.conf + test_harness_mods=${CURRENT_MOD}${ADDITIONAL_MODS} test_harness_stop_server=${STOP_SERVER} test_harness_failfast=${FAILFAST} EOT @@ -66,7 +74,6 @@ EOF # Customize the test_harness_mods value to list the mods to test. Empty value means all, including the test harness itself COPY <<-EOF /etc/minetest/minetest.conf.base - test_harness_mods=test_harness test_harness_run_tests=true max_forceloaded_blocks=9999 name=admin @@ -79,6 +86,7 @@ COPY <<-EOF /etc/minetest/minetest.conf.base water_level=-2 mgflat_ground_level=-1 mg_flags=nocaves,nodungeons,nolight,nodecorations,nobiomes,noores + ${MINETEST_ADD_CONF} EOF RUN chown -R minetest:minetest /var/lib/minetest/.minetest /usr/local/sbin/run_minetest && \ diff --git a/.util/docker-compose.yaml b/.util/docker-compose.yaml index bc580d4..4aca421 100644 --- a/.util/docker-compose.yaml +++ b/.util/docker-compose.yaml @@ -4,23 +4,17 @@ services: restart: "no" build: dockerfile: Dockerfile + args: + DEPENDENCIES: "${DEPENDENCIES}" + MINETEST_ADD_CONF: "${MINETEST_ADD_CONF:-}" ports: - "30000:30000/udp" volumes: - - "$PWD:/usr/local/share/minetest/games/minetest/mods/test_harness" + - "$PWD:/usr/local/share/minetest/games/minetest/mods/${CURRENT_MOD}" environment: SHOW_DEBUG: ${SHOW_DEBUG} RUN_TESTS: ${RUN_TESTS} STOP_SERVER: ${STOP_SERVER} FAILFAST: ${FAILFAST} - client1: - image: ${CLIENT_IMAGE} - restart: on-failure:10 - depends_on: - - "server" - environment: - SERVER: server - PORT: 30000 - PLAYERNAME: player1 - RANDOM_INPUT: 0 - PASSWORD: test \ No newline at end of file + CURRENT_MOD: "${CURRENT_MOD}" + ADDITIONAL_MODS: "${ADDITIONAL_MODS:-}" diff --git a/.util/run_tests.sh b/.util/run_tests.sh index e8929a4..609cc2a 100755 --- a/.util/run_tests.sh +++ b/.util/run_tests.sh @@ -7,25 +7,32 @@ script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) usage() { cat < /dev/null)" ]; then + die "CLIENT IMAGE $CLIENT_IMAGE does not exists" +fi +export ENV_FILE="" +if [ -f "$PWD/.mod_env" ]; then + ENV_FILE="--env-file \"$PWD/.mod_env\"" +fi -eval $docker_cmd compose -f "${script_dir}/docker-compose.yaml" down -eval $docker_cmd compose -f "${script_dir}/docker-compose.yaml" pull -eval $docker_cmd compose -f "${script_dir}/docker-compose.yaml" build --pull -eval $docker_cmd compose -f "${script_dir}/docker-compose.yaml" up --force-recreate --exit-code-from server --abort-on-container-exit +docker_compose_file=`mktemp --suffix=.yaml docker-composeXXXXXX` + +cat "${script_dir}/docker-compose.yaml" > $docker_compose_file +for (( clienti=1; clienti<=$client_nb; clienti++ )); do + cat <> $docker_compose_file + client${clienti}: + image: \${CLIENT_IMAGE} + restart: on-failure:10 + depends_on: + - "server" + environment: + SERVER: server + PORT: 30000 + PLAYERNAME: player${clienti} + RANDOM_INPUT: 0 + PASSWORD: test +EOF + +done + + +eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" down +eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" pull +eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" build --pull +eval $docker_cmd compose $ENV_FILE -f "${$docker_compose_file}" up --force-recreate --exit-code-from server --abort-on-container-exit exit_code=$? exit $exit_code \ No newline at end of file diff --git a/README.md b/README.md index 8e594d0..041256e 100644 --- a/README.md +++ b/README.md @@ -21,30 +21,24 @@ The tests are run using Docker or Podman. Therefor you'll need to have either on This mod's goal is to provide a test framework for implementing tests for minetest mods. 1. Declare `test_harness` as an optional dependencies for your mod -2. Clone or download the `docker-test-harness` mod. -3. Place the `test_harness` directory into the `mods` folder of your Minetest world. -4. copy the `.util` folder in your project -5. Customize the `.util/Dockerfile` to include the mods and game your project depends on (download and copy) -6. Customize the `.util/Dockerfile` to adapt the `/etc/minetest/minetest.conf.base` file to list the mods you want to test (`test_harness_mods`), and to adjust the server configuration to your project test settings. -7. Do not forget the following setting in `/etc/minetest/minetest.conf.base` to enable automatic test execution:/etc/minetest/minetest.conf.base - ```ini - test_harness_run_tests = true - ``` -8. In your source files (in the `init.lua` for example) add the following lines (or equivalent) +2. copy the `.util` folder in your project +3. copy the `.util/.mod_env.tmpl` to `.mod_env` at the root of your folder. +4. adapt its content. Do not remove the test_harness dependency definition. +5. if necessary, customize the `.util/Dockerfile` to adapt the `/etc/minetest/minetest.conf.base` file to list the mods you want to test (`test_harness_mods`), and to adjust the server configuration to your project test settings. +6. In your source files (in the `init.lua` for example) add the following lines (or equivalent) ```lua if minetest.settings:get_bool("test_harness_run_tests", false) then dofile(minetest.get_modpath("my_mod").. "/test/init.lua") end ``` -9. In your test file(s), get an instance of the method allowing the registration of your tests: +7. In your test file(s), get an instance of the method allowing the registration of your tests: ```lua local register_test = test_harness.get_test_registrator("my_mod", my_mod.version_string) ``` -10. Register the test by calling `register_test` -11. Adapt the `.util/docker-compose.yaml` to declare all the client intances with their players needed by your tests -12. Run the test by lauching the `run_tests.sh` script. For example with Podman +8. Register the test by calling `register_test` +9. Run the test by lauching the `run_tests.sh` script. For example with Podman ```shell - $ .util/run_tests.sh --podman + $ .util/run_tests.sh --podman -c 1 ``` See the available options with `.util/run_tests.sh --help` diff --git a/client/Dockerfile b/client/Dockerfile index d41598b..aa2478f 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -1,4 +1,4 @@ -ARG OPENGL_BASE_IMAGE="registry.apps.education.fr/iri/minetest/docker-test-harness/opengl:latest" +ARG OPENGL_BASE_IMAGE="registry.apps.education.fr/iri/minetest/docker_test_harness/opengl:latest" FROM alpine:latest AS compile ARG MINETEST_VERSION=5.9.1