refactor: make internals easier to test and add unit tests #2

Merged
Emmanuel BENOîT merged 13 commits from tseeker/gomonop:20240719-more-tests into master 2024-07-20 10:01:05 +02:00
5 changed files with 30 additions and 19 deletions
Showing only changes of commit 5432360f0e - Show all commits

View file

@ -215,9 +215,9 @@ func NewProgram() program.Program {
return program return program
} }
// Terminate the monitoring check program. // Return the program's output value.
func (program *checkProgram) Done() { func (program *checkProgram) Output() *plugin.Plugin {
program.plugin.Done() return program.plugin
} }
// Check the values that were specified from the command line. Returns true // Check the values that were specified from the command line. Returns true

View file

@ -85,13 +85,9 @@ func NewProgram() program.Program {
return program return program
} }
// Terminate the monitoring check program. // Return the program's output value.
func (program *checkProgram) Done() { func (program *checkProgram) Output() *plugin.Plugin {
if r := recover(); r != nil { return program.plugin
program.plugin.SetState(plugin.StatusUnknown, "Internal error")
program.plugin.AddLinef("Error info: %v", r)
}
program.plugin.Done()
} }
// Check the values that were specified from the command line. Returns true if the arguments made sense. // Check the values that were specified from the command line. Returns true if the arguments made sense.

13
main.go
View file

@ -7,6 +7,7 @@ import (
"nocternity.net/gomonop/cmd/sslcert" "nocternity.net/gomonop/cmd/sslcert"
"nocternity.net/gomonop/cmd/zoneserial" "nocternity.net/gomonop/cmd/zoneserial"
"nocternity.net/gomonop/pkg/plugin"
"nocternity.net/gomonop/pkg/program" "nocternity.net/gomonop/pkg/program"
"nocternity.net/gomonop/pkg/version" "nocternity.net/gomonop/pkg/version"
) )
@ -55,7 +56,17 @@ func getProgram() program.Program {
func main() { func main() {
program := getProgram() program := getProgram()
defer program.Done()
output := program.Output()
defer func() {
if r := recover(); r != nil {
output.SetState(plugin.StatusUnknown, "Internal error")
output.AddLinef("Error info: %v", r)
}
fmt.Println(output.String())
os.Exit(output.ExitCode())
}()
if program.CheckArguments() { if program.CheckArguments() {
program.RunCheck() program.RunCheck()
} }

View file

@ -5,7 +5,6 @@ package plugin // import nocternity.net/gomonop/pkg/perfdata
import ( import (
"container/list" "container/list"
"fmt" "fmt"
"os"
"strings" "strings"
"nocternity.net/gomonop/pkg/perfdata" "nocternity.net/gomonop/pkg/perfdata"
@ -70,10 +69,9 @@ func (p *Plugin) AddPerfData(pd *perfdata.PerfData) {
p.perfData[pd.Label] = pd p.perfData[pd.Label] = pd
} }
// Done generates the plugin's text output from its name, status, text data // String generates the plugin's text output from its name, status, text data
// and performance data, before exiting with the code corresponding to the // and performance data.
// status. func (p *Plugin) String() string {
func (p *Plugin) Done() {
var strBuilder strings.Builder var strBuilder strings.Builder
strBuilder.WriteString(p.name) strBuilder.WriteString(p.name)
strBuilder.WriteString(" ") strBuilder.WriteString(" ")
@ -99,6 +97,10 @@ func (p *Plugin) Done() {
strBuilder.WriteString(em.Value.(string)) strBuilder.WriteString(em.Value.(string))
} }
} }
fmt.Println(strBuilder.String()) return strBuilder.String()
os.Exit(int(p.status)) }
// ExitCode returns the plugin's exit code.
func (p *Plugin) ExitCode() int {
return int(p.status)
} }

View file

@ -1,9 +1,11 @@
package program // import nocternity.net/gomonop/pkg/program package program // import nocternity.net/gomonop/pkg/program
import "nocternity.net/gomonop/pkg/plugin"
type Program interface { type Program interface {
Output() *plugin.Plugin
CheckArguments() bool CheckArguments() bool
RunCheck() RunCheck()
Done()
} }
type Builder func() Program type Builder func() Program