Skip to content

Commit cfc6096

Browse files
fix(list): detect containers with overridden manager label (#2116)
When `--additional-flags --label=manager=...` overrides the manager label at creation time, `distrobox list` would hide the container and `distrobox rm` would silently no-op (it filters through the same list). Match the distrobox.* label set alongside `manager` so containers remain detectable when the manager label is overridden, restoring v1's loose `*distrobox*` substring behavior.
1 parent 695613a commit cfc6096

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

pkg/containermanager/containermanager.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,18 @@ type RmOptions struct {
7272
ContainerHome string
7373
}
7474

75+
// IsDistrobox returns true if any label key or value contains "distrobox".
76+
// We can't just check manager=distrobox because users can override it with
77+
// --additional-flags --label=manager=foo (apx does this). The
78+
// distrobox.unshare_groups label is always set on creation, so the
79+
// substring match catches those containers too.
7580
func (c Container) IsDistrobox() bool {
76-
return c.Labels["manager"] == "distrobox"
81+
for key, value := range c.Labels {
82+
if strings.Contains(key, "distrobox") || strings.Contains(value, "distrobox") {
83+
return true
84+
}
85+
}
86+
return false
7787
}
7888

7989
func (c Container) IsRunning() bool {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package containermanager_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/89luca89/distrobox/pkg/containermanager"
9+
)
10+
11+
func TestContainer_IsDistrobox_StandardManagerLabel(t *testing.T) {
12+
c := containermanager.Container{
13+
Labels: map[string]string{"manager": "distrobox", "distrobox.unshare_groups": "0"},
14+
}
15+
assert.True(t, c.IsDistrobox())
16+
}
17+
18+
// Regression: when the user overrides the manager label via
19+
// `--additional-flags --label=manager=apx`, the container is still a
20+
// distrobox container — the `distrobox.unshare_groups` label is always set on
21+
// creation and is enough to identify it.
22+
func TestContainer_IsDistrobox_ManagerLabelOverridden(t *testing.T) {
23+
c := containermanager.Container{
24+
Labels: map[string]string{"manager": "apx", "distrobox.unshare_groups": "0"},
25+
}
26+
assert.True(t, c.IsDistrobox())
27+
}
28+
29+
func TestContainer_IsDistrobox_NoDistroboxLabels(t *testing.T) {
30+
c := containermanager.Container{
31+
Labels: map[string]string{"manager": "toolbox"},
32+
}
33+
assert.False(t, c.IsDistrobox())
34+
}
35+
36+
func TestContainer_IsDistrobox_NilLabels(t *testing.T) {
37+
c := containermanager.Container{Labels: nil}
38+
assert.False(t, c.IsDistrobox())
39+
}

0 commit comments

Comments
 (0)