Skip to content

Commit

Permalink
Add tests for multiple ports
Browse files Browse the repository at this point in the history
  • Loading branch information
JayH5 committed Oct 27, 2015
1 parent d8d3927 commit f7c360c
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions consular/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,69 @@ def test_TASK_RUNNING_no_ports(self):
'status': 'ok'
})

@inlineCallbacks
def test_TASK_RUNNING_multiple_ports(self):
"""
When a TASK_RUNNING event is received from Marathon, and the task has
multiple ports, the task should be registered as a service in Consul
with the lowest port.
"""
d = self.request('POST', '/events', {
"eventType": "status_update_event",
"timestamp": "2014-03-01T23:29:30.158Z",
"slaveId": "20140909-054127-177048842-5050-1494-0",
"taskId": "my-app_0-1396592784349",
"taskStatus": "TASK_RUNNING",
"appId": "/my-app",
"host": "slave-1234.acme.org",
"ports": [4567, 1234, 6789],
"version": "2014-04-04T06:26:23.051Z"
})

# Store the task as a service in Consul with the lowest port
consul_request = yield self.requests.get()
self.assertEqual(consul_request['method'], 'PUT')
self.assertEqual(
consul_request['url'],
'http://slave-1234.acme.org:8500/v1/agent/service/register')
self.assertEqual(consul_request['data'], json.dumps({
'Name': 'my-app',
'ID': 'my-app_0-1396592784349',
'Address': 'slave-1234.acme.org',
'Port': 1234,
'Tags': [
'consular-reg-id=test',
'consular-app-id=/my-app',
],
}))
consul_request['deferred'].callback(
FakeResponse(200, [], json.dumps({})))

# We should get the app info for the event
marathon_app_request = yield self.requests.get()
self.assertEqual(marathon_app_request['method'], 'GET')
self.assertEqual(marathon_app_request['url'],
'http://localhost:8080/v2/apps/my-app')
marathon_app_request['deferred'].callback(
FakeResponse(200, [], json.dumps({
'app': {
'id': '/my-app',
}
})))

# Check if any existing labels stored in Consul
consul_kv_request = yield self.requests.get()
self.assertEqual(consul_kv_request['method'], 'GET')
self.assertEqual(consul_kv_request['url'],
'http://localhost:8500/v1/kv/consular/my-app?keys=')
consul_kv_request['deferred'].callback(
FakeResponse(200, [], json.dumps([])))

response = yield d
self.assertEqual((yield response.json()), {
'status': 'ok'
})

@inlineCallbacks
def test_TASK_KILLED(self):
d = self.request('POST', '/events', {
Expand Down Expand Up @@ -626,6 +689,53 @@ def test_sync_app_tasks_no_ports(self):
FakeResponse(200, [], json.dumps({})))
yield d

@inlineCallbacks
def test_sync_app_tasks_multiple_ports(self):
"""
When syncing an app with a task with multiple ports, Consul is updated
with a service entry for the task with the lowest port.
"""
d = self.consular.sync_app_tasks({'id': '/my-app'})

# First Consular fetches the tasks for the app
marathon_request = yield self.requests.get()
self.assertEqual(marathon_request['method'], 'GET')
self.assertEqual(
marathon_request['url'],
'http://localhost:8080/v2/apps/my-app/tasks')

# Respond with one task
marathon_request['deferred'].callback(
FakeResponse(200, [], json.dumps({
'tasks': [
{
'id': 'my-task-id',
'host': '0.0.0.0',
'ports': [4567, 1234, 6789]
}
]}))
)

# Consular should register the task in Consul with the lowest port
consul_request = yield self.requests.get()
self.assertEqual(
consul_request['url'],
'http://0.0.0.0:8500/v1/agent/service/register')
self.assertEqual(consul_request['data'], json.dumps({
'Name': 'my-app',
'ID': 'my-task-id',
'Address': '0.0.0.0',
'Port': 1234,
'Tags': [
'consular-reg-id=test',
'consular-app-id=/my-app',
],
}))
self.assertEqual(consul_request['method'], 'PUT')
consul_request['deferred'].callback(
FakeResponse(200, [], json.dumps({})))
yield d

@inlineCallbacks
def test_sync_app_labels(self):
app = {
Expand Down

0 comments on commit f7c360c

Please sign in to comment.