summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2022-07-01 13:37:03 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2022-07-01 13:44:34 +0200
commitac908aa749f9854a257f6d4957e1342a4db0e1f9 (patch)
tree50df9d279d9cfceca82d91f7a327056f2710729a
parent207733ab8d3863b469c0c173714953eedda60bb8 (diff)
downloadalpine-conf-ac908aa749f9854a257f6d4957e1342a4db0e1f9.zip
tests: add fake adduser
So we can simulate what happens when adduser fails ref https://gitlab.alpinelinux.org/alpine/alpine-conf/-/issues/10519
-rw-r--r--setup-user.in4
-rwxr-xr-xtests/bin/adduser92
-rwxr-xr-xtests/fake_adduser_test21
3 files changed, 115 insertions, 2 deletions
diff --git a/setup-user.in b/setup-user.in
index 0c61d3a..34a5a38 100644
--- a/setup-user.in
+++ b/setup-user.in
@@ -69,9 +69,9 @@ while true; do
fi
if [ -n "$fullname" ]; then
- $MOCK adduser -g "$fullname" $nopassword "$username" && break
+ adduser -g "$fullname" $nopassword "$username" && break
else
- $MOCK adduser $nopassword "$username" && break
+ adduser $nopassword "$username" && break
fi
if ! [ -n "$interactive" ]; then
exit 1
diff --git a/tests/bin/adduser b/tests/bin/adduser
new file mode 100755
index 0000000..72defc4
--- /dev/null
+++ b/tests/bin/adduser
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+prog=${0##*/}
+usage() {
+ cat <<EOF
+BusyBox v1.35.0 (2022-06-21 14:15:10 UTC) multi-call binary.
+
+Usage: adduser [OPTIONS] USER [GROUP]
+
+Create new user, or add USER to GROUP
+
+ -h DIR Home directory
+ -g GECOS GECOS field
+ -s SHELL Login shell
+ -G GRP Group
+ -S Create a system user
+ -D Don't assign a password
+ -H Don't create home directory
+ -u UID User id
+ -k SKEL Skeleton directory (/etc/skel)
+EOF
+}
+
+OPTS=$(getopt -l help -o "h:g:s:G:SDHu:k:" -n $prog -- "$@") || {
+ usage >&2
+ exit 1
+}
+
+eval set -- "$OPTS"
+while true; do
+ opt="$1"
+ case "$opt" in
+ --help)
+ usage >&2
+ exit 0
+ ;;
+ -h)
+ shift
+ homedir="$1"
+ ;;
+ -g)
+ shift
+ gecos="$1"
+ ;;
+ -s)
+ shift
+ shell="$1"
+ ;;
+ -G)
+ shift
+ shell="$1"
+ ;;
+ -S)
+ systemuser=1
+ ;;
+ -D)
+ omitpassword=1
+ ;;
+ -H)
+ omithomedir=1
+ ;;
+ -u)
+ shift
+ uid="$1"
+ ;;
+ -k)
+ shift
+ skel="$1"
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *) usage "1" >&2
+ ;;
+ esac
+ shift
+done
+
+if [ $# -eq 0 ]; then
+ usage >&2
+ exit 1
+fi
+
+if [ "$ADDUSER_EXIST" = "$1" ]; then
+ echo "adduser: user '$1' in use" >&2
+ exit 1
+fi
+
+eval set -- "$OPTS"
+echo "adduser $@"
+
diff --git a/tests/fake_adduser_test b/tests/fake_adduser_test
new file mode 100755
index 0000000..fe13690
--- /dev/null
+++ b/tests/fake_adduser_test
@@ -0,0 +1,21 @@
+#!/usr/bin/env atf-sh
+
+. $(atf_get_srcdir)/test_env.sh
+init_tests \
+ fake_adduser_usage \
+ fake_adduser_failure
+
+fake_adduser_usage_body() {
+ init_env
+ atf_check -s exit:0 \
+ -e match:"Usage: adduser" \
+ adduser --help
+}
+
+fake_adduser_failure_body() {
+ init_env
+ ADDUSER_EXIST=juser atf_check -s exit:1 \
+ -e match:"adduser: user 'juser' in use" \
+ adduser juser
+}
+