|
1 | 1 | package docker |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | containertypes "github.com/moby/moby/api/types/container" |
7 | 8 | mounttypes "github.com/moby/moby/api/types/mount" |
8 | 9 | "github.com/stretchr/testify/require" |
9 | 10 | ) |
10 | 11 |
|
| 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 | + |
11 | 55 | func TestMountForDestination(t *testing.T) { |
12 | 56 | tests := []struct { |
13 | 57 | name string |
|
0 commit comments