Skip to content

Commit c475dfa

Browse files
committed
Merge pull request #1 from xinity/check_isrunning
ADD IS_RUNNING
2 parents cca0150 + e28dcd0 commit c475dfa

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

build/dockerfile

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

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

7-
USER mario
8-
97
RUN apt-get update && apt-get install -y vim
108

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

@@ -40,15 +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\""}},
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{"EPHEMERAL", "bash", "-c", "test ! \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
44+
{Args: []string{"EPHEMERAL", "bash", "-c", "! command -v \"vim\" 1>/dev/null 2>&1"}},
4845
{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\""}},
46+
{Args: []string{"EPHEMERAL", "bash", "-c", "command -v \"vim\" 1>/dev/null 2>&1"}},
47+
// {Args: []string{"EPHEMERAL", "bash", "-c", "test \"$(dpkg-query -W -f='${Status}' vim)\" = \"install ok installed\""}},
48+
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"root\""}},
49+
{Args: []string{"USER", "mario"}},
50+
{Args: []string{"EPHEMERAL", "bash", "-c", "test $(whoami) = \"mario\""}},
5151
{Args: []string{"CMD", "catalina.sh", "run"}},
52+
{Args: []string{"EPHEMERAL", "bash", "-c", "pidof java"}},
5253
}
5354

5455
const (

build/testfile

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ 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+
@BEFORE RUN_APT
12+
ASSERT_FALSE IS_INSTALLED 'vim'
13+
14+
@AFTER RUN_APT
15+
ASSERT_TRUE IS_INSTALLED 'vim'
16+
1117
@BEFORE USER_MARIO
1218
ASSERT_TRUE CURRENT_USER_IS 'root'
1319

1420
@AFTER USER_MARIO
1521
ASSERT_TRUE CURRENT_USER_IS 'mario'
1622

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

0 commit comments

Comments
 (0)