builder: negative test support, fix for shykes's broken dockerfile

Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
This commit is contained in:
Erik Hollensbe 2014-08-07 00:42:10 -07:00
parent 3f5f6b038f
commit 4122a981ae
3 changed files with 36 additions and 3 deletions

View File

@ -8,10 +8,15 @@ package parser
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
var (
dockerFileErrJSONNesting = errors.New("You may not nest arrays in Dockerfile statements.")
)
// ignore the current argument. This will still leave a command parsed, but
// will not incorporate the arguments into the ast.
func parseIgnore(rest string) (*Node, error) {
@ -86,6 +91,8 @@ func parseJSON(rest string) (*Node, error) {
for _, str := range myJson {
switch str.(type) {
case []interface{}:
return nil, dockerFileErrJSONNesting
case float64:
str = strconv.FormatFloat(str.(float64), 'G', -1, 64)
}
@ -110,6 +117,8 @@ func parseMaybeJSON(rest string) (*Node, error) {
node, err := parseJSON(rest)
if err == nil {
return node, nil
} else if err == dockerFileErrJSONNesting {
return nil, err
}
}

View File

@ -8,9 +8,10 @@ import (
)
const testDir = "testfiles"
const negativeTestDir = "testfiles-negative"
func TestTestData(t *testing.T) {
f, err := os.Open(testDir)
func getDirs(t *testing.T, dir string) []os.FileInfo {
f, err := os.Open(dir)
if err != nil {
t.Fatal(err)
}
@ -22,7 +23,29 @@ func TestTestData(t *testing.T) {
t.Fatal(err)
}
for _, dir := range dirs {
return dirs
}
func TestTestNegative(t *testing.T) {
for _, dir := range getDirs(t, negativeTestDir) {
dockerfile := filepath.Join(negativeTestDir, dir.Name(), "Dockerfile")
df, err := os.Open(dockerfile)
if err != nil {
t.Fatalf("Dockerfile missing for %s: %s", dir.Name(), err.Error())
}
_, err = Parse(df)
if err == nil {
t.Fatalf("No error parsing broken dockerfile for %s: %s", dir.Name(), err.Error())
}
df.Close()
}
}
func TestTestData(t *testing.T) {
for _, dir := range getDirs(t, testDir) {
dockerfile := filepath.Join(testDir, dir.Name(), "Dockerfile")
resultfile := filepath.Join(testDir, dir.Name(), "result")

View File

@ -0,0 +1 @@
CMD [ "echo", [ "nested json" ] ]