Skip to content

Commit

Permalink
Merge pull request canonical#8205 from sergiocazzolato/tests-fix-crea…
Browse files Browse the repository at this point in the history
…te-user-2

tests: just remove user when the system is not managed on create-user-2 test
  • Loading branch information
sergiocazzolato authored Feb 28, 2020
2 parents e3e82b4 + e2ba8ec commit 9d0f438
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
9 changes: 8 additions & 1 deletion tests/core/create-user-2/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ environment:
USER_NAME: mvo

restore: |
if [ -e managed.device ]; then
exit 0
fi
snap remove-user "$USER_NAME"
execute: |
echo "snap create-user -- ensure failure when run as non-root user without sudo"
expected="error: while creating user: access denied"
if obtained=$(su - test /bin/sh -c "snap create-user $USER_EMAIL 2>&1"); then
echo "create-user command should have failed"
fi
fi
[[ "$obtained" =~ $expected ]]
if [ "$(snap managed)" = "true" ]; then
Expand All @@ -25,6 +28,10 @@ execute: |
exit 1
fi
MATCH "cannot create user: device already managed" < create.error
# Leave a file indicating the device was initially managed
touch managed.device
exit 0
fi
Expand Down
34 changes: 20 additions & 14 deletions tests/core/create-user/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ summary: Ensure that snap create-user works in ubuntu-core
# TODO:UC20: enable for UC20
systems: [ubuntu-core-1*]

environment:
USER_EMAIL: [email protected]
USER_NAME: mvo

restore: |
# FIXME: use deluser here now that it supports --extrausers
sed -i '/^mvo/d' /var/lib/extrausers/passwd
sed -i '/^mvo/d' /var/lib/extrausers/shadow
sed -i '/^mvo/d' /var/lib/extrausers/group
rm -rf /home/mvo
rm -f create.error
if [ -e managed.device ]; then
exit 0
fi
snap remove-user "$USER_NAME"
execute: |
if [ "$MANAGED_DEVICE" = "true" ]; then
if snap create-user --sudoer [email protected] 2>create.error; then
if snap create-user --sudoer "$USER_EMAIL" 2>create.error; then
echo "Did not get expected error creating user in managed device"
exit 1
fi
MATCH "cannot create user: device already managed" < create.error
# Leave a file indicating the device was initially managed
touch managed.device
exit 0
fi
echo "Adding invalid user"
Expand All @@ -30,23 +36,23 @@ execute: |
MATCH "$expected" <<<"$output"
echo "Adding valid user"
expected='created user "mvo"'
output=$(snap create-user --sudoer [email protected])
expected="created user \"$USER_NAME\""
output=$(snap create-user --sudoer "$USER_EMAIL")
if [ "$output" != "$expected" ]; then
echo "Unexpected output $output"
exit 1
fi
echo "Ensure there are ssh keys imported"
MATCH ssh-rsa < /home/mvo/.ssh/authorized_keys
MATCH ssh-rsa < /home/"$USER_NAME"/.ssh/authorized_keys
echo "Ensure the user is a sudo user"
sudo -u mvo sudo true
sudo -u "$USER_NAME" sudo true
echo "ensure the user's home directory exists"
test -d /home/mvo
test -d /home/"$USER_NAME"
echo "ensure ~/.snap/auth.json was created"
test -f /home/mvo/.snap/auth.json
test -f /home/"$USER_NAME"/.snap/auth.json
echo "ensure user's email was stored in ~/.snap/auth.json"
MATCH '"email":"[email protected]"' < /home/mvo/.snap/auth.json
MATCH "\"email\":\"$USER_EMAIL\"" < /home/"$USER_NAME"/.snap/auth.json
16 changes: 15 additions & 1 deletion tests/main/remove-user/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,32 @@ environment:
USER_NAME: mvo

prepare: |
# Note: make this test work with the user already created in the device
if [ "$(snap managed)" = "true" ]; then
# Leave a file indicating the device was initially managed
touch managed.device
exit 0
fi
snap create-user --sudoer "$USER_EMAIL"
restore: |
if [ -e managed.device ]; then
exit 0
fi
userdel --extrausers -r "$USER_NAME" || true
rm -rf "/etc/sudoers.d/create-user-$USER_NAME"
execute: |
if [ -e managed.device ]; then
exit 0
fi
echo "sanity check: user in passwd"
id "$USER_NAME"
echo "sanity check: has sudoer file"
test -f "/etc/sudoers.d/create-user-$USER_NAME"
echo "sanity check: user has a home"
echo "sanity check: user has a home"
test -d "/home/$USER_NAME"
echo "snap remove-user fails when run as non-root user without sudo"
Expand Down

0 comments on commit 9d0f438

Please sign in to comment.