def env_prod(): environment = Environment( "production", basedir=os.path.dirname(__file__) + "/fixture/basic_service", ) environment.load() return environment
class Deployment(object): environment = None def __init__(self, env_name, host_name, overrides, host_data, timeout, platform): self.env_name = env_name self.host_name = host_name self.overrides = overrides self.host_data = host_data self.timeout = timeout self.platform = platform def load(self): from batou.environment import Environment self.environment = Environment(self.env_name, self.timeout, self.platform) self.environment.deployment = self self.environment.load() self.environment.overrides = self.overrides for hostname, data in self.host_data.items(): self.environment.hosts[hostname].data.update(data) self.environment.configure() def deploy(self, root, predict_only): root = self.environment.get_root(root, self.host_name) root.component.deploy(predict_only)
def load(self): from batou.environment import Environment self.environment = Environment( self.env_name, self.timeout, self.platform) self.environment.deployment = self self.environment.load() self.environment.overrides = self.overrides self.environment.configure()
def test_load_should_use_config(sample_service): e = Environment("test-with-env-config") e.load() assert e.service_user == "joe" assert e.host_domain == "example.com" assert e.branch == "release" assert e.hosts["foo1"].service_user == "bob"
def test_resolver_overrides(sample_service): e = Environment('test-resolver') e.load() assert {'localhost': '127.0.0.2'} == e._resolve_override assert {'localhost': '::2'} == e._resolve_v6_override assert '127.0.0.2' == batou.utils.resolve('localhost') assert '::2' == batou.utils.resolve_v6('localhost', 0)
def test_resolver_overrides(sample_service): e = Environment("test-resolver") e.load() assert {"localhost": "127.0.0.2"} == e._resolve_override assert {"localhost": "::2"} == e._resolve_v6_override assert "127.0.0.2" == batou.utils.resolve("localhost") assert "::2" == batou.utils.resolve_v6("localhost", 0)
def test_load_hosts_should_load_single_hosts_section(add_root): e = Environment("name") config = Config(None) config.config.read_string(""" [hosts] foo = bar """) e.load_hosts(config) add_root.assert_called_once_with("bar", e.hosts["foo"], [], False)
def test_load_hosts_should_load_single_hosts_section(add_root): e = Environment('name') config = Config(None) config.config.read_string(""" [hosts] foo = bar """) e.load_hosts(config) add_root.assert_called_once_with('bar', 'foo', [], False)
def test_multiple_components(sample_service): e = Environment('test-multiple-components') e.load() components = dict( (host, list(sorted(c.name for c in e.root_dependencies(host=host)))) for host in sorted(e.hosts.keys())) assert components == dict(localhost=['hello1', 'hello2'], otherhost=['hello3', 'hello4'], thishost=['hello5', 'hello6'])
def test_normalize_hostname_regression_11156(): # The issue here was that we used "rstrip" which works on characters, # not substrings. Having the domain (example.com) start with an eee # causes the hostname to get stripped of it's "eee"s ending in an # empty hostname accidentally. e = Environment('name') e.hosts['eee.example.com'] = host = Mock() e.host_domain = 'example.com' assert host == e.get_host('eee')
def test_load_hosts_should_load_multi_hosts_section(add_root): pass e = Environment("name") config = Config(None) config.config.read_string(""" [host:foo] components = bar """) e.load_hosts(config) add_root.assert_called_once_with("bar", e.hosts['foo'], [], False)
def test_host_data_is_passed_to_host_object(): e = Environment('name') config = Config(None) config.config.read_string(""" [host:foo] components = bar data-alias = baz """) e.load_hosts(config) assert 'baz' == e.hosts['foo'].data['alias']
def test_host_data_is_passed_to_host_object(): e = Environment("name") config = Config(None) config.config.read_string(""" [host:foo] components = bar data-alias = baz """) e.load_hosts(config) assert "baz" == e.hosts["foo"].data["alias"]
def test_load_hosts_single_should_use_env_platform(add_root): e = Environment('name') e.platform = mock.sentinel.platform config = Config(None) config.config.read_string(""" [hosts] foo = bar """) e.load_hosts(config) assert e.hosts['foo'].platform == mock.sentinel.platform
def test_load_hosts_multi_should_use_env_platform(add_root): e = Environment('name') e.platform = mock.sentinel.platform config = Config(None) config.config.read_string(""" [host:foo] components = bar ignore = True """) e.load_hosts(config) assert e.hosts['foo'].platform == mock.sentinel.platform
def test_load_hosts_multi_should_use_ignore_flag(add_root): pass e = Environment('name') config = Config(None) config.config.read_string(""" [host:foo] components = bar ignore = True """) e.load_hosts(config) assert e.hosts['foo'].ignore
def test_multiple_components(sample_service): e = Environment("test-multiple-components") e.load() components = dict( (host, list(sorted(c.name for c in e.root_dependencies(host=host)))) for host in sorted(e.hosts.keys())) assert components == dict( localhost=["hello1", "hello2"], otherhost=["hello3", "hello4"], thishost=["hello5", "hello6"], )
def load(self): from batou.environment import Environment self.environment = Environment(self.env_name, self.timeout, self.platform) self.environment.deployment = self self.environment.load() self.environment.overrides = self.overrides for hostname, data in self.host_data.items(): self.environment.hosts[hostname].data.update(data) self.environment.configure()
def test_load_hosts_multi_should_use_host_platform_if_given(add_root): pass e = Environment('name') e.platform = mock.sentinel.platform config = Config(None) config.config.read_string(""" [host:foo] components = bar platform = specific """) e.load_hosts(config) assert e.hosts['foo'].platform == 'specific'
def test_load_hosts_should_break_on_duplicate_definition(add_root): e = Environment('name') config = Config(None) config.config.read_string(""" [hosts] foo = bar [host:foo] components = bar """) e.load_hosts(config) assert e.exceptions assert 'foo' == e.exceptions[0].hostname
def test_load_hosts_should_merge_single_and_multi_definition(add_root): e = Environment('name') config = Config(None) config.config.read_string(""" [hosts] foo = bar [host:baz] components = bar """) e.load_hosts(config) assert [mock.call('bar', 'foo', [], False), mock.call('bar', 'baz', [], False)] == \ add_root.call_args_list
def test_load_hosts_should_merge_single_and_multi_definition(add_root): e = Environment("name") config = Config(None) config.config.read_string(""" [hosts] foo = bar [host:baz] components = bar """) e.load_hosts(config) assert [ mock.call("bar", e.hosts["foo"], [], False), mock.call("bar", e.hosts["baz"], [], False), ] == add_root.call_args_list
def test_components_for_host_can_be_retrieved_from_environment(sample_service): e = Environment('test-overlapping-components') e.load() def _get_components(hostname): return sorted(e.components_for(host=e.hosts[hostname]).keys()) assert ['hello1', 'hello2'] == _get_components('localhost') assert ['hello3'] == _get_components('other') assert ['hello2', 'hello3'] == _get_components('this') localhost = e.hosts['localhost'] assert 'hello1' in localhost.components assert 'hello2' in localhost.components assert 'hello3' not in localhost.components
def test_components_for_host_can_be_retrieved_from_environment(sample_service): e = Environment("test-overlapping-components") e.load() def _get_components(hostname): return sorted(e.components_for(host=e.hosts[hostname]).keys()) assert ["hello1", "hello2"] == _get_components("localhost") assert ["hello3"] == _get_components("other") assert ["hello2", "hello3"] == _get_components("this") localhost = e.hosts["localhost"] assert "hello1" in localhost.components assert "hello2" in localhost.components assert "hello3" not in localhost.components
def root(tmpdir): environment = Environment("test", basedir=str(tmpdir)) environment._set_defaults() os.chdir(str(tmpdir)) class MyComponent(Component): pass compdef = ComponentDefinition(MyComponent) compdef.defdir = str(tmpdir) environment.components[compdef.name] = compdef root = environment.add_root(compdef.name, "host") root.prepare() root.component.deploy() return root
def test_log_in_component_configure_is_put_out(output, sample_service): e = Environment("test-with-provide-require") e.load() e.configure() log = "\n".join(c[0][0].strip() for c in output.call_args_list) # Provide is *always* logged first, due to provide/require ordering. assert ( """\ Provide Pre sub Sub! Post sub""" == log ) output.reset_mock() for root in e.root_dependencies(): root.component.deploy(True) log = "\n".join(c[0][0].strip() for c in output.call_args_list) assert ( """\ localhost > Hello localhost: <Hello (localhost) "Hello"> verify: asdf=None\ """ == log )
def env(): env = Environment("test") for component in list(globals().values()): if not isinstance(component, type): continue if issubclass(component, Component): compdef = ComponentDefinition(component) env.components[compdef.name] = compdef return env
def test_load_ignores_predefined_environment_settings(sample_service): e = Environment('test-with-env-config') e.service_user = '******' e.host_domain = 'sample.com' e.branch = 'default' e.load() assert e.service_user == 'bob' assert e.host_domain == 'sample.com' assert e.branch == 'default'
def test_load_ignores_predefined_environment_settings(sample_service): e = Environment("test-with-env-config") e.service_user = "******" e.host_domain = "sample.com" e.branch = "default" e.load() assert e.service_user == "bob" assert e.host_domain == "sample.com" assert e.branch == "default"
def test_service_early_resource(): env = Environment('dev', basedir=os.path.dirname(__file__) + '/fixture/service_early_resource') env.load() env.configure() assert env.resources.get('zeo') == ['127.0.0.1:9000']