Skip to content
This repository has been archived by the owner on Feb 12, 2019. It is now read-only.

Commit

Permalink
simplefs stub (#735)
Browse files Browse the repository at this point in the history
* simplefs
* initial simplefs tests
  • Loading branch information
Steve Sanders committed Feb 13, 2017
1 parent 9c5fdf3 commit 31fd34c
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 124 deletions.
2 changes: 2 additions & 0 deletions libkbfs/keybase_daemon_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"github.com/keybase/kbfs/simplefs"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -240,6 +241,7 @@ func (k *KeybaseDaemonRPC) OnConnect(ctx context.Context,
keybase1.NotifyPaperKeyProtocol(k),
keybase1.NotifyFSRequestProtocol(k),
keybase1.TlfKeysProtocol(k),
keybase1.SimpleFSProtocol(&simplefs.SimpleFS{}),
}

if k.protocols != nil {
Expand Down
119 changes: 119 additions & 0 deletions simplefs/simplefs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.

package simplefs

import (
"errors"

"golang.org/x/net/context"

"github.com/keybase/client/go/protocol/keybase1"
)

// SimpleFS - implement keybase1.SimpleFS
type SimpleFS struct{}

// make sure the interface is implemented
var _ keybase1.SimpleFSInterface = (*SimpleFS)(nil)

// SimpleFSList - Begin list of items in directory at path
// Retrieve results with readList()
// Can be a single file to get flags/status
func (k *SimpleFS) SimpleFSList(_ context.Context, arg keybase1.SimpleFSListArg) error {
return errors.New("not implemented")
}

// SimpleFSListRecursive - Begin recursive list of items in directory at path
func (k *SimpleFS) SimpleFSListRecursive(_ context.Context, arg keybase1.SimpleFSListRecursiveArg) error {
return errors.New("not implemented")
}

// SimpleFSReadList - Get list of Paths in progress. Can indicate status of pending
// to get more entries.
func (k *SimpleFS) SimpleFSReadList(context.Context, keybase1.OpID) (keybase1.SimpleFSListResult, error) {
return keybase1.SimpleFSListResult{}, errors.New("not implemented")
}

// SimpleFSCopy - Begin copy of file or directory
func (k *SimpleFS) SimpleFSCopy(_ context.Context, arg keybase1.SimpleFSCopyArg) error {
return errors.New("not implemented")
}

// SimpleFSCopyRecursive - Begin recursive copy of directory
func (k *SimpleFS) SimpleFSCopyRecursive(_ context.Context, arg keybase1.SimpleFSCopyRecursiveArg) error {
return errors.New("not implemented")
}

// SimpleFSMove - Begin move of file or directory, from/to KBFS only
func (k *SimpleFS) SimpleFSMove(_ context.Context, arg keybase1.SimpleFSMoveArg) error {
return errors.New("not implemented")
}

// SimpleFSRename - Rename file or directory, KBFS side only
func (k *SimpleFS) SimpleFSRename(_ context.Context, arg keybase1.SimpleFSRenameArg) error {
return errors.New("not implemented")
}

// SimpleFSOpen - Create/open a file and leave it open
// or create a directory
// Files must be closed afterwards.
func (k *SimpleFS) SimpleFSOpen(_ context.Context, arg keybase1.SimpleFSOpenArg) error {
return errors.New("not implemented")
}

// SimpleFSSetStat - Set/clear file bits - only executable for now
func (k *SimpleFS) SimpleFSSetStat(_ context.Context, arg keybase1.SimpleFSSetStatArg) error {
return errors.New("not implemented")
}

// SimpleFSRead - Read (possibly partial) contents of open file,
// up to the amount specified by size.
// Repeat until zero bytes are returned or error.
// If size is zero, read an arbitrary amount.
func (k *SimpleFS) SimpleFSRead(_ context.Context, arg keybase1.SimpleFSReadArg) (keybase1.FileContent, error) {
return keybase1.FileContent{}, errors.New("not implemented")
}

// SimpleFSWrite - Append content to opened file.
// May be repeated until OpID is closed.
func (k *SimpleFS) SimpleFSWrite(_ context.Context, arg keybase1.SimpleFSWriteArg) error {
return errors.New("not implemented")
}

// SimpleFSRemove - Remove file or directory from filesystem
func (k *SimpleFS) SimpleFSRemove(_ context.Context, arg keybase1.SimpleFSRemoveArg) error {
return errors.New("not implemented")
}

// SimpleFSStat - Get info about file
func (k *SimpleFS) SimpleFSStat(_ context.Context, path keybase1.Path) (keybase1.Dirent, error) {
return keybase1.Dirent{}, errors.New("not implemented")
}

// SimpleFSMakeOpid - Convenience helper for generating new random value
func (k *SimpleFS) SimpleFSMakeOpid(_ context.Context) (keybase1.OpID, error) {
return keybase1.OpID{}, errors.New("not implemented")
}

// SimpleFSClose - Close OpID, cancels any pending operation.
// Must be called after list/copy/remove
func (k *SimpleFS) SimpleFSClose(_ context.Context, opid keybase1.OpID) error {
return errors.New("not implemented")
}

// SimpleFSCheck - Check progress of pending operation
func (k *SimpleFS) SimpleFSCheck(_ context.Context, opid keybase1.OpID) (keybase1.Progress, error) {
return 0, errors.New("not implemented")
}

// SimpleFSGetOps - Get all the outstanding operations
func (k *SimpleFS) SimpleFSGetOps(_ context.Context) ([]keybase1.OpDescription, error) {
return []keybase1.OpDescription{}, errors.New("not implemented")
}

// SimpleFSWait - Blocking wait for the pending operation to finish
func (k *SimpleFS) SimpleFSWait(_ context.Context, opid keybase1.OpID) error {
return errors.New("not implemented")
}
146 changes: 146 additions & 0 deletions simplefs/simplefs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.

package simplefs

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

"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func newTempRemotePath() (keybase1.Path, error) {
// TODO: make a KBFS type path instead of a local one
tempdir, err := ioutil.TempDir("", "simpleFstest")
return keybase1.NewPathWithKbfs(tempdir), err
}

// TODO: This is for deleting a KBFS type path, but for now it just expects a local one
func deleteTempRemotePath(path keybase1.Path) {
os.RemoveAll(path.Kbfs())
}

func TestList(t *testing.T) {
ctx := context.Background()
sfs := &SimpleFS{}

// make a temp remote directory + files we will clean up later
path1, err := newTempRemotePath()
defer deleteTempRemotePath(path1)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(path1.Kbfs(), "test1.txt"), []byte("foo"), 0644)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(path1.Kbfs(), "test2.txt"), []byte("foo"), 0644)
require.NoError(t, err)

opid, err := sfs.SimpleFSMakeOpid(ctx)
require.NoError(t, err)

err = sfs.SimpleFSList(ctx, keybase1.SimpleFSListArg{
OpID: opid,
Path: path1,
})
require.NoError(t, err)

listResult, err := sfs.SimpleFSReadList(ctx, opid)
require.NoError(t, err)

assert.Len(t, listResult.Entries, 2, "Expected 2 directory entries in listing")

// Assume we've exhausted the list now, so expect error
_, err = sfs.SimpleFSReadList(ctx, opid)
require.Error(t, err)

err = sfs.SimpleFSClose(ctx, opid)
require.NoError(t, err)

// Verify error on double close
err = sfs.SimpleFSClose(ctx, opid)
require.Error(t, err)
}

func TestCopyToLocal(t *testing.T) {
ctx := context.Background()
sfs := &SimpleFS{}

// make a temp remote directory + file(s) we will clean up later
path1, err := newTempRemotePath()
defer deleteTempRemotePath(path1)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(path1.Kbfs(), "test1.txt"), []byte("foo"), 0644)
require.NoError(t, err)

// make a temp local dest directory + files we will clean up later
tempdir2, err := ioutil.TempDir("", "simpleFstest")
defer os.RemoveAll(tempdir2)
require.NoError(t, err)
path2 := keybase1.NewPathWithLocal(tempdir2)

opid, err := sfs.SimpleFSMakeOpid(ctx)
require.NoError(t, err)

err = sfs.SimpleFSCopy(ctx, keybase1.SimpleFSCopyArg{
OpID: opid,
Src: keybase1.NewPathWithKbfs(filepath.Join(path1.Kbfs(), "test1.txt")),
Dest: path2, // TODO: must the dest include a name?
})
require.NoError(t, err)

err = sfs.SimpleFSClose(ctx, opid)
require.NoError(t, err)

// Verify error on double close
err = sfs.SimpleFSClose(ctx, opid)
require.Error(t, err)

exists, err := libkb.FileExists(filepath.Join(tempdir2, "test1.txt"))
require.NoError(t, err)
assert.True(t, exists, "File copy destination must exist")
}

func TestCopyToRemote(t *testing.T) {
ctx := context.Background()
sfs := &SimpleFS{}

// make a temp remote directory + file(s) we will clean up later
path2, err := newTempRemotePath()
require.NoError(t, err)

// make a temp local dest directory + files we will clean up later
tempdir, err := ioutil.TempDir("", "simpleFstest")
defer os.RemoveAll(tempdir)
require.NoError(t, err)
path1 := keybase1.NewPathWithLocal(tempdir)
defer deleteTempRemotePath(path1)
err = ioutil.WriteFile(filepath.Join(path1.Local(), "test1.txt"), []byte("foo"), 0644)
require.NoError(t, err)

opid, err := sfs.SimpleFSMakeOpid(ctx)
require.NoError(t, err)

err = sfs.SimpleFSCopy(ctx, keybase1.SimpleFSCopyArg{
OpID: opid,
Src: keybase1.NewPathWithLocal(filepath.Join(path1.Kbfs(), "test1.txt")),
Dest: path2, // TODO: must the dest include a name?
})
require.NoError(t, err)

err = sfs.SimpleFSClose(ctx, opid)
require.NoError(t, err)

// Verify error on double close
err = sfs.SimpleFSClose(ctx, opid)
require.Error(t, err)

exists, err := libkb.FileExists(filepath.Join(path1.Local(), "test1.txt"))
require.NoError(t, err)
assert.True(t, exists, "File copy destination must exist")
}
Loading

0 comments on commit 31fd34c

Please sign in to comment.