Skip to content

Commit c55c28b

Browse files
committed
Fix env var handling
1 parent 68baa08 commit c55c28b

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (c *Config) SetDefaults(v *viper.Viper, prefix string) {
6262
v.SetDefault(p+"events.buffer_size", 1000)
6363

6464
// Teranode defaults
65+
v.SetDefault(p+"teranode.broadcast_urls", []string{})
66+
v.SetDefault(p+"teranode.datahub_urls", []string{})
67+
v.SetDefault(p+"teranode.auth_token", "")
6568
v.SetDefault(p+"teranode.timeout", "30s")
6669

6770
// Validator defaults
@@ -72,6 +75,7 @@ func (c *Config) SetDefaults(v *viper.Viper, prefix string) {
7275

7376
// Auth defaults
7477
v.SetDefault(p+"auth.enabled", false)
78+
v.SetDefault(p+"auth.token", "")
7579

7680
// Webhook defaults
7781
v.SetDefault(p+"webhook.prune_interval", "1h")

config/config_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package config
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/spf13/viper"
8+
)
9+
10+
func TestEnvVarBroadcastURLs(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
envValue string
14+
want []string
15+
}{
16+
{
17+
name: "single URL",
18+
envValue: "https://arc.taal.com",
19+
want: []string{"https://arc.taal.com"},
20+
},
21+
{
22+
name: "multiple URLs comma separated",
23+
envValue: "https://arc.taal.com,https://arc2.taal.com,https://arc3.taal.com",
24+
want: []string{"https://arc.taal.com", "https://arc2.taal.com", "https://arc3.taal.com"},
25+
},
26+
{
27+
name: "empty value",
28+
envValue: "",
29+
want: []string{},
30+
},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
v := viper.New()
36+
cfg := &Config{}
37+
cfg.SetDefaults(v, "")
38+
39+
v.SetEnvPrefix("ARCADE")
40+
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
41+
v.AutomaticEnv()
42+
43+
t.Setenv("ARCADE_TERANODE_BROADCAST_URLS", tt.envValue)
44+
45+
if err := v.Unmarshal(cfg); err != nil {
46+
t.Fatalf("failed to unmarshal config: %v", err)
47+
}
48+
49+
if len(cfg.Teranode.BroadcastURLs) != len(tt.want) {
50+
t.Fatalf("got %d URLs, want %d: %v", len(cfg.Teranode.BroadcastURLs), len(tt.want), cfg.Teranode.BroadcastURLs)
51+
}
52+
53+
for i, got := range cfg.Teranode.BroadcastURLs {
54+
if got != tt.want[i] {
55+
t.Errorf("URL[%d] = %q, want %q", i, got, tt.want[i])
56+
}
57+
}
58+
})
59+
}
60+
}
61+
62+
func TestEnvVarDataHubURLs(t *testing.T) {
63+
v := viper.New()
64+
cfg := &Config{}
65+
cfg.SetDefaults(v, "")
66+
67+
v.SetEnvPrefix("ARCADE")
68+
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
69+
v.AutomaticEnv()
70+
71+
t.Setenv("ARCADE_TERANODE_DATAHUB_URLS", "https://hub1.example.com,https://hub2.example.com")
72+
73+
if err := v.Unmarshal(cfg); err != nil {
74+
t.Fatalf("failed to unmarshal config: %v", err)
75+
}
76+
77+
if len(cfg.Teranode.DataHubURLs) != 2 {
78+
t.Fatalf("got %d URLs, want 2: %v", len(cfg.Teranode.DataHubURLs), cfg.Teranode.DataHubURLs)
79+
}
80+
81+
if cfg.Teranode.DataHubURLs[0] != "https://hub1.example.com" {
82+
t.Errorf("DataHubURLs[0] = %q, want %q", cfg.Teranode.DataHubURLs[0], "https://hub1.example.com")
83+
}
84+
85+
if cfg.Teranode.DataHubURLs[1] != "https://hub2.example.com" {
86+
t.Errorf("DataHubURLs[1] = %q, want %q", cfg.Teranode.DataHubURLs[1], "https://hub2.example.com")
87+
}
88+
}

0 commit comments

Comments
 (0)