From 7e8d48f7bacb1837e655e665a8b564fb4c703d23 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 22 Sep 2015 11:29:53 -0700 Subject: [PATCH] agent: test config parsing --- command/agent/config.go | 22 +++--- command/agent/config_test.go | 138 +++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 9 deletions(-) diff --git a/command/agent/config.go b/command/agent/config.go index ed5f742fa..775d79fab 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -482,16 +482,10 @@ func LoadConfig(path string) (*Config, error) { } } -// LoadConfigFile loads the configuration from the given file. -func LoadConfigFile(path string) (*Config, error) { - // Read the file - d, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - +// LoadConfigString is used to parse a config string +func LoadConfigString(s string) (*Config, error) { // Parse! - obj, err := hcl.Parse(string(d)) + obj, err := hcl.Parse(s) if err != nil { return nil, err } @@ -505,6 +499,16 @@ func LoadConfigFile(path string) (*Config, error) { return &result, nil } +// LoadConfigFile loads the configuration from the given file. +func LoadConfigFile(path string) (*Config, error) { + // Read the file + d, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + return LoadConfigString(string(d)) +} + func getString(o *hclobj.Object) string { if o == nil || o.Type != hclobj.ValueTypeString { return "" diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 5849cd1c2..5269d6e23 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -296,3 +296,141 @@ func TestConfig_Listener(t *testing.T) { t.Fatalf("expected 0.0.0.0:24000, got: %q", addr) } } + +func TestConfig_LoadConfigString(t *testing.T) { + // Load the config + config, err := LoadConfigString(testConfig) + if err != nil { + t.Fatalf("err: %s", err) + } + + // Expected output + expect := &Config{ + Region: "foobar", + Datacenter: "dc2", + NodeName: "my-web", + DataDir: "/tmp/nomad", + LogLevel: "ERR", + BindAddr: "192.168.0.1", + EnableDebug: true, + Ports: &Ports{ + HTTP: 1234, + RPC: 2345, + Serf: 3456, + }, + Addresses: &Addresses{ + HTTP: "127.0.0.1", + RPC: "127.0.0.2", + Serf: "127.0.0.3", + }, + AdvertiseAddrs: &AdvertiseAddrs{ + RPC: "127.0.0.3", + Serf: "127.0.0.4", + }, + Client: &ClientConfig{ + Enabled: true, + StateDir: "/tmp/client-state", + AllocDir: "/tmp/alloc", + Servers: []string{"a.b.c:80", "127.0.0.1:1234"}, + NodeID: "xyz123", + NodeClass: "linux-medium-64bit", + Meta: map[string]string{ + "foo": "bar", + "baz": "zip", + }, + }, + Server: &ServerConfig{ + Enabled: true, + Bootstrap: true, + BootstrapExpect: 5, + DataDir: "/tmp/data", + ProtocolVersion: 3, + NumSchedulers: 2, + EnabledSchedulers: []string{"test"}, + }, + Telemetry: &Telemetry{ + StatsiteAddr: "127.0.0.1:1234", + StatsdAddr: "127.0.0.1:2345", + DisableHostname: true, + }, + LeaveOnInt: true, + LeaveOnTerm: true, + EnableSyslog: true, + SyslogFacility: "LOCAL1", + DisableUpdateCheck: true, + DisableAnonymousSignature: true, + Atlas: &AtlasConfig{ + Infrastructure: "armon/test", + Token: "abcd", + Join: true, + Endpoint: "127.0.0.1:1234", + }, + } + + // Check parsing + if !reflect.DeepEqual(config, expect) { + t.Fatalf("bad: got: %#v\nexpect: %#v", config, expect) + } +} + +const testConfig = ` +region = "foobar" +datacenter = "dc2" +name = "my-web" +data_dir = "/tmp/nomad" +log_level = "ERR" +bind_addr = "192.168.0.1" +enable_debug = true +ports { + http = 1234 + rpc = 2345 + serf = 3456 +} +addresses { + http = "127.0.0.1" + rpc = "127.0.0.2" + serf = "127.0.0.3" +} +advertise { + rpc = "127.0.0.3" + serf = "127.0.0.4" +} +client { + enabled = true + state_dir = "/tmp/client-state" + alloc_dir = "/tmp/alloc" + servers = ["a.b.c:80", "127.0.0.1:1234"] + node_id = "xyz123" + node_class = "linux-medium-64bit" + meta { + foo = "bar" + baz = "zip" + } +} +server { + enabled = true + bootstrap = true + bootstrap_expect = 5 + data_dir = "/tmp/data" + protocol_version = 3 + num_schedulers = 2 + enabled_schedulers = ["test"] +} +telemetry { + statsite_address = "127.0.0.1:1234" + statsd_address = "127.0.0.1:2345" + disable_hostname = true +} +leave_on_interrupt = true +leave_on_terminate = true +enable_syslog = true +syslog_facility = "LOCAL1" +disable_update_check = true +disable_anonymous_signature = true +atlas { + infrastructure = "armon/test" + token = "abcd" + join = true + endpoint = "127.0.0.1:1234" +} +`