From b1b2e76ccda5479d845656623c4cb90ba542c37c Mon Sep 17 00:00:00 2001 From: Richard Patel Date: Wed, 4 May 2022 11:55:46 +0200 Subject: [PATCH] fix config loading --- example-config.yml | 3 --- types/auth.go | 14 +++++++------- types/config.go | 32 ++++++++++++++++++-------------- types/config_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 24 deletions(-) create mode 100644 types/config_test.go diff --git a/example-config.yml b/example-config.yml index 6f1936b..2360aae 100644 --- a/example-config.yml +++ b/example-config.yml @@ -11,9 +11,6 @@ target_groups: # URL scheme, use "http" or "https". scheme: http - # Snapshot path on a node. - snapshot_path: "/snapshot/tar.bz2" - # ------------------------------------------------ # Discovery # ------------------------------------------------ diff --git a/types/auth.go b/types/auth.go index 637faa7..b0c6e38 100644 --- a/types/auth.go +++ b/types/auth.go @@ -25,8 +25,8 @@ import ( ) type BasicAuth struct { - Username string `json:"username"` - Password string `json:"password"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` } func (b *BasicAuth) Apply(header http.Header) { @@ -35,7 +35,7 @@ func (b *BasicAuth) Apply(header http.Header) { } type BearerAuth struct { - Token string `json:"token"` + Token string `json:"token" yaml:"token"` } func (b *BearerAuth) Apply(header http.Header) { @@ -43,10 +43,10 @@ func (b *BearerAuth) Apply(header http.Header) { } type TLSConfig struct { - CAFile string `json:"ca_file"` - CertFile string `json:"cert_file"` - KeyFile string `json:"key_file"` - InsecureSkipVerify bool `json:"insecure_skip_verify"` + CAFile string `json:"ca_file" yaml:"ca_file"` + CertFile string `json:"cert_file" yaml:"cert_file"` + KeyFile string `json:"key_file" yaml:"key_file"` + InsecureSkipVerify bool `json:"insecure_skip_verify" yaml:"insecure_skip_verify"` } func init() { diff --git a/types/config.go b/types/config.go index 138718e..87e7873 100644 --- a/types/config.go +++ b/types/config.go @@ -27,32 +27,36 @@ import ( // Config describes the root-level config file. type Config struct { - ScrapeInterval time.Duration `json:"scrape_interval"` - TargetGroups []*TargetGroup `json:"target_groups"` + ScrapeInterval time.Duration `json:"scrape_interval" yaml:"scrape_interval"` + TargetGroups []*TargetGroup `json:"target_groups" yaml:"target_groups"` } // LoadConfig reads the config object from the file system. func LoadConfig(filePath string) (*Config, error) { - configBytes, err := os.ReadFile(filePath) + f, err := os.Open(filePath) if err != nil { return nil, err } + defer f.Close() + conf := new(Config) - confErr := yaml.Unmarshal(configBytes, conf) + decoder := yaml.NewDecoder(f) + decoder.KnownFields(true) + confErr := decoder.Decode(conf) return conf, confErr } // TargetGroup explains how to retrieve snapshots from a group of Solana nodes. type TargetGroup struct { - Group string `json:"group"` - Scheme string `json:"scheme"` - APIPath string `json:"api_path"` - BasicAuth *BasicAuth `json:"basic_auth"` - BearerAuth *BearerAuth `json:"bearer_auth"` - TLSConfig *TLSConfig `json:"tls_config"` + Group string `json:"group" yaml:"group"` + Scheme string `json:"scheme" yaml:"scheme"` + APIPath string `json:"api_path" yaml:"api_path"` + BasicAuth *BasicAuth `json:"basic_auth" yaml:"basic_auth"` + BearerAuth *BearerAuth `json:"bearer_auth" yaml:"bearer_auth"` + TLSConfig *TLSConfig `json:"tls_config" yaml:"tls_config"` - StaticTargets *StaticTargets `json:"static_targets"` - FileTargets *FileTargets `json:"file_targets"` + StaticTargets *StaticTargets `json:"static_targets" yaml:"static_targets"` + FileTargets *FileTargets `json:"file_targets" yaml:"file_targets"` } func (t *TargetGroup) Discoverer() discovery.Discoverer { @@ -67,7 +71,7 @@ func (t *TargetGroup) Discoverer() discovery.Discoverer { // StaticTargets is a hardcoded list of Solana nodes. type StaticTargets struct { - Targets []string `json:"targets"` + Targets []string `json:"targets" yaml:"targets"` } func (s *StaticTargets) DiscoverTargets(_ context.Context) ([]string, error) { @@ -76,7 +80,7 @@ func (s *StaticTargets) DiscoverTargets(_ context.Context) ([]string, error) { // FileTargets reads targets from a JSON file. type FileTargets struct { - Path string `json:"path"` + Path string `json:"path" yaml:"path"` } func (d *FileTargets) DiscoverTargets(_ context.Context) ([]string, error) { diff --git a/types/config_test.go b/types/config_test.go new file mode 100644 index 0000000..5f13e53 --- /dev/null +++ b/types/config_test.go @@ -0,0 +1,39 @@ +package types + +import ( + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestLoadConfig(t *testing.T) { + _, testFile, _, ok := runtime.Caller(0) + assert.True(t, ok) + exampleConfig := filepath.Join(filepath.Dir(testFile), "../example-config.yml") + + actual, err := LoadConfig(exampleConfig) + require.NoError(t, err) + + expected := &Config{ + ScrapeInterval: 15 * time.Second, + TargetGroups: []*TargetGroup{ + { + Group: "mainnet", + Scheme: "http", + StaticTargets: &StaticTargets{ + Targets: []string{ + "solana-mainnet-1.example.org:8899", + "solana-mainnet-2.example.org:8899", + "solana-mainnet-3.example.org:8899", + }, + }, + }, + }, + } + + assert.Equal(t, expected, actual) +}