Skip to content

Commit 5ba3fa6

Browse files
committed
make test generic
Signed-off-by: Mai Bui <[email protected]>
1 parent 28f3c99 commit 5ba3fa6

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

tests/container_hardening/test_container_hardening.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
logger = logging.getLogger(__name__)
1010

1111

12-
def test_bgp_dev(duthost):
12+
def test_container_privileged(duthost):
1313
"""
14-
Test bgp container has no access to /dev/vda* or /dev/sda*
14+
Test container without --privileged flag has no access to /dev/vda* or /dev/sda*
1515
"""
16-
cmd = duthost.shell("docker exec bgp bash -c 'df -h | grep /etc/hosts' | awk '{print $1}'")
17-
rc, device = cmd['rc'], cmd['stdout']
18-
if rc != 0:
19-
pytest_assert(False, 'Failed to get the device name.')
20-
if not device.startswith('/dev/'):
21-
pytest_assert(False, 'Invalid device {}.'.format(device))
22-
output = duthost.shell("docker exec bgp bash -c 'ls {}'".format(device), module_ignore_errors=True)['stdout']
23-
pytest_assert(not output, 'The partition {} exists.'.format(device))
16+
container_names = duthost.shell(r'docker ps -a --format \{\{.Names\}\}')['stdout_lines']
17+
for container_name in container_names:
18+
if container_name == 'bgp':
19+
docker_exec_cmd = 'docker exec {} bash -c '.format(container_name)
20+
cmd = duthost.shell(docker_exec_cmd + "'df -h | grep /etc/hosts' | awk '{print $1}'")
21+
rc, device = cmd['rc'], cmd['stdout']
22+
pytest_assert(rc == 0, 'Failed to get the device name.')
23+
pytest_assert(device.startswith('/dev/'), 'Invalid device {}.'.format(device))
24+
output = duthost.shell(docker_exec_cmd + "'ls {}'".format(device), module_ignore_errors=True)['stdout']
25+
pytest_assert(not output, 'The partition {} exists.'.format(device))

0 commit comments

Comments
 (0)