Skip to content

Unit tests shouldn't assume that a certain directory is present #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/vm/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
"golang.org/x/sys/unix"
)

const (
varRunDir = "/var/run/firecracker-containerd/"
)

// ShimDir holds files, sockets and FIFOs scoped to a single shim managing the
// VM with the given VMID. It is unique per-VM and containerd namespace.
func ShimDir(namespace, vmID string) (Dir, error) {
return shimDir("/var/run/firecracker-containerd/", namespace, vmID)
}

func shimDir(varRunDir, namespace, vmID string) (Dir, error) {
if err := identifiers.Validate(namespace); err != nil {
return "", errors.Wrap(err, "invalid namespace")
}
Expand Down
18 changes: 13 additions & 5 deletions internal/vm/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@
package vm

import (
"io/ioutil"
"os"
"path"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

func TestShimDir(t *testing.T) {
runDir, err := ioutil.TempDir("", "run")
require.NoError(t, err)
defer os.RemoveAll(runDir)

tests := []struct {
name string
ns string
Expand All @@ -40,22 +48,22 @@ func TestShimDir(t *testing.T) {
{name: "id with ?", ns: "test", id: "?", outErr: `invalid vm id: identifier "?" must match`},
{name: "id with *", ns: "test", id: "*", outErr: `invalid vm id: identifier "*" must match`},
{name: "id with ,", ns: "test", id: ",", outErr: `invalid vm id: identifier "," must match`},
{name: "valid", ns: "ns", id: "1", outDir: "/run/firecracker-containerd/ns/1"},
{name: "valid with dashes", ns: "test-123", id: "123-456", outDir: "/run/firecracker-containerd/test-123/123-456"},
{name: "valid with dots", ns: "test.123", id: "123.456", outDir: "/run/firecracker-containerd/test.123/123.456"},
{name: "valid", ns: "ns", id: "1", outDir: "ns/1"},
{name: "valid with dashes", ns: "test-123", id: "123-456", outDir: "test-123/123-456"},
{name: "valid with dots", ns: "test.123", id: "123.456", outDir: "test.123/123.456"},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(t *testing.T) {
dir, err := ShimDir(test.ns, test.id)
dir, err := shimDir(runDir, test.ns, test.id)

if test.outErr != "" {
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), test.outErr), err.Error())
} else {
assert.NoError(t, err)
assert.EqualValues(t, dir, test.outDir)
assert.EqualValues(t, dir, path.Join(runDir, test.outDir))
}
})
}
Expand Down