Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from testcases import AutopilotPatternTest, WaitTimeoutError, \
dump_environment_to_file
import requests

from IPy import IP


class NginxStackTest(AutopilotPatternTest):
Expand Down Expand Up @@ -52,17 +52,23 @@ def test_scaleup_and_down(self):
self.instrument(self.wait_for_containers,
{'backend': 2, "nginx": 1, "consul": 1}, timeout=300)
self.instrument(self.wait_for_service, 'backend', count=2, timeout=60)
self.compare_backends()

# netsplit a backend
# get our expected IP addresses
_, backend1_ip = self.get_ips('backend_1')
_, backend2_ip = self.get_ips('backend_2')

# make sure nginx has both of them
self.compare_backends([backend1_ip, backend2_ip])

# netsplit a backend and make sure nginx converges
self.docker_exec('backend_2', 'ifconfig eth0 down')
self.instrument(self.wait_for_service, 'backend', count=1)
self.compare_backends()
self.compare_backends([backend1_ip])

# heal netsplit
# heal netsplit and make sure nginx converges
self.docker_exec('backend_2', 'ifconfig eth0 up')
self.instrument(self.wait_for_service, 'backend', count=2, timeout=60)
self.compare_backends()
self.compare_backends([backend1_ip, backend2_ip])

def wait_for_containers(self, expected={}, timeout=30):
"""
Expand Down Expand Up @@ -100,28 +106,35 @@ def wait_for_cns(self, timeout=60):
if r.status_code == 200:
break
except requests.exceptions.ConnectionError:
pass
timeout -= 1
time.sleep(1)
timeout -= 1
time.sleep(1)
else:
self.fail("nginx has not become reachable at {}"
.format(self.nginx_cns))

def get_possible_backend_ips(self, timeout=60):
_, ips = self.get_service_ips('backend')
ips.sort()
return ips

def compare_backends(self):
expected = self.get_service_instances_from_consul('backend').sort()
actual = list(set([self.query_for_backend() for _ in range(10)])).sort()
self.assertEqual(expected, actual,
'Expected {} but got {} for Nginx backends'
.format(expected, actual))

def query_for_backend(self):
r = requests.get('http://{}'.format(self.nginx_cns)) # TODO: SSL
if r.status_code != 200:
self.fail('Expected 200 OK but got {}'.format(r.status_code))
return r.text.strip('HelloWorld\n ')

def compare_backends(self, expected, timeout=60):
expected.sort()
patt = 'server \d{2,3}\.\d{2,3}\.\d{2,3}\.\d{2,3}\:3001;'
while timeout > 0:
conf = self.docker_exec('nginx_1',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a reliable way to get the site.conf... will 'nginx_1' always exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, but if nginx_1 doesn't exist at this point, the test should fail anyways.

'cat /etc/nginx/conf.d/site.conf')
actual = re.findall(patt, conf)
actual = [IP(a.replace('server', '').replace(':3001;', '').strip())
for a in actual]
actual.sort()
if actual == expected:
break

timeout -= 1
time.sleep(1)
else:
self.fail("expected {} but got {} for Nginx backends"
.format(expected, actual))


if __name__ == "__main__":
Expand Down