From c958824bb65d2d627271fb4015f5926970957287 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Tue, 20 Jan 2015 20:30:59 -0800 Subject: [PATCH] agent: use squash mapstructure tag to properly decode embedded structs --- command/agent/config.go | 16 +++++++++++----- command/agent/config_test.go | 8 +++++--- command/agent/http_test.go | 3 ++- command/agent/util_test.go | 12 ++++++------ 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/command/agent/config.go b/command/agent/config.go index 163fa18712..da444ab5cd 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -348,26 +348,32 @@ type Config struct { UnixSockets UnixSocketConfig `mapstructure:"unix_sockets"` } -// UnixSocketConfig contains information about a unix socket, and +// UnixSocketPermissions contains information about a unix socket, and // implements the FilePermissions interface. -type UnixSocketConfig struct { +type UnixSocketPermissions struct { Usr string `mapstructure:"user"` Grp string `mapstructure:"group"` Perms string `mapstructure:"mode"` } -func (u UnixSocketConfig) User() string { +func (u UnixSocketPermissions) User() string { return u.Usr } -func (u UnixSocketConfig) Group() string { +func (u UnixSocketPermissions) Group() string { return u.Grp } -func (u UnixSocketConfig) Mode() string { +func (u UnixSocketPermissions) Mode() string { return u.Perms } +// UnixSocketConfig stores information about various unix sockets which +// Consul creates and uses for communication. +type UnixSocketConfig struct { + UnixSocketPermissions `mapstructure:",squash"` +} + // unixSocketAddr tests if a given address describes a domain socket, // and returns the relevant path part of the string if it is. func unixSocketAddr(addr string) (string, bool) { diff --git a/command/agent/config_test.go b/command/agent/config_test.go index afdb245233..3d4d4b7404 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -1016,9 +1016,11 @@ func TestMergeConfig(t *testing.T) { "Access-Control-Allow-Origin": "*", }, UnixSockets: UnixSocketConfig{ - Usr: "500", - Grp: "500", - Perms: "0700", + UnixSocketPermissions{ + Usr: "500", + Grp: "500", + Perms: "0700", + }, }, } diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 67d597ed21..19e8f95af9 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -70,7 +70,8 @@ func TestHTTPServer_UnixSocket(t *testing.T) { // Only testing mode, since uid/gid might not be settable // from test environment. - c.UnixSockets = UnixSocketConfig{Perms: "0777"} + c.UnixSockets = UnixSocketConfig{} + c.UnixSockets.Perms = "0777" }) defer os.RemoveAll(dir) defer srv.Shutdown() diff --git a/command/agent/util_test.go b/command/agent/util_test.go index df5dba3fad..ab47c5e0f0 100644 --- a/command/agent/util_test.go +++ b/command/agent/util_test.go @@ -51,22 +51,22 @@ func TestSetFilePermissions(t *testing.T) { defer os.Remove(path) // Bad UID fails - if err := setFilePermissions(path, UnixSocketConfig{Usr: "%"}); err == nil { + if err := setFilePermissions(path, UnixSocketPermissions{Usr: "%"}); err == nil { t.Fatalf("should fail") } // Bad GID fails - if err := setFilePermissions(path, UnixSocketConfig{Grp: "%"}); err == nil { + if err := setFilePermissions(path, UnixSocketPermissions{Grp: "%"}); err == nil { t.Fatalf("should fail") } // Bad mode fails - if err := setFilePermissions(path, UnixSocketConfig{Perms: "%"}); err == nil { + if err := setFilePermissions(path, UnixSocketPermissions{Perms: "%"}); err == nil { t.Fatalf("should fail") } // Allows omitting user/group/mode - if err := setFilePermissions(path, UnixSocketConfig{}); err != nil { + if err := setFilePermissions(path, UnixSocketPermissions{}); err != nil { t.Fatalf("err: %s", err) } @@ -74,7 +74,7 @@ func TestSetFilePermissions(t *testing.T) { if err := os.Chmod(path, 0700); err != nil { t.Fatalf("err: %s", err) } - if err := setFilePermissions(path, UnixSocketConfig{}); err != nil { + if err := setFilePermissions(path, UnixSocketPermissions{}); err != nil { t.Fatalf("err: %s", err) } fi, err := os.Stat(path) @@ -86,7 +86,7 @@ func TestSetFilePermissions(t *testing.T) { } // Changes mode if given - if err := setFilePermissions(path, UnixSocketConfig{Perms: "0777"}); err != nil { + if err := setFilePermissions(path, UnixSocketPermissions{Perms: "0777"}); err != nil { t.Fatalf("err: %s", err) } fi, err = os.Stat(path)