mirror of
https://github.com/kerberos-io/agent.git
synced 2026-11-21 22:50:39 +00:00
Avoid dereferencing the nil client returned when Hub credentials are absent. Log the disabled worker and cover enabled, disabled, and offline configurations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package cloud
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kerberos-io/agent/machinery/src/models"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestHandleRealtimeProcessingWithoutMQTTClient(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
config models.Config
|
|
wantWarning bool
|
|
}{
|
|
{
|
|
name: "enabled without Hub credentials",
|
|
config: models.Config{RealtimeProcessing: "true"},
|
|
wantWarning: true,
|
|
},
|
|
{
|
|
name: "disabled",
|
|
config: models.Config{RealtimeProcessing: "false"},
|
|
},
|
|
{
|
|
name: "offline",
|
|
config: models.Config{RealtimeProcessing: "true", Offline: "true"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
hook := test.NewGlobal()
|
|
defer hook.Reset()
|
|
|
|
configuration := &models.Configuration{Config: tt.config}
|
|
// No cursor or decoder should be accessed without an MQTT client.
|
|
HandleRealtimeProcessing(nil, configuration, nil, nil, nil)
|
|
|
|
warnings := 0
|
|
for _, entry := range hook.AllEntries() {
|
|
if entry.Level == log.WarnLevel {
|
|
warnings++
|
|
if entry.Message != "cloud.RealtimeProcessing(): stopping as MQTT client is not configured." {
|
|
t.Errorf("unexpected warning: %s", entry.Message)
|
|
}
|
|
}
|
|
}
|
|
wantWarnings := 0
|
|
if tt.wantWarning {
|
|
wantWarnings = 1
|
|
}
|
|
if warnings != wantWarnings {
|
|
t.Fatalf("warnings = %d, want %d", warnings, wantWarnings)
|
|
}
|
|
})
|
|
}
|
|
}
|