Skip to content

Commit 83aa82f

Browse files
author
Rachid Zarouali
committed
ADD CONTAINS
1 parent c475dfa commit 83aa82f

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

build/dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ RUN useradd -d /home/mario -m -s /bin/bash mario
44

55
COPY words /usr/local/tomcat/webapps/
66

7+
RUN bash -c 'echo bar >> /tmp/foo.txt'
8+
79
RUN apt-get update && apt-get install -y vim
810

911
USER mario

build/tester.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,22 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) {
208208
}
209209
test += command.Args[2]
210210
ephemeral.Args = append(ephemeral.Args, test)
211+
212+
case "CONTAINS":
213+
if len(command.Args) != 4 {
214+
return nil, fmt.Errorf("Condition %s accept one and only one argument (found %d)", "CONTAINS", len(command.Args)-3)
215+
}
216+
ephemeral.Args = append(ephemeral.Args, "bash", "-c")
217+
test := "grep "
218+
if command.Args[0] == commands.AssertFalse {
219+
test += "! "
220+
}
221+
test += command.Args[2] + " " + command.Args[3]
222+
ephemeral.Args = append(ephemeral.Args, test)
223+
211224

212225
default:
213-
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")
226+
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")
214227
}
215228

216229
return ephemeral, nil

build/tester_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestNewTester(t *testing.T) {
2525
t.Errorf("Failed to test file %s", tests)
2626
}
2727

28-
if blockNum := len(tests.testBlocks); blockNum != 8 {
29-
t.Errorf("Expected 8 blocks, found %d", blockNum)
28+
if blockNum := len(tests.testBlocks); blockNum != 9 {
29+
t.Errorf("Expected 9 blocks, found %d", blockNum)
3030
}
3131
}
3232

@@ -40,6 +40,8 @@ func TestInjection(t *testing.T) {
4040
{Args: []string{"EPHEMERAL", "bash", "-c", "test ! -f /usr/local/tomcat/webapps/words"}},
4141
{Args: []string{"COPY", "words", "/usr/local/tomcat/webapps/"}},
4242
{Args: []string{"EPHEMERAL", "bash", "-c", "test -f /usr/local/tomcat/webapps/words"}},
43+
{Args: []string{"RUN", "bash", "-c", "echo bar >> /tmp/foo.txt"}},
44+
{Args: []string{"EPHEMERAL", "bash", "-c", "grep bar /tmp/foo.txt"}},
4345
// {Args: []string{"EPHEMERAL", "bash", "-c", "test ! \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
4446
{Args: []string{"EPHEMERAL", "bash", "-c", "! command -v \"vim\" 1>/dev/null 2>&1"}},
4547
{Args: []string{"RUN", "apt-get", "update", "&&", "apt-get", "install", "-y", "vim"}},

build/testfile

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ ASSERT_FALSE FILE_EXISTS '/usr/local/tomcat/webapps/words'
88
@AFTER COPY_WORDS
99
ASSERT_TRUE FILE_EXISTS '/usr/local/tomcat/webapps/words'
1010

11+
@AFTER RUN_BASH
12+
ASSERT_TRUE CONTAINS 'bar' '/tmp/foo.txt'
13+
1114
@BEFORE RUN_APT
1215
ASSERT_FALSE IS_INSTALLED 'vim'
1316

0 commit comments

Comments
 (0)