Skip to content

Commit 0bbf8dc

Browse files
committed
fix: container mount inspection logic
1 parent af06c6a commit 0bbf8dc

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

backend/pkg/dockerutil/mount_utils.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func GetHostPathForContainerPath(ctx context.Context, dockerCli *client.Client,
2020
return "", nil // No docker client, can't discover
2121
}
2222

23-
// 1. Get current container ID (usually the short ID is the hostname)
24-
hostname, err := os.Hostname()
23+
// 1. Prefer robust current-container detection and fall back to hostname.
24+
inspectTarget, err := getCurrentContainerInspectTargetInternal(GetCurrentContainerID, os.Hostname)
2525
if err != nil {
2626
return "", err
2727
}
2828

2929
// 2. Inspect self
30-
inspect, err := libarcane.ContainerInspectWithCompatibility(ctx, dockerCli, hostname, client.ContainerInspectOptions{})
30+
inspect, err := libarcane.ContainerInspectWithCompatibility(ctx, dockerCli, inspectTarget, client.ContainerInspectOptions{})
3131
if err != nil {
3232
// Not running in a container or can't reach docker daemon
3333
return "", err
@@ -70,6 +70,27 @@ func GetHostPathForContainerPath(ctx context.Context, dockerCli *client.Client,
7070
return "", nil
7171
}
7272

73+
func getCurrentContainerInspectTargetInternal(currentContainerID func() (string, error), hostname func() (string, error)) (string, error) {
74+
if currentContainerID != nil {
75+
if containerID, err := currentContainerID(); err == nil {
76+
if containerID = strings.TrimSpace(containerID); containerID != "" {
77+
return containerID, nil
78+
}
79+
}
80+
}
81+
82+
if hostname == nil {
83+
hostname = os.Hostname
84+
}
85+
86+
value, err := hostname()
87+
if err != nil {
88+
return "", err
89+
}
90+
91+
return strings.TrimSpace(value), nil
92+
}
93+
7394
// MountForDestination returns a Mount suitable for container creation that mirrors an
7495
// existing container mount at the given destination.
7596
//

backend/pkg/dockerutil/mount_utils_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
package docker
22

33
import (
4+
"errors"
45
"testing"
56

67
containertypes "github.com/moby/moby/api/types/container"
78
mounttypes "github.com/moby/moby/api/types/mount"
89
"github.com/stretchr/testify/require"
910
)
1011

12+
func TestGetCurrentContainerInspectTargetInternal(t *testing.T) {
13+
t.Run("prefers detected container id over hostname", func(t *testing.T) {
14+
target, err := getCurrentContainerInspectTargetInternal(
15+
func() (string, error) { return "0123456789ab", nil },
16+
func() (string, error) { return "rpi4", nil },
17+
)
18+
19+
require.NoError(t, err)
20+
require.Equal(t, "0123456789ab", target)
21+
})
22+
23+
t.Run("falls back to hostname when container id unavailable", func(t *testing.T) {
24+
target, err := getCurrentContainerInspectTargetInternal(
25+
func() (string, error) { return "", errors.New("no container id found") },
26+
func() (string, error) { return "rpi4", nil },
27+
)
28+
29+
require.NoError(t, err)
30+
require.Equal(t, "rpi4", target)
31+
})
32+
33+
t.Run("trims whitespace from detected container id", func(t *testing.T) {
34+
target, err := getCurrentContainerInspectTargetInternal(
35+
func() (string, error) { return " 0123456789ab ", nil },
36+
func() (string, error) { return "rpi4", nil },
37+
)
38+
39+
require.NoError(t, err)
40+
require.Equal(t, "0123456789ab", target)
41+
})
42+
43+
t.Run("returns hostname error when fallback fails", func(t *testing.T) {
44+
target, err := getCurrentContainerInspectTargetInternal(
45+
func() (string, error) { return "", errors.New("no container id found") },
46+
func() (string, error) { return "", errors.New("hostname unavailable") },
47+
)
48+
49+
require.Error(t, err)
50+
require.Contains(t, err.Error(), "hostname unavailable")
51+
require.Equal(t, "", target)
52+
})
53+
}
54+
1155
func TestMountForDestination(t *testing.T) {
1256
tests := []struct {
1357
name string

0 commit comments

Comments
 (0)