refactor: make internals easier to test and add unit tests ()

This PR refactors most of the internals to make them easier to test (and also because the names didn't make sense). It adds unit tests for all internal components.

Reviewed-on: 
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
This commit is contained in:
Emmanuel BENOîT 2024-07-20 10:01:05 +02:00 committed by Emmanuel BENOîT
parent dcd732cc34
commit 78af496fe9
20 changed files with 939 additions and 369 deletions
cmd/zoneserial

View file

@ -14,7 +14,7 @@ import (
"nocternity.net/gomonop/pkg/perfdata"
"nocternity.net/gomonop/pkg/plugin"
"nocternity.net/gomonop/pkg/program"
"nocternity.net/gomonop/pkg/results"
)
//-------------------------------------------------------------------------------------------------------
@ -55,8 +55,8 @@ type programFlags struct {
// Program data including configuration and runtime data.
type checkProgram struct {
programFlags // Flags from the command line
plugin *plugin.Plugin // Plugin output state
programFlags // Flags from the command line
plugin *results.Results // Plugin output state
}
// Parse command line arguments and store their values. If the -h flag is present,
@ -77,43 +77,39 @@ func (flags *programFlags) parseArguments() {
}
// Initialise the monitoring check program.
func NewProgram() program.Program {
func NewProgram() plugin.Plugin {
program := &checkProgram{
plugin: plugin.New("DNS zone serial match check"),
plugin: results.New("DNS zone serial match check"),
}
program.parseArguments()
return program
}
// Terminate the monitoring check program.
func (program *checkProgram) Done() {
if r := recover(); r != nil {
program.plugin.SetState(plugin.UNKNOWN, "Internal error")
program.plugin.AddLinef("Error info: %v", r)
}
program.plugin.Done()
// Return the program's output value.
func (program *checkProgram) Results() *results.Results {
return program.plugin
}
// Check the values that were specified from the command line. Returns true if the arguments made sense.
func (program *checkProgram) CheckArguments() bool {
if program.hostname == "" {
program.plugin.SetState(plugin.UNKNOWN, "no DNS hostname specified")
program.plugin.SetState(results.StatusUnknown, "no DNS hostname specified")
return false
}
if program.port < 1 || program.port > 65535 {
program.plugin.SetState(plugin.UNKNOWN, "invalid DNS port number")
program.plugin.SetState(results.StatusUnknown, "invalid DNS port number")
return false
}
if program.zone == "" {
program.plugin.SetState(plugin.UNKNOWN, "no DNS zone specified")
program.plugin.SetState(results.StatusUnknown, "no DNS zone specified")
return false
}
if program.rsHostname == "" {
program.plugin.SetState(plugin.UNKNOWN, "no reference DNS hostname specified")
program.plugin.SetState(results.StatusUnknown, "no reference DNS hostname specified")
return false
}
if program.rsPort < 1 || program.rsPort > 65535 {
program.plugin.SetState(plugin.UNKNOWN, "invalid reference DNS port number")
program.plugin.SetState(results.StatusUnknown, "invalid reference DNS port number")
return false
}
program.hostname = strings.ToLower(program.hostname)
@ -180,12 +176,12 @@ func (program *checkProgram) RunCheck() {
cOk, cSerial := program.getSerial("checked", checkResponse)
rOk, rSerial := program.getSerial("reference", refResponse)
if !(cOk && rOk) {
program.plugin.SetState(plugin.UNKNOWN, "could not read serials")
program.plugin.SetState(results.StatusUnknown, "could not read serials")
return
}
if cSerial == rSerial {
program.plugin.SetState(plugin.OK, "serials match")
program.plugin.SetState(results.StatusOK, "serials match")
} else {
program.plugin.SetState(plugin.CRITICAL, "serials mismatch")
program.plugin.SetState(results.StatusCritical, "serials mismatch")
}
}