fix: reject ambiguous multiple default daemons, add missing tests
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
dbb0933b27
commit
2ddb33084a
2 changed files with 36 additions and 3 deletions
|
|
@ -61,18 +61,29 @@ func (m *Manifest) SlotNames() []string {
|
|||
|
||||
// DefaultDaemon returns the name, spec, and true for the default daemon.
|
||||
// A daemon is the default if it has Default:true, or if it is the only daemon
|
||||
// in the map. Returns empty values and false if no default can be determined.
|
||||
// in the map. If multiple daemons have Default:true, returns false (ambiguous).
|
||||
// Returns empty values and false if no default can be determined.
|
||||
func (m *Manifest) DefaultDaemon() (string, DaemonSpec, bool) {
|
||||
if len(m.Daemons) == 0 {
|
||||
return "", DaemonSpec{}, false
|
||||
}
|
||||
|
||||
// Look for an explicit default.
|
||||
// Look for an explicit default; reject ambiguous multiple defaults.
|
||||
var defaultName string
|
||||
var defaultSpec DaemonSpec
|
||||
for name, spec := range m.Daemons {
|
||||
if spec.Default {
|
||||
return name, spec, true
|
||||
if defaultName != "" {
|
||||
// Multiple defaults — ambiguous.
|
||||
return "", DaemonSpec{}, false
|
||||
}
|
||||
defaultName = name
|
||||
defaultSpec = spec
|
||||
}
|
||||
}
|
||||
if defaultName != "" {
|
||||
return defaultName, defaultSpec, true
|
||||
}
|
||||
|
||||
// If exactly one daemon exists, treat it as the implicit default.
|
||||
if len(m.Daemons) == 1 {
|
||||
|
|
|
|||
|
|
@ -132,6 +132,28 @@ func TestManifest_DefaultDaemon_Bad_NoDaemons(t *testing.T) {
|
|||
assert.Empty(t, spec.Binary)
|
||||
}
|
||||
|
||||
func TestManifest_DefaultDaemon_Bad_MultipleDefaults(t *testing.T) {
|
||||
m := Manifest{
|
||||
Daemons: map[string]DaemonSpec{
|
||||
"api": {Binary: "./bin/api", Default: true},
|
||||
"worker": {Binary: "./bin/worker", Default: true},
|
||||
},
|
||||
}
|
||||
_, _, ok := m.DefaultDaemon()
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestManifest_DefaultDaemon_Bad_MultipleNoneDefault(t *testing.T) {
|
||||
m := Manifest{
|
||||
Daemons: map[string]DaemonSpec{
|
||||
"api": {Binary: "./bin/api"},
|
||||
"worker": {Binary: "./bin/worker"},
|
||||
},
|
||||
}
|
||||
_, _, ok := m.DefaultDaemon()
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestManifest_DefaultDaemon_Good_SingleImplicit(t *testing.T) {
|
||||
m := Manifest{
|
||||
Daemons: map[string]DaemonSpec{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue