Skip to content

Commit

Permalink
removes global filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent b394dbb commit b34ab9e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 53 deletions.
20 changes: 12 additions & 8 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@

package main

import "log"
import (
"log"

func build(c Client, appName string, cmd []string) {
"github.com/tsuru/tsuru/fs"
)

func build(c Client, appName string, cmd []string, filesystem fs.Fs) {
log.SetFlags(0)
envs, err := c.getAppEnvs(appName)
if err != nil {
log.Fatal(err)
}
err = execScript(cmd, envs, nil)
err = execScript(cmd, envs, nil, filesystem)
if err != nil {
log.Fatal(err)
}
}

func deploy(c Client, appName string) {
func deploy(c Client, appName string, filesystem fs.Fs) {
log.SetFlags(0)
var yamlData TsuruYaml
envs, err := c.registerUnit(appName, yamlData)
if err != nil {
log.Fatal(err)
}
diff, firstDeploy, err := readDiffDeploy()
diff, firstDeploy, err := readDiffDeploy(filesystem)
if err != nil {
log.Fatal(err)
}
Expand All @@ -35,15 +39,15 @@ func deploy(c Client, appName string) {
log.Fatal(err)
}
}
yamlData, err = loadTsuruYaml()
yamlData, err = loadTsuruYaml(filesystem)
if err != nil {
log.Fatal(err)
}
err = buildHooks(yamlData, envs)
err = buildHooks(yamlData, envs, filesystem)
if err != nil {
log.Fatal(err)
}
err = loadProcesses(&yamlData)
err = loadProcesses(&yamlData, filesystem)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *S) TestBuild(c *check.C) {
URL: server.URL,
Token: "fake-token",
}
build(client, "app1", []string{"ls"})
build(client, "app1", []string{"ls"}, s.fs)
}

func (s *S) TestDeploy(c *check.C) {
Expand Down Expand Up @@ -71,5 +71,5 @@ func (s *S) TestDeploy(c *check.C) {
URL: server.URL,
Token: "fake-token",
}
deploy(client, "app1")
deploy(client, "app1", s.fs)
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func main() {

switch command[len(command)-1] {
case "build":
build(c, appName, command[:len(command)-1])
build(c, appName, command[:len(command)-1], &fs.OsFs{})
case "deploy-only":
deploy(c, appName)
deploy(c, appName, &fs.OsFs{})
case "deploy":
// backward compatibility with tsuru < 1.4.0
command = command[:len(command)-1]
fallthrough
default:
build(c, appName, command)
deploy(c, appName)
build(c, appName, command, &fs.OsFs{})
deploy(c, appName, &fs.OsFs{})
}
}
5 changes: 0 additions & 5 deletions suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ var _ = check.Suite(&S{})

func (s *S) SetUpTest(c *check.C) {
s.fs = &fstest.RecordingFs{}
fsystem = s.fs
s.exec = &exectest.FakeExecutor{}
osExecutor = s.exec
err := s.fs.Mkdir(defaultWorkingDir, 0777)
c.Assert(err, check.IsNil)
}

func (s *S) TeardownTest(c *check.C) {
fsystem = nil
}
35 changes: 13 additions & 22 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ var (
appEnvsFile = "/tmp/app_envs"
)

var fsystem fs.Fs

func filesystem() fs.Fs {
if fsystem == nil {
fsystem = &fs.OsFs{}
}
return fsystem
}

var osExecutor exec.Executor

func executor() exec.Executor {
Expand All @@ -44,7 +35,7 @@ func executor() exec.Executor {
return osExecutor
}

func execScript(cmds []string, envs []bind.EnvVar, w io.Writer) error {
func execScript(cmds []string, envs []bind.EnvVar, w io.Writer, filesystem fs.Fs) error {
if w == nil {
w = ioutil.Discard
}
Expand All @@ -53,7 +44,7 @@ func execScript(cmds []string, envs []bind.EnvVar, w io.Writer) error {
return err
}
workingDir := defaultWorkingDir
if _, err := filesystem().Stat(defaultWorkingDir); err != nil {
if _, err := filesystem.Stat(defaultWorkingDir); err != nil {
if os.IsNotExist(err) {
workingDir = "/"
} else {
Expand Down Expand Up @@ -98,11 +89,11 @@ type Hook struct {
func (t *TsuruYaml) isEmpty() bool {
return len(t.Hooks.BuildHooks) == 0 && t.Processes == nil
}
func loadTsuruYaml() (TsuruYaml, error) {
func loadTsuruYaml(filesystem fs.Fs) (TsuruYaml, error) {
var tsuruYamlData TsuruYaml
for _, yamlFile := range tsuruYamlFiles {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, yamlFile)
f, err := filesystem().Open(filePath)
f, err := filesystem.Open(filePath)
if err != nil {
continue
}
Expand All @@ -120,14 +111,14 @@ func loadTsuruYaml() (TsuruYaml, error) {
return tsuruYamlData, nil
}

func buildHooks(yamlData TsuruYaml, envs []bind.EnvVar) error {
func buildHooks(yamlData TsuruYaml, envs []bind.EnvVar, filesystem fs.Fs) error {
cmds := append([]string{}, yamlData.Hooks.BuildHooks...)
fmt.Fprintln(os.Stdout, "---- Running build hooks ----")
return execScript(cmds, envs, os.Stdout)
return execScript(cmds, envs, os.Stdout, filesystem)
}

func readProcfile(path string) (string, error) {
f, err := filesystem().Open(fmt.Sprintf("%v/Procfile", path))
func readProcfile(path string, filesystem fs.Fs) (string, error) {
f, err := filesystem.Open(fmt.Sprintf("%v/Procfile", path))
if err != nil {
return "", err
}
Expand All @@ -141,8 +132,8 @@ func readProcfile(path string) (string, error) {

var procfileRegex = regexp.MustCompile(`^([\w-]+):\s*(\S.+)$`)

func loadProcesses(t *TsuruYaml) error {
procfile, err := readProcfile(defaultWorkingDir)
func loadProcesses(t *TsuruYaml, filesystem fs.Fs) error {
procfile, err := readProcfile(defaultWorkingDir, filesystem)
if err != nil {
return err
}
Expand All @@ -160,11 +151,11 @@ func loadProcesses(t *TsuruYaml) error {
return nil
}

func readDiffDeploy() (string, bool, error) {
func readDiffDeploy(filesystem fs.Fs) (string, bool, error) {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, "diff")
f, err := filesystem().Open(filePath)
f, err := filesystem.Open(filePath)
defer f.Close()
defer filesystem().Remove(filePath)
defer filesystem.Remove(filePath)
if err != nil {
return "", true, nil
}
Expand Down
24 changes: 12 additions & 12 deletions tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *S) TestExecScript(c *check.C) {
Public: true,
}}
buf := bytes.NewBufferString("")
err := execScript(cmds, envs, buf)
err := execScript(cmds, envs, buf, s.fs)
c.Assert(err, check.IsNil)
executedCmds := s.exec.GetCommands("/bin/bash")
c.Assert(len(executedCmds), check.Equals, 2)
Expand All @@ -53,7 +53,7 @@ func (s *S) TestExecScript(c *check.C) {
func (s *S) TestExecScriptWithError(c *check.C) {
cmds := []string{"not-exists"}
osExecutor = &exectest.ErrorExecutor{}
err := execScript(cmds, nil, nil)
err := execScript(cmds, nil, nil, s.fs)
c.Assert(err, check.NotNil)
}

Expand All @@ -68,7 +68,7 @@ func (s *S) TestExecScriptWorkingDirNotExist(c *check.C) {
Value: "bar",
Public: true,
}}
err = execScript(cmds, envs, nil)
err = execScript(cmds, envs, nil, s.fs)
c.Assert(err, check.IsNil)
executedCmds := s.exec.GetCommands("/bin/bash")
c.Assert(len(executedCmds), check.Equals, 1)
Expand Down Expand Up @@ -110,7 +110,7 @@ healthcheck:
"allowed_failures": 0,
},
}
t, err := loadTsuruYaml()
t, err := loadTsuruYaml(s.fs)
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
}
Expand All @@ -124,7 +124,7 @@ func (s *S) TestHooks(c *check.C) {
Value: "bar",
Public: true,
}}
err := buildHooks(tsuruYaml, envs)
err := buildHooks(tsuruYaml, envs, s.fs)
c.Assert(err, check.IsNil)
executedCmds := s.exec.GetCommands("/bin/bash")
c.Assert(len(executedCmds), check.Equals, 2)
Expand All @@ -149,7 +149,7 @@ func (s *S) TestLoadProcesses(c *check.C) {
},
}
t := TsuruYaml{}
err = loadProcesses(&t)
err = loadProcesses(&t, s.fs)
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
}
Expand All @@ -172,7 +172,7 @@ another-worker: run-task
},
}
t := TsuruYaml{}
err = loadProcesses(&t)
err = loadProcesses(&t, s.fs)
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
}
Expand All @@ -186,7 +186,7 @@ func (s *S) TestDontLoadWrongProcfile(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(s.fs.HasAction(fmt.Sprintf("create %s", procfilePath)), check.Equals, true)
t := TsuruYaml{}
err = loadProcesses(&t)
err = loadProcesses(&t, s.fs)
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, `invalid Procfile, no processes found in "web:\n\t@python test.py"`)
}
Expand All @@ -213,14 +213,14 @@ func (s *S) TestDiffDeploy(c *check.C) {
_, err := s.fs.Create(diffPath)
c.Assert(err, check.IsNil)
c.Assert(s.fs.HasAction(fmt.Sprintf("create %s", diffPath)), check.Equals, true)
result, first, err := readDiffDeploy()
result, first, err := readDiffDeploy(s.fs)
c.Assert(err, check.IsNil)
c.Assert(result, check.DeepEquals, diff)
c.Assert(first, check.Equals, false)
}

func (s *S) TestReadProcfileNotFound(c *check.C) {
_, err := readProcfile("./fake-path")
_, err := readProcfile("./fake-path", s.fs)
_, ok := err.(syscall.Errno)
c.Assert(ok, check.Equals, true)
}
Expand All @@ -234,7 +234,7 @@ func (s *S) TestReadProcfileFound(c *check.C) {
_, err = procfile.Write([]byte(procfileContent))
c.Assert(err, check.IsNil)
c.Assert(procfile.Close(), check.IsNil)
result, err := readProcfile(procfilePath)
result, err := readProcfile(procfilePath, s.fs)
c.Assert(err, check.IsNil)
c.Assert(result, check.Equals, expected)
}
Expand All @@ -248,7 +248,7 @@ func (s *S) TestReadProcfileNormalizeCRLFToLF(c *check.C) {
_, err = procfile.Write([]byte(procfileContent))
c.Assert(err, check.IsNil)
c.Assert(procfile.Close(), check.IsNil)
result, err := readProcfile(procfilePath)
result, err := readProcfile(procfilePath, s.fs)
c.Assert(err, check.IsNil)
c.Assert(result, check.Equals, expected)
}

0 comments on commit b34ab9e

Please sign in to comment.