This modifies the initial configuration error handling logic to avoid a bunch of duplicate code and fix some cases that were not logging the errors as expected. It accomplishes this by consolidating the error reporting in a single place in the caller and introducing a custom error type which signals that the usage message should be suppressed in order to retain the primary behavior the originally was the reason for the prints being in the config function to begin with. Also, the application name is passed into the function now since it is needed in both places.
89 lines
2.9 KiB
Go
89 lines
2.9 KiB
Go
// Copyright (c) 2018 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// In order to test command line arguments and environment variables, append
|
|
// the flags to the os.Args variable like so:
|
|
// os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
|
|
//
|
|
// For environment variables, use the following to set the variable before the
|
|
// func that loads the configuration is called:
|
|
// os.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2")
|
|
//
|
|
// These args and env variables will then get parsed during configuration load.
|
|
|
|
// TestLoadConfig ensures that basic configuration loading succeeds.
|
|
func TestLoadConfig(t *testing.T) {
|
|
appName := filepath.Base(os.Args[0])
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
_, _, err := loadConfig(appName)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load dcrd config: %s", err)
|
|
}
|
|
}
|
|
|
|
// TestDefaultAltDNSNames ensures that there are no additional hostnames added
|
|
// by default during the configuration load phase.
|
|
func TestDefaultAltDNSNames(t *testing.T) {
|
|
appName := filepath.Base(os.Args[0])
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
cfg, _, err := loadConfig(appName)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load dcrd config: %s", err)
|
|
}
|
|
if len(cfg.AltDNSNames) != 0 {
|
|
t.Fatalf("Invalid default value for altdnsnames: %s", cfg.AltDNSNames)
|
|
}
|
|
}
|
|
|
|
// TestAltDNSNamesWithEnv ensures the DCRD_ALT_DNSNAMES environment variable is
|
|
// parsed into a slice of additional hostnames as intended.
|
|
func TestAltDNSNamesWithEnv(t *testing.T) {
|
|
appName := filepath.Base(os.Args[0])
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
os.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2")
|
|
cfg, _, err := loadConfig(appName)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load dcrd config: %s", err)
|
|
}
|
|
hostnames := strings.Join(cfg.AltDNSNames, ",")
|
|
if hostnames != "hostname1,hostname2" {
|
|
t.Fatalf("altDNSNames should be %s but was %s", "hostname1,hostname2",
|
|
hostnames)
|
|
}
|
|
}
|
|
|
|
// TestAltDNSNamesWithArg ensures the altdnsnames configuration option parses
|
|
// additional hostnames into a slice of hostnames as intended.
|
|
func TestAltDNSNamesWithArg(t *testing.T) {
|
|
appName := filepath.Base(os.Args[0])
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
old := os.Args
|
|
os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
|
|
cfg, _, err := loadConfig(appName)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load dcrd config: %s", err)
|
|
}
|
|
hostnames := strings.Join(cfg.AltDNSNames, ",")
|
|
if hostnames != "hostname1,hostname2" {
|
|
t.Fatalf("altDNSNames should be %s but was %s", "hostname1,hostname2",
|
|
hostnames)
|
|
}
|
|
os.Args = old
|
|
}
|
|
|
|
// init parses the -test.* flags from the command line arguments list and then
|
|
// removes them to allow go-flags tests to succeed.
|
|
func init() {
|
|
os.Args = os.Args[:1]
|
|
}
|