Skip to content

Commit

Permalink
Add ability to specify multiple container networks AND provider instance
Browse files Browse the repository at this point in the history
This change allows users to use a comma-separated list in the container
network settings. This will then expand to pass each network on
container creation. If any of the networks aren't found, then an
exception will be raised.

I also threw in the ability to specify an alternate provider instance to
load when doing most broker actions, like checkout.
  • Loading branch information
JacobCallahan committed Aug 5, 2024
1 parent 414f942 commit 484dc0a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
11 changes: 6 additions & 5 deletions broker/binds/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ def image_info(self, name):
def create_container(self, image, command=None, **kwargs):
"""Create and return running container instance."""
if net_name := settings.container.network:
if not self.get_network_by_attrs({"name": net_name}):
raise UserError(
f"Network '{settings.container.network}' not found on container host."
)
kwargs["networks"] = {net_name: {"NetworkId": net_name}}
net_dict = {}
for name in net_name.split(","):
if not self.get_network_by_attrs({"name": name}):
raise UserError(f"Network '{name}' not found on container host.")
net_dict[name] = {"NetworkId": name}
kwargs["networks"] = net_dict
kwargs = self._sanitize_create_args(kwargs)
return self.client.containers.create(image, command, **kwargs)

Expand Down
19 changes: 15 additions & 4 deletions tests/providers/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ def pull_image(tag_name):

def create_container(self, container_host, **kwargs):
if net_name := settings.container.network:
if not self.get_network_by_attrs({"name": net_name}):
raise Exception(f"Network '{settings.container.network}' not found on container host.")
kwargs["networks"] = {net_name: {"NetworkId": net_name}}
net_dict = {}
for name in net_name.split(","):
if not self.get_network_by_attrs({"name": name}):
raise Exception(
f"Network '{name}' not found on container host."
)
net_dict[name] = {"NetworkId": name}
kwargs["networks"] = net_dict
with open("tests/data/container/fake_containers.json") as container_file:
container_data = json.load(container_file)
image_data = self.pull_image(container_host)
Expand Down Expand Up @@ -93,12 +98,18 @@ def test_host_creation(container_stub):
assert host.hostname == "f37d3058317f"


def test_ipv6_host_creation(container_stub):
def test_single_network(container_stub):
settings.container.network = "podman2"
cont = container_stub.run_container(container_host="ch-d:ubi8")
assert cont.kwargs["networks"] == {'podman2': {'NetworkId': 'podman2'}}


def test_multiple_networks(container_stub):
settings.container.network = "podman1,podman2"
cont = container_stub.run_container(container_host="ch-d:ubi8")
assert cont.kwargs["networks"] == {'podman1': {'NetworkId': 'podman1'}, 'podman2': {'NetworkId': 'podman2'}}


def test_image_lookup_failure(container_stub):
with pytest.raises(Broker.ProviderError) as err:
container_stub.run_container(container_host="this-does-not-exist")
Expand Down
7 changes: 7 additions & 0 deletions tests/test_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def test_full_init():
assert broker_inst._kwargs["test_action"] == "blank"


def test_specified_instance():
"""Make sure that a specified instance is used"""
broker_inst = Broker(nick="test_nick", TestProvider="test2")
host_checkout = broker_inst.checkout()
assert host_checkout._broker_provider_instance == "test2"


def test_broker_e2e():
"""Run through the base functionality of broker"""
broker_inst = Broker(nick="test_nick")
Expand Down

0 comments on commit 484dc0a

Please sign in to comment.