xref: /freebsd/libexec/nuageinit/tests/nuage.sh (revision 7e97c6adffde3bd6f60f042ed2603335c005c6a7)
1#-
2# Copyright (c) 2022-2025 Baptiste Daroussin <bapt@FreeBSD.org>
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7export NUAGE_FAKE_ROOTDIR="$PWD"
8
9atf_test_case sethostname
10atf_test_case settimezone
11atf_test_case addsshkey
12atf_test_case adduser
13atf_test_case adduser_passwd
14atf_test_case addgroup
15atf_test_case addfile
16
17settimezone_body()
18{
19	atf_check /usr/libexec/flua $(atf_get_srcdir)/settimezone.lua
20	if [ ! -f etc/localtime ]; then
21		atf_fail "localtime not written"
22	fi
23}
24
25sethostname_body()
26{
27	atf_check /usr/libexec/flua $(atf_get_srcdir)/sethostname.lua
28	if [ ! -f etc/rc.conf.d/hostname ]; then
29		atf_fail "hostname not written"
30	fi
31	atf_check -o inline:"hostname=\"myhostname\"\n" cat etc/rc.conf.d/hostname
32}
33
34addsshkey_body()
35{
36	atf_check /usr/libexec/flua $(atf_get_srcdir)/addsshkey.lua
37	if [ ! -f .ssh/authorized_keys ]; then
38		atf_fail "ssh key not added"
39	fi
40	atf_check -o inline:"40700\n" stat -f %p .ssh
41	atf_check -o inline:"100600\n" stat -f %p .ssh/authorized_keys
42	atf_check -o inline:"mykey\n" cat .ssh/authorized_keys
43	atf_check /usr/libexec/flua $(atf_get_srcdir)/addsshkey.lua
44	atf_check -o inline:"mykey\nmykey\n" cat .ssh/authorized_keys
45}
46
47adduser_head()
48{
49	atf_set "require.user" root
50}
51adduser_body()
52{
53	mkdir etc
54	printf "root:*:0:0::0:0:Charlie &:/root:/bin/sh\n" > etc/master.passwd
55	pwd_mkdb -d etc etc/master.passwd
56	printf "wheel:*:0:root\n" > etc/group
57	atf_check -e inline:"nuageinit: Argument should be a table\nnuageinit: Argument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/adduser.lua
58	test -d home/impossible_username || atf_fail "home not created"
59	atf_check -o inline:"impossible_username::1001:1001::0:0:impossible_username User:/home/impossible_username:/bin/sh\n" grep impossible_username etc/master.passwd
60}
61
62adduser_passwd_body()
63{
64	mkdir etc
65	printf "root:*:0:0::0:0:Charlie &:/root:/bin/sh\n" > etc/master.passwd
66	pwd_mkdb -d etc etc/master.passwd
67	printf "wheel:*:0:root\n" > etc/group
68	atf_check /usr/libexec/flua $(atf_get_srcdir)/adduser_passwd.lua
69	test -d home/foo || atf_fail "home not created"
70	passhash=`awk -F ':' '/^foo:/ {print $2}' etc/master.passwd`
71	atf_check -s exit:0 -o inline:$passhash \
72		$(atf_get_srcdir)/crypt $passhash "bar"
73	passhash=`awk -F ':' '/^foocrypted:/ {print $2}' etc/master.passwd`
74	atf_check -s exit:0 -o inline:$passhash \
75		$(atf_get_srcdir)/crypt $passhash "barcrypted"
76}
77
78addgroup_body()
79{
80	mkdir etc
81	printf "wheel:*:0:root\n" > etc/group
82	atf_check -e inline:"nuageinit: Argument should be a table\nnuageinit: Argument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/addgroup.lua
83	atf_check -o inline:"impossible_groupname:*:1001:\n" grep impossible_groupname etc/group
84}
85
86addfile_body()
87{
88	mkdir tmp
89	atf_check /usr/libexec/flua $(atf_get_srcdir)/addfile.lua
90}
91
92atf_init_test_cases()
93{
94	atf_add_test_case sethostname
95	atf_add_test_case addsshkey
96	atf_add_test_case adduser
97	atf_add_test_case adduser_passwd
98	atf_add_test_case addgroup
99	atf_add_test_case addfile
100}
101