diff options
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/files-changed | 25 | ||||
| -rwxr-xr-x | bin/gen-cgroup-testdata | 48 | ||||
| -rwxr-xr-x | bin/snapshot-cgroup | 42 |
3 files changed, 115 insertions, 0 deletions
diff --git a/bin/files-changed b/bin/files-changed new file mode 100755 index 0000000..3ffbad6 --- /dev/null +++ b/bin/files-changed @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Detect if any files have changed in the git repository. Output an appropriate +# error message if they have changed. + +if [[ -n $(git status -s) ]]; then + git diff --minimal HEAD + echo + echo "**************************************************" + case "$1" in + "proto") + echo "* .pb.go files and .proto files are out of sync. *" + echo "* Run \"make gen\" to generate them. *" + ;; + "format") + echo "* C or Go files have incorrect formatting. *" + echo "* Run \"make format\" to fix them. *" + ;; + *) + echo "* Files have changed in this repository. *" + ;; + esac + echo "**************************************************" + git reset HEAD --hard + exit 1 +fi
\ No newline at end of file diff --git a/bin/gen-cgroup-testdata b/bin/gen-cgroup-testdata new file mode 100755 index 0000000..3d9215b --- /dev/null +++ b/bin/gen-cgroup-testdata @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# gen-cgroup-testdata - Generate cgroup testdata by running +# bin/snapshot-cgroup inside Docker containers with known resource limits. +# +# Usage: gen-cgroup-testdata +# +# Prerequisites: Docker on a host running cgroup v2. +# +# Each testdata directory contains: +# expected.json - {"cpu_quota": <float>, "memory_limit": <int>} +# proc/ - snapshot of /proc/self/cgroup +# sys/ - snapshot of cgroup control files + +set -euo pipefail + +cd "$(dirname "$0")/.." + +testdata="cgroup/testdata" +snapshot_script="bin/snapshot-cgroup" + +generate() { + local name="$1" cpu_quota="$2" memory_limit="$3" + shift 3 + local outdir="$testdata/$name" + + echo "Generating $name..." + rm -rf "$outdir" + mkdir -p "$outdir" + + docker run --rm \ + --user "$(id -u):$(id -g)" \ + "$@" \ + -v "$PWD/$snapshot_script:/snapshot:ro" \ + -v "$PWD/$outdir:/out" \ + debian:bookworm-slim \ + /snapshot /out + + cat > "$outdir/expected.json" <<EOF +{"cpu_quota": $cpu_quota, "memory_limit": $memory_limit} +EOF +} + +generate "v2-two-cores-256m" 2.0 268435456 --cpus=2 --memory=256m +generate "v2-quarter-core-64m" 0.25 67108864 --cpus=0.25 --memory=64m +generate "v2-no-limit" null null + +echo "v2 testdata generated successfully." diff --git a/bin/snapshot-cgroup b/bin/snapshot-cgroup new file mode 100755 index 0000000..5fed85f --- /dev/null +++ b/bin/snapshot-cgroup @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# +# snapshot-cgroup - Copy cgroup v2 files from the live system into a +# directory tree suitable for use with TestIntegrationCgroupLimits. +# +# Usage: snapshot-cgroup <output-dir> +# +# The script reads /proc/self/cgroup to find the v2 group path and copies +# exactly the files that the cgroup package needs: +# +# proc/self/cgroup +# sys/fs/cgroup/<group>/cpu.max +# sys/fs/cgroup/<group>/memory.max + +set -euo pipefail + +if [[ $# -ne 1 ]]; then + echo "Usage: $0 <output-dir>" >&2 + exit 1 +fi + +out="$1" +mkdir -p "$out" + +copy_file() { + local src="$1" dst="$out/$2" + mkdir -p "$(dirname "$dst")" + cp "$src" "$dst" +} + +copy_file /proc/self/cgroup proc/self/cgroup + +group=$(awk -F: '/^0::/ { print $3 }' /proc/self/cgroup) +cgdir="/sys/fs/cgroup${group}" + +for f in cpu.max memory.max; do + if [[ -f "$cgdir/$f" ]]; then + copy_file "$cgdir/$f" "sys/fs/cgroup${group}/$f" + fi +done + +echo "Snapshot written to $out" |