Skip to content
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

Fix tests on windows and add workflow #143

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
97 changes: 30 additions & 67 deletions copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
"time"

"github.com/containerd/continuity/fs/fstest"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonistiigi/fsutil"
"golang.org/x/sys/unix"
)

// requiresRoot skips tests that require root
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestCopyDirectory(t *testing.T) {
fstest.CreateDir("/home", 0755),
)

exp := "add:/etc,add:/etc/hosts,add:/etc/hosts.allow,add:/home,add:/usr,add:/usr/local,add:/usr/local/lib,add:/usr/local/lib/libnothing.so,add:/usr/local/lib/libnothing.so.2"
exp := filepath.FromSlash("add:/etc,add:/etc/hosts,add:/etc/hosts.allow,add:/home,add:/usr,add:/usr/local,add:/usr/local/lib,add:/usr/local/lib/libnothing.so,add:/usr/local/lib/libnothing.so.2")

if err := testCopy(t, apply, exp); err != nil {
t.Fatalf("Copy test failed: %+v", err)
Expand All @@ -60,7 +59,7 @@ func TestCopyDirectoryWithLocalSymlink(t *testing.T) {
fstest.Symlink("nothing.txt", "link-no-nothing.txt"),
)

exp := "add:/link-no-nothing.txt,add:/nothing.txt"
exp := filepath.FromSlash("add:/link-no-nothing.txt,add:/nothing.txt")

if err := testCopy(t, apply, exp); err != nil {
t.Fatalf("Copy test failed: %+v", err)
Expand All @@ -84,49 +83,6 @@ func TestCopyToWorkDir(t *testing.T) {
require.NoError(t, err)
}

func TestCopyDevicesAndFifo(t *testing.T) {
requiresRoot(t)

t1 := t.TempDir()

err := mknod(filepath.Join(t1, "char"), unix.S_IFCHR|0444, int(unix.Mkdev(1, 9)))
require.NoError(t, err)

err = mknod(filepath.Join(t1, "block"), unix.S_IFBLK|0441, int(unix.Mkdev(3, 2)))
require.NoError(t, err)

err = mknod(filepath.Join(t1, "socket"), unix.S_IFSOCK|0555, 0)
require.NoError(t, err)

err = unix.Mkfifo(filepath.Join(t1, "fifo"), 0555)
require.NoError(t, err)

t2 := t.TempDir()

err = Copy(context.TODO(), t1, ".", t2, ".")
require.NoError(t, err)

fi, err := os.Lstat(filepath.Join(t2, "char"))
require.NoError(t, err)
assert.Equal(t, os.ModeCharDevice, fi.Mode()&os.ModeCharDevice)
assert.Equal(t, os.FileMode(0444), fi.Mode()&0777)

fi, err = os.Lstat(filepath.Join(t2, "block"))
require.NoError(t, err)
assert.Equal(t, os.ModeDevice, fi.Mode()&os.ModeDevice)
assert.Equal(t, os.FileMode(0441), fi.Mode()&0777)

fi, err = os.Lstat(filepath.Join(t2, "fifo"))
require.NoError(t, err)
assert.Equal(t, os.ModeNamedPipe, fi.Mode()&os.ModeNamedPipe)
assert.Equal(t, os.FileMode(0555), fi.Mode()&0777)

fi, err = os.Lstat(filepath.Join(t2, "socket"))
require.NoError(t, err)
assert.NotEqual(t, os.ModeSocket, fi.Mode()&os.ModeSocket) // socket copied as stub
assert.Equal(t, os.FileMode(0555), fi.Mode()&0777)
}

func TestCopySingleFile(t *testing.T) {
t1 := t.TempDir()

Expand Down Expand Up @@ -168,7 +124,7 @@ func TestCopySingleFile(t *testing.T) {
_, err = os.Stat(filepath.Join(t4, "a/b/c/foo2.txt"))
require.NoError(t, err)

require.Equal(t, "add:/a/b/c/foo2.txt", ch.String())
require.Equal(t, filepath.FromSlash("add:/a/b/c/foo2.txt"), ch.String())
}

func TestCopyOverrideFile(t *testing.T) {
Expand Down Expand Up @@ -221,7 +177,7 @@ func TestCopyDirectoryBasename(t *testing.T) {
err = fstest.CheckDirectoryEqual(t1, t2)
require.NoError(t, err)

require.Equal(t, "add:/foo,add:/foo/bar,add:/foo/bar/baz.txt", ch.String())
require.Equal(t, filepath.FromSlash("add:/foo,add:/foo/bar,add:/foo/bar/baz.txt"), ch.String())

ch = &changeCollector{}
err = Copy(context.TODO(), t1, "foo", t2, "foo", WithCopyInfo(CopyInfo{
Expand All @@ -230,7 +186,7 @@ func TestCopyDirectoryBasename(t *testing.T) {
}))
require.NoError(t, err)

require.Equal(t, "add:/foo/bar,add:/foo/bar/baz.txt", ch.String())
require.Equal(t, filepath.FromSlash("add:/foo/bar,add:/foo/bar/baz.txt"), ch.String())

err = fstest.CheckDirectoryEqual(t1, t2)
require.NoError(t, err)
Expand Down Expand Up @@ -311,9 +267,12 @@ func TestCopyExistingDirDest(t *testing.T) {
st, err := os.Lstat(filepath.Join(t2, "dir"))
require.NoError(t, err)
require.Equal(t, st.Mode()&os.ModePerm, os.FileMode(0700))
uid, gid := getUIDGID(st)
require.Equal(t, 1, uid)
require.Equal(t, 1, gid)
var uid, gid int
if runtime.GOOS != "windows" {
uid, gid = getUIDGID(st)
require.Equal(t, 1, uid)
require.Equal(t, 1, gid)
}

// verify that non-existing file was created
_, err = os.Lstat(filepath.Join(t2, "dir/foo.txt"))
Expand All @@ -323,9 +282,11 @@ func TestCopyExistingDirDest(t *testing.T) {
st, err = os.Lstat(filepath.Join(t2, "dir/bar.txt"))
require.NoError(t, err)
require.Equal(t, os.FileMode(0644), st.Mode()&os.ModePerm)
uid, gid = getUIDGID(st)
require.Equal(t, 0, uid)
require.Equal(t, 0, gid)
if runtime.GOOS != "windows" {
uid, gid = getUIDGID(st)
require.Equal(t, 0, uid)
require.Equal(t, 0, gid)
}
dt, err := os.ReadFile(filepath.Join(t2, "dir/bar.txt"))
require.NoError(t, err)
require.Equal(t, "bar-contents", string(dt))
Expand Down Expand Up @@ -376,7 +337,9 @@ func TestCopySymlinks(t *testing.T) {
// verify that existing destination dir's metadata was not overwritten
st, err := os.Lstat(filepath.Join(t2, "foo"))
require.NoError(t, err)
require.Equal(t, os.FileMode(0644), st.Mode()&os.ModePerm)
if runtime.GOOS != "windows" {
require.Equal(t, os.FileMode(0644), st.Mode()&os.ModePerm)
}
require.Equal(t, 0, int(st.Mode()&os.ModeSymlink))
dt, err := os.ReadFile(filepath.Join(t2, "foo"))
require.NoError(t, err)
Expand Down Expand Up @@ -439,37 +402,37 @@ func TestCopyIncludeExclude(t *testing.T) {
name: "include bar",
opts: []Opt{WithIncludePattern("bar")},
expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"},
expectedChanges: "add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo"),
},
{
name: "include *",
opts: []Opt{WithIncludePattern("*")},
expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3", "foo2"},
expectedChanges: "add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo,add:/foo2",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo,add:/foo2"),
},
{
name: "include bar/foo",
opts: []Opt{WithIncludePattern("bar/foo")},
expectedResults: []string{"bar", "bar/foo"},
expectedChanges: "add:/bar/foo",
expectedChanges: filepath.FromSlash("add:/bar/foo"),
},
{
name: "include bar except bar/foo",
opts: []Opt{WithIncludePattern("bar"), WithIncludePattern("!bar/foo")},
expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"},
expectedChanges: "add:/bar,add:/bar/baz,add:/bar/baz/foo3",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3"),
},
{
name: "include bar/foo and foo*",
opts: []Opt{WithIncludePattern("bar/foo"), WithIncludePattern("foo*")},
expectedResults: []string{"bar", "bar/foo", "foo2"},
expectedChanges: "add:/bar/foo,add:/foo2",
expectedChanges: filepath.FromSlash("add:/bar/foo,add:/foo2"),
},
{
name: "include b*",
opts: []Opt{WithIncludePattern("b*")},
expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"},
expectedChanges: "add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo"),
},
{
name: "include bar/f*",
Expand Down Expand Up @@ -530,25 +493,25 @@ func TestCopyIncludeExclude(t *testing.T) {
name: "doublestar matching second item in path",
opts: []Opt{WithIncludePattern("**/baz")},
expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"},
expectedChanges: "add:/bar/baz,add:/bar/baz/foo3",
expectedChanges: filepath.FromSlash("add:/bar/baz,add:/bar/baz/foo3"),
},
{
name: "doublestar matching first item in path",
opts: []Opt{WithIncludePattern("**/bar")},
expectedResults: []string{"bar", "bar/foo", "bar/baz", "bar/baz/foo3"},
expectedChanges: "add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/baz/foo3,add:/bar/foo"),
},
{
name: "doublestar exclude",
opts: []Opt{WithIncludePattern("bar"), WithExcludePattern("**/foo3")},
expectedResults: []string{"bar", "bar/foo", "bar/baz"},
expectedChanges: "add:/bar,add:/bar/baz,add:/bar/foo",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/baz,add:/bar/foo"),
},
{
name: "exclude bar/baz",
opts: []Opt{WithExcludePattern("bar/baz")},
expectedResults: []string{"bar", "bar/foo", "foo2"},
expectedChanges: "add:/bar,add:/bar/foo,add:/foo2",
expectedChanges: filepath.FromSlash("add:/bar,add:/bar/foo,add:/foo2"),
},
}

Expand Down
58 changes: 58 additions & 0 deletions copy/copy_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//go:build !windows
// +build !windows

package fs

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)

func TestCopyDevicesAndFifo(t *testing.T) {
requiresRoot(t)

t1 := t.TempDir()

err := mknod(filepath.Join(t1, "char"), unix.S_IFCHR|0444, int(unix.Mkdev(1, 9)))
require.NoError(t, err)

err = mknod(filepath.Join(t1, "block"), unix.S_IFBLK|0441, int(unix.Mkdev(3, 2)))
require.NoError(t, err)

err = mknod(filepath.Join(t1, "socket"), unix.S_IFSOCK|0555, 0)
require.NoError(t, err)

err = unix.Mkfifo(filepath.Join(t1, "fifo"), 0555)
require.NoError(t, err)

t2 := t.TempDir()

err = Copy(context.TODO(), t1, ".", t2, ".")
require.NoError(t, err)

fi, err := os.Lstat(filepath.Join(t2, "char"))
require.NoError(t, err)
assert.Equal(t, os.ModeCharDevice, fi.Mode()&os.ModeCharDevice)
assert.Equal(t, os.FileMode(0444), fi.Mode()&0777)

fi, err = os.Lstat(filepath.Join(t2, "block"))
require.NoError(t, err)
assert.Equal(t, os.ModeDevice, fi.Mode()&os.ModeDevice)
assert.Equal(t, os.FileMode(0441), fi.Mode()&0777)

fi, err = os.Lstat(filepath.Join(t2, "fifo"))
require.NoError(t, err)
assert.Equal(t, os.ModeNamedPipe, fi.Mode()&os.ModeNamedPipe)
assert.Equal(t, os.FileMode(0555), fi.Mode()&0777)

fi, err = os.Lstat(filepath.Join(t2, "socket"))
require.NoError(t, err)
assert.NotEqual(t, os.ModeSocket, fi.Mode()&os.ModeSocket) // socket copied as stub
assert.Equal(t, os.FileMode(0555), fi.Mode()&0777)
}
4 changes: 4 additions & 0 deletions copy/copy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
seTakeOwnershipPrivilege = "SeTakeOwnershipPrivilege"
)

func getUIDGID(fi os.FileInfo) (uid, gid int) {
return 0, 0
}

func (c *copier) copyFileInfo(fi os.FileInfo, src, name string) error {
if err := os.Chmod(name, fi.Mode()); err != nil {
return errors.Wrapf(err, "failed to chmod %s", name)
Expand Down
3 changes: 2 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"hash"
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/tonistiigi/fsutil/types"
Expand Down Expand Up @@ -32,7 +33,7 @@ func getWalkerFn(root string) walkerFn {
}

p := &currentPath{
path: path,
path: filepath.FromSlash(path),
gabriel-samfira marked this conversation as resolved.
Show resolved Hide resolved
stat: stat,
}

Expand Down
4 changes: 2 additions & 2 deletions diff_containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io"
"os"
"path/filepath"
"strings"

"github.com/tonistiigi/fsutil/types"
Expand Down Expand Up @@ -110,7 +111,7 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, fil
if filter != nil {
filter(f2.path, &statCopy)
}
f2copy = &currentPath{path: f2.path, stat: &statCopy}
f2copy = &currentPath{path: filepath.FromSlash(f2.path), stat: &statCopy}
}
k, p := pathChange(f1, f2copy)
switch k {
Expand Down Expand Up @@ -169,7 +170,6 @@ func pathChange(lower, upper *currentPath) (ChangeKind, string) {
if upper == nil {
return ChangeKindDelete, lower.path
}

switch i := ComparePath(lower.path, upper.path); {
case i < 0:
// File in lower that is not in upper
Expand Down
Loading