diff --git a/containers/Containerfile.test b/containers/Containerfile.test new file mode 100644 index 0000000..ddc07e1 --- /dev/null +++ b/containers/Containerfile.test @@ -0,0 +1,17 @@ +FROM debian:trixie +MAINTAINER Juno Takano juno@jutty.dev + +RUN apt-get --update install -y sudo +RUN useradd -mU sudoer +RUN echo "sudoer ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + +USER sudoer +WORKDIR /home/sudoer +RUN mkdir -p .config/tori +RUN touch .config/tori/tori.conf +RUN echo "su_command = sudo" > .config/tori/tori.conf + +COPY tori /usr/local/bin/tori +COPY test.sh /usr/local/bin/test.sh + +CMD test.sh diff --git a/containers/Containerfile.test-clean b/containers/Containerfile.test-clean new file mode 100644 index 0000000..6167460 --- /dev/null +++ b/containers/Containerfile.test-clean @@ -0,0 +1,20 @@ +FROM debian:trixie +MAINTAINER Juno Takano juno@jutty.dev + +# RUN apt-get --update install -y sudo +# RUN useradd -mU sudoer +# RUN echo "sudoer ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers + +RUN mkdir -p /root/.config/tori +RUN touch /root/.config/tori/tori.conf + +# USER sudoer +# WORKDIR /home/sudoer +# RUN mkdir -p .config/tori +# RUN touch .config/tori/tori.conf +# RUN echo "su_command = sudo" > .config/tori/tori.conf + +COPY tori /usr/local/bin/tori +COPY test-clean.sh /usr/local/bin/test-clean.sh + +CMD test-clean.sh diff --git a/containers/build-run.sh b/containers/build-run.sh new file mode 100755 index 0000000..f577572 --- /dev/null +++ b/containers/build-run.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +set -eu + +./build.sh "$1" && clear +./run.sh "$1" diff --git a/containers/build.sh b/containers/build.sh new file mode 100755 index 0000000..affcd99 --- /dev/null +++ b/containers/build.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env sh + +set -eu +suffix=$(printf '%s' "$1" | sed 's/.*\.//') +binary=tori +tag="$binary:$suffix" +shift + +if podman container exists "$tag"; then + podman stop --time 3 "$tag" +fi + +if ! [ -f "../target/debug/$binary" ]; then + cd .. + cargo build + cd - +fi + +cp -v ../target/debug/$binary $binary + +podman build \ + --tag "$tag" \ + -f "Containerfile.$suffix" "$@" + +if [ -f $binary ]; then + rm -v $binary +fi diff --git a/containers/run.sh b/containers/run.sh new file mode 100755 index 0000000..207865f --- /dev/null +++ b/containers/run.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env sh + +set -eu +suffix=$(printf '%s' "$1" | sed 's/.*\.//') +binary=tori +name="$binary-$suffix" +tag="$binary:$suffix" +shift + +podman run \ + --replace \ + --name "$name" \ + --publish 3008:80 \ + --init \ + "$@" \ + "$tag" diff --git a/containers/test-clean.sh b/containers/test-clean.sh new file mode 100755 index 0000000..1bbcdb7 --- /dev/null +++ b/containers/test-clean.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env sh + +set -eu + +info() { printf ' [info] %b\n' "$1"; } +announce() { printf ' [test] %b\n' "$1"; } +ok() { printf " [ OK ] %b\n" "$1"; } +fail() { printf " [FAIL] %b\n" "$1"; exit 1; } + +try() { + actual="$1" + expected="$2" + fail_message="${3:-}" + ok_message="${4:-}" + + if [ "$actual" = "$expected" ]; then + ok "$ok_message" + else + fail "Expected <$expected>, got <$actual> $fail_message" + fi +} + +announce "Fresh install has no manually installed packages" +tori_manual=$(tori manual) +try "$tori_manual" "" + +info "Updating apt packages" +apt-get update >/dev/null + +announce "Manually installed package is the only package in 'tori manual'" +apt-get install -y figlet >/dev/null 2>&1 +tori_manual=$(tori manual) +try "$tori_manual" figlet + +announce "Manually installed packages are the only packages in 'tori manual'" +apt-get install -y sudo >/dev/null 2>&1 +tori_manual=$(tori manual | sort) +try "$tori_manual" "$(printf 'figlet\nsudo')" diff --git a/containers/test.sh b/containers/test.sh new file mode 100755 index 0000000..08ba0d0 --- /dev/null +++ b/containers/test.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env sh + +set -eu + +info() { printf ' [info] %b\n' "$1"; } +announce() { printf ' [test] %b\n' "$1"; } +ok() { printf " [ OK ] %b\n" "$1"; } +fail() { printf " [FAIL] %b\n" "$1"; exit 1; } + +try() { + actual="$1" + expected="$2" + operator="${3:-=}" + fail_message="${3:-}" + ok_message="${4:-}" + + # shellcheck disable=1073,1072,1009 + if [ "$actual" "$operator" "$expected" ]; then + ok "$ok_message" + else + fail_message=${fail_message:+": $fail_message"} + fail "Expected <$expected>, got <$actual>$fail_message" + fi +} + +announce "sudo works" +whoami=$(whoami) +sudo_whoami=$(sudo whoami) +echo try "$whoami" "$sudo_whoami" != +try "$whoami" "$sudo_whoami" != +echo try "$sudo_whoami" root +try "$sudo_whoami" root + +info "Updating apt packages" +sudo apt-get update >/dev/null + +announce "Manually installed packages are the only packages in 'tori manual'" +sudo apt-get install -y sudo >/dev/null 2>&1 +tori_manual=$(tori manual | sort) +try "$tori_manual" "sudo" diff --git a/src/conf.rs b/src/conf.rs index cfa81fc..71db0d5 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -81,7 +81,10 @@ fn get_root() -> PathBuf { root } else { if let Ok(user) = std::env::var("USER") { - PathBuf::from("/home").join(user).join(".config").join("tori") + PathBuf::from("/home") + .join(user) + .join(".config") + .join("tori") } else { eprintln!("Failed to determine home directory"); PathBuf::from("/etc/tori") diff --git a/src/os/debian.rs b/src/os/debian.rs index 7cf85b8..7e7f417 100644 --- a/src/os/debian.rs +++ b/src/os/debian.rs @@ -4,7 +4,7 @@ use crate::{ conf::Configuration, log::elog, os::{ - OperatingSystem, Kind, + Kind, OperatingSystem, pkg::{self, Package, PackagerVariant, Packages}, }, run::{Command, executor::read},