Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 1aeece1

Browse files
wait for service containers to come up on their designated tcp socket (#304)
* wait for service containers to come up on their designated tcp socket * use container_ip in integration tests
1 parent 6da2958 commit 1aeece1

File tree

9 files changed

+42
-26
lines changed

9 files changed

+42
-26
lines changed

tests/helpers/assertions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import socket
34

45

56
def has_datapoint_with_metric_name(fake_services, metric_name):
@@ -81,3 +82,11 @@ def any_metric_has_any_dim_key(fake_services, metrics, dim_keys):
8182
print("Found metric \"%s\" with dimension key \"%s\"." % (dp.metric, dim.key))
8283
return True
8384
return False
85+
86+
87+
def tcp_socket_open(host, port):
88+
"""
89+
Returns True if there is an open TCP socket at the given host/port
90+
"""
91+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
92+
return sock.connect_ex((host, port)) == 0

tests/monitors/apache_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_service
6+
from tests.helpers.util import wait_for, run_agent, run_service, container_ip
77
from tests.helpers.assertions import *
88

99
apache_config = string.Template("""
@@ -15,7 +15,9 @@
1515

1616
def test_apache():
1717
with run_service("apache") as apache_container:
18-
config = apache_config.substitute(host=apache_container.attrs["NetworkSettings"]["IPAddress"])
18+
host = container_ip(apache_container)
19+
config = apache_config.substitute(host=host)
20+
assert wait_for(p(tcp_socket_open, host, 80), 60), "service didn't start"
1921

2022
with run_agent(config) as [backend, _, _]:
2123
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "apache")), "Didn't get apache datapoints"

tests/monitors/cassandra_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_service
6+
from tests.helpers.util import wait_for, run_agent, run_service, container_ip
77
from tests.helpers.assertions import *
88

99
cassandra_config = string.Template("""
@@ -17,7 +17,7 @@
1717

1818
def test_cassandra():
1919
with run_service("cassandra") as cassandra_cont:
20-
config = cassandra_config.substitute(host=cassandra_cont.attrs["NetworkSettings"]["IPAddress"])
20+
config = cassandra_config.substitute(host=container_ip(cassandra_cont))
2121

2222
# Wait for the JMX port to be open in the container
2323
assert wait_for(p(container_cmd_exit_0, cassandra_cont,

tests/monitors/couchbase_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import string
33

44
from tests.helpers import fake_backend
5-
from tests.helpers.util import wait_for, run_agent, run_service
6-
from tests.helpers.assertions import has_datapoint_with_dim
5+
from tests.helpers.util import wait_for, run_agent, run_service, container_ip
6+
from tests.helpers.assertions import has_datapoint_with_dim, tcp_socket_open
77

88
couchbase_config = string.Template("""
99
monitors:
@@ -18,8 +18,10 @@
1818

1919
def test_couchbase():
2020
with run_service("couchbase", hostname="node1.cluster") as couchbase_container:
21-
config = couchbase_config.substitute(host=couchbase_container.attrs["NetworkSettings"]["IPAddress"])
21+
host = container_ip(couchbase_container)
22+
config = couchbase_config.substitute(host=host)
23+
assert wait_for(p(tcp_socket_open, host, 8091), 60), "service didn't start"
2224

2325
with run_agent(config) as [backend, _, _]:
24-
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "couchbase"), 40), \
26+
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "couchbase")), \
2527
"Didn't get couchbase datapoints"

tests/monitors/etcd_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_container
6+
from tests.helpers.util import wait_for, run_agent, run_container, container_ip
77
from tests.helpers.assertions import *
88

99

@@ -19,7 +19,9 @@
1919

2020
def test_etcd_monitor():
2121
with run_container("quay.io/coreos/etcd:v2.3.8", command=ETCD_COMMAND) as etcd_cont:
22-
config = etcd_config.substitute(host=etcd_cont.attrs["NetworkSettings"]["IPAddress"])
22+
host = container_ip(etcd_cont)
23+
config = etcd_config.substitute(host=host)
24+
assert wait_for(p(tcp_socket_open, host, 2379), 60), "service didn't start"
2325

2426
with run_agent(config) as [backend, _, _]:
2527
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "etcd")), "Didn't get etcd datapoints"

tests/monitors/mongodb_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_container
6+
from tests.helpers.util import wait_for, run_agent, run_container, container_ip
77
from tests.helpers.assertions import *
88

99
mongo_config = string.Template("""
@@ -16,7 +16,9 @@
1616

1717
def test_mongo():
1818
with run_container("mongo:3.6") as mongo_cont:
19-
config = mongo_config.substitute(host=mongo_cont.attrs["NetworkSettings"]["IPAddress"])
19+
host = container_ip(mongo_cont)
20+
config = mongo_config.substitute(host=host)
21+
assert wait_for(p(tcp_socket_open, host, 27017), 60), "service didn't start"
2022

2123
with run_agent(config) as [backend, _, _]:
2224
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "mongo")), "Didn't get mongo datapoints"

tests/monitors/nginx_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_service
6+
from tests.helpers.util import wait_for, run_agent, run_service, container_ip
77
from tests.helpers.assertions import *
88

99
nginx_config = string.Template("""
@@ -15,7 +15,9 @@
1515

1616
def test_nginx():
1717
with run_service("nginx") as nginx_container:
18-
config = nginx_config.substitute(host=nginx_container.attrs["NetworkSettings"]["IPAddress"])
18+
host = container_ip(nginx_container)
19+
config = nginx_config.substitute(host=host)
20+
assert wait_for(p(tcp_socket_open, host, 80), 60), "service didn't start"
1921

2022
with run_agent(config) as [backend, _, _]:
2123
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "nginx")), "Didn't get nginx datapoints"

tests/monitors/postgresql_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_container
6+
from tests.helpers.util import wait_for, run_agent, run_container, container_ip
77
from tests.helpers.assertions import *
88

99
config_temp = string.Template("""
@@ -41,7 +41,9 @@
4141

4242
def test_postgresql():
4343
with run_container("postgres:10", environment=env) as cont:
44-
config = config_temp.substitute(host=cont.attrs["NetworkSettings"]["IPAddress"])
44+
host = container_ip(cont)
45+
config = config_temp.substitute(host=host)
46+
assert wait_for(p(tcp_socket_open, host, 5432), 60), "service didn't start"
4547

4648
with run_agent(config) as [backend, _, _]:
4749
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "postgresql")), "Didn't get postgresql datapoints"

tests/monitors/rabbitmq_test.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
import string
44

55
from tests.helpers import fake_backend
6-
from tests.helpers.util import wait_for, run_agent, run_container
6+
from tests.helpers.util import wait_for, run_agent, run_container, container_ip
77
from tests.helpers.assertions import *
88

9-
def wait_for_rabbit_to_start(cont):
10-
# 3D38 is 15672 in hex, the port we need to be listening
11-
assert wait_for(p(container_cmd_exit_0, cont, "sh -c 'cat /proc/net/tcp | grep 3D38'"), 40), "rabbitmq didn't start"
12-
13-
149
rabbitmq_config = string.Template("""
1510
monitors:
1611
- type: collectd/rabbitmq
@@ -24,9 +19,9 @@ def wait_for_rabbit_to_start(cont):
2419

2520
def test_rabbitmq():
2621
with run_container("rabbitmq:3.6-management") as rabbitmq_cont:
27-
host = rabbitmq_cont.attrs["NetworkSettings"]["IPAddress"]
22+
host = container_ip(rabbitmq_cont)
2823
config = rabbitmq_config.substitute(host=host)
29-
wait_for_rabbit_to_start(rabbitmq_cont)
24+
assert wait_for(p(tcp_socket_open, host, 15672), 60), "service didn't start"
3025

3126
with run_agent(config) as [backend, _, _]:
3227
assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "rabbitmq")), "Didn't get rabbitmq datapoints"
@@ -35,9 +30,9 @@ def test_rabbitmq():
3530

3631
def test_rabbitmq_broker_name():
3732
with run_container("rabbitmq:3.6-management") as rabbitmq_cont:
38-
host = rabbitmq_cont.attrs["NetworkSettings"]["IPAddress"]
33+
host = container_ip(rabbitmq_cont)
3934
config = rabbitmq_config.substitute(host=host)
40-
wait_for_rabbit_to_start(rabbitmq_cont)
35+
assert wait_for(p(tcp_socket_open, host, 15672), 60), "service didn't start"
4136

4237
with run_agent("""
4338
monitors:

0 commit comments

Comments
 (0)