dcrd/config_test.go
Corey Osman ea54d032df rpcserver: Adds ability to allow alternative dns names for TLS.
- rpcserver: Adds ability to allow alternative dns names for TLS.
- Adds AltDNSNames config for passing additional hostnames to the
  TLS certificate generate method.  Without this change there is no
  way to pass in alternative dns names for the rpc server.
- Additionally this commit allows the user to set 'DCRD_ALT_DNSNAMES'
  as an environment variable when starting dcrd.  This is useful for
  docker and other app runtimes that require apps to be 12 factor.
2018-10-10 23:40:40 -05:00

69 lines
2.0 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 (
"flag"
"io/ioutil"
"os"
"strings"
"testing"
)
// in order to test command line arguments and environment variables
// you will need to append the flags to the os.Args variable like so
// os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
// For environment variables you can use the
// os.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2") to set the variable
// before loadConfig() is called
// These args and env variables will then get parsed by loadConfig()
func setup() {
// Temp config file is used to ensure there are no external influences
// from previously set env variables or default config files.
file, _ := ioutil.TempFile("", "dcrd_test_file.cfg")
defer os.Remove(file.Name())
// Parse the -test.* flags before removing them from the command line
// arguments list, which we do to allow go-flags to succeed.
flag.Parse()
os.Args = os.Args[:1]
}
func TestLoadConfig(t *testing.T) {
_, _, err := loadConfig()
if err != nil {
t.Errorf("Failed to load dcrd config: %s\n", err.Error())
}
}
func TestDefaultAltDNSNames(t *testing.T) {
cfg, _, _ := loadConfig()
if len(cfg.AltDNSNames) != 0 {
t.Errorf("Invalid default value for altdnsnames: %s\n", cfg.AltDNSNames)
}
}
func TestAltDNSNamesWithEnv(t *testing.T) {
os.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2")
cfg, _, _ := loadConfig()
hostnames := strings.Join(cfg.AltDNSNames, ",")
if hostnames != "hostname1,hostname2" {
t.Errorf("altDNSNames should be %s but was %s", "hostname1,hostname2", hostnames)
}
}
func TestAltDNSNamesWithArg(t *testing.T) {
setup()
old := os.Args
os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
cfg, _, _ := loadConfig()
hostnames := strings.Join(cfg.AltDNSNames, ",")
if hostnames != "hostname1,hostname2" {
t.Errorf("altDNSNames should be %s but was %s", "hostname1,hostname2", hostnames)
}
os.Args = old
}