-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to support better default connect string generation
- Loading branch information
Showing
7 changed files
with
147 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
blueox.ports | ||
~~~~~~~~ | ||
This module provides utilities for generating connect strings. | ||
BlueOx, thanks to it's 0mq roots has a somewhat complex relationship with | ||
ports, which we'd like to abstract from the user as much as possible. | ||
:copyright: (c) 2015 by Rhett Garber | ||
:license: ISC, see LICENSE for more details. | ||
""" | ||
import os | ||
|
||
|
||
ENV_VAR_CONTROL_HOST = 'BLUEOX_CLIENT_HOST' | ||
ENV_VAR_COLLECT_HOST = 'BLUEOX_HOST' | ||
|
||
DEFAULT_HOST = '127.0.0.1' | ||
DEFAULT_CONTROL_PORT = 3513 | ||
DEFAULT_COLLECT_PORT = 3514 | ||
|
||
|
||
def _default_host(host, default_host, default_port): | ||
"""Build a default host string | ||
""" | ||
if not host: | ||
host = default_host | ||
if ':' not in host: | ||
host = "{}:{}".format(host, default_port) | ||
|
||
return host | ||
|
||
|
||
def default_control_host(host=None): | ||
default_host = os.environ.get(ENV_VAR_CONTROL_HOST, DEFAULT_HOST) | ||
return _default_host(host, default_host, DEFAULT_CONTROL_PORT) | ||
|
||
|
||
def default_collect_host(host=None): | ||
default_host = os.environ.get(ENV_VAR_COLLECT_HOST, DEFAULT_HOST) | ||
return _default_host(host, default_host, DEFAULT_COLLECT_PORT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import os | ||
from testify import * | ||
|
||
from blueox import ports | ||
|
||
|
||
class DefaultHostTest(TestCase): | ||
def test_none(self): | ||
host = ports._default_host(None, 'localhost', 123) | ||
|
||
hostname, port = host.split(':') | ||
assert_equal(hostname, 'localhost') | ||
assert_equal(port, '123') | ||
|
||
def test_host(self): | ||
host = ports._default_host('master', 'localhost', 123) | ||
|
||
hostname, port = host.split(':') | ||
assert_equal(hostname, 'master') | ||
assert_equal(port, '123') | ||
|
||
def test_explicit(self): | ||
host = ports._default_host('master:1234', 'localhost', 123) | ||
|
||
hostname, port = host.split(':') | ||
assert_equal(hostname, 'master') | ||
assert_equal(port, '1234') | ||
|
||
|
||
class DefaultControlHost(TestCase): | ||
@teardown | ||
def clear_env(self): | ||
try: | ||
del os.environ['BLUEOX_CLIENT_HOST'] | ||
except KeyError: | ||
pass | ||
|
||
def test_emtpy(self): | ||
host = ports.default_control_host() | ||
assert_equal(host, "127.0.0.1:3513") | ||
|
||
def test_env(self): | ||
os.environ['BLUEOX_CLIENT_HOST'] = 'master' | ||
host = ports.default_control_host() | ||
assert_equal(host, "master:3513") | ||
|
||
def test_env_port(self): | ||
os.environ['BLUEOX_CLIENT_HOST'] = 'master:123' | ||
host = ports.default_control_host() | ||
assert_equal(host, "master:123") | ||
|
||
|
||
class DefaultCollectHost(TestCase): | ||
@teardown | ||
def clear_env(self): | ||
try: | ||
del os.environ['BLUEOX_HOST'] | ||
except KeyError: | ||
pass | ||
|
||
def test_emtpy(self): | ||
host = ports.default_collect_host() | ||
assert_equal(host, "127.0.0.1:3514") | ||
|
||
def test_env(self): | ||
os.environ['BLUEOX_HOST'] = 'master' | ||
host = ports.default_collect_host() | ||
assert_equal(host, "master:3514") | ||
|
||
def test_env_port(self): | ||
os.environ['BLUEOX_HOST'] = 'master:123' | ||
host = ports.default_collect_host() | ||
assert_equal(host, "master:123") |