Path: blob/main/libexec/nuageinit/tests/addsudo.lua
288915 views
#!/usr/libexec/flua1---2-- SPDX-License-Identifier: BSD-2-Clause3--4-- Copyright (c) 2026 Baptiste Daroussin <[email protected]>56local n = require("nuage")78local root = os.getenv("NUAGE_FAKE_ROOTDIR")9if not root then10root = ""11end1213local function get_localbase()14local f = io.popen("sysctl -in user.localbase 2> /dev/null")15local lb = f:read("*l")16f:close()17if lb == nil or lb:len() == 0 then18lb = "/usr/local"19end20return lb21end2223local function read_sudoers()24local path = root .. get_localbase() .. "/etc/sudoers.d/90-nuageinit-users"25local f = io.open(path, "r")26if not f then27return nil28end29local content = f:read("*a")30f:close()31return content32end3334-- test with a single string rule35n.addsudo({ name = "testuser", sudo = "ALL=(ALL) NOPASSWD:ALL" })36local content = read_sudoers()37if not content then38n.err("sudoers file not created")39end40if content ~= "testuser ALL=(ALL) NOPASSWD:ALL\n" then41n.err("unexpected sudoers content for string rule: '" .. content .. "'")42end4344-- remove file for next test45os.remove(root .. get_localbase() .. "/etc/sudoers.d/90-nuageinit-users")4647-- test with a table of rules48n.addsudo({49name = "testuser",50sudo = { "ALL=(ALL) NOPASSWD:/usr/sbin/pw", "ALL=(ALL) ALL" }51})52content = read_sudoers()53if not content then54n.err("sudoers file not created for table")55end56if content ~= "testuser ALL=(ALL) NOPASSWD:/usr/sbin/pw\ntestuser ALL=(ALL) ALL\n" then57n.err("unexpected sudoers content for table: '" .. content .. "'")58end5960os.exit(0)616263