Skip to content

Commit

Permalink
add localfs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yaronha committed May 11, 2019
1 parent ff7b6a4 commit 3637f8c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 15 deletions.
3 changes: 2 additions & 1 deletion backends/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (c *LocalClient) ListDir(fileChan chan *FileDetails, task *ListDirTask, sum
fileDetails := &FileDetails{
Key: localPath, Size: fi.Size(), Mtime: fi.ModTime(), Mode: uint32(fi.Mode()), OriginalMtime: fi.ModTime(),
}
c.logger.DebugWith("List file", "key", localPath, "modified", fi.ModTime(), "size", fi.Size())
c.logger.DebugWith("List file", "key", localPath,
"modified", fi.ModTime(), "size", fi.Size(), "mode", fi.Mode())

summary.TotalBytes += fi.Size()
summary.TotalFiles += 1
Expand Down
14 changes: 0 additions & 14 deletions backends/local_test.go

This file was deleted.

101 changes: 101 additions & 0 deletions tests/localfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package tests

import (
"fmt"
"github.com/nuclio/logger"
"github.com/stretchr/testify/suite"
"github.com/v3io/xcp/backends"
"github.com/v3io/xcp/common"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)

var log logger.Logger
var tempdir string

func writeFiles() (string, error) {
content := []byte("dummy content")

tmpfn := filepath.Join(tempdir, "a.txt")
if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
return "", err
}

tmpfn = filepath.Join(tempdir, "a.csv")
if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
return "", err
}

return tempdir, nil
}

type testLocalBackend struct {
suite.Suite
}

func (suite *testLocalBackend) TestAWrite() {
content := []byte("dummy content")
src, err := common.UrlParse(tempdir)
suite.Require().Nil(err)

client, err := backends.NewLocalClient(log, src)
suite.Require().Nil(err)

w, err := client.Writer(filepath.Join(tempdir, "a.txt"), nil)
suite.Require().Nil(err)
n, err := w.Write(content)
suite.Require().Nil(err)
suite.Require().Equal(n, len(content))

opts := backends.WriteOptions{
Mtime: time.Now().Add(-23 * time.Hour),
Mode: 777}
w, err = client.Writer(filepath.Join(tempdir, "a.csv"), &opts)
suite.Require().Nil(err)
n, err = w.Write(content)
suite.Require().Nil(err)
suite.Require().Equal(n, len(content))
}

func (suite *testLocalBackend) TestList() {

src, err := common.UrlParse(tempdir)
suite.Require().Nil(err)

listTask := backends.ListDirTask{
Source: src,
Filter: "*.*",
}

client, err := backends.NewLocalClient(log, src)
suite.Require().Nil(err)

fileChan := make(chan *backends.FileDetails, 1000)
summary := &backends.ListSummary{}

err = client.ListDir(fileChan, &listTask, summary)
suite.Require().Nil(err)

fmt.Printf("Total files: %d, Total size: %d\n",
summary.TotalFiles, summary.TotalBytes)

suite.Require().Equal(summary.TotalFiles, 2)
}

func TestLocalBackendSuite(t *testing.T) {
log, _ = common.NewLogger("debug")
var err error
tempdir, err = ioutil.TempDir("", "xcptest")
//tempdir = "../tst1"
if err != nil {
t.Fatal(err)
}

fmt.Println("Target dir: ", tempdir)

os.RemoveAll(tempdir) // clean up
suite.Run(t, new(testLocalBackend))
}

0 comments on commit 3637f8c

Please sign in to comment.