mirror of https://github.com/status-im/consul.git
config: move testing shims to BuilderOpts
And remove the devMode field from builder. This change helps make the Builder state more explicit by moving inputs to the BuilderOps struct, leaving only fields that can change during Builder.Build on the Builder struct.
This commit is contained in:
parent
0a44906fe3
commit
78f767e78c
|
@ -92,8 +92,7 @@ func Load(opts BuilderOpts, extraHead Source, overrides ...Source) (*RuntimeConf
|
||||||
// since not all pre-conditions have to be satisfied when performing
|
// since not all pre-conditions have to be satisfied when performing
|
||||||
// syntactical tests.
|
// syntactical tests.
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
// devMode stores the value of the -dev flag, and enables development mode.
|
opts BuilderOpts
|
||||||
devMode *bool
|
|
||||||
|
|
||||||
// Head, Sources, and Tail are used to manage the order of the
|
// Head, Sources, and Tail are used to manage the order of the
|
||||||
// config sources, as described in the comments above.
|
// config sources, as described in the comments above.
|
||||||
|
@ -105,15 +104,6 @@ type Builder struct {
|
||||||
// parsing the configuration.
|
// parsing the configuration.
|
||||||
Warnings []string
|
Warnings []string
|
||||||
|
|
||||||
// hostname is a shim for testing, allowing tests to specify a replacement
|
|
||||||
// for os.Hostname.
|
|
||||||
hostname func() (string, error)
|
|
||||||
|
|
||||||
// getPrivateIPv4 and getPublicIPv6 are shims for testing, allowing tests to
|
|
||||||
// specify a replacement for ipaddr.GetPrivateIPv4 and ipaddr.GetPublicIPv6.
|
|
||||||
getPrivateIPv4 func() ([]*net.IPAddr, error)
|
|
||||||
getPublicIPv6 func() ([]*net.IPAddr, error)
|
|
||||||
|
|
||||||
// err contains the first error that occurred during
|
// err contains the first error that occurred during
|
||||||
// building the runtime configuration.
|
// building the runtime configuration.
|
||||||
err error
|
err error
|
||||||
|
@ -135,7 +125,7 @@ func NewBuilder(opts BuilderOpts) (*Builder, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
b := &Builder{
|
b := &Builder{
|
||||||
devMode: opts.DevMode,
|
opts: opts,
|
||||||
Head: []Source{DefaultSource(), DefaultEnterpriseSource()},
|
Head: []Source{DefaultSource(), DefaultEnterpriseSource()},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,14 +457,14 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
|
||||||
switch {
|
switch {
|
||||||
case ipaddr.IsAnyV4(advertiseAddr):
|
case ipaddr.IsAnyV4(advertiseAddr):
|
||||||
addrtyp = "private IPv4"
|
addrtyp = "private IPv4"
|
||||||
detect = b.getPrivateIPv4
|
detect = b.opts.getPrivateIPv4
|
||||||
if detect == nil {
|
if detect == nil {
|
||||||
detect = ipaddr.GetPrivateIPv4
|
detect = ipaddr.GetPrivateIPv4
|
||||||
}
|
}
|
||||||
|
|
||||||
case ipaddr.IsAnyV6(advertiseAddr):
|
case ipaddr.IsAnyV6(advertiseAddr):
|
||||||
addrtyp = "public IPv6"
|
addrtyp = "public IPv6"
|
||||||
detect = b.getPublicIPv6
|
detect = b.opts.getPublicIPv6
|
||||||
if detect == nil {
|
if detect == nil {
|
||||||
detect = ipaddr.GetPublicIPv6
|
detect = ipaddr.GetPublicIPv6
|
||||||
}
|
}
|
||||||
|
@ -994,7 +984,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
|
||||||
DataDir: dataDir,
|
DataDir: dataDir,
|
||||||
Datacenter: datacenter,
|
Datacenter: datacenter,
|
||||||
DefaultQueryTime: b.durationVal("default_query_time", c.DefaultQueryTime),
|
DefaultQueryTime: b.durationVal("default_query_time", c.DefaultQueryTime),
|
||||||
DevMode: b.boolVal(b.devMode),
|
DevMode: b.boolVal(b.opts.DevMode),
|
||||||
DisableAnonymousSignature: b.boolVal(c.DisableAnonymousSignature),
|
DisableAnonymousSignature: b.boolVal(c.DisableAnonymousSignature),
|
||||||
DisableCoordinates: b.boolVal(c.DisableCoordinates),
|
DisableCoordinates: b.boolVal(c.DisableCoordinates),
|
||||||
DisableHostNodeID: b.boolVal(c.DisableHostNodeID),
|
DisableHostNodeID: b.boolVal(c.DisableHostNodeID),
|
||||||
|
@ -1900,7 +1890,7 @@ func (b *Builder) tlsCipherSuites(name string, v *string) []uint16 {
|
||||||
func (b *Builder) nodeName(v *string) string {
|
func (b *Builder) nodeName(v *string) string {
|
||||||
nodeName := b.stringVal(v)
|
nodeName := b.stringVal(v)
|
||||||
if nodeName == "" {
|
if nodeName == "" {
|
||||||
fn := b.hostname
|
fn := b.opts.hostname
|
||||||
if fn == nil {
|
if fn == nil {
|
||||||
fn = os.Hostname
|
fn = os.Hostname
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,13 +172,13 @@ func TestBuilder_BuildAndValidate_NodeName(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func patchBuilderShims(b *Builder) {
|
func patchBuilderShims(b *Builder) {
|
||||||
b.hostname = func() (string, error) {
|
b.opts.hostname = func() (string, error) {
|
||||||
return "thehostname", nil
|
return "thehostname", nil
|
||||||
}
|
}
|
||||||
b.getPrivateIPv4 = func() ([]*net.IPAddr, error) {
|
b.opts.getPrivateIPv4 = func() ([]*net.IPAddr, error) {
|
||||||
return []*net.IPAddr{ipAddr("10.0.0.1")}, nil
|
return []*net.IPAddr{ipAddr("10.0.0.1")}, nil
|
||||||
}
|
}
|
||||||
b.getPublicIPv6 = func() ([]*net.IPAddr, error) {
|
b.opts.getPublicIPv6 = func() ([]*net.IPAddr, error) {
|
||||||
return []*net.IPAddr{ipAddr("dead:beef::1")}, nil
|
return []*net.IPAddr{ipAddr("dead:beef::1")}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package config
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,6 +27,15 @@ type BuilderOpts struct {
|
||||||
|
|
||||||
// HCL contains an arbitrary config in hcl format.
|
// HCL contains an arbitrary config in hcl format.
|
||||||
HCL []string
|
HCL []string
|
||||||
|
|
||||||
|
// hostname is a shim for testing, allowing tests to specify a replacement
|
||||||
|
// for os.Hostname.
|
||||||
|
hostname func() (string, error)
|
||||||
|
|
||||||
|
// getPrivateIPv4 and getPublicIPv6 are shims for testing, allowing tests to
|
||||||
|
// specify a replacement for ipaddr.GetPrivateIPv4 and ipaddr.GetPublicIPv6.
|
||||||
|
getPrivateIPv4 func() ([]*net.IPAddr, error)
|
||||||
|
getPublicIPv6 func() ([]*net.IPAddr, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddFlags adds the command line flags for the agent.
|
// AddFlags adds the command line flags for the agent.
|
||||||
|
|
|
@ -4886,13 +4886,13 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
|
||||||
|
|
||||||
patchBuilderShims(b)
|
patchBuilderShims(b)
|
||||||
if tt.hostname != nil {
|
if tt.hostname != nil {
|
||||||
b.hostname = tt.hostname
|
b.opts.hostname = tt.hostname
|
||||||
}
|
}
|
||||||
if tt.privatev4 != nil {
|
if tt.privatev4 != nil {
|
||||||
b.getPrivateIPv4 = tt.privatev4
|
b.opts.getPrivateIPv4 = tt.privatev4
|
||||||
}
|
}
|
||||||
if tt.publicv6 != nil {
|
if tt.publicv6 != nil {
|
||||||
b.getPublicIPv6 = tt.publicv6
|
b.opts.getPublicIPv6 = tt.publicv6
|
||||||
}
|
}
|
||||||
|
|
||||||
// read the source fragements
|
// read the source fragements
|
||||||
|
@ -4937,9 +4937,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
x.hostname = b.hostname
|
patchBuilderShims(x)
|
||||||
x.getPrivateIPv4 = func() ([]*net.IPAddr, error) { return []*net.IPAddr{ipAddr("10.0.0.1")}, nil }
|
|
||||||
x.getPublicIPv6 = func() ([]*net.IPAddr, error) { return []*net.IPAddr{ipAddr("dead:beef::1")}, nil }
|
|
||||||
expected, err := x.Build()
|
expected, err := x.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("build default failed: %s", err)
|
t.Fatalf("build default failed: %s", err)
|
||||||
|
|
Loading…
Reference in New Issue