2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2015-01-28 21:28:48 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
2016-11-22 14:26:02 -05:00
|
|
|
"runtime"
|
2015-01-28 21:28:48 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-03-13 17:25:37 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
2015-01-28 21:28:48 -05:00
|
|
|
)
|
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
func TestShellParser4EnvVars(t *testing.T) {
|
|
|
|
fn := "envVarTest"
|
2016-11-22 14:26:02 -05:00
|
|
|
lineCount := 0
|
2015-11-07 15:05:55 -05:00
|
|
|
|
|
|
|
file, err := os.Open(fn)
|
2017-03-13 17:25:37 -04:00
|
|
|
assert.NilError(t, err)
|
2015-01-28 21:28:48 -05:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
2015-10-18 23:55:53 -04:00
|
|
|
envs := []string{"PWD=/home", "SHELL=bash", "KOREAN=한국어"}
|
2015-01-28 21:28:48 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2016-11-22 14:26:02 -05:00
|
|
|
lineCount++
|
2015-01-28 21:28:48 -05:00
|
|
|
|
|
|
|
// Trim comments and blank lines
|
|
|
|
i := strings.Index(line, "#")
|
|
|
|
if i >= 0 {
|
|
|
|
line = line[:i]
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
words := strings.Split(line, "|")
|
2017-03-13 17:25:37 -04:00
|
|
|
assert.Equal(t, len(words), 3)
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2017-03-13 17:25:37 -04:00
|
|
|
platform := strings.TrimSpace(words[0])
|
|
|
|
source := strings.TrimSpace(words[1])
|
|
|
|
expected := strings.TrimSpace(words[2])
|
2015-01-28 21:28:48 -05:00
|
|
|
|
2016-11-22 14:26:02 -05:00
|
|
|
// Key W=Windows; A=All; U=Unix
|
2017-03-13 17:25:37 -04:00
|
|
|
if platform != "W" && platform != "A" && platform != "U" {
|
|
|
|
t.Fatalf("Invalid tag %s at line %d of %s. Must be W, A or U", platform, lineCount, fn)
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 17:25:37 -04:00
|
|
|
if ((platform == "W" || platform == "A") && runtime.GOOS == "windows") ||
|
|
|
|
((platform == "U" || platform == "A") && runtime.GOOS != "windows") {
|
|
|
|
newWord, err := ProcessWord(source, envs, '\\')
|
|
|
|
if expected == "error" {
|
|
|
|
assert.Error(t, err, "")
|
|
|
|
} else {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, newWord, []string{expected})
|
2016-11-22 14:26:02 -05:00
|
|
|
}
|
2015-01-28 21:28:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-22 16:12:43 -04:00
|
|
|
|
2015-11-07 15:05:55 -05:00
|
|
|
func TestShellParser4Words(t *testing.T) {
|
|
|
|
fn := "wordsTest"
|
|
|
|
|
|
|
|
file, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't open '%s': %s", err, fn)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
envs := []string{}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "ENV ") {
|
|
|
|
line = strings.TrimLeft(line[3:], " ")
|
|
|
|
envs = append(envs, line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
words := strings.Split(line, "|")
|
|
|
|
if len(words) != 2 {
|
|
|
|
t.Fatalf("Error in '%s' - should be exactly one | in: %q", fn, line)
|
|
|
|
}
|
|
|
|
test := strings.TrimSpace(words[0])
|
|
|
|
expected := strings.Split(strings.TrimLeft(words[1], " "), ",")
|
|
|
|
|
2016-10-21 14:55:39 -04:00
|
|
|
result, err := ProcessWords(test, envs, '\\')
|
2015-11-07 15:05:55 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
result = []string{"error"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) != len(expected) {
|
|
|
|
t.Fatalf("Error. %q was suppose to result in %q, but got %q instead", test, expected, result)
|
|
|
|
}
|
|
|
|
for i, w := range expected {
|
|
|
|
if w != result[i] {
|
|
|
|
t.Fatalf("Error. %q was suppose to result in %q, but got %q instead", test, expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 16:12:43 -04:00
|
|
|
func TestGetEnv(t *testing.T) {
|
|
|
|
sw := &shellWord{
|
|
|
|
word: "",
|
|
|
|
envs: nil,
|
|
|
|
pos: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{}
|
|
|
|
if sw.getEnv("foo") != "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("2 - 'foo' should map to ''")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo"}
|
|
|
|
if sw.getEnv("foo") != "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("3 - 'foo' should map to ''")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo="}
|
|
|
|
if sw.getEnv("foo") != "" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("4 - 'foo' should map to ''")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo=bar"}
|
|
|
|
if sw.getEnv("foo") != "bar" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("5 - 'foo' should map to 'bar'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.envs = []string{"foo=bar", "car=hat"}
|
|
|
|
if sw.getEnv("foo") != "bar" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("6 - 'foo' should map to 'bar'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
if sw.getEnv("car") != "hat" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("7 - 'car' should map to 'hat'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we grab the first 'car' in the list
|
|
|
|
sw.envs = []string{"foo=bar", "car=hat", "car=bike"}
|
|
|
|
if sw.getEnv("car") != "hat" {
|
2016-12-25 01:37:31 -05:00
|
|
|
t.Fatal("8 - 'car' should map to 'hat'")
|
2015-08-22 16:12:43 -04:00
|
|
|
}
|
|
|
|
}
|