diff --git a/_modules/paas_docker.py b/_modules/paas_docker.py index 004758e..2088c61 100644 --- a/_modules/paas_docker.py +++ b/_modules/paas_docker.py @@ -1,29 +1,50 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------- # Salt — PaaS Docker execution module # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Project: Nasqueron # Created: 2018-10-07 # Description: Functions related to data format conversions # License: BSD-2-Clause # ------------------------------------------------------------- def get_image(default_image, args): """ A function to output a pillar key in JSON. State Example:: {% image = salt['paas_docker.get_image']("nasqueron/mysql", container) %} """ image = default_image if 'image' in args: image = args['image'] if 'version' in args: image += ":" + str(args['version']) return image + + +def get_subnets(): + """ + A function to get the Docker subnets list. + + CLI Example: + + salt * paas_docker.get_subnets + """ + try: + networks = __pillar__['docker_networks'][__grains__['id']] + except KeyError: + networks = {} + + # Defined Docker subnet + subnets = [network["subnet"] for network in networks.values()] + # Default Docker subnet + subnets.append("172.17.0.0/16") + + return subnets diff --git a/_tests/data/paas_docker.yaml b/_tests/data/paas_docker.yaml new file mode 100644 index 0000000..937c27e --- /dev/null +++ b/_tests/data/paas_docker.yaml @@ -0,0 +1,7 @@ +docker_networks: + egladil: + cd: + subnet: 172.18.1.0/24 + ci: + subnet: 172.18.2.0/24 + voidserver: {} diff --git a/_tests/modules/test_paas_docker.py b/_tests/modules/test_paas_docker.py index fc4ad1b..5e5143a 100644 --- a/_tests/modules/test_paas_docker.py +++ b/_tests/modules/test_paas_docker.py @@ -1,41 +1,62 @@ from importlib.machinery import SourceFileLoader import unittest salt_test_case = SourceFileLoader('salt_test_case', "salt_test_case.py").load_module() docker = SourceFileLoader('docker', '../_modules/paas_docker.py').load_module() class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase): + def setUp(self): + self.initialize_mocks() + self.instance = docker + + self.mock_pillar('data/paas_docker.yaml') + + self.mock_grains() + self.grains['id'] = 'egladil' + def test_get_image(self): container = { "image": "foo", "version": "42" } self.assertEqual("foo:42", docker.get_image("not_foo", container)) def test_get_image_without_version(self): container = { "image": "foo", } self.assertEqual("foo", docker.get_image("not_foo", container)) def test_get_image_without_image(self): container = { "version": "42" } self.assertEqual("not_foo:42", docker.get_image("not_foo", container)) def test_get_image_without_anything(self): self.assertEqual("not_foo", docker.get_image("not_foo", {})) def test_get_image_with_numeric_version(self): container = { "image": "foo", "version": 2.5 } self.assertEqual("foo:2.5", docker.get_image("not_foo", container)) + + def test_get_subnets(self): + expected = ['172.18.1.0/24', '172.18.2.0/24', '172.17.0.0/16'] + + self.assertEqual(expected, docker.get_subnets()) + + def test_get_subnets_when_none_are_defined(self): + # Only the default Docker one + expected = ['172.17.0.0/16'] + + self.grains['id'] = 'voidserver' + self.assertEqual(expected, docker.get_subnets()) diff --git a/roles/paas-docker/docker/firewall.sls b/roles/paas-docker/docker/firewall.sls new file mode 100644 index 0000000..e69de29