Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD CONTAINS #7

Merged
merged 1 commit into from
Jan 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Temporary Items

# Visual Studio Code
.vscode

.idea
2 changes: 2 additions & 0 deletions build/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 19 additions & 8 deletions build/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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":
Expand All @@ -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
Expand All @@ -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"
}
18 changes: 10 additions & 8 deletions build/tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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"}},
}

Expand Down
6 changes: 5 additions & 1 deletion build/testfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
ASSERT_TRUE IS_INSTALLED 'vim'