From ea6262f0ae371a9a1951b54a8ff670c9350bbf8f Mon Sep 17 00:00:00 2001 From: Michael Marineau Date: Fri, 16 May 2014 20:27:50 -0700 Subject: [PATCH] fix(etcd): fix runtime panic when etcd section is missing. The etcd code tries to assign ee["name"] even when the map was never defined and assigning to an uninitialized map causes a panic. --- initialize/etcd.go | 4 ++++ initialize/etcd_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/initialize/etcd.go b/initialize/etcd.go index d13b98a..ce0a367 100644 --- a/initialize/etcd.go +++ b/initialize/etcd.go @@ -31,6 +31,10 @@ func (ee EtcdEnvironment) String() (out string) { // Unit creates a Unit file drop-in for etcd, using any configured // options and adding a default MachineID if unset. func (ee EtcdEnvironment) Unit(root string) (*system.Unit, error) { + if ee == nil { + return nil, nil + } + if _, ok := ee["name"]; !ok { if machineID := system.MachineID(root); machineID != "" { ee["name"] = machineID diff --git a/initialize/etcd_test.go b/initialize/etcd_test.go index 8327969..ae1fa84 100644 --- a/initialize/etcd_test.go +++ b/initialize/etcd_test.go @@ -152,3 +152,15 @@ Environment="ETCD_NAME=node007" t.Fatalf("File has incorrect contents") } } + +func TestEtcdEnvironmentWhenNil(t *testing.T) { + // EtcdEnvironment will be a nil map if it wasn't in the yaml + var ee EtcdEnvironment + if ee != nil { + t.Fatalf("EtcdEnvironment is not nil") + } + u, err := ee.Unit("") + if u != nil || err != nil { + t.Fatalf("Unit returned a non-nil value for nil input") + } +}