mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
This cleans up code recently added to support the --altdnsnames flag and
DCRD_ALT_DNSNAMES environment variable to match the project standards,
simplify it, and correct some issues with standalone test binaries.
- Gets rid of the setup func which had several issues:
- It was creating a file that was not needed (and without even
checking the error)
- Since it was parsing flags and missing with os.Args in a func, it
was not possible to individual run the test funcs without failure
due to the additional flags
- Performs the flag parsing and removal in an init func that only runs
once when the tests are run (and before TestMain)
- Avoids creating empty objects when nil will suffice
- Uses full sentences and periods in the comment and properly spaces
them out so they are consistent with the rest of the code
- Adds comments to all of the added test functions as required by the
code contribution guidelines
- Closes the create temp files before trying to write to them in the cert
generation
- Defers the removal of the temp files directly after they are created so
they are removed properly if the next one fails to create
- Uses consistent camel case in the variable names
82 lines
2.5 KiB
Go
82 lines
2.5 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"
|
|
"os"
|
|
"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) {
|
|
_, _, err := loadConfig()
|
|
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) {
|
|
cfg, _, err := loadConfig()
|
|
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) {
|
|
os.Setenv("DCRD_ALT_DNSNAMES", "hostname1,hostname2")
|
|
cfg, _, err := loadConfig()
|
|
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) {
|
|
old := os.Args
|
|
os.Args = append(os.Args, "--altdnsnames=\"hostname1,hostname2\"")
|
|
cfg, _, err := loadConfig()
|
|
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() {
|
|
flag.Parse()
|
|
os.Args = os.Args[:1]
|
|
}
|