Inspect only the containers carrying the lookup label
This commit is contained in:
parent
8bd9f06f12
commit
e7a0f7b930
2 changed files with 52 additions and 0 deletions
|
|
@ -26,6 +26,9 @@ class Docker(ProcessorInterface):
|
||||||
|
|
||||||
self.parsed_object = {}
|
self.parsed_object = {}
|
||||||
for container in self.client.containers.list():
|
for container in self.client.containers.list():
|
||||||
|
if not any(self.label in key for key in container.labels):
|
||||||
|
continue
|
||||||
|
|
||||||
# Issue 32 - Docker container cannot connect to containers in different network.
|
# Issue 32 - Docker container cannot connect to containers in different network.
|
||||||
if ha_proxy_network_name not in container.attrs["NetworkSettings"]["Networks"].keys():
|
if ha_proxy_network_name not in container.attrs["NetworkSettings"]["Networks"].keys():
|
||||||
ha_proxy_network.connect(container.name)
|
ha_proxy_network.connect(container.name)
|
||||||
|
|
|
||||||
|
|
@ -105,4 +105,53 @@ def test_processor_docker():
|
||||||
container2.stop()
|
container2.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def test_processor_docker_ignores_unlabeled():
|
||||||
|
try:
|
||||||
|
client = docker.from_env()
|
||||||
|
except docker.errors.DockerException:
|
||||||
|
pytest.skip("There is no docker environment")
|
||||||
|
|
||||||
|
if len(client.containers.list()) > 0:
|
||||||
|
pytest.skip("I cannot run this test with other containers running.")
|
||||||
|
|
||||||
|
network = client.networks.create("test_processor_docker_network", driver="bridge")
|
||||||
|
# The most recent container is inspected first, so the labeled one defines the network to use.
|
||||||
|
unlabeled = client.containers.run("byjg/static-httpserver",
|
||||||
|
name="test_processor_docker_unlabeled",
|
||||||
|
detach=True,
|
||||||
|
auto_remove=True,
|
||||||
|
remove=True)
|
||||||
|
time.sleep(1)
|
||||||
|
container = client.containers.run("byjg/static-httpserver",
|
||||||
|
name="test_processor_docker_labeled",
|
||||||
|
detach=True,
|
||||||
|
auto_remove=True,
|
||||||
|
remove=True,
|
||||||
|
network=network.name,
|
||||||
|
labels={
|
||||||
|
"easyhaproxy.labeled.port": "80",
|
||||||
|
"easyhaproxy.labeled.localport": "8080",
|
||||||
|
"easyhaproxy.labeled.host": "labeled.local",
|
||||||
|
})
|
||||||
|
try:
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
static = ProcessorInterface.factory(ProcessorInterface.DOCKER)
|
||||||
|
|
||||||
|
# The labeled container is served through the network it already belongs to.
|
||||||
|
labeled_ip = client.containers.get(container.name).attrs["NetworkSettings"]["Networks"][network.name][
|
||||||
|
"IPAddress"]
|
||||||
|
assert _get_ip_host(static.get_parsed_object(), "easyhaproxy.labeled") == labeled_ip
|
||||||
|
|
||||||
|
# The container without any label is neither inspected nor connected to the network.
|
||||||
|
unlabeled_networks = client.containers.get(unlabeled.name).attrs["NetworkSettings"]["Networks"]
|
||||||
|
assert list(unlabeled_networks.keys()) == ["bridge"]
|
||||||
|
assert unlabeled_networks["bridge"]["IPAddress"] not in static.get_parsed_object()
|
||||||
|
finally:
|
||||||
|
container.stop()
|
||||||
|
unlabeled.stop()
|
||||||
|
time.sleep(1)
|
||||||
|
network.remove()
|
||||||
|
|
||||||
|
|
||||||
# test_processor_docker()
|
# test_processor_docker()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue