From a67cbd37b06bfb3fdd3a9bc645fe25b4b106df08 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Tue, 28 Mar 2023 11:44:01 +0000 Subject: [PATCH] Add test for ssh-keys in nocloud yaml --- lib/tiny-cloud/init-main | 4 ++-- tests/init-main.test | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/tiny-cloud/init-main b/lib/tiny-cloud/init-main index 2094141..b246a53 100644 --- a/lib/tiny-cloud/init-main +++ b/lib/tiny-cloud/init-main @@ -26,7 +26,7 @@ set_ssh_keys() { local user="$CLOUD_USER" local pwent=$(getent passwd "$user") local group=$(echo "$pwent" | cut -d: -f4) - local ssh_dir="$(echo "$pwent" | cut -d: -f6)/.ssh" + local ssh_dir="${ROOT}$(echo "$pwent" | cut -d: -f6)/.ssh" local keys_file="$ssh_dir/authorized_keys" if [ ! -d "$ssh_dir" ]; then @@ -36,7 +36,7 @@ set_ssh_keys() { touch "$keys_file" chmod 600 "$keys_file" - chown -R "$user:$group" "$ssh_dir" + $MOCK chown -R "$user:$group" "$ssh_dir" imds @ssh-keys > "$keys_file" } diff --git a/tests/init-main.test b/tests/init-main.test index d89e13f..eef865a 100755 --- a/tests/init-main.test +++ b/tests/init-main.test @@ -7,7 +7,8 @@ export MOCK=echo lib="$srcdir"/lib/tiny-cloud/init-main init_tests \ - set_hostname + set_hostname \ + set_ssh_keys set_nocloud_meta() { @@ -51,3 +52,20 @@ set_hostname_body() { fi } +set_ssh_keys_body() { + fake_bin getent <<-EOF + #!/bin/sh + echo "alpine:x:1000:1000:Linux User,,,:/home/alpine:/bin/sh" + EOF + set_nocloud_meta user-data <<-EOF + public-keys: + - openssh-key: ssh-ed25519 keydata + - openssh-key: ssh-rsa foobar + EOF + CLOUD="nocloud" atf_check \ + -o match:"chown.*/\.ssh" \ + sh -c ". \"$lib\"; set_ssh_keys" + atf_check -o match:"^ssh-ed25519 keydata" \ + -o match:"^ssh-rsa foobar" \ + cat home/alpine/.ssh/authorized_keys +}