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
4 changed files with 26 additions and 26 deletions
Showing only changes of commit b3aa7dfcad - Show all commits

View file

@ -224,20 +224,20 @@ func (program *checkProgram) Done() {
// if the arguments made sense.
func (program *checkProgram) CheckArguments() bool {
if program.hostname == "" {
program.plugin.SetState(plugin.UNKNOWN, "no hostname specified")
program.plugin.SetState(plugin.StatusUnknown, "no hostname specified")
return false
}
if program.port < 1 || program.port > 65535 {
program.plugin.SetState(plugin.UNKNOWN, "invalid or missing port number")
program.plugin.SetState(plugin.StatusUnknown, "invalid or missing port number")
return false
}
if program.warn != -1 && program.crit != -1 && program.warn <= program.crit {
program.plugin.SetState(plugin.UNKNOWN, "nonsensical thresholds")
program.plugin.SetState(plugin.StatusUnknown, "nonsensical thresholds")
return false
}
if _, ok := certGetters[program.startTLS]; !ok {
errstr := "unsupported StartTLS protocol " + program.startTLS
program.plugin.SetState(plugin.UNKNOWN, errstr)
program.plugin.SetState(plugin.StatusUnknown, errstr)
return false
}
program.hostname = strings.ToLower(program.hostname)
@ -262,13 +262,13 @@ func (program *checkProgram) getCertificate() error {
// matches the requested host name.
func (program *checkProgram) checkSANlessCertificate() bool {
if !program.ignoreCnOnly || len(program.extraNames) != 0 {
program.plugin.SetState(plugin.WARNING,
program.plugin.SetState(plugin.StatusWarning,
"certificate doesn't have SAN domain names")
return false
}
dn := strings.ToLower(program.certificate.Subject.String())
if !strings.HasPrefix(dn, fmt.Sprintf("cn=%s,", program.hostname)) {
program.plugin.SetState(plugin.CRITICAL, "incorrect certificate CN")
program.plugin.SetState(plugin.StatusCritical, "incorrect certificate CN")
return false
}
return true
@ -298,7 +298,7 @@ func (program *checkProgram) checkNames() bool {
certificateIsOk = program.checkHostName(name) && certificateIsOk
}
if !certificateIsOk {
program.plugin.SetState(plugin.CRITICAL, "names missing from SAN domain names")
program.plugin.SetState(plugin.StatusCritical, "names missing from SAN domain names")
}
return certificateIsOk
}
@ -308,7 +308,7 @@ func (program *checkProgram) checkNames() bool {
// values.
func (program *checkProgram) checkCertificateExpiry(tlDays int) (plugin.Status, string) {
if tlDays <= 0 {
return plugin.CRITICAL, "certificate expired"
return plugin.StatusCritical, "certificate expired"
}
var limitStr string
@ -317,15 +317,15 @@ func (program *checkProgram) checkCertificateExpiry(tlDays int) (plugin.Status,
switch {
case program.crit > 0 && tlDays <= program.crit:
limitStr = fmt.Sprintf(" (<= %d)", program.crit)
state = plugin.CRITICAL
state = plugin.StatusCritical
case program.warn > 0 && tlDays <= program.warn:
limitStr = fmt.Sprintf(" (<= %d)", program.warn)
state = plugin.WARNING
state = plugin.StatusWarning
default:
limitStr = ""
state = plugin.OK
state = plugin.StatusOK
}
statusString := fmt.Sprintf("certificate will expire in %d days%s",
@ -351,7 +351,7 @@ func (program *checkProgram) setPerfData(tlDays int) {
func (program *checkProgram) RunCheck() {
err := program.getCertificate()
if err != nil {
program.plugin.SetState(plugin.UNKNOWN, err.Error())
program.plugin.SetState(plugin.StatusUnknown, err.Error())
} else if program.checkNames() {
timeLeft := time.Until(program.certificate.NotAfter)
tlDays := int((timeLeft + 86399*time.Second) / (24 * time.Hour))

View file

@ -88,7 +88,7 @@ func NewProgram() program.Program {
// Terminate the monitoring check program.
func (program *checkProgram) Done() {
if r := recover(); r != nil {
program.plugin.SetState(plugin.UNKNOWN, "Internal error")
program.plugin.SetState(plugin.StatusUnknown, "Internal error")
program.plugin.AddLinef("Error info: %v", r)
}
program.plugin.Done()
@ -97,23 +97,23 @@ func (program *checkProgram) Done() {
// 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(plugin.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(plugin.StatusUnknown, "invalid DNS port number")
return false
}
if program.zone == "" {
program.plugin.SetState(plugin.UNKNOWN, "no DNS zone specified")
program.plugin.SetState(plugin.StatusUnknown, "no DNS zone specified")
return false
}
if program.rsHostname == "" {
program.plugin.SetState(plugin.UNKNOWN, "no reference DNS hostname specified")
program.plugin.SetState(plugin.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(plugin.StatusUnknown, "invalid reference DNS port number")
return false
}
program.hostname = strings.ToLower(program.hostname)
@ -180,12 +180,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(plugin.StatusUnknown, "could not read serials")
return
}
if cSerial == rSerial {
program.plugin.SetState(plugin.OK, "serials match")
program.plugin.SetState(plugin.StatusOK, "serials match")
} else {
program.plugin.SetState(plugin.CRITICAL, "serials mismatch")
program.plugin.SetState(plugin.StatusCritical, "serials mismatch")
}
}

View file

@ -26,7 +26,7 @@ type Plugin struct {
func New(name string) *Plugin {
p := new(Plugin)
p.name = name
p.status = UNKNOWN
p.status = StatusUnknown
p.message = "no status set"
p.perfData = make(map[string]*perfdata.PerfData)
return p

View file

@ -7,10 +7,10 @@ type Status int
// Plugin exit statuses.
const (
OK Status = iota
WARNING
CRITICAL
UNKNOWN
StatusOK Status = iota
StatusWarning
StatusCritical
StatusUnknown
)
// String representations of the plugin statuses.