git: e573007af24a - stable/14 - nuageinit: tests: Cleanup

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Tue, 08 Oct 2024 07:07:50 UTC
The branch stable/14 has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=e573007af24ac41dbd3f8f959949f30b6047587e

commit e573007af24ac41dbd3f8f959949f30b6047587e
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2024-07-30 03:26:21 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-10-08 07:04:02 +0000

    nuageinit: tests: Cleanup
    
    - Export NUAGE_FAKE_ROOTDIR only once
    - Use the header section of the test to require the root user
    - Use the PWD environment variable
    - Set the root/sys shell as /bin/sh
    - Use RFC 5737 reserved IP addresses
    
    Signed-off-by: Jose Luis Duran <jlduran@gmail.com>
    (cherry picked from commit e72457c4f5166eef2a27249e02f3c1e9a1cf852d)
---
 libexec/nuageinit/tests/nuage.sh     |  14 +--
 libexec/nuageinit/tests/nuageinit.sh | 165 ++++++++++++++++-------------------
 2 files changed, 83 insertions(+), 96 deletions(-)

diff --git a/libexec/nuageinit/tests/nuage.sh b/libexec/nuageinit/tests/nuage.sh
index 6d8a2746de1f..293a0a4a9a83 100644
--- a/libexec/nuageinit/tests/nuage.sh
+++ b/libexec/nuageinit/tests/nuage.sh
@@ -4,6 +4,8 @@
 # SPDX-License-Identifier: BSD-2-Clause
 #
 
+export NUAGE_FAKE_ROOTDIR="$PWD"
+
 atf_test_case sethostname
 atf_test_case addsshkey
 atf_test_case adduser
@@ -11,7 +13,6 @@ atf_test_case addgroup
 
 sethostname_body()
 {
-	export NUAGE_FAKE_ROOTDIR="$(pwd)"
 	atf_check /usr/libexec/flua $(atf_get_srcdir)/sethostname.lua
 	if [ ! -f etc/rc.conf.d/hostname ]; then
 		atf_fail "hostname not written"
@@ -32,14 +33,14 @@ addsshkey_body()
 	atf_check -o inline:"mykey\nmykey\n" cat .ssh/authorized_keys
 }
 
+adduser_head()
+{
+	atf_set "require.user" root
+}
 adduser_body()
 {
-	export NUAGE_FAKE_ROOTDIR="$(pwd)"
-	if [ $(id -u) -ne 0 ]; then
-		atf_skip "root required"
-	fi
 	mkdir etc
-	printf "root:*:0:0::0:0:Charlie &:/root:/bin/csh\n" > etc/master.passwd
+	printf "root:*:0:0::0:0:Charlie &:/root:/bin/sh\n" > etc/master.passwd
 	pwd_mkdb -d etc etc/master.passwd
 	printf "wheel:*:0:root\n" > etc/group
 	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
@@ -49,7 +50,6 @@ adduser_body()
 
 addgroup_body()
 {
-	export NUAGE_FAKE_ROOTDIR="$(pwd)"
 	mkdir etc
 	printf "wheel:*:0:root\n" > etc/group
 	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
diff --git a/libexec/nuageinit/tests/nuageinit.sh b/libexec/nuageinit/tests/nuageinit.sh
index 434fb3095929..f7f39ce32ad8 100644
--- a/libexec/nuageinit/tests/nuageinit.sh
+++ b/libexec/nuageinit/tests/nuageinit.sh
@@ -4,6 +4,8 @@
 # SPDX-License-Identifier: BSD-2-Clause
 #
 
+export NUAGE_FAKE_ROOTDIR="$PWD"
+
 atf_test_case args
 atf_test_case nocloud
 atf_test_case nocloud_userdata_script
@@ -27,56 +29,51 @@ args_body()
 
 nocloud_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
-	atf_check -s exit:1 -e match:"nuageinit: error parsing nocloud.*" /usr/libexec/nuageinit ${here}/media/nuageinit/ nocloud
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	printf "instance-id: iid-local01\nlocal-hostname: cloudimg\n" > ${here}/media/nuageinit/meta-data
-	atf_check -s exit:0 /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+	atf_check -s exit:1 -e match:"nuageinit: error parsing nocloud.*" /usr/libexec/nuageinit "${PWD}"/media/nuageinit/ nocloud
+	printf "instance-id: iid-local01\nlocal-hostname: cloudimg\n" > "${PWD}"/media/nuageinit/meta-data
+	atf_check -s exit:0 /usr/libexec/nuageinit "${PWD}"/media/nuageinit nocloud
 	atf_check -o inline:"hostname=\"cloudimg\"\n" cat etc/rc.conf.d/hostname
 	cat > media/nuageinit/meta-data << EOF
 instance-id: iid-local01
 hostname: myhost
 EOF
-	atf_check -s exit:0 /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+	atf_check -s exit:0 /usr/libexec/nuageinit "${PWD}"/media/nuageinit nocloud
 	atf_check -o inline:"hostname=\"myhost\"\n" cat etc/rc.conf.d/hostname
 }
 
 nocloud_userdata_script_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
-	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
-	printf "#!/bin/sh\necho yeah\n" > ${here}/media/nuageinit/user-data
-	chmod 755 ${here}/media/nuageinit/user-data
-	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+	printf "instance-id: iid-local01\n" > "${PWD}"/media/nuageinit/meta-data
+	printf "#!/bin/sh\necho yeah\n" > "${PWD}"/media/nuageinit/user-data
+	chmod 755 "${PWD}"/media/nuageinit/user-data
+	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit "${PWD}"/media/nuageinit nocloud
 }
 
 nocloud_user_data_script_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
-	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
-	printf "#!/bin/sh\necho yeah\n" > ${here}/media/nuageinit/user_data
-	chmod 755 ${here}/media/nuageinit/user_data
-	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+	printf "instance-id: iid-local01\n" > "${PWD}"/media/nuageinit/meta-data
+	printf "#!/bin/sh\necho yeah\n" > "${PWD}"/media/nuageinit/user_data
+	chmod 755 "${PWD}"/media/nuageinit/user_data
+	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit "${PWD}"/media/nuageinit nocloud
 }
 
+nocloud_userdata_cloudconfig_users_head()
+{
+	atf_set "require.user" root
+}
 nocloud_userdata_cloudconfig_users_body()
 {
-	here=$(pwd)
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	if [ $(id -u) -ne 0 ]; then
-		atf_skip "root required"
-	fi
 	mkdir -p media/nuageinit
-	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
+	printf "instance-id: iid-local01\n" > "${PWD}"/media/nuageinit/meta-data
 	mkdir -p etc
 	cat > etc/master.passwd << EOF
-root:*:0:0::0:0:Charlie &:/root:/bin/csh
-sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+root:*:0:0::0:0:Charlie &:/root:/bin/sh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/sh
 EOF
-	pwd_mkdb -d etc ${here}/etc/master.passwd
+	pwd_mkdb -d etc "${PWD}"/etc/master.passwd
 	cat > etc/group << EOF
 wheel:*:0:root
 users:*:1:
@@ -94,7 +91,7 @@ users:
     groups: users
     passwd: $6$j212wezy$7H/1LT4f9/N3wpgNunhsIqtMj62OKiS3nyNwuizouQc3u7MbYCarYeAHWYPYb2FT.lbioDm2RrkJPb9BZMN1O/
 EOF
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit nocloud
 	cat > expectedgroup << EOF
 wheel:*:0:root,freebsd
 users:*:1:foobar
@@ -104,33 +101,33 @@ freebsd:*:1003:
 foobar:*:1004:
 EOF
 	cat > expectedpasswd << 'EOF'
-root:*:0:0::0:0:Charlie &:/root:/bin/csh
-sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+root:*:0:0::0:0:Charlie &:/root:/bin/sh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/sh
 freebsd:freebsd:1001:1003::0:0:FreeBSD User:/home/freebsd:/bin/sh
 foobar:$6$j212wezy$7H/1LT4f9/N3wpgNunhsIqtMj62OKiS3nyNwuizouQc3u7MbYCarYeAHWYPYb2FT.lbioDm2RrkJPb9BZMN1O/:1002:1004::0:0:Foo B. Bar:/home/foobar:/bin/sh
 EOF
-	sed -i "" "s/freebsd:.*:1001/freebsd:freebsd:1001/" ${here}/etc/master.passwd
-	atf_check -o file:expectedpasswd cat ${here}/etc/master.passwd
-	atf_check -o file:expectedgroup cat ${here}/etc/group
+	sed -i "" "s/freebsd:.*:1001/freebsd:freebsd:1001/" "${PWD}"/etc/master.passwd
+	atf_check -o file:expectedpasswd cat "${PWD}"/etc/master.passwd
+	atf_check -o file:expectedgroup cat "${PWD}"/etc/group
 }
 
+nocloud_network_head()
+{
+	atf_set "require.user" root
+}
 nocloud_network_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
 	mkdir -p etc
 	cat > etc/master.passwd << EOF
-root:*:0:0::0:0:Charlie &:/root:/bin/csh
-sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+root:*:0:0::0:0:Charlie &:/root:/bin/sh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/sh
 EOF
-	pwd_mkdb -d etc ${here}/etc/master.passwd
+	pwd_mkdb -d etc "${PWD}"/etc/master.passwd
 	cat > etc/group << EOF
 wheel:*:0:root
 users:*:1:
 EOF
-	if [ $(id -u) -ne 0 ]; then
-		atf_skip "root required"
-	fi
 	mynetworks=$(ifconfig -l ether)
 	if [ -z "$mynetworks" ]; then
 		atf_skip "a network interface is needed"
@@ -138,7 +135,7 @@ EOF
 	set -- $mynetworks
 	myiface=$1
 	myaddr=$(ifconfig $myiface ether | awk '/ether/ { print $2 }')
-	printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
+	printf "instance-id: iid-local01\n" > "${PWD}"/media/nuageinit/meta-data
 	cat > media/nuageinit/user-data << EOF
 #cloud-config
 network:
@@ -149,51 +146,48 @@ network:
       match:
         macaddress: "$myaddr"
       addresses:
-        - 192.168.14.2/24
-        - 2001:1::1/64
-      gateway4: 192.168.14.1
-      gateway6: 2001:1::2
+        - 192.0.2.2/24
+        - 2001:db8::2/64
+      gateway4: 192.0.2.1
+      gateway6: 2001:db8::1
 EOF
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit nocloud
 	cat > network << EOF
-ifconfig_${myiface}="inet 192.168.14.2/24"
-ifconfig_${myiface}_ipv6="inet6 2001:1::1/64"
+ifconfig_${myiface}="inet 192.0.2.2/24"
+ifconfig_${myiface}_ipv6="inet6 2001:db8::2/64"
 ipv6_network_interfaces="${myiface}"
 ipv6_default_interface="${myiface}"
 EOF
 	cat > routing << EOF
-defaultrouter="192.168.14.1"
-ipv6_defaultrouter="2001:1::2"
-ipv6_route_${myiface}="2001:1::2 -prefixlen 128 -interface ${myiface}"
+defaultrouter="192.0.2.1"
+ipv6_defaultrouter="2001:db8::1"
+ipv6_route_${myiface}="2001:db8::1 -prefixlen 128 -interface ${myiface}"
 EOF
-	atf_check -o file:network cat ${here}/etc/rc.conf.d/network
-	atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
+	atf_check -o file:network cat "${PWD}"/etc/rc.conf.d/network
+	atf_check -o file:routing cat "${PWD}"/etc/rc.conf.d/routing
 }
+
 config2_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
-	atf_check -s exit:1 -e match:"nuageinit: error parsing config-2 meta_data.json:.*" /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check -s exit:1 -e match:"nuageinit: error parsing config-2 meta_data.json:.*" /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	printf "{}" > media/nuageinit/meta_data.json
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	cat > media/nuageinit/meta_data.json << EOF
 {
     "hostname": "cloudimg"
 }
 EOF
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	atf_check -o inline:"hostname=\"cloudimg\"\n" cat etc/rc.conf.d/hostname
 }
 
+config2_pubkeys_head()
+{
+	atf_set "require.user" root
+}
 config2_pubkeys_body()
 {
-	here=$(pwd)
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	if [ $(id -u) -ne 0 ]; then
-		atf_skip "root required"
-	fi
 	mkdir -p media/nuageinit
 	touch media/nuageinit/meta_data.json
 	cat > media/nuageinit/user-data << EOF
@@ -203,26 +197,24 @@ ssh_authorized_keys:
 EOF
 	mkdir -p etc
 	cat > etc/master.passwd << EOF
-root:*:0:0::0:0:Charlie &:/root:/bin/csh
-sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+root:*:0:0::0:0:Charlie &:/root:/bin/sh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/sh
 EOF
-	pwd_mkdb -d etc ${here}/etc/master.passwd
+	pwd_mkdb -d etc "${PWD}"/etc/master.passwd
 	cat > etc/group << EOF
 wheel:*:0:root
 users:*:1:
 EOF
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	atf_check -o inline:"ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n" cat home/freebsd/.ssh/authorized_keys
 }
 
-
+config2_pubkeys_user_data_head()
+{
+	atf_set "require.user" root
+}
 config2_pubkeys_user_data_body()
 {
-	here=$(pwd)
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	if [ $(id -u) -ne 0 ]; then
-		atf_skip "root required"
-	fi
 	mkdir -p media/nuageinit
 	touch media/nuageinit/meta_data.json
 	cat > media/nuageinit/user_data << EOF
@@ -232,15 +224,15 @@ ssh_authorized_keys:
 EOF
 	mkdir -p etc
 	cat > etc/master.passwd << EOF
-root:*:0:0::0:0:Charlie &:/root:/bin/csh
-sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+root:*:0:0::0:0:Charlie &:/root:/bin/sh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/sh
 EOF
-	pwd_mkdb -d etc ${here}/etc/master.passwd
+	pwd_mkdb -d etc "${PWD}"/etc/master.passwd
 	cat > etc/group << EOF
 wheel:*:0:root
 users:*:1:
 EOF
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	atf_check -o inline:"ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n" cat home/freebsd/.ssh/authorized_keys
 }
 
@@ -292,7 +284,6 @@ EOF
 
 config2_network_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
 	printf "{}" > media/nuageinit/meta_data.json
 	mynetworks=$(ifconfig -l ether)
@@ -322,7 +313,7 @@ cat > media/nuageinit/network_data.json << EOF
             "type": "ipv6",
             "link": "iface0",
             // supports condensed IPv6 with CIDR netmask
-            "ip_address": "2001:cdba::3257:9652/24",
+            "ip_address": "2001:db8::3257:9652/64",
             "gateway": "fd00::1",
             "routes": [
                 {
@@ -341,11 +332,10 @@ cat > media/nuageinit/network_data.json << EOF
     ]
 }
 EOF
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	cat > network << EOF
 ifconfig_${myiface}="DHCP"
-ifconfig_${myiface}_ipv6="inet6 2001:cdba::3257:9652/24"
+ifconfig_${myiface}_ipv6="inet6 2001:db8::3257:9652/64"
 ipv6_network_interfaces="${myiface}"
 ipv6_default_interface="${myiface}"
 EOF
@@ -354,13 +344,12 @@ ipv6_defaultrouter="fd00::1"
 ipv6_route_${myiface}="fd00::1 -prefixlen 128 -interface ${myiface}"
 ipv6_static_routes="${myiface}"
 EOF
-	atf_check -o file:network cat ${here}/etc/rc.conf.d/network
-	atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
+	atf_check -o file:network cat "${PWD}"/etc/rc.conf.d/network
+	atf_check -o file:routing cat "${PWD}"/etc/rc.conf.d/routing
 }
 
 config2_network_static_v4_body()
 {
-	here=$(pwd)
 	mkdir -p media/nuageinit
 	printf "{}" > media/nuageinit/meta_data.json
 	mynetworks=$(ifconfig -l ether)
@@ -402,9 +391,7 @@ cat > media/nuageinit/network_data.json << EOF
     ]
 }
 EOF
-
-	export NUAGE_FAKE_ROOTDIR=$(pwd)
-	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check /usr/libexec/nuageinit "${PWD}"/media/nuageinit config-2
 	cat > network << EOF
 ifconfig_${myiface}="inet 10.184.0.244 netmask 255.255.240.0"
 EOF
@@ -413,8 +400,8 @@ route_cloudinit1_${myiface}="-net 10.0.0.0 11.0.0.1 255.0.0.0"
 defaultrouter="23.253.157.1"
 static_routes="cloudinit1_${myiface}"
 EOF
-	atf_check -o file:network cat ${here}/etc/rc.conf.d/network
-	atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
+	atf_check -o file:network cat "${PWD}"/etc/rc.conf.d/network
+	atf_check -o file:routing cat "${PWD}"/etc/rc.conf.d/routing
 }
 
 atf_init_test_cases()