Skip to content

Commit f09b86d

Browse files
authored
Merge pull request #303 from kzys/var-run
Unit tests shouldn't assume that a certain directory is present
2 parents 13aeab3 + cc67b05 commit f09b86d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

internal/vm/dir.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ import (
2828
"golang.org/x/sys/unix"
2929
)
3030

31-
const (
32-
varRunDir = "/var/run/firecracker-containerd/"
33-
)
34-
3531
// ShimDir holds files, sockets and FIFOs scoped to a single shim managing the
3632
// VM with the given VMID. It is unique per-VM and containerd namespace.
3733
func ShimDir(namespace, vmID string) (Dir, error) {
34+
return shimDir("/var/run/firecracker-containerd/", namespace, vmID)
35+
}
36+
37+
func shimDir(varRunDir, namespace, vmID string) (Dir, error) {
3838
if err := identifiers.Validate(namespace); err != nil {
3939
return "", errors.Wrap(err, "invalid namespace")
4040
}

internal/vm/dir_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@
1414
package vm
1515

1616
import (
17+
"io/ioutil"
18+
"os"
19+
"path"
1720
"strings"
1821
"testing"
1922

2023
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2125
)
2226

2327
var invalidContainerIDs = []string{"", "id?", "*", "id/1", "id\\"}
2428

2529
func TestShimDir(t *testing.T) {
30+
runDir, err := ioutil.TempDir("", "run")
31+
require.NoError(t, err)
32+
defer os.RemoveAll(runDir)
33+
2634
tests := []struct {
2735
name string
2836
ns string
@@ -40,22 +48,22 @@ func TestShimDir(t *testing.T) {
4048
{name: "id with ?", ns: "test", id: "?", outErr: `invalid vm id: identifier "?" must match`},
4149
{name: "id with *", ns: "test", id: "*", outErr: `invalid vm id: identifier "*" must match`},
4250
{name: "id with ,", ns: "test", id: ",", outErr: `invalid vm id: identifier "," must match`},
43-
{name: "valid", ns: "ns", id: "1", outDir: "/run/firecracker-containerd/ns/1"},
44-
{name: "valid with dashes", ns: "test-123", id: "123-456", outDir: "/run/firecracker-containerd/test-123/123-456"},
45-
{name: "valid with dots", ns: "test.123", id: "123.456", outDir: "/run/firecracker-containerd/test.123/123.456"},
51+
{name: "valid", ns: "ns", id: "1", outDir: "ns/1"},
52+
{name: "valid with dashes", ns: "test-123", id: "123-456", outDir: "test-123/123-456"},
53+
{name: "valid with dots", ns: "test.123", id: "123.456", outDir: "test.123/123.456"},
4654
}
4755

4856
for _, tc := range tests {
4957
test := tc
5058
t.Run(test.name, func(t *testing.T) {
51-
dir, err := ShimDir(test.ns, test.id)
59+
dir, err := shimDir(runDir, test.ns, test.id)
5260

5361
if test.outErr != "" {
5462
assert.Error(t, err)
5563
assert.True(t, strings.Contains(err.Error(), test.outErr), err.Error())
5664
} else {
5765
assert.NoError(t, err)
58-
assert.EqualValues(t, dir, test.outDir)
66+
assert.EqualValues(t, dir, path.Join(runDir, test.outDir))
5967
}
6068
})
6169
}

0 commit comments

Comments
 (0)