Skip to content

Commit

Permalink
chore(depot): add List and GetFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengq committed Mar 12, 2014
1 parent bedca9d commit d5250ab
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
47 changes: 43 additions & 4 deletions depot/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"path/filepath"
)

const (
DefaultFileDepotDir = ".etcd-ca"
)

// Tag includes name and permission requirement
// Permission requirement is used in two ways:
// 1. Set the permission for data when Put
Expand All @@ -24,19 +28,18 @@ type Tag struct {
// Depot is in charge of data storage
type Depot interface {
Put(tag *Tag, data []byte) error
Check(tag *Tag) bool
Get(tag *Tag) ([]byte, error)
Delete(tag *Tag) error
Check(tag *Tag) bool
}


// FileDepot is a implementation of Depot using file system
type FileDepot struct {
// Absolute path of directory that holds all files
dirPath string
}

func New(dir string) (*FileDepot, error) {
func NewFileDepot(dir string) (*FileDepot, error) {
dirpath, err := filepath.Abs(dir)
if err != nil {
return nil, err
Expand Down Expand Up @@ -80,7 +83,7 @@ func (d *FileDepot) Put(tag *Tag, data []byte) error {

func (d *FileDepot) Check(tag *Tag) bool {
name := d.path(tag.name)
if fi, err := os.Stat(name); err == nil && ^fi.Mode() & tag.perm == 0 {
if fi, err := os.Stat(name); err == nil && ^fi.Mode()&tag.perm == 0 {
return true
}
return false
Expand All @@ -96,3 +99,39 @@ func (d *FileDepot) Get(tag *Tag) ([]byte, error) {
func (d *FileDepot) Delete(tag *Tag) error {
return os.Remove(d.path(tag.name))
}

func (d *FileDepot) List() []*Tag {
tags := make([]*Tag, 0)

filepath.Walk(d.dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
return nil
}
rel, err := filepath.Rel(d.dirPath, path)
if err != nil {
return nil
}
if rel != info.Name() {
return nil
}
tags = append(tags, &Tag{info.Name(), info.Mode()})
return nil
})

return tags
}

func (d *FileDepot) GetFile(tag *Tag) (os.FileInfo, []byte, error) {
if !d.Check(tag) {
return nil, nil, errors.New("permission denied")
}
fi, err := os.Stat(d.path(tag.name))
if err != nil {
return nil, nil, err
}
b, err := ioutil.ReadFile(d.path(tag.name))
return fi, b, err
}
56 changes: 51 additions & 5 deletions depot/depot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (

const (
data = "It is a trap only!"
dir = ".etcd-ca"
dir = ".etcd-ca-test"
)

var (
tag = &Tag{"host.pem", 0600}
wrongTag = &Tag{"host.pem", 0666}
tag = &Tag{"host.pem", 0600}
tag2 = &Tag{"host2.pem", 0600}
wrongTag = &Tag{"host.pem", 0666}
wrongTag2 = &Tag{"host.pem2", 0600}
)

func getDepot(t *testing.T) Depot {
func getDepot(t *testing.T) *FileDepot {
os.RemoveAll(dir)

d, err := New(dir)
d, err := NewFileDepot(dir)
if err != nil {
t.Fatal("Failed init Depot:", err)
}
Expand All @@ -30,6 +31,7 @@ func getDepot(t *testing.T) Depot {
// TestDepotCRUD tests to create, update and delete data
func TestDepotCRUD(t *testing.T) {
d := getDepot(t)
defer os.RemoveAll(dir)

if err := d.Put(tag, []byte(data)); err != nil {
t.Fatal("Failed putting file into Depot:", err)
Expand All @@ -56,6 +58,7 @@ func TestDepotCRUD(t *testing.T) {

func TestDepotPutNil(t *testing.T) {
d := getDepot(t)
defer os.RemoveAll(dir)

if err := d.Put(tag, nil); err == nil {
t.Fatal("Expect not to put nil into Depot:", err)
Expand All @@ -70,6 +73,7 @@ func TestDepotPutNil(t *testing.T) {

func TestDepotCheckFailure(t *testing.T) {
d := getDepot(t)
defer os.RemoveAll(dir)

if err := d.Put(tag, []byte(data)); err != nil {
t.Fatal("Failed putting file into Depot:", err)
Expand All @@ -88,6 +92,7 @@ func TestDepotCheckFailure(t *testing.T) {

func TestDepotGetFailure(t *testing.T) {
d := getDepot(t)
defer os.RemoveAll(dir)

if err := d.Put(tag, []byte(data)); err != nil {
t.Fatal("Failed putting file into Depot:", err)
Expand All @@ -103,3 +108,44 @@ func TestDepotGetFailure(t *testing.T) {

d.Delete(tag)
}

func TestDepotList(t *testing.T) {
d := getDepot(t)
defer os.RemoveAll(dir)

if err := d.Put(tag, []byte(data)); err != nil {
t.Fatal("Failed putting file into Depot:", err)
}
if err := d.Put(tag2, []byte(data)); err != nil {
t.Fatal("Failed putting file into Depot:", err)
}

tags := d.List()
if len(tags) != 2 {
t.Fatal("Expect to list 2 instead of", len(tags))
}
if tags[0].name != tag.name || tags[1].name != tag2.name {
t.Fatal("Failed getting file tags back")
}
}

func TestDepotGetFile(t *testing.T) {
d := getDepot(t)
defer os.RemoveAll(dir)

if err := d.Put(tag, []byte(data)); err != nil {
t.Fatal("Failed putting file into Depot:", err)
}

fi, dataRead, err := d.GetFile(tag)
if err != nil {
t.Fatal("Failed getting file from Depot:", err)
}
if bytes.Compare(dataRead, []byte(data)) != 0 {
t.Fatal("Failed getting the previous data")
}

if fi.Mode() != tag.perm {
t.Fatal("Failed setting permission")
}
}

1 comment on commit d5250ab

@philips
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Please sign in to comment.