git: a49b3b10aae2 - main - nuageinit: add addsudo tests

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Sun, 10 May 2026 15:56:27 UTC
The branch main has been updated by bapt:

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

commit a49b3b10aae2db1f4a4ecf310fdedc80eb6bb7e2
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-05-10 15:54:47 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-05-10 15:54:47 +0000

    nuageinit: add addsudo tests
---
 libexec/nuageinit/tests/Makefile    |  1 +
 libexec/nuageinit/tests/addsudo.lua | 61 +++++++++++++++++++++++++++++++++++++
 libexec/nuageinit/tests/nuage.sh    |  7 +++++
 3 files changed, 69 insertions(+)

diff --git a/libexec/nuageinit/tests/Makefile b/libexec/nuageinit/tests/Makefile
index feca49784c9e..d13ad14f1d8f 100644
--- a/libexec/nuageinit/tests/Makefile
+++ b/libexec/nuageinit/tests/Makefile
@@ -19,5 +19,6 @@ ${PACKAGE}FILES+=	settimezone.lua
 ${PACKAGE}FILES+=	warn.lua
 ${PACKAGE}FILES+=	addfile.lua
 ${PACKAGE}FILES+=	decode_base64.lua
+${PACKAGE}FILES+=	addsudo.lua
 
 .include <bsd.test.mk>
diff --git a/libexec/nuageinit/tests/addsudo.lua b/libexec/nuageinit/tests/addsudo.lua
new file mode 100644
index 000000000000..7fc5865d83f4
--- /dev/null
+++ b/libexec/nuageinit/tests/addsudo.lua
@@ -0,0 +1,61 @@
+#!/usr/libexec/flua
+---
+-- SPDX-License-Identifier: BSD-2-Clause
+--
+-- Copyright (c) 2026 Baptiste Daroussin <bapt@FreeBSD.org>
+
+local n = require("nuage")
+
+local root = os.getenv("NUAGE_FAKE_ROOTDIR")
+if not root then
+	root = ""
+end
+
+local function get_localbase()
+	local f = io.popen("sysctl -in user.localbase 2> /dev/null")
+	local lb = f:read("*l")
+	f:close()
+	if lb == nil or lb:len() == 0 then
+		lb = "/usr/local"
+	end
+	return lb
+end
+
+local function read_sudoers()
+	local path = root .. get_localbase() .. "/etc/sudoers.d/90-nuageinit-users"
+	local f = io.open(path, "r")
+	if not f then
+		return nil
+	end
+	local content = f:read("*a")
+	f:close()
+	return content
+end
+
+-- test with a single string rule
+n.addsudo({ name = "testuser", sudo = "ALL=(ALL) NOPASSWD:ALL" })
+local content = read_sudoers()
+if not content then
+	n.err("sudoers file not created")
+end
+if content ~= "testuser ALL=(ALL) NOPASSWD:ALL\n" then
+	n.err("unexpected sudoers content for string rule: '" .. content .. "'")
+end
+
+-- remove file for next test
+os.remove(root .. get_localbase() .. "/etc/sudoers.d/90-nuageinit-users")
+
+-- test with a table of rules
+n.addsudo({
+	name = "testuser",
+	sudo = { "ALL=(ALL) NOPASSWD:/usr/sbin/pw", "ALL=(ALL) ALL" }
+})
+content = read_sudoers()
+if not content then
+	n.err("sudoers file not created for table")
+end
+if content ~= "testuser ALL=(ALL) NOPASSWD:/usr/sbin/pw\ntestuser ALL=(ALL) ALL\n" then
+	n.err("unexpected sudoers content for table: '" .. content .. "'")
+end
+
+os.exit(0)
diff --git a/libexec/nuageinit/tests/nuage.sh b/libexec/nuageinit/tests/nuage.sh
index 1c8d717a5b67..9e3442281c15 100644
--- a/libexec/nuageinit/tests/nuage.sh
+++ b/libexec/nuageinit/tests/nuage.sh
@@ -15,6 +15,7 @@ atf_test_case adduser_passwd
 atf_test_case addgroup
 atf_test_case addfile
 atf_test_case decode_base64
+atf_test_case addsudo
 
 settimezone_body()
 {
@@ -97,6 +98,11 @@ decode_base64_body()
 	atf_check /usr/libexec/flua $(atf_get_srcdir)/decode_base64.lua
 }
 
+addsudo_body()
+{
+	atf_check /usr/libexec/flua $(atf_get_srcdir)/addsudo.lua
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case sethostname
@@ -106,4 +112,5 @@ atf_init_test_cases()
 	atf_add_test_case addgroup
 	atf_add_test_case addfile
 	atf_add_test_case decode_base64
+	atf_add_test_case addsudo
 }