agent: use squash mapstructure tag to properly decode embedded structs

This commit is contained in:
Ryan Uber 2015-01-20 20:30:59 -08:00
parent 5a8703bb00
commit c958824bb6
4 changed files with 24 additions and 15 deletions

View File

@ -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) {

View File

@ -1016,10 +1016,12 @@ func TestMergeConfig(t *testing.T) {
"Access-Control-Allow-Origin": "*",
},
UnixSockets: UnixSocketConfig{
UnixSocketPermissions{
Usr: "500",
Grp: "500",
Perms: "0700",
},
},
}
c := MergeConfig(a, b)

View File

@ -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()

View File

@ -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)