Skip to content

Commit

Permalink
cmd, pkg/podman: Turn IsToolboxContainer() into Container.IsToolbx()
Browse files Browse the repository at this point in the history
This makes it possible to confine the details of detecting a Toolbx
container within the podman package, because it was not possible to use
podman.IsToolboxContainer() when listing all the Toolbx containers.

#1491
  • Loading branch information
debarshiray committed May 17, 2024
1 parent 4578161 commit 13627cc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
9 changes: 2 additions & 7 deletions src/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,8 @@ func getContainers() ([]podman.Container, error) {
var toolboxContainers []podman.Container

for containers.Next() {
container := containers.Get()
for label := range toolboxLabels {
labels := container.Labels()
if _, ok := labels[label]; ok {
toolboxContainers = append(toolboxContainers, container)
break
}
if container := containers.Get(); container.IsToolbx() {
toolboxContainers = append(toolboxContainers, container)
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ func rm(cmd *cobra.Command, args []string) error {
}

for _, container := range args {
if _, err := podman.IsToolboxContainer(container); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
containerObj, err := podman.InspectContainer(container)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: failed to inspect container %s\n", container)
continue
}

if !containerObj.IsToolbx() {
fmt.Fprintf(os.Stderr, "Error: %s is not a Toolbx container\n", container)
continue
}

Expand Down
25 changes: 25 additions & 0 deletions src/pkg/podman/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Container interface {
EntryPointPID() int
ID() string
Image() string
IsToolbx() bool
Labels() map[string]string
Mounts() []string
Name() string
Expand Down Expand Up @@ -85,6 +86,14 @@ func (container *containerInspect) Image() string {
return container.image
}

func (container *containerInspect) IsToolbx() bool {
if isToolbx(container.labels) {
return true
}

return false
}

func (container *containerInspect) Labels() map[string]string {
return container.labels
}
Expand Down Expand Up @@ -172,6 +181,14 @@ func (container *containerPS) Image() string {
return container.image
}

func (container *containerPS) IsToolbx() bool {
if isToolbx(container.labels) {
return true
}

return false
}

func (container *containerPS) Labels() map[string]string {
return container.labels
}
Expand Down Expand Up @@ -280,3 +297,11 @@ func (containers *Containers) Next() bool {
func (containers *Containers) Reset() {
containers.i = 0
}

func isToolbx(labels map[string]string) bool {
if labels["com.github.containers.toolbox"] == "true" || labels["com.github.debarshiray.toolbox"] == "true" {
return true
}

return false
}
23 changes: 23 additions & 0 deletions src/pkg/podman/containerInspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID int
id string
image string
isToolbx bool
labels map[string]string
mounts []string
name string
Expand Down Expand Up @@ -110,6 +111,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "b23f7c69ddec697f803b8acc40e85d212198c1baea9ffe193e7b3e0d2a020a39",
image: "localhost/fedora-toolbox-user:29",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -196,6 +198,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 5302,
id: "b23f7c69ddec697f803b8acc40e85d212198c1baea9ffe193e7b3e0d2a020a39",
image: "localhost/fedora-toolbox-user:29",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -282,6 +285,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 5302,
id: "b23f7c69ddec697f803b8acc40e85d212198c1baea9ffe193e7b3e0d2a020a39",
image: "localhost/fedora-toolbox-user:29",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -407,6 +411,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "4f8922191fc19f51fa120eda6b0bf0ca3c498469f30ee57a673e6c9ac2d0d4bb",
image: "registry.fedoraproject.org/f30/fedora-toolbox:30",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -533,6 +538,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "4f8922191fc19f51fa120eda6b0bf0ca3c498469f30ee57a673e6c9ac2d0d4bb",
image: "registry.fedoraproject.org/f30/fedora-toolbox:30",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -659,6 +665,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 11175,
id: "4f8922191fc19f51fa120eda6b0bf0ca3c498469f30ee57a673e6c9ac2d0d4bb",
image: "registry.fedoraproject.org/f30/fedora-toolbox:30",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -781,6 +788,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "7dfa257361547c0c67ed8678fe1c4de784b647c848deec2b0541cf040a1c64ee",
image: "registry.fedoraproject.org/fedora-toolbox:32",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -903,6 +911,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "7dfa257361547c0c67ed8678fe1c4de784b647c848deec2b0541cf040a1c64ee",
image: "registry.fedoraproject.org/fedora-toolbox:32",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -1025,6 +1034,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 4407,
id: "7dfa257361547c0c67ed8678fe1c4de784b647c848deec2b0541cf040a1c64ee",
image: "registry.fedoraproject.org/fedora-toolbox:32",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -1146,6 +1156,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "9effd41d07eea253926c08b7e61182d2cb6563abffc41c1ff7c1e57c42da1dab",
image: "registry.fedoraproject.org/fedora-toolbox:35",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -1267,6 +1278,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "9effd41d07eea253926c08b7e61182d2cb6563abffc41c1ff7c1e57c42da1dab",
image: "registry.fedoraproject.org/fedora-toolbox:35",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -1388,6 +1400,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 8253,
id: "9effd41d07eea253926c08b7e61182d2cb6563abffc41c1ff7c1e57c42da1dab",
image: "registry.fedoraproject.org/fedora-toolbox:35",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.github.debarshiray.toolbox": "true",
Expand Down Expand Up @@ -1507,6 +1520,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "a4599f0effa73cb8051d0b5650e28be7f7f9cd6655a584c48c14e7075201b7d7",
image: "registry.fedoraproject.org/fedora-toolbox:38",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.redhat.component": "fedora-toolbox",
Expand Down Expand Up @@ -1625,6 +1639,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "a4599f0effa73cb8051d0b5650e28be7f7f9cd6655a584c48c14e7075201b7d7",
image: "registry.fedoraproject.org/fedora-toolbox:38",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.redhat.component": "fedora-toolbox",
Expand Down Expand Up @@ -1743,6 +1758,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 11686,
id: "a4599f0effa73cb8051d0b5650e28be7f7f9cd6655a584c48c14e7075201b7d7",
image: "registry.fedoraproject.org/fedora-toolbox:38",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"com.redhat.component": "fedora-toolbox",
Expand Down Expand Up @@ -1855,6 +1871,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "6571a3f51998bccbee1608495c7bf28d42264b883c7cca9d03cfb6b5ef5f44f1",
image: "registry.fedoraproject.org/fedora-toolbox:40",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"name": "fedora-toolbox",
Expand Down Expand Up @@ -1966,6 +1983,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "6571a3f51998bccbee1608495c7bf28d42264b883c7cca9d03cfb6b5ef5f44f1",
image: "registry.fedoraproject.org/fedora-toolbox:40",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"name": "fedora-toolbox",
Expand Down Expand Up @@ -2077,6 +2095,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 3792,
id: "6571a3f51998bccbee1608495c7bf28d42264b883c7cca9d03cfb6b5ef5f44f1",
image: "registry.fedoraproject.org/fedora-toolbox:40",
isToolbx: true,
labels: map[string]string{
"com.github.containers.toolbox": "true",
"name": "fedora-toolbox",
Expand Down Expand Up @@ -2125,6 +2144,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "f62203a35f867cbdeb0d340741455cea23bd5fccff19c33ef453aaa163152142",
image: "registry.fedoraproject.org/fedora:40",
isToolbx: false,
labels: map[string]string{
"name": "fedora",
"version": "40",
Expand Down Expand Up @@ -2166,6 +2186,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 0,
id: "f62203a35f867cbdeb0d340741455cea23bd5fccff19c33ef453aaa163152142",
image: "registry.fedoraproject.org/fedora:40",
isToolbx: false,
labels: map[string]string{
"name": "fedora",
"version": "40",
Expand Down Expand Up @@ -2207,6 +2228,7 @@ func TestContainerInspect(t *testing.T) {
entryPointPID: 4462,
id: "f62203a35f867cbdeb0d340741455cea23bd5fccff19c33ef453aaa163152142",
image: "registry.fedoraproject.org/fedora:40",
isToolbx: false,
labels: map[string]string{
"name": "fedora",
"version": "40",
Expand All @@ -2230,6 +2252,7 @@ func TestContainerInspect(t *testing.T) {
assert.Equal(t, tc.expect.entryPointPID, container.EntryPointPID())
assert.Equal(t, tc.expect.id, container.ID())
assert.Equal(t, tc.expect.image, container.Image())
assert.Equal(t, tc.expect.isToolbx, container.IsToolbx())
assert.Equal(t, tc.expect.labels, container.Labels())
assert.Equal(t, tc.expect.mounts, container.Mounts())
assert.Equal(t, tc.expect.name, container.Name())
Expand Down
14 changes: 0 additions & 14 deletions src/pkg/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,6 @@ func InspectImage(image string) (map[string]interface{}, error) {
return info[0], nil
}

func IsToolboxContainer(container string) (bool, error) {
containerObj, err := InspectContainer(container)
if err != nil {
return false, fmt.Errorf("failed to inspect container %s", container)
}

labels := containerObj.Labels()
if labels["com.github.containers.toolbox"] != "true" && labels["com.github.debarshiray.toolbox"] != "true" {
return false, fmt.Errorf("%s is not a Toolbx container", container)
}

return true, nil
}

func IsToolboxImage(image string) (bool, error) {
info, err := InspectImage(image)
if err != nil {
Expand Down

0 comments on commit 13627cc

Please sign in to comment.