From 71a22c2a3464f14911dce11ab3da0ea8b9d3676b Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 17 Dec 2021 13:42:33 +0100 Subject: [PATCH] outposts: add unittests for docker controller Signed-off-by: Jens Langhammer --- authentik/outposts/controllers/docker.py | 4 +- .../outposts/tests/test_controller_docker.py | 124 ++++++++++++++++++ authentik/root/settings.py | 2 - 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 authentik/outposts/tests/test_controller_docker.py diff --git a/authentik/outposts/controllers/docker.py b/authentik/outposts/controllers/docker.py index ad044880e..daa69321c 100644 --- a/authentik/outposts/controllers/docker.py +++ b/authentik/outposts/controllers/docker.py @@ -110,7 +110,7 @@ class DockerController(BaseController): image = self.get_container_image() try: self.client.images.pull(image) - except DockerException: + except DockerException: # pragma: no cover image = f"goauthentik.io/{self.outpost.type}:latest" self.client.images.pull(image) return image @@ -144,7 +144,7 @@ class DockerController(BaseController): True, ) - def _migrate_container_name(self): + def _migrate_container_name(self): # pragma: no cover """Migrate 2021.9 to 2021.10+""" old_name = f"authentik-proxy-{self.outpost.uuid.hex}" try: diff --git a/authentik/outposts/tests/test_controller_docker.py b/authentik/outposts/tests/test_controller_docker.py new file mode 100644 index 000000000..7d47d1ac4 --- /dev/null +++ b/authentik/outposts/tests/test_controller_docker.py @@ -0,0 +1,124 @@ +"""Docker controller tests""" +from django.test import TestCase +from docker.models.containers import Container + +from authentik.managed.manager import ObjectManager +from authentik.outposts.controllers.base import ControllerException +from authentik.outposts.controllers.docker import DockerController +from authentik.outposts.managed import MANAGED_OUTPOST +from authentik.outposts.models import DockerServiceConnection, Outpost, OutpostType +from authentik.providers.proxy.controllers.docker import ProxyDockerController + + +class DockerControllerTests(TestCase): + """Docker controller tests""" + + def setUp(self) -> None: + self.outpost = Outpost.objects.create( + name="test", + type=OutpostType.PROXY, + ) + self.integration = DockerServiceConnection(name="test") + ObjectManager().run() + + def test_init_managed(self): + """Docker controller shouldn't do anything for managed outpost""" + controller = DockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + self.assertIsNone(controller.up()) + self.assertIsNone(controller.down()) + + def test_init_invalid(self): + """Ensure init fails with invalid client""" + with self.assertRaises(ControllerException): + DockerController(self.outpost, self.integration) + + def test_env_valid(self): + """Test environment check""" + controller = DockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + env = [f"{key}={value}" for key, value in controller._get_env().items()] + container = Container(attrs={"Config": {"Env": env}}) + self.assertFalse(controller._comp_env(container)) + + def test_env_invalid(self): + """Test environment check""" + controller = DockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + container = Container(attrs={"Config": {"Env": []}}) + self.assertTrue(controller._comp_env(container)) + + def test_label_valid(self): + """Test label check""" + controller = DockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + container = Container(attrs={"Config": {"Labels": controller._get_labels()}}) + self.assertFalse(controller._comp_labels(container)) + + def test_label_invalid(self): + """Test label check""" + controller = DockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + container = Container(attrs={"Config": {"Labels": {}}}) + self.assertTrue(controller._comp_labels(container)) + container = Container(attrs={"Config": {"Labels": {"io.goauthentik.outpost-uuid": "foo"}}}) + self.assertTrue(controller._comp_labels(container)) + + def test_port_valid(self): + """Test port check""" + controller = ProxyDockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + container = Container( + attrs={ + "NetworkSettings": { + "Ports": { + "9000/tcp": [{"HostIp": "", "HostPort": "9000"}], + "9443/tcp": [{"HostIp": "", "HostPort": "9443"}], + } + }, + "State": "", + } + ) + with self.settings(TEST=False): + self.assertFalse(controller._comp_ports(container)) + container.attrs["State"] = "running" + self.assertFalse(controller._comp_ports(container)) + + def test_port_invalid(self): + """Test port check""" + controller = ProxyDockerController( + Outpost.objects.filter(managed=MANAGED_OUTPOST).first(), self.integration + ) + container_no_ports = Container( + attrs={"NetworkSettings": {"Ports": None}, "State": "running"} + ) + container_missing_port = Container( + attrs={ + "NetworkSettings": { + "Ports": { + "9443/tcp": [{"HostIp": "", "HostPort": "9443"}], + } + }, + "State": "running", + } + ) + container_mismatched_host = Container( + attrs={ + "NetworkSettings": { + "Ports": { + "9443/tcp": [{"HostIp": "", "HostPort": "123"}], + } + }, + "State": "running", + } + ) + with self.settings(TEST=False): + self.assertFalse(controller._comp_ports(container_no_ports)) + self.assertTrue(controller._comp_ports(container_missing_port)) + self.assertTrue(controller._comp_ports(container_mismatched_host)) diff --git a/authentik/root/settings.py b/authentik/root/settings.py index e22134f7c..f457ea501 100644 --- a/authentik/root/settings.py +++ b/authentik/root/settings.py @@ -345,8 +345,6 @@ TIME_ZONE = "UTC" USE_I18N = True -USE_L10N = True - USE_TZ = True LOCALE_PATHS = ["./locale"]