Skip to content

Commit ff1ef59

Browse files
author
Rachid Zarouali
committed
ADD CONTAINS
1 parent cca0150 commit ff1ef59

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ Temporary Items
5656

5757
# Visual Studio Code
5858
.vscode
59-
59+
.idea

build/dockerfile

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

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

7-
USER mario
7+
RUN bash -c 'echo bar >> /tmp/foo.txt'
88

99
RUN apt-get update && apt-get install -y vim
1010

11+
USER mario
12+
1113
CMD catalina.sh run

build/tester.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) {
172172
}
173173
test += "-f " + command.Args[2]
174174
ephemeral.Args = append(ephemeral.Args, test)
175-
176-
case "CURRENT_USER_IS":
175+
176+
case "CURRENT_USER_IS":
177177
if len(command.Args) != 3 {
178178
return nil, fmt.Errorf("Condition %s accept one and only one argument (found %d)", "CURRENT_USER_IS", len(command.Args)-2)
179179
}
@@ -182,7 +182,7 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) {
182182
if command.Args[0] == commands.AssertFalse {
183183
test += "! "
184184
}
185-
test += "$(whoami) = \"" + command.Args[2] + "\""
185+
test += "$(whoami) = \"" + command.Args[2] + "\""
186186
ephemeral.Args = append(ephemeral.Args, test)
187187

188188
case "IS_INSTALLED":
@@ -194,12 +194,36 @@ func Assert2Ephemeral(command *parser.Command) (*parser.Command, error) {
194194
if command.Args[0] == commands.AssertFalse {
195195
test += "! "
196196
}
197-
test += isInstalledGeneric(command.Args[2])
197+
test += isInstalledGeneric(command.Args[2])
198+
ephemeral.Args = append(ephemeral.Args, test)
199+
200+
case "IS_RUNNING":
201+
if len(command.Args) != 3 {
202+
return nil, fmt.Errorf("Condition %s accept one and only one argument (found %d)", "IS_RUNNING", len(command.Args)-2)
203+
}
204+
ephemeral.Args = append(ephemeral.Args, "bash", "-c")
205+
test := "pidof "
206+
if command.Args[0] == commands.AssertFalse {
207+
test += "! "
208+
}
209+
test += command.Args[2]
198210
ephemeral.Args = append(ephemeral.Args, test)
199211

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+
200224

201225
default:
202-
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")
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")
203227
}
204228

205229
return ephemeral, nil
@@ -222,10 +246,10 @@ func PrintTestsStats(stats *TestStats) {
222246

223247
// func isInstalledDebian(packagename string) string {
224248
// return "\"$(dpkg-query -W -f='${Status}' " +
225-
// packagename +
249+
// packagename +
226250
// ")\" = \"install ok installed\""
227251
// }
228252

229253
func isInstalledGeneric(packagename string) string {
230-
return "command -v \"" + packagename + "\" 1>/dev/null 2>&1"
254+
return "command -v \"" + packagename + "\" 1>/dev/null 2>&1"
231255
}

build/tester_test.go

+12-9
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 != 7 {
29-
t.Errorf("Expected 7 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,15 +40,18 @@ 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{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"root\""}},
44-
{Args: []string{"USER", "mario"}},
45-
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"mario\""}},
46-
// {Args: []string{"EPHEMERAL", "bash", "-c", "test ! \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
47-
{Args: []string{"EPHEMERAL", "bash", "-c", "! command -v \"vim\" 1>/dev/null 2>&1"}},
43+
{Args: []string{"RUN", "bash", "-c", "echo bar >> /tmp/foo.txt"}},
44+
{Args: []string{"EPHEMERAL", "bash", "-c", "grep bar /tmp/foo.txt"}},
45+
// {Args: []string{"EPHEMERAL", "bash", "-c", "test ! \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
46+
{Args: []string{"EPHEMERAL", "bash", "-c", "! command -v \"vim\" 1>/dev/null 2>&1"}},
4847
{Args: []string{"RUN", "apt-get", "update", "&&", "apt-get", "install", "-y", "vim"}},
49-
{Args: []string{"EPHEMERAL", "bash", "-c", "command -v \"vim\" 1>/dev/null 2>&1"}},
50-
// {Args: []string{"EPHEMERAL", "bash", "-c", "test \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
48+
{Args: []string{"EPHEMERAL", "bash", "-c", "command -v \"vim\" 1>/dev/null 2>&1"}},
49+
// {Args: []string{"EPHEMERAL", "bash", "-c", "test \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
50+
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"root\""}},
51+
{Args: []string{"USER", "mario"}},
52+
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"mario\""}},
5153
{Args: []string{"CMD", "catalina.sh", "run"}},
54+
{Args: []string{"EPHEMERAL", "bash", "-c", "pidof java"}},
5255
}
5356

5457
const (

build/testfile

+11-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ 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+
14+
@BEFORE RUN_APT
15+
ASSERT_FALSE IS_INSTALLED 'vim'
16+
17+
@AFTER RUN_APT
18+
ASSERT_TRUE IS_INSTALLED 'vim'
19+
1120
@BEFORE USER_MARIO
1221
ASSERT_TRUE CURRENT_USER_IS 'root'
1322

1423
@AFTER USER_MARIO
1524
ASSERT_TRUE CURRENT_USER_IS 'mario'
1625

17-
@BEFORE RUN_APT
18-
ASSERT_FALSE IS_INSTALLED 'vim'
19-
20-
@AFTER RUN_APT
21-
ASSERT_TRUE IS_INSTALLED 'vim'
26+
@AFTER CMD_CATALINA
27+
ASSERT_TRUE IS_RUNNING 'java'

0 commit comments

Comments
 (0)