Skip to content

Commit

Permalink
adds an executor backed filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent adb6289 commit cce13ba
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
45 changes: 45 additions & 0 deletions filesystem.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"bytes"
"io/ioutil"
"os"
"strings"

"github.com/tsuru/tsuru/exec"

"github.com/tsuru/tsuru/fs"
)
Expand Down Expand Up @@ -36,3 +40,44 @@ func (f *localFS) CheckFile(name string) (bool, error) {
func (f *localFS) RemoveFile(name string) error {
return f.Fs.Remove(name)
}

// executorFS is a filesystem backed by an executor
type executorFS struct {
executor exec.Executor
}

func (f *executorFS) ReadFile(name string) ([]byte, error) {
out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
opts := exec.ExecuteOptions{
Cmd: "cat",
Args: []string{name},
Stdout: out,
Stderr: errOut,
}
if err := f.executor.Execute(opts); err != nil {
return nil, err
}
return out.Bytes(), nil
}

func (f *executorFS) CheckFile(name string) (bool, error) {
opts := exec.ExecuteOptions{
Cmd: "stat",
Args: []string{name},
}
if err := f.executor.Execute(opts); err != nil {
if strings.Contains(strings.ToLower(err.Error()), "no such") {
return false, nil
}
return false, err
}
return true, nil
}

func (f *executorFS) RemoveFile(name string) error {
return f.executor.Execute(exec.ExecuteOptions{
Cmd: "rm",
Args: []string{name},
})
}
50 changes: 50 additions & 0 deletions filesystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"io/ioutil"
"os"

"github.com/tsuru/tsuru/exec"

"gopkg.in/check.v1"
)

type FilesystemSuite struct{}

var _ = check.Suite(&FilesystemSuite{})

func (s *FilesystemSuite) TestExecutorFSReadFile(c *check.C) {
tmpFile, err := ioutil.TempFile("", "")
c.Assert(err, check.IsNil)
defer os.Remove(tmpFile.Name())
err = ioutil.WriteFile(tmpFile.Name(), []byte("this is the file content"), 0666)
c.Assert(err, check.IsNil)
fs := executorFS{executor: &exec.OsExecutor{}}
data, err := fs.ReadFile(tmpFile.Name())
c.Assert(err, check.IsNil)
c.Assert(data, check.DeepEquals, []byte("this is the file content"))
}

func (s *FilesystemSuite) TestExecutorFSCheckFile(c *check.C) {
tmpFile, err := ioutil.TempFile("", "")
c.Assert(err, check.IsNil)
defer os.Remove(tmpFile.Name())
fs := executorFS{executor: &exec.OsExecutor{}}
exists, err := fs.CheckFile(tmpFile.Name())
c.Assert(err, check.IsNil)
c.Assert(exists, check.Equals, true)
os.Remove(tmpFile.Name())
exists, err = fs.CheckFile(tmpFile.Name())
c.Assert(exists, check.Equals, false)
}

func (s *FilesystemSuite) TestExecutorFSRemoveFile(c *check.C) {
tmpFile, err := ioutil.TempFile("", "")
c.Assert(err, check.IsNil)
defer os.Remove(tmpFile.Name())
fs := executorFS{executor: &exec.OsExecutor{}}
err = fs.RemoveFile(tmpFile.Name())
c.Assert(err, check.IsNil)
_, err = os.Stat(tmpFile.Name())
c.Assert(os.IsNotExist(err), check.Equals, true)
}

0 comments on commit cce13ba

Please sign in to comment.