Skip to content

Commit 8620c02

Browse files
committed
Merge pull request #7 from xinity/check_contains
Implement FILE_CONTAINS template
2 parents cca0150 + 741d9a0 commit 8620c02

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
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

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ COPY words /usr/local/tomcat/webapps/
66

77
USER mario
88

9+
RUN bash -c 'echo bar >> /tmp/foo.txt'
10+
911
RUN apt-get update && apt-get install -y vim
1012

1113
CMD catalina.sh run

build/tester.go

+19-8
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,23 @@ 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])
198198
ephemeral.Args = append(ephemeral.Args, test)
199-
199+
200+
case "FILE_CONTAINS":
201+
if len(command.Args) != 4 {
202+
return nil, fmt.Errorf("Condition %s accept two and only two argument (found %d)", "FILE_CONTAINS", len(command.Args)-3)
203+
}
204+
ephemeral.Args = append(ephemeral.Args, "bash", "-c")
205+
test := "grep -q "
206+
if command.Args[0] == commands.AssertFalse {
207+
test += "! "
208+
}
209+
test += command.Args[2] + " " + command.Args[3]
210+
ephemeral.Args = append(ephemeral.Args, test)
200211

201212
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")
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", "FILE_CONTAINS")
203214
}
204215

205216
return ephemeral, nil
@@ -222,10 +233,10 @@ func PrintTestsStats(stats *TestStats) {
222233

223234
// func isInstalledDebian(packagename string) string {
224235
// return "\"$(dpkg-query -W -f='${Status}' " +
225-
// packagename +
236+
// packagename +
226237
// ")\" = \"install ok installed\""
227238
// }
228239

229240
func isInstalledGeneric(packagename string) string {
230-
return "command -v \"" + packagename + "\" 1>/dev/null 2>&1"
241+
return "command -v \"" + packagename + "\" 1>/dev/null 2>&1"
231242
}

build/tester_test.go

+10-8
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 != 8 {
29+
t.Errorf("Expected 8 blocks, found %d", blockNum)
3030
}
3131
}
3232

@@ -40,14 +40,16 @@ 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\""}},
43+
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"root\""}},
4444
{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"}},
45+
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"mario\""}},
46+
{Args: []string{"RUN", "bash", "-c", "echo bar >> /tmp/foo.txt"}},
47+
{Args: []string{"EPHEMERAL", "bash", "-c", "grep -q bar /tmp/foo.txt"}},
48+
// {Args: []string{"EPHEMERAL", "bash", "-c", "test ! \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
49+
{Args: []string{"EPHEMERAL", "bash", "-c", "! command -v \"vim\" 1>/dev/null 2>&1"}},
4850
{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\""}},
51+
{Args: []string{"EPHEMERAL", "bash", "-c", "command -v \"vim\" 1>/dev/null 2>&1"}},
52+
// {Args: []string{"EPHEMERAL", "bash", "-c", "test \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
5153
{Args: []string{"CMD", "catalina.sh", "run"}},
5254
}
5355

build/testfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ ASSERT_TRUE CURRENT_USER_IS 'root'
1414
@AFTER USER_MARIO
1515
ASSERT_TRUE CURRENT_USER_IS 'mario'
1616

17+
@AFTER RUN_BASH
18+
ASSERT_TRUE CONTAINS 'bar' '/tmp/foo.txt'
19+
1720
@BEFORE RUN_APT
1821
ASSERT_FALSE IS_INSTALLED 'vim'
1922

2023
@AFTER RUN_APT
21-
ASSERT_TRUE IS_INSTALLED 'vim'
24+
ASSERT_TRUE IS_INSTALLED 'vim'
25+

0 commit comments

Comments
 (0)