Skip to content

Commit

Permalink
fix: resolve paths in config object
Browse files Browse the repository at this point in the history
  • Loading branch information
xmik committed Feb 1, 2019
1 parent 5580923 commit faaca44
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* fix: resolve relative paths in config object

### 0.2.0 (2019-Jan-25)

* feature: support configurable exit behavior for docker-compose driver if a non-default container stops #17197
Expand Down
55 changes: 44 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/user"
"strings"
"path/filepath"
)

type Config struct {
Expand Down Expand Up @@ -152,17 +153,19 @@ func getCLIConfig() Config {
// version is returned as the first log message
os.Exit(0)
}

workDirInnerAbs := getAbsPathOrPanic(workDirInner)
workDirOuterAbs := getAbsPathOrPanic(workDirOuter)
identityDirOuterAbs := getAbsPathOrPanic(identityDirOuter)
return Config{
Action: action,
ConfigFile: config,
Driver: driver,
Debug: debug,
Interactive: interactive,
RemoveContainers: removeContainers,
WorkDirInner: workDirInner,
WorkDirOuter: workDirOuter,
IdentityDirOuter: identityDirOuter,
WorkDirInner: workDirInnerAbs,
WorkDirOuter: workDirOuterAbs,
IdentityDirOuter: identityDirOuterAbs,
BlacklistVariables: blacklistVariables,
RunCommand: runCommand,
DockerImage: image,
Expand All @@ -173,6 +176,18 @@ func getCLIConfig() Config {
}
}

func getAbsPathOrPanic(path string) string {
if path == "" {
return path
} else {
absPath, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return absPath
}
}

// While parsing CLI arguments, after all the flags are handled, we want to treat the rest of the arguments
// as 1 element, as docker or docker-compose run command.
// We cannot just use strings.Join(runCommandArr, " ") because this would result in missing quotes.
Expand Down Expand Up @@ -244,12 +259,26 @@ func ConfigToMap(config Config) map[string]string {
return configMap
}

func ensureNoOuterQuotes(input string) string {
if strings.HasPrefix(input, "\"") && strings.HasSuffix(input, "\"") {
output := strings.TrimPrefix(input, "\"")
output = strings.TrimSuffix(output, "\"")
return output
}
if strings.HasPrefix(input, "'") && strings.HasSuffix(input, "'") {
output := strings.TrimPrefix(input, "'")
output = strings.TrimSuffix(output, "'")
return output
}
return input
}

// getFileConfig never returns error. If config file does not exist,
// it returns Config object with default values.
func getFileConfig(logger *Logger, filePath string) Config {
func getFileConfig(logger *Logger, pathToFile string) Config {
config := Config{}
if _, err := os.Stat(filePath); err == nil {
contents, err := ioutil.ReadFile(filePath)
if _, err := os.Stat(pathToFile); err == nil {
contents, err := ioutil.ReadFile(pathToFile)
if err != nil {
panic(err)
}
Expand All @@ -263,6 +292,7 @@ func getFileConfig(logger *Logger, filePath string) Config {
kv := strings.SplitN(line, "=", 2)
key := kv[0]
value := kv[1]
value = ensureNoOuterQuotes(value)

switch key {
case "DOJO_DRIVER":
Expand All @@ -276,11 +306,14 @@ func getFileConfig(logger *Logger, filePath string) Config {
case "DOJO_DOCKER_COMPOSE_OPTIONS":
config.DockerComposeOptions = value
case "DOJO_WORK_OUTER":
config.WorkDirOuter = value
dir := getAbsPathOrPanic(value)
config.WorkDirOuter = dir
case "DOJO_WORK_INNER":
config.WorkDirInner = value
dir := getAbsPathOrPanic(value)
config.WorkDirInner = dir
case "DOJO_IDENTITY_OUTER":
config.IdentityDirOuter = value
dir := getAbsPathOrPanic(value)
config.IdentityDirOuter = dir
case "DOJO_EXIT_BEHAVIOR":
config.ExitBehavior = value
case "DOJO_BLACKLIST_VARIABLES":
Expand All @@ -295,7 +328,7 @@ func getFileConfig(logger *Logger, filePath string) Config {
}
}
} else {
logger.Log("debug", fmt.Sprintf("Config file does not exist: %s", filePath))
logger.Log("debug", fmt.Sprintf("Config file does not exist: %s", pathToFile))
}
return config
}
Expand Down
48 changes: 45 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@ func Test_smartJoinCommandArgs(t *testing.T){
}
}

func Test_getAbsPathOrPanic(t *testing.T) {
currentDirectory, err := os.Getwd()
if err != nil {
panic(err)
}
var mytests = []struct {
input string
expectedOutput string
}{
{"", ""},
{"/tmp/123", "/tmp/123"},
{"123", currentDirectory+"/123"},
{"./123", currentDirectory+"/123"},
}
for _,v := range mytests {
outputCmd := getAbsPathOrPanic(v.input)
assert.Equal(t, v.expectedOutput, outputCmd)
}
}

func Test_ensureNoOuterQuotes(t *testing.T) {
var mytests = []struct {
input string
expectedOutput string
}{
{"", ""},
{"/tmp/123", "/tmp/123"},
{"\"/tmp/123\"", "/tmp/123"},
{"'/tmp/123'", "/tmp/123"},
{"\"/tmp/123", "\"/tmp/123"},
{"/tmp/123\"", "/tmp/123\""},
{"'/tmp/123", "'/tmp/123"},
{"/tmp/123'", "/tmp/123'"},
}
for _,v := range mytests {
outputCmd := ensureNoOuterQuotes(v.input)
assert.Equal(t, v.expectedOutput, outputCmd, v.input)
}
}

func Test_getCLIConfig(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
Expand Down Expand Up @@ -137,8 +177,10 @@ func Test_getFileConfig(t *testing.T) {
fmt.Fprintf(file, "DOJO_DRIVER=somedriver\n")
fmt.Fprintf(file, "DOJO_DOCKER_OPTIONS=-v /tmp/bla:/home/dojo/bla:ro -e ABC=123\n")
fmt.Fprintf(file, "DOJO_DOCKER_COMPOSE_FILE=docker-compose.yml\n")
// absolute path
fmt.Fprintf(file, "DOJO_WORK_OUTER=/tmp/123\n")
fmt.Fprintf(file, "DOJO_WORK_INNER=/tmp/inner\n")
// relative path
fmt.Fprintf(file, "DOJO_WORK_INNER=inner\n")
fmt.Fprintf(file, "DOJO_IDENTITY_OUTER=/tmp/outer\n")
fmt.Fprintf(file, "DOJO_BLACKLIST_VARIABLES=VAR1,VAR2,ABC\n")
fmt.Fprintf(file, "DOJO_LOG_LEVEL=info\n")
Expand All @@ -152,7 +194,6 @@ func Test_getFileConfig(t *testing.T) {
DockerOptions: "-v /tmp/bla:/home/dojo/bla:ro -e ABC=123",
DockerComposeFile: "docker-compose.yml",
WorkDirOuter: "/tmp/123",
WorkDirInner: "/tmp/inner",
IdentityDirOuter: "/tmp/outer",
BlacklistVariables: "VAR1,VAR2,ABC",
Debug: "false",
Expand All @@ -164,7 +205,8 @@ func Test_getFileConfig(t *testing.T) {
assert.Equal(t, expectedConfig.DockerOptions, config.DockerOptions)
assert.Equal(t, expectedConfig.DockerComposeFile, config.DockerComposeFile)
assert.Equal(t, expectedConfig.WorkDirOuter, config.WorkDirOuter)
assert.Equal(t, expectedConfig.WorkDirInner, config.WorkDirInner)
// relative path got saved as absolute path
assert.Contains(t, config.WorkDirInner, "/inner")
assert.Equal(t, expectedConfig.IdentityDirOuter, config.IdentityDirOuter)
assert.Equal(t, expectedConfig.BlacklistVariables, config.BlacklistVariables)
assert.Equal(t, expectedConfig.Debug, config.Debug)
Expand Down
18 changes: 18 additions & 0 deletions test/02-docker-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ DOJO_PATH=$(readlink -f "./bin/dojo")
testEnvFileIsRemoved
testDockerContainerIsRemoved
}
@test "driver: docker, action: run; custom, relative work directory" {
cleanUpDockerContainer
cleanUpEnvFiles
run ${DOJO_PATH} -c test/test-files/Dojofile --debug=true --test=true --image=alpine:3.8 whoami
assert_output --partial "Dojo version"
assert_output --partial "root"
assert_output --partial "alpine:3.8 whoami"
assert_output --partial "Exit status from run command: 0"
assert_output --partial "Exit status from cleaning: 0"
assert_output --partial "Exit status from signals: 0"
refute_output --partial "WARN"
refute_output --partial "warn"
refute_output --partial "ERROR"
refute_output --partial "error"
assert_equal "$status" 0
testEnvFileIsRemoved
testDockerContainerIsRemoved
}
@test "driver: docker, action: run, --rm=false" {
cleanUpDockerContainer
cleanUpEnvFiles
Expand Down
2 changes: 2 additions & 0 deletions test/test-files/Dojofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DOJO_DRIVER=docker
DOJO_WORK_OUTER="./test/test-files/custom-relative"
Empty file.

0 comments on commit faaca44

Please sign in to comment.