mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
feat(config): support multi-service yaml config
This commit is contained in:
1 parent
655db8dcbf
commit
f3a23ee91e
19 files changed
+709
-268
No files matched your search
+118
@@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNormalizeConfigGeneratesNamesFromAdapterAndHost(t *testing.T) {
|
||||
services, err := normalizeConfig(appConfig{
|
||||
Services: []serviceFileConfig{
|
||||
{Adapter: "redis", Config: map[string]string{"url": "redis://default@cache.example.com:6379"}},
|
||||
{Adapter: "mongodb", Config: map[string]string{"uri": "mongodb+srv://user:pass@cluster.mongodb.net", "database": "keepalive", "collection": "counter"}},
|
||||
{Adapter: "couchbase", Config: map[string]string{"connection_string": "couchbases://cb.example.net", "username": "u", "password": "p", "bucket_name": "b", "scope_name": "_default", "collection_name": "_default"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("normalizeConfig returned error: %v", err)
|
||||
}
|
||||
|
||||
want := []string{"redis-cache-example-com", "mongodb-cluster-mongodb-net", "couchbase-cb-example-net"}
|
||||
for i, service := range services {
|
||||
if service.Name != want[i] {
|
||||
t.Fatalf("services[%d].Name = %q, want %q", i, service.Name, want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeepaliveExampleConfigParses(t *testing.T) {
|
||||
services, err := loadConfigFile("keepalive.example.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("loadConfigFile returned error: %v", err)
|
||||
}
|
||||
if len(services) != 4 {
|
||||
t.Fatalf("len(services) = %d, want 4", len(services))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeConfigSuffixesDuplicateGeneratedNames(t *testing.T) {
|
||||
services, err := normalizeConfig(appConfig{
|
||||
Services: []serviceFileConfig{
|
||||
{Adapter: "redis", Config: map[string]string{"url": "redis://cache.example.com:6379"}},
|
||||
{Adapter: "redis", Config: map[string]string{"url": "redis://cache.example.com:6379"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("normalizeConfig returned error: %v", err)
|
||||
}
|
||||
|
||||
if services[0].Name != "redis-cache-example-com" {
|
||||
t.Fatalf("services[0].Name = %q", services[0].Name)
|
||||
}
|
||||
if services[1].Name != "redis-cache-example-com-2" {
|
||||
t.Fatalf("services[1].Name = %q", services[1].Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeConfigRejectsDuplicateExplicitNames(t *testing.T) {
|
||||
_, err := normalizeConfig(appConfig{
|
||||
Services: []serviceFileConfig{
|
||||
{Name: "cache", Adapter: "redis", Config: map[string]string{"url": "redis://one.example.com:6379"}},
|
||||
{Name: "cache", Adapter: "redis", Config: map[string]string{"url": "redis://two.example.com:6379"}},
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("normalizeConfig returned nil error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeConfigRejectsExplicitNameThatDuplicatesGeneratedName(t *testing.T) {
|
||||
_, err := normalizeConfig(appConfig{
|
||||
Services: []serviceFileConfig{
|
||||
{Adapter: "redis", Config: map[string]string{"url": "redis://cache.example.com:6379"}},
|
||||
{Name: "redis-cache-example-com", Adapter: "redis", Config: map[string]string{"url": "redis://other.example.com:6379"}},
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("normalizeConfig returned nil error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeConfigAppliesGlobalAndServiceDefaults(t *testing.T) {
|
||||
services, err := normalizeConfig(appConfig{
|
||||
Interval: "2m",
|
||||
CounterKey: "global-counter",
|
||||
Services: []serviceFileConfig{
|
||||
{Adapter: "redis", Config: map[string]string{"url": "redis://cache.example.com:6379"}},
|
||||
{Adapter: "redis", Interval: "30s", CounterKey: "local-counter", Config: map[string]string{"url": "redis://other.example.com:6379"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("normalizeConfig returned error: %v", err)
|
||||
}
|
||||
|
||||
if services[0].Interval != 2*time.Minute {
|
||||
t.Fatalf("services[0].Interval = %s, want 2m", services[0].Interval)
|
||||
}
|
||||
if services[0].Config["counter_key"] != "global-counter" {
|
||||
t.Fatalf("services[0] counter_key = %q", services[0].Config["counter_key"])
|
||||
}
|
||||
if services[1].Interval != 30*time.Second {
|
||||
t.Fatalf("services[1].Interval = %s, want 30s", services[1].Interval)
|
||||
}
|
||||
if services[1].Config["counter_key"] != "local-counter" {
|
||||
t.Fatalf("services[1] counter_key = %q", services[1].Config["counter_key"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeConfigRejectsNonPositiveInterval(t *testing.T) {
|
||||
_, err := normalizeConfig(appConfig{
|
||||
Interval: "0s",
|
||||
Services: []serviceFileConfig{
|
||||
{Adapter: "redis", Config: map[string]string{"url": "redis://cache.example.com:6379"}},
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("normalizeConfig returned nil error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user