Skip to content

Commit

Permalink
Add tests to lookup package
Browse files Browse the repository at this point in the history
- Refactored a little bit (naming)
- Add some tests and fixing tiny bug

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Jul 13, 2016
1 parent a12288b commit 397668b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docker/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestParseCommand(t *testing.T) {
func TestParseBindsAndVolumes(t *testing.T) {
ctx := &Context{}
ctx.ComposeFiles = []string{"foo/docker-compose.yml"}
ctx.ResourceLookup = &lookup.FileConfigLookup{}
ctx.ResourceLookup = &lookup.FileResourceLookup{}

abs, err := filepath.Abs(".")
assert.Nil(t, err)
Expand All @@ -36,7 +36,7 @@ func TestParseBindsAndVolumes(t *testing.T) {
func TestParseLabels(t *testing.T) {
ctx := &Context{}
ctx.ComposeFiles = []string{"foo/docker-compose.yml"}
ctx.ResourceLookup = &lookup.FileConfigLookup{}
ctx.ResourceLookup = &lookup.FileResourceLookup{}
bashCmd := "bash"
fooLabel := "foo.label"
fooLabelValue := "service.config.value"
Expand Down
2 changes: 1 addition & 1 deletion docker/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const ComposeVersion = "1.5.0"
// NewProject creates a Project with the specified context.
func NewProject(context *Context, parseOptions *config.ParseOptions) (project.APIProject, error) {
if context.ResourceLookup == nil {
context.ResourceLookup = &lookup.FileConfigLookup{}
context.ResourceLookup = &lookup.FileResourceLookup{}
}

if context.EnvironmentLookup == nil {
Expand Down
12 changes: 6 additions & 6 deletions lookup/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func relativePath(file, relativeTo string) string {
// stdin: return the current working directory if possible.
if relativeTo == "-" {
if cwd, err := os.Getwd(); err == nil {
return cwd
return filepath.Join(cwd, file)
}
}

Expand All @@ -39,15 +39,15 @@ func relativePath(file, relativeTo string) string {
return abs
}

// FileConfigLookup is a "bare" structure that implements the project.ResourceLookup interface
type FileConfigLookup struct {
// FileResourceLookup is a "bare" structure that implements the project.ResourceLookup interface
type FileResourceLookup struct {
}

// Lookup returns the content and the actual filename of the file that is "built" using the
// specified file and relativeTo string. file and relativeTo are supposed to be file path.
// If file starts with a slash ('/'), it tries to load it, otherwise it will build a
// filename using the folder part of relativeTo joined with file.
func (f *FileConfigLookup) Lookup(file, relativeTo string) ([]byte, string, error) {
func (f *FileResourceLookup) Lookup(file, relativeTo string) ([]byte, string, error) {
file = relativePath(file, relativeTo)
logrus.Debugf("Reading file %s", file)
bytes, err := ioutil.ReadFile(file)
Expand All @@ -56,11 +56,11 @@ func (f *FileConfigLookup) Lookup(file, relativeTo string) ([]byte, string, erro

// ResolvePath returns the path to be used for the given path volume. This
// function already takes care of relative paths.
func (f *FileConfigLookup) ResolvePath(path, inFile string) string {
func (f *FileResourceLookup) ResolvePath(path, relativeTo string) string {
vs := strings.SplitN(path, ":", 2)
if len(vs) != 2 || filepath.IsAbs(vs[0]) {
return path
}
vs[0] = relativePath(vs[0], inFile)
vs[0] = relativePath(vs[0], relativeTo)
return strings.Join(vs, ":")
}
36 changes: 34 additions & 2 deletions lookup/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lookup
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
Expand All @@ -27,7 +28,7 @@ func TestLookupError(t *testing.T) {
input{"does/not/exists/file", "/tmp/"}: "open /tmp/does/not/exists/file: no such file or directory",
}

fileConfigLookup := FileConfigLookup{}
fileConfigLookup := FileResourceLookup{}

for invalid, expectedError := range invalids {
_, _, err := fileConfigLookup.Lookup(invalid.file, invalid.relativeTo)
Expand All @@ -51,7 +52,7 @@ func TestLookupOK(t *testing.T) {
t.Fatal(err)
}

fileConfigLookup := FileConfigLookup{}
fileConfigLookup := FileResourceLookup{}

valids := map[input]string{
input{"file1", tmpFolder + "/"}: "content1",
Expand All @@ -68,3 +69,34 @@ func TestLookupOK(t *testing.T) {
}
}
}

func TestResolvePath(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

cases := []struct {
path string
relativeTo string
expected string
}{
{"../path:something", "./docker-compose.yml", filepath.Join(cwd, "../path") + ":something"},
{"../path:something", "docker-compose.yml", filepath.Join(cwd, "../path") + ":something"},
{"../path:something", "/tmp/docker-compose.yml", "/path:something"},
{"path:something", "/tmp/docker-compose.yml", "/tmp/path:something"},
{"/path:something", "/tmp/docker-compose.yml", "/path:something"},
{"path:something", "-", filepath.Join(cwd, "path") + ":something"},
{"path/:something", "-", filepath.Join(cwd, "path") + ":something"},
{"../path:something", "-", filepath.Join(cwd, "../path") + ":something"},
}

fileConfigLookup := FileResourceLookup{}

for index, c := range cases {
actual := fileConfigLookup.ResolvePath(c.path, c.relativeTo)
if actual != c.expected {
t.Errorf("Expected %s, got %s for case %d", c.expected, actual, index)
}
}
}

0 comments on commit 397668b

Please sign in to comment.