mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 09:25:46 +03:00
agent: config merge works + tests
This commit is contained in:
@@ -175,14 +175,61 @@ func DefaultConfig() *Config {
|
||||
}
|
||||
|
||||
// Merge merges two configurations.
|
||||
func (c *Config) Merge(c2 *Config) *Config {
|
||||
result := new(Config)
|
||||
func (a *Config) Merge(b *Config) *Config {
|
||||
var result Config = *a
|
||||
|
||||
result.Telemetry = c.Telemetry
|
||||
if c2.Telemetry != nil {
|
||||
result.Telemetry = c2.Telemetry
|
||||
if b.Region != "" {
|
||||
result.Region = b.Region
|
||||
}
|
||||
return result
|
||||
if b.Datacenter != "" {
|
||||
result.Datacenter = b.Datacenter
|
||||
}
|
||||
if b.NodeName != "" {
|
||||
result.NodeName = b.NodeName
|
||||
}
|
||||
if b.DataDir != "" {
|
||||
result.DataDir = b.DataDir
|
||||
}
|
||||
if b.LogLevel != "" {
|
||||
result.LogLevel = b.LogLevel
|
||||
}
|
||||
if b.HttpAddr != "" {
|
||||
result.HttpAddr = b.HttpAddr
|
||||
}
|
||||
if b.EnableDebug {
|
||||
result.EnableDebug = true
|
||||
}
|
||||
// TODO: merge client config
|
||||
if b.Client != nil {
|
||||
result.Client = b.Client
|
||||
}
|
||||
// TODO: merge server config
|
||||
if b.Server != nil {
|
||||
result.Server = b.Server
|
||||
}
|
||||
// TODO: merge telemetry config
|
||||
if b.Telemetry != nil {
|
||||
result.Telemetry = b.Telemetry
|
||||
}
|
||||
if b.LeaveOnInt {
|
||||
result.LeaveOnInt = true
|
||||
}
|
||||
if b.LeaveOnTerm {
|
||||
result.LeaveOnTerm = true
|
||||
}
|
||||
if b.EnableSyslog {
|
||||
result.EnableSyslog = true
|
||||
}
|
||||
if b.SyslogFacility != "" {
|
||||
result.SyslogFacility = b.SyslogFacility
|
||||
}
|
||||
if b.DisableUpdateCheck {
|
||||
result.DisableUpdateCheck = true
|
||||
}
|
||||
if b.DisableAnonymousSignature {
|
||||
result.DisableAnonymousSignature = true
|
||||
}
|
||||
return &result
|
||||
}
|
||||
|
||||
// LoadConfig loads the configuration at the given path, regardless if
|
||||
|
||||
45
command/agent/config_test.go
Normal file
45
command/agent/config_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfig_Merge(t *testing.T) {
|
||||
c1 := &Config{
|
||||
Region: "region1",
|
||||
Datacenter: "dc1",
|
||||
NodeName: "node1",
|
||||
DataDir: "/tmp/dir1",
|
||||
LogLevel: "INFO",
|
||||
HttpAddr: "127.0.0.1:4646",
|
||||
EnableDebug: false,
|
||||
LeaveOnInt: false,
|
||||
LeaveOnTerm: false,
|
||||
EnableSyslog: false,
|
||||
SyslogFacility: "local0.info",
|
||||
DisableUpdateCheck: false,
|
||||
DisableAnonymousSignature: false,
|
||||
}
|
||||
|
||||
c2 := &Config{
|
||||
Region: "region2",
|
||||
Datacenter: "dc2",
|
||||
NodeName: "node2",
|
||||
DataDir: "/tmp/dir2",
|
||||
LogLevel: "DEBUG",
|
||||
HttpAddr: "0.0.0.0:80",
|
||||
EnableDebug: true,
|
||||
LeaveOnInt: true,
|
||||
LeaveOnTerm: true,
|
||||
EnableSyslog: true,
|
||||
SyslogFacility: "local0.debug",
|
||||
DisableUpdateCheck: true,
|
||||
DisableAnonymousSignature: true,
|
||||
}
|
||||
|
||||
result := c1.Merge(c2)
|
||||
if !reflect.DeepEqual(result, c2) {
|
||||
t.Fatalf("bad: %#v", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user