Skip to content

Commit

Permalink
a test case for device configuration type garden water feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
fikin committed Aug 17, 2024
1 parent 5f78f10 commit dd22e0d
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
115 changes: 115 additions & 0 deletions integration-tests/fs/fountain-bootstrap-sw.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
--[[
Garden fountain controller
]]

local fs = require("factory-settings")

local pwd = "pswd"

local adminUsr = "admin"
local adminPwd = pwd

-- minimal HomeAssistant modules and credentials
fs("web-ha2")
:set("credentials.usr", adminUsr):set("credentials.pwd", adminPwd)
:done()

-- configure new HASS web (for devices)
fs("http-srv")
:set("webModules", {
"web-ha2", -- "devices" HASS web handler
})
:done()

-- minimal set of modules on.
fs("init-seq")
:set("bootsequence", {
"bootstrap",
"http-srv",
})
:done()

local devinfo = {
manufacturer = "niki",
name = "Garden fountain",
model = "nodemcu device",
}

local pump = {
name = "pump",
type = "switch-gpio",
internal = false,
spec = {
name = "pump",
},
settings = {
-- pin 0 is normally HIGH i.e. inverted/set to on
-- used relay is also with normally HIGH input
pin = 0,
inverted = true,
set_float = false,
is_on = false,
},
}

local leds = {
name = "leds",
type = "switch-gpio",
internal = false,
spec = {
name = "leds",
},
settings = {
-- pin 1 is normally HIGH i.e. inverted/set to off
-- used relay is also with normally LOW input
pin = 1,
inverted = true,
set_float = false,
is_on = false,
},
}

local water_level = {
name = "water_level",
type = "sensor-gpio",
internal = false,
spec = {
name = "water level",
},
settings = {
pin = 5,
inverted = false,
set_float = false,
debounceMs = 300,
},
}

local pump_control = {
name = "pump_control",
type = "switch-func",
internal = false,
spec = {
name = "pump auto control",
},
settings = {
funcname = "fnc-gated-switch",
scheduleMs = 300,
sensorId = "water_level",
switchId = "pump",
is_on = true,
},
}

local df = require("device-factory")
df({
info = devinfo,
devices = {
pump,
water_level,
leds,
pump_control,
},
})

-- restart at the end since the boot sequence is modified.
node.restart()
78 changes: 78 additions & 0 deletions integration-tests/lua/testFountain.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local lu = require("luaunit")
local nodemcu = require("nodemcu")

local bootstrapSwLua = "bootstrap-sw.lua"

local function copyFileToSPIFFS(from, to)
local openFileFn = require("file_obj")
local f1, _ = openFileFn(from, "r")
assert(f1)
local f2, _ = openFileFn(nodemcu.fileLoc(to), "w")
assert(f2)
f2:write(f1:read(1024 * 1024))
f1:close()
f2:close()
end

local function setupBeforeAll()
-- set boot delay 0
_G["NODEMCU_NO_BOOT_DELAY"] = true
-- setup systemctl bootloader
_G["BOOTPROTECT"] = "systemctl"
-- copy test bootstrap-sw.lua to SPIFFS
-- copyFileToSPIFFS("fountain-bootstrap-sw.lua", bootstrapSwLua)
file.remove(bootstrapSwLua)
lu.assertIsTrue(file.exists("fountain-" .. bootstrapSwLua))
lu.assertIsTrue(file.rename("fountain-" .. bootstrapSwLua, bootstrapSwLua))
end

local function setup()
nodemcu.reset()
-- cleanup loaded modules
for _, i in ipairs({"init", "state", "systemctl"}) do
package.loaded[i] = nil
end
end

local function assertLFSReboot()
setup()
lu.assertIsFalse(file.exists("LFS.img.PANIC.txt"))
lu.assertIsTrue(file.exists("LFS.img"))
local ok, err = pcall(require, "init")
lu.assertIsFalse(ok)
lu.assertStrContains(err, "node.LFS.reload")
-- simulate node.LFS.reload removal of image file
file.remove("LFS.img")
end

local function assertBootstrapReboot()
setup()
lu.assertIsTrue(file.exists(bootstrapSwLua))
local ok, err = pcall(require, "init")
lu.assertIsFalse(ok)
lu.assertStrContains(err, "node.restart")
lu.assertIsFalse(file.exists("boostsrap-sw.lua"))
end

local function assertDeviceUp()
setup()
require("init")
lu.assertIsNil(_G["init"])
lu.assertEquals(require("systemctl").status(), "ok")
require("systemctl").gc()
end

function testOk()
nodemcu.reset()

setupBeforeAll()

assertLFSReboot()
assertBootstrapReboot()
assertDeviceUp()
-- assertWaterLevelLow()
-- raiseWaterLevel()
-- assertWaterLevelHigh()
end

os.exit(lu.run())

0 comments on commit dd22e0d

Please sign in to comment.