From 02eefad4328e728b2ef69c70316737297b61a068 Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Tue, 21 May 2024 08:48:45 +0200 Subject: [PATCH 1/3] test: add more tests for the purge case --- test/run.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/run.sh b/test/run.sh index f405d48..34db923 100755 --- a/test/run.sh +++ b/test/run.sh @@ -225,12 +225,23 @@ sleep 3 # do a request after TTL to invalidate the cache res_tmp=$(fetch_time "${CACHED_ENDPOINT}") res1=$(fetch_time "${CACHED_ENDPOINT}") +res1_1=$(fetch_time "${CACHED_ENDPOINT}/cached") +res1_2=$(fetch_time "${CACHED_ENDPOINT}/purged") sleep 1 curl -sL -X PURGE "${CACHED_ENDPOINT}" >/dev/null +curl -sL -X PURGE "${CACHED_ENDPOINT}/purged" >/dev/null res2=$(fetch_time "${CACHED_ENDPOINT}") +res2_1=$(fetch_time "${CACHED_ENDPOINT}/cached") +res2_2=$(fetch_time "${CACHED_ENDPOINT}/purged") if [ "${res1}" -eq "${res2}" ]; then error "cache was not purged" fi +if [ "${res1_1}" -ne "${res2_1}" ]; then + error "cache was purged" +fi +if [ "${res1_2}" -eq "${res2_2}" ]; then + error "cache was not purged" +fi # If we are at this point, no test failed From 413f7b36381039014094a9726707c7a585b494a0 Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Tue, 21 May 2024 13:04:33 +0200 Subject: [PATCH 2/3] test: purge POST requests scenario --- config/default.vcl | 4 ++++ test/app/package-lock.json | 41 ++++++++++++++++++++++++++++++++------ test/app/package.json | 1 + test/app/src/index.ts | 17 +++++++++------- test/run.sh | 19 ++++++++++++++++++ 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/config/default.vcl b/config/default.vcl index ccf2613..550016d 100644 --- a/config/default.vcl +++ b/config/default.vcl @@ -78,6 +78,8 @@ sub vcl_backend_fetch { if (bereq.http.X-Body-Len) { set bereq.method = "POST"; } + + return (fetch); } # Indicate whether the response was served from cache or not @@ -88,4 +90,6 @@ sub vcl_deliver { } else { set resp.http.X-Cache = "MISS"; } + + return (deliver); } diff --git a/test/app/package-lock.json b/test/app/package-lock.json index bf2831d..21d8e7f 100644 --- a/test/app/package-lock.json +++ b/test/app/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "MIT", "dependencies": { + "@fastify/formbody": "^7.4.0", "fastify": "^4.27.0" }, "devDependencies": { @@ -121,6 +122,15 @@ "fast-json-stringify": "^5.7.0" } }, + "node_modules/@fastify/formbody": { + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@fastify/formbody/-/formbody-7.4.0.tgz", + "integrity": "sha512-H3C6h1GN56/SMrZS8N2vCT2cZr7mIHzBHzOBa5OPpjfB/D6FzP9mMpE02ZzrFX0ANeh0BAJdoXKOF2e7IbV+Og==", + "dependencies": { + "fast-querystring": "^1.0.0", + "fastify-plugin": "^4.0.0" + } + }, "node_modules/@fastify/merge-json-schemas": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/@fastify/merge-json-schemas/-/merge-json-schemas-0.1.1.tgz", @@ -1811,6 +1821,11 @@ "toad-cache": "^3.3.0" } }, + "node_modules/fastify-plugin": { + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-4.5.1.tgz", + "integrity": "sha512-stRHYGeuqpEZTL1Ef0Ovr2ltazUT9g844X5z/zEBFLG8RYlpDiOCIG+ATvYEp+/zmc7sN29mcIMp8gvYplYPIQ==" + }, "node_modules/fastq": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", @@ -3868,9 +3883,9 @@ } }, "node_modules/word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", "dev": true, "engines": { "node": ">=0.10.0" @@ -3989,6 +4004,15 @@ "fast-json-stringify": "^5.7.0" } }, + "@fastify/formbody": { + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@fastify/formbody/-/formbody-7.4.0.tgz", + "integrity": "sha512-H3C6h1GN56/SMrZS8N2vCT2cZr7mIHzBHzOBa5OPpjfB/D6FzP9mMpE02ZzrFX0ANeh0BAJdoXKOF2e7IbV+Og==", + "requires": { + "fast-querystring": "^1.0.0", + "fastify-plugin": "^4.0.0" + } + }, "@fastify/merge-json-schemas": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/@fastify/merge-json-schemas/-/merge-json-schemas-0.1.1.tgz", @@ -5233,6 +5257,11 @@ "toad-cache": "^3.3.0" } }, + "fastify-plugin": { + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-4.5.1.tgz", + "integrity": "sha512-stRHYGeuqpEZTL1Ef0Ovr2ltazUT9g844X5z/zEBFLG8RYlpDiOCIG+ATvYEp+/zmc7sN29mcIMp8gvYplYPIQ==" + }, "fastq": { "version": "1.17.1", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.1.tgz", @@ -6675,9 +6704,9 @@ } }, "word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", "dev": true }, "wrappy": { diff --git a/test/app/package.json b/test/app/package.json index 6a9d1ac..8156827 100644 --- a/test/app/package.json +++ b/test/app/package.json @@ -31,6 +31,7 @@ "typescript": "^5.4.5" }, "dependencies": { + "@fastify/formbody": "^7.4.0", "fastify": "^4.27.0" } } diff --git a/test/app/src/index.ts b/test/app/src/index.ts index b900d34..986c440 100644 --- a/test/app/src/index.ts +++ b/test/app/src/index.ts @@ -1,17 +1,20 @@ -import fastify from 'fastify'; +import fastify from "fastify"; +import fastifyFormbody from "@fastify/formbody"; // fetch values from environment variables const port = process.env.SERVER_PORT || 8080; -const host = process.env.SERVER_HOST || '::'; +const host = process.env.SERVER_HOST || "::"; // init fastify const server = fastify({ logger: true, }); +server.register(fastifyFormbody); + // default route -server.all('/', async () => ({ - hello: 'world', +server.all("/", async () => ({ + hello: "world", time: Date.now(), })); @@ -20,9 +23,9 @@ server.all<{ Params: { code: number; }; -}>('/error/:code', async (request, reply) => { +}>("/error/:code", async (request, reply) => { reply.code(request.params.code).send({ - hello: 'error', + hello: "error", time: Date.now(), code: request.params.code, }); @@ -33,7 +36,7 @@ server.all<{ Params: { name: string; }; -}>('/:name', async (request) => ({ +}>("/:name", async (request) => ({ hello: request.params.name, time: Date.now(), })); diff --git a/test/run.sh b/test/run.sh index 34db923..13c79c3 100755 --- a/test/run.sh +++ b/test/run.sh @@ -224,6 +224,8 @@ info "Check if we can purge a cache entry…" sleep 3 # do a request after TTL to invalidate the cache res_tmp=$(fetch_time "${CACHED_ENDPOINT}") +res_tmp=$(fetch_time "${CACHED_ENDPOINT}/cached") +res_tmp=$(fetch_time "${CACHED_ENDPOINT}/purged") res1=$(fetch_time "${CACHED_ENDPOINT}") res1_1=$(fetch_time "${CACHED_ENDPOINT}/cached") res1_2=$(fetch_time "${CACHED_ENDPOINT}/purged") @@ -244,6 +246,23 @@ if [ "${res1_2}" -eq "${res2_2}" ]; then fi +info "Check if we can purge a cache entry (POST scenario)…" +# We cache a POST request +req1=$(curl -sL -X POST --data '{"foo": "bar"}' http://localhost:8081/test-cache-purge | jq .time) +# We cache another POST request +req2=$(curl -sL -X POST --data '{"foo": "foobar"}' http://localhost:8081/test-cache-purge | jq .time) +# We request the first POST request to be purged +curl -sL -X PURGE --data '{"foo": "bar"}' http://localhost:8081/test-cache-purge >/dev/null +# We check the requests +req3=$(curl -sL -X POST --data '{"foo": "foo"}' http://localhost:8081/test-cache-purge | jq .time) +req4=$(curl -sL -X POST --data '{"foo": "foobar"}' http://localhost:8081/test-cache-purge | jq .time) +if [ "${req1}" -eq "${req3}" ]; then + error "cache was not purged" +fi +if [ "${req2}" -ne "${req4}" ]; then + error "cache was purged" +fi + # If we are at this point, no test failed info "All tests passed :)" docker compose down From d247546ba459404bd419305266c772a4e441891c Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Tue, 21 May 2024 15:15:51 +0200 Subject: [PATCH 3/3] feat: enable logs --- .changeset/sour-humans-run.md | 6 ++++++ Dockerfile | 1 + README.md | 1 + entrypoint.sh | 11 +++++++++-- 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .changeset/sour-humans-run.md diff --git a/.changeset/sour-humans-run.md b/.changeset/sour-humans-run.md new file mode 100644 index 0000000..53c6e3a --- /dev/null +++ b/.changeset/sour-humans-run.md @@ -0,0 +1,6 @@ +--- +"varnish-post": minor +--- + +It is now possible to enable logs, by setting `ENABLE_LOGS` to `true`, which is now the default value. +To disable them, just put any other value, like `false` for example. diff --git a/Dockerfile b/Dockerfile index c0850f8..a42b373 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ ENV VARNISH_SIZE="100M" ENV DISABLE_ERROR_CACHING="true" ENV DISABLE_ERROR_CACHING_TTL="30s" ENV CONFIG_FILE="default.vcl" +ENV ENABLE_LOGS="true" # Install some dependencies RUN apt-get update \ diff --git a/README.md b/README.md index 4be705b..9eb6e7e 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,4 @@ You can use following environment variables for configuration: - `DISABLE_ERROR_CACHING`: disable the caching of errors (default: `true`) - `DISABLE_ERROR_CACHING_TTL`: time where requests should be directly sent to the backend after an error occured (default: `30s`) - `CONFIG_FILE`: the name of the configuration file to use (default: `default.vcl`) +- `ENABLE_LOGS`: enable logs (default: `true`) diff --git a/entrypoint.sh b/entrypoint.sh index 998b62c..b46ad1d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,8 +1,10 @@ #!/bin/sh +ENABLE_LOGS="${ENABLE_LOGS}" + set -eu -# environment variables substitution +# Environment variables substitution for SRC_LOCATION in $(find /templates -type f); do DST_LOCATION=$(echo "${SRC_LOCATION}" | sed 's/^\/templates/\/etc\/varnish/') envsubst \ @@ -11,9 +13,14 @@ for SRC_LOCATION in $(find /templates -type f); do echo "INFO: generated '${DST_LOCATION}' from '${SRC_LOCATION}' (environment variables substitution)" done +# Display logs if configured +if [ "${ENABLE_LOGS}" = "true" ]; then + varnishncsa& +fi + set -x -# run varnish +# Run Varnish varnishd \ -F \ -f "/etc/varnish/${CONFIG_FILE}" \