From 83aa82f5910caf6ee39d983de873a393ad1128b1 Mon Sep 17 00:00:00 2001 From: Rachid Zarouali Date: Sun, 3 Jan 2016 14:33:39 +0100 Subject: [PATCH] ADD CONTAINS --- build/dockerfile | 2 ++ build/tester.go | 15 ++++++++++++++- build/tester_test.go | 6 ++++-- build/testfile | 3 +++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/build/dockerfile b/build/dockerfile index 50fdc0a..d44a242 100644 --- a/build/dockerfile +++ b/build/dockerfile @@ -4,6 +4,8 @@ RUN useradd -d /home/mario -m -s /bin/bash mario COPY words /usr/local/tomcat/webapps/ +RUN bash -c 'echo bar >> /tmp/foo.txt' + RUN apt-get update && apt-get install -y vim USER mario diff --git a/build/tester.go b/build/tester.go index f382ea3..12cd7bf 100644 --- a/build/tester.go +++ b/build/tester.go @@ -208,9 +208,22 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) { } test += command.Args[2] ephemeral.Args = append(ephemeral.Args, test) + + case "CONTAINS": + if len(command.Args) != 4 { + return nil, fmt.Errorf("Condition %s accept one and only one argument (found %d)", "CONTAINS", len(command.Args)-3) + } + ephemeral.Args = append(ephemeral.Args, "bash", "-c") + test := "grep " + if command.Args[0] == commands.AssertFalse { + test += "! " + } + test += command.Args[2] + " " + command.Args[3] + ephemeral.Args = append(ephemeral.Args, test) + default: - return nil, fmt.Errorf("Condition %s is not supported. Only %s, %s, %s and %s are currently supported. Please open an issue if you want to add support for it.", command.Args[1], "USER_EXISTS", "FILE_EXISTS", "CURRENT_USER_IS", "IS_INSTALLED", "IS_RUNNING") + return nil, fmt.Errorf("Condition %s is not supported. Only %s, %s, %s and %s are currently supported. Please open an issue if you want to add support for it.", command.Args[1], "USER_EXISTS", "FILE_EXISTS", "CURRENT_USER_IS", "IS_INSTALLED", "IS_RUNNING", "CONTAINS") } return ephemeral, nil diff --git a/build/tester_test.go b/build/tester_test.go index c2c7103..2b9680a 100644 --- a/build/tester_test.go +++ b/build/tester_test.go @@ -25,8 +25,8 @@ func TestNewTester(t *testing.T) { t.Errorf("Failed to test file %s", tests) } - if blockNum := len(tests.testBlocks); blockNum != 8 { - t.Errorf("Expected 8 blocks, found %d", blockNum) + if blockNum := len(tests.testBlocks); blockNum != 9 { + t.Errorf("Expected 9 blocks, found %d", blockNum) } } @@ -40,6 +40,8 @@ func TestInjection(t *testing.T) { {Args: []string{"EPHEMERAL", "bash", "-c", "test ! -f /usr/local/tomcat/webapps/words"}}, {Args: []string{"COPY", "words", "/usr/local/tomcat/webapps/"}}, {Args: []string{"EPHEMERAL", "bash", "-c", "test -f /usr/local/tomcat/webapps/words"}}, + {Args: []string{"RUN", "bash", "-c", "echo bar >> /tmp/foo.txt"}}, + {Args: []string{"EPHEMERAL", "bash", "-c", "grep bar /tmp/foo.txt"}}, // {Args: []string{"EPHEMERAL", "bash", "-c", "test ! \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}}, {Args: []string{"EPHEMERAL", "bash", "-c", "! command -v \"vim\" 1>/dev/null 2>&1"}}, {Args: []string{"RUN", "apt-get", "update", "&&", "apt-get", "install", "-y", "vim"}}, diff --git a/build/testfile b/build/testfile index 0f8fae5..93bab55 100644 --- a/build/testfile +++ b/build/testfile @@ -8,6 +8,9 @@ ASSERT_FALSE FILE_EXISTS '/usr/local/tomcat/webapps/words' @AFTER COPY_WORDS ASSERT_TRUE FILE_EXISTS '/usr/local/tomcat/webapps/words' +@AFTER RUN_BASH +ASSERT_TRUE CONTAINS 'bar' '/tmp/foo.txt' + @BEFORE RUN_APT ASSERT_FALSE IS_INSTALLED 'vim'