diff --git a/.gitignore b/.gitignore index 83991e6..b45ef3f 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,4 @@ Temporary Items # Visual Studio Code .vscode - +.idea diff --git a/build/dockerfile b/build/dockerfile index e7a3b6a..a2152a7 100644 --- a/build/dockerfile +++ b/build/dockerfile @@ -6,6 +6,8 @@ COPY words /usr/local/tomcat/webapps/ USER mario +RUN bash -c 'echo bar >> /tmp/foo.txt' + RUN apt-get update && apt-get install -y vim CMD catalina.sh run diff --git a/build/tester.go b/build/tester.go index c56dbdc..6cfb60a 100644 --- a/build/tester.go +++ b/build/tester.go @@ -172,8 +172,8 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) { } test += "-f " + command.Args[2] ephemeral.Args = append(ephemeral.Args, test) - - case "CURRENT_USER_IS": + + case "CURRENT_USER_IS": if len(command.Args) != 3 { return nil, fmt.Errorf("Condition %s accept one and only one argument (found %d)", "CURRENT_USER_IS", len(command.Args)-2) } @@ -182,7 +182,7 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) { if command.Args[0] == commands.AssertFalse { test += "! " } - test += "$(whoami) = \"" + command.Args[2] + "\"" + test += "$(whoami) = \"" + command.Args[2] + "\"" ephemeral.Args = append(ephemeral.Args, test) case "IS_INSTALLED": @@ -194,12 +194,23 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) { if command.Args[0] == commands.AssertFalse { test += "! " } - test += isInstalledGeneric(command.Args[2]) + test += isInstalledGeneric(command.Args[2]) ephemeral.Args = append(ephemeral.Args, test) - + + case "FILE_CONTAINS": + if len(command.Args) != 4 { + return nil, fmt.Errorf("Condition %s accept two and only two argument (found %d)", "FILE_CONTAINS", len(command.Args)-3) + } + ephemeral.Args = append(ephemeral.Args, "bash", "-c") + test := "grep -q " + 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") + 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", "FILE_CONTAINS") } return ephemeral, nil @@ -222,10 +233,10 @@ func PrintTestsStats(stats *TestStats) { // func isInstalledDebian(packagename string) string { // return "\"$(dpkg-query -W -f='${Status}' " + -// packagename + +// packagename + // ")\" = \"install ok installed\"" // } func isInstalledGeneric(packagename string) string { - return "command -v \"" + packagename + "\" 1>/dev/null 2>&1" + return "command -v \"" + packagename + "\" 1>/dev/null 2>&1" } diff --git a/build/tester_test.go b/build/tester_test.go index 09b0e3d..bd7d11b 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 != 7 { - t.Errorf("Expected 7 blocks, found %d", blockNum) + if blockNum := len(tests.testBlocks); blockNum != 8 { + t.Errorf("Expected 8 blocks, found %d", blockNum) } } @@ -40,14 +40,16 @@ 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{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"root\""}}, + {Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"root\""}}, {Args: []string{"USER", "mario"}}, - {Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"mario\""}}, -// {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{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"mario\""}}, + {Args: []string{"RUN", "bash", "-c", "echo bar >> /tmp/foo.txt"}}, + {Args: []string{"EPHEMERAL", "bash", "-c", "grep -q 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"}}, - {Args: []string{"EPHEMERAL", "bash", "-c", "command -v \"vim\" 1>/dev/null 2>&1"}}, -// {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{"EPHEMERAL", "bash", "-c", "test \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}}, {Args: []string{"CMD", "catalina.sh", "run"}}, } diff --git a/build/testfile b/build/testfile index 414901a..b158d43 100644 --- a/build/testfile +++ b/build/testfile @@ -14,8 +14,12 @@ ASSERT_TRUE CURRENT_USER_IS 'root' @AFTER USER_MARIO ASSERT_TRUE CURRENT_USER_IS 'mario' +@AFTER RUN_BASH +ASSERT_TRUE CONTAINS 'bar' '/tmp/foo.txt' + @BEFORE RUN_APT ASSERT_FALSE IS_INSTALLED 'vim' @AFTER RUN_APT -ASSERT_TRUE IS_INSTALLED 'vim' \ No newline at end of file +ASSERT_TRUE IS_INSTALLED 'vim' +