Skip to content

Commit

Permalink
test: improve the symlink unit tests for file store (#414)
Browse files Browse the repository at this point in the history
Related to #402

Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
  • Loading branch information
Wwwsylvia authored Jan 17, 2023
1 parent 05595eb commit 39ce054
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 78 deletions.
119 changes: 42 additions & 77 deletions content/file/file_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package file
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"testing"
Expand All @@ -46,8 +45,9 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
t.Fatal("error calling WriteFile(), error =", err)
}
// create symlink to a relative path
symlink := filepath.Join(dirPath, "test_symlink")
if err := os.Symlink(fileName, symlink); err != nil {
symlinkName := "test_symlink"
symlinkPath := filepath.Join(dirPath, symlinkName)
if err := os.Symlink(fileName, symlinkPath); err != nil {
t.Fatal("error calling Symlink(), error =", err)
}

Expand All @@ -63,23 +63,6 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
if err != nil {
t.Fatal("Store.Add() error =", err)
}
val, ok := src.digestToPath.Load(desc.Digest)
if !ok {
t.Fatal("failed to find internal gz")
}
tmpPath := val.(string)
zrc, err := os.Open(tmpPath)
if err != nil {
t.Fatal("failed to open internal gz, error =", err)
}
gotgz, err := io.ReadAll(zrc)
if err != nil {
t.Fatal("failed to read internal gz, error =", err)
}
if err := zrc.Close(); err != nil {
t.Error("failed to close internal gz, error =", err)
}

// pack a manifest
manifestDesc, err := oras.Pack(ctx, src, "dir", []ocispec.Descriptor{desc}, oras.PackOptions{})
if err != nil {
Expand All @@ -97,21 +80,21 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
t.Fatal("oras.CopyGraph() error =", err)
}

// compare content
rc, err := dstAbs.Fetch(ctx, desc)
// verify extracted symlink
extractedSymlink := filepath.Join(tempDir, dirName, symlinkName)
symlinkDst, err := os.Readlink(extractedSymlink)
if err != nil {
t.Fatal("Store.Fetch() error =", err)
t.Fatal("failed to get symlink destination, error =", err)
}
got, err := io.ReadAll(rc)
if err != nil {
t.Fatal("Store.Fetch().Read() error =", err)
if want := fileName; symlinkDst != want {
t.Errorf("symlink destination = %v, want %v", symlinkDst, want)
}
err = rc.Close()
got, err := os.ReadFile(extractedSymlink)
if err != nil {
t.Error("Store.Fetch().Close() error =", err)
t.Fatal("failed to read symlink file, error =", err)
}
if !bytes.Equal(got, gotgz) {
t.Errorf("Store.Fetch() = %v, want %v", got, gotgz)
if !bytes.Equal(got, content) {
t.Errorf("symlink content = %v, want %v", got, content)
}

// copy to another file store created from a relative root, to trigger extracting directory
Expand All @@ -128,21 +111,21 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
t.Fatal("oras.CopyGraph() error =", err)
}

// compare content
rc, err = dstRel.Fetch(ctx, desc)
// verify extracted symlink
extractedSymlink = filepath.Join(tempDir, dirName, symlinkName)
symlinkDst, err = os.Readlink(extractedSymlink)
if err != nil {
t.Fatal("Store.Fetch() error =", err)
t.Fatal("failed to get symlink destination, error =", err)
}
got, err = io.ReadAll(rc)
if err != nil {
t.Fatal("Store.Fetch().Read() error =", err)
if want := fileName; symlinkDst != want {
t.Errorf("symlink destination = %v, want %v", symlinkDst, want)
}
err = rc.Close()
got, err = os.ReadFile(extractedSymlink)
if err != nil {
t.Error("Store.Fetch().Close() error =", err)
t.Fatal("failed to read symlink file, error =", err)
}
if !bytes.Equal(got, gotgz) {
t.Errorf("Store.Fetch() = %v, want %v", got, gotgz)
if !bytes.Equal(got, content) {
t.Errorf("symlink content = %v, want %v", got, content)
}
}

Expand Down Expand Up @@ -180,23 +163,6 @@ func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) {
if err != nil {
t.Fatal("Store.Add() error =", err)
}
val, ok := src.digestToPath.Load(desc.Digest)
if !ok {
t.Fatal("failed to find internal gz")
}
tmpPath := val.(string)
zrc, err := os.Open(tmpPath)
if err != nil {
t.Fatal("failed to open internal gz, error =", err)
}
gotgz, err := io.ReadAll(zrc)
if err != nil {
t.Fatal("failed to read internal gz, error =", err)
}
if err := zrc.Close(); err != nil {
t.Error("failed to close internal gz, error =", err)
}

// pack a manifest
manifestDesc, err := oras.Pack(ctx, src, "dir", []ocispec.Descriptor{desc}, oras.PackOptions{})
if err != nil {
Expand All @@ -216,21 +182,20 @@ func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) {
t.Fatal("oras.CopyGraph() error =", err)
}

// compare content
rc, err := dstAbs.Fetch(ctx, desc)
// verify extracted symlink
symlinkDst, err := os.Readlink(symlink)
if err != nil {
t.Fatal("Store.Fetch() error =", err)
t.Fatal("failed to get symlink destination, error =", err)
}
got, err := io.ReadAll(rc)
if err != nil {
t.Fatal("Store.Fetch().Read() error =", err)
if want := filePath; symlinkDst != want {
t.Errorf("symlink destination = %v, want %v", symlinkDst, want)
}
err = rc.Close()
got, err := os.ReadFile(symlink)
if err != nil {
t.Error("Store.Fetch().Close() error =", err)
t.Fatal("failed to read symlink file, error =", err)
}
if !bytes.Equal(got, gotgz) {
t.Errorf("Store.Fetch() = %v, want %v", got, gotgz)
if !bytes.Equal(got, content) {
t.Errorf("symlink content = %v, want %v", got, content)
}

// remove the original testing directory and create a new store using a relative path
Expand All @@ -248,21 +213,21 @@ func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) {
if err := oras.CopyGraph(ctx, src, dstRel, manifestDesc, oras.DefaultCopyGraphOptions); err != nil {
t.Fatal("oras.CopyGraph() error =", err)
}
// compare content
rc, err = dstRel.Fetch(ctx, desc)

// verify extracted symlink
symlinkDst, err = os.Readlink(symlink)
if err != nil {
t.Fatal("Store.Fetch() error =", err)
t.Fatal("failed to get symlink destination, error =", err)
}
got, err = io.ReadAll(rc)
if err != nil {
t.Fatal("Store.Fetch().Read() error =", err)
if want := filePath; symlinkDst != want {
t.Errorf("symlink destination = %v, want %v", symlinkDst, want)
}
err = rc.Close()
got, err = os.ReadFile(symlink)
if err != nil {
t.Error("Store.Fetch().Close() error =", err)
t.Fatal("failed to read symlink file, error =", err)
}
if !bytes.Equal(got, gotgz) {
t.Errorf("Store.Fetch() = %v, want %v", got, gotgz)
if !bytes.Equal(got, content) {
t.Errorf("symlink content = %v, want %v", got, content)
}

// copy to another file store created from an outside root, to trigger extracting directory
Expand Down
2 changes: 1 addition & 1 deletion content/oci/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Storage struct {
func NewStorage(root string) (*Storage, error) {
rootAbs, err := filepath.Abs(root)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to resolve absolute path for %s: %w", root, err)
}

return &Storage{
Expand Down

0 comments on commit 39ce054

Please sign in to comment.