- Introduce explicit source interfaces: sources.PollSource and sources.StreamSource, with shared sources.Input (Name() only). - Remove mandatory Kind() from the base source contract to support sources that emit multiple kinds. - Add config.SourceMode (poll, stream, or omitted/auto) and SourceConfig.Kinds (plural expected kinds), while keeping legacy SourceConfig.Kind for compatibility. - Enforce mode semantics in config validation (poll requires every, stream forbids every) and detect mode/driver mismatches in sources.Registry. - Update docs and tests for the new source model and config behavior.
165 lines
3.6 KiB
Go
165 lines
3.6 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestValidate_RouteKindsEmptyIsAllowed(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{Name: "src1", Driver: "driver1", Every: Duration{Duration: time.Minute}},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
Routes: []RouteConfig{
|
|
{Sink: "sink1", Kinds: nil}, // omitted
|
|
{Sink: "sink1", Kinds: []string{}}, // explicit empty
|
|
},
|
|
}
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidate_RouteKindsRejectsBlankEntries(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{Name: "src1", Driver: "driver1", Every: Duration{Duration: time.Minute}},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
Routes: []RouteConfig{
|
|
{Sink: "sink1", Kinds: []string{"observation", " ", "alert"}},
|
|
},
|
|
}
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "routes[0].kinds[1]") {
|
|
t.Fatalf("expected error to mention blank kind entry, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidate_SourceModePollRequiresEvery(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{Name: "src1", Driver: "driver1", Mode: SourceModePoll},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
}
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), `sources[0].every`) {
|
|
t.Fatalf("expected error to mention sources[0].every, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidate_SourceModeStreamRejectsEvery(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{
|
|
Name: "src1",
|
|
Driver: "driver1",
|
|
Mode: SourceModeStream,
|
|
Every: Duration{Duration: time.Minute},
|
|
},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
}
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), `sources[0].every`) {
|
|
t.Fatalf("expected error to mention sources[0].every, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidate_SourceModeRejectsUnknownValue(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{
|
|
Name: "src1",
|
|
Driver: "driver1",
|
|
Mode: SourceMode("batch"),
|
|
Every: Duration{Duration: time.Minute},
|
|
},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
}
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), `sources[0].mode`) {
|
|
t.Fatalf("expected error to mention sources[0].mode, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidate_SourceKindAndKindsConflict(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{
|
|
Name: "src1",
|
|
Driver: "driver1",
|
|
Every: Duration{Duration: time.Minute},
|
|
Kind: "observation",
|
|
Kinds: []string{"forecast"},
|
|
},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
}
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), `sources[0].kind`) {
|
|
t.Fatalf("expected error to mention sources[0].kind, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidate_SourceKindsRejectBlankEntries(t *testing.T) {
|
|
cfg := &Config{
|
|
Sources: []SourceConfig{
|
|
{
|
|
Name: "src1",
|
|
Driver: "driver1",
|
|
Every: Duration{Duration: time.Minute},
|
|
Kinds: []string{"observation", " "},
|
|
},
|
|
},
|
|
Sinks: []SinkConfig{
|
|
{Name: "sink1", Driver: "stdout"},
|
|
},
|
|
}
|
|
|
|
err := cfg.Validate()
|
|
if err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), `sources[0].kinds[1]`) {
|
|
t.Fatalf("expected error to mention sources[0].kinds[1], got: %v", err)
|
|
}
|
|
}
|