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.
This commit is contained in:
Michael Marineau 2014-05-16 20:27:50 -07:00
parent f83ce07416
commit ea6262f0ae
2 changed files with 16 additions and 0 deletions

View File

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

View File

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