1
0
mirror of https://github.com/Tecnativa/docker-socket-proxy synced 2025-01-20 12:41:07 +00:00
docker-socket-proxy/tests/test_service.py

63 lines
2.2 KiB
Python
Raw Normal View History

import logging
2020-11-25 09:19:44 +00:00
import pytest
2020-12-02 10:49:50 +00:00
from plumbum import ProcessExecutionError
from plumbum.cmd import docker
2020-12-02 10:49:50 +00:00
from .conftest import proxy
2020-12-02 10:49:50 +00:00
logger = logging.getLogger()
2020-12-02 10:49:50 +00:00
def test_default_permissions(sleeping_container):
allowed_calls = (("version",),)
forbidden_calls = (
("pull", "alpine"),
("--rm", "alpine", "--name", sleeping_container),
("logs", sleeping_container),
("wait", sleeping_container),
("rm", "-f", sleeping_container),
("restart", sleeping_container),
("network", "ls"),
("config", "ls"),
("service", "ls"),
("stack", "ls"),
("secret", "ls"),
("plugin", "ls"),
("info",),
("system", "info"),
("build", "."),
("swarm", "init"),
)
2020-12-02 10:49:50 +00:00
with proxy():
for args in allowed_calls:
docker(*args)
for args in forbidden_calls:
with pytest.raises(ProcessExecutionError):
docker(*args)
2020-11-09 16:34:02 +00:00
def test_container_permissions():
with _docker_proxy(CONTAINERS=1) as (docker, test_container):
_query_docker_with_proxy("logs", test_container, allowed=True)
_query_docker_with_proxy("inspect", test_container, allowed=True)
_query_docker_with_proxy("wait", test_container, allowed=False)
_query_docker_with_proxy("run", "--rm", "alpine", allowed=False)
_query_docker_with_proxy("rm", "-f", test_container, allowed=False)
_query_docker_with_proxy("restart", test_container, allowed=False)
2020-11-09 16:34:02 +00:00
def test_post_permissions():
with _docker_proxy(POST=1) as (docker, test_container):
_query_docker_with_proxy("rm", "-f", test_container, allowed=False)
_query_docker_with_proxy("pull", "alpine", allowed=False)
_query_docker_with_proxy("run", "--rm", "alpine", allowed=False)
_query_docker_with_proxy("network", "create", "foobar", allowed=False)
2020-11-09 16:34:02 +00:00
def test_network_post_permissions():
with _docker_proxy(POST=1, NETWORKS=1) as (docker, test_container):
_query_docker_with_proxy("network", "ls", allowed=True)
_query_docker_with_proxy("network", "create", "foo", allowed=True)
_query_docker_with_proxy("network", "rm", "foo", allowed=True)