fix(influx): select an active config when deleting the active config

pull/18713/head
Johnny Steenbergen 2020-06-24 15:03:14 -07:00 committed by Johnny Steenbergen
parent 74722d5191
commit 5612930a83
2 changed files with 30 additions and 18 deletions

View File

@ -221,6 +221,14 @@ func (svc localConfigsSVC) DeleteConfig(name string) (Config, error) {
}
delete(cfgs, name)
if p.Active && len(cfgs) > 0 {
for name, cfg := range cfgs {
cfg.Active = true
cfgs[name] = cfg
break
}
}
return p, svc.writeConfigs(cfgs)
}

View File

@ -770,31 +770,35 @@ func TestConfigDelete(t *testing.T) {
},
stored: Configs{
"a2": {
Name: "a2",
Host: "host2",
Org: "org2",
Token: "tok2",
Active: true,
Name: "a2",
Host: "host2",
Org: "org2",
Token: "tok2",
},
},
},
}
for _, c := range cases {
svc, store := newBufferSVC()
_ = store.writeConfigs(c.exists)
result, err := svc.DeleteConfig(c.target)
influxtesting.ErrorsEqual(t, err, c.err)
if err == nil {
if diff := cmp.Diff(result, c.result); diff != "" {
t.Fatalf("delete config %s failed, diff %s", c.name, diff)
}
stored, err := store.ListConfigs()
if err != nil {
t.Fatalf("delete config %s to list result, err %s", c.name, err.Error())
}
if diff := cmp.Diff(stored, c.stored); diff != "" {
t.Fatalf("delete config %s failed, diff %s", c.name, diff)
fn := func(t *testing.T) {
svc, store := newBufferSVC()
_ = store.writeConfigs(c.exists)
result, err := svc.DeleteConfig(c.target)
influxtesting.ErrorsEqual(t, err, c.err)
if err == nil {
if diff := cmp.Diff(result, c.result); diff != "" {
t.Fatalf("delete config %s failed, diff %s", c.name, diff)
}
stored, err := store.ListConfigs()
if err != nil {
t.Fatalf("delete config %s to list result, err %s", c.name, err.Error())
}
if diff := cmp.Diff(stored, c.stored); diff != "" {
t.Fatalf("delete config %s failed, diff %s", c.name, diff)
}
}
}
t.Run(c.name, fn)
}
}