def test_deploy_to_instances(self, pool): no_group_pool = Mock() pools = [Mock(), Mock(), Mock()] groups = [ testutils.mock_attr(batch_size=10), testutils.mock_attr(batch_size=50) ] hosts = [ testutils.mock_attr(group=groups[0]), testutils.mock_attr(group=groups[1]), testutils.mock_attr(group=None) ] pool.Pool.side_effect = pools pool.Group.return_value = no_group_pool self.env.groups.all.return_value = groups self.env.hosts.all.return_value = hosts self.env._deploy_to_instances('sha') pools[2].spawn.assert_has_calls([ call(hosts[0].pull_nodes, pools[0], 'sha'), call(hosts[1].pull_nodes, pools[1], 'sha'), call(hosts[2].pull_nodes, no_group_pool, 'sha') ], any_order=True) for p in pool: p.join.assert_called_with()
def test_should_create_image(self): backend = self.backend backend.store_images = False eq_(backend.should_create_image('prefix-name', 'prefix'), (False, None)) backend.store_images = True eq_(backend.should_create_image('prefix-name', 'prefix'), (True, None)) image = mock_attr(name='prefix-name', id='id') self.cs.images.list.return_value = [image] eq_(backend.should_create_image('prefix-name', 'prefix'), (False, 'id')) im1 = mock_attr(name='prefix-foo') im2 = mock_attr(name='otherprefix-foo') self.cs.images.list.return_value = [im1, im2] backend.store_images = False backend.should_create_image('prefix-name', 'prefix') assert not im1.delete.called assert not im2.delete.called backend.store_images = True backend.should_create_image('prefix-name', 'prefix') im1.delete.assert_called_with() assert not im2.delete.called
def test_create_plugin(subc): plugin = mock_attr(name='plugin_name') subc.return_value = [plugin, mock_attr(name='foo')] options, parent = Mock(), Mock() result_plugin = plugins.create_plugin('plugin_name', options, parent) plugin.assert_called_with(options, parent) eq_(result_plugin, plugin()) eq_(plugins.create_plugin('undefined', options, parent), None)
def setUp(self): patcher = patch('stretch.agent.client.requests') self.requests = patcher.start() self.addCleanup(patcher.stop) patcher = patch_settings('STRETCH_AGENT_CERT', '/cert.pem') patcher.start() self.addCleanup(patcher.stop) self.host = mock_attr(address='127.0.0.1') self.host.environment = mock_attr(name='env', pk=2) self.host.environment.app_paths = {'node': '/path'} self.client = AgentClient(self.host)
def setUp(self): patcher = patch("stretch.agent.client.requests") self.requests = patcher.start() self.addCleanup(patcher.stop) patcher = patch_settings("STRETCH_AGENT_CERT", "/cert.pem") patcher.start() self.addCleanup(patcher.stop) self.host = mock_attr(address="127.0.0.1") self.host.environment = mock_attr(name="env", pk=2) self.host.environment.app_paths = {"node": "/path"} self.client = AgentClient(self.host)
def test_add_instance_from_group(self, get_key, set_dict): get_key.return_value = '/e' host = mock_attr(group=mock_attr(pk=2), address='1.1.1.1', environment=Mock()) instance = mock_attr(host=host, pk=3) self.cm.add_instance(instance) set_dict.assert_called_with('/e/groups/2/3', { 'address': '1.1.1.1', 'ports': {}, 'enabled': False })
def test_init(self): self.cs.images.list.return_value = [mock_attr(name='image22')] options = { 'username': '******', 'api_key': '------', 'region': 'dfw', 'domainname': 'example.com', 'image': 'foo', 'ram': 1 } with assert_raises(backends.RackspaceBackend.ImageNotFound): backends.RackspaceBackend(options) options['image'] = 'mage2' with assert_raises(backends.RackspaceBackend.FlavorNotFound): backends.RackspaceBackend(options) options['ram'] = 512 backends.RackspaceBackend(options) pyrax = self.pyrax pyrax.connect_to_cloudservers.assert_called_with(region='DFW') pyrax.connect_to_cloud_loadbalancers.assert_called_with(region='DFW') pyrax.set_credentials.assert_called_with('barfoo', '------')
def test_add_node(self): node = mock_attr(pk=1) self.client.add_node(node) self.requests.post.assert_called_with( 'https://127.0.0.1:1337/v1/nodes', data={'id': '1'}, cert='/cert.pem')
def test_add_env(self, get_key): env = mock_attr(name='env_name') with patch.multiple(self.cm, sync_env_config=DEFAULT, set=DEFAULT) as values: self.cm.add_env(env) values['set'].assert_called_with('/e/name', 'env_name') values['sync_env_config'].assert_called_with(env)
def test_post(self): parser = Mock() parser.parse_args.return_value = {'key': 'value'} self.obj.create.return_value = testutils.mock_attr(data={'data': '1'}) self.assertEquals(self.list_resource.post(parser), ({ 'data': '1' }, 201)) self.obj.create.assert_called_with({'key': 'value'})
def test_pull(self, run_task): node = mock_attr(name="node", pk=1) node.get_image.return_value = "image" node.ports.all.return_value = [mock_attr(name="http", number=80), mock_attr(name="https", number=443)] self.client.pull(node) run_task.assert_called_with( "nodes/1", "pull", { "sha": None, "app_path": "/path", "ports": '{"http": 80, "https": 443}', "env_id": "2", "env_name": "env", "image": "image", }, ) node.get_image.assert_called_with(local=True)
def test_pull(self, run_task): node = mock_attr(name='node', pk=1) node.get_image.return_value = 'image' node.ports.all.return_value = [ mock_attr(name='http', number=80), mock_attr(name='https', number=443) ] self.client.pull(node) run_task.assert_called_with( 'nodes/1', 'pull', { 'sha': None, 'app_path': '/path', 'ports': '{"http": 80, "https": 443}', 'env_id': '2', 'env_name': 'env', 'image': 'image' }) node.get_image.assert_called_with(local=True)
def setUp(self): for attr in ('environment', 'host', 'node', 'save'): patcher = self.patch_instance(attr) patcher.start() self.addCleanup(patcher.stop) self.env = Mock() self.node = Mock() self.host = mock_attr(nodes=[self.node]) self.instance = self.create_instance()
def test_get(self): self.etcd_client.get.return_value = testutils.mock_attr(value='value') eq_(self.cm.get('/key'), 'value') self.etcd_client.get.assert_called_with('/key') def mock_get(key): raise KeyError self.etcd_client.get = mock_get with assert_raises(KeyError): self.cm.get('/key')
def setUp(self): super(TestRackspaceBackend, self).setUp() patcher = patch('stretch.backends.pyrax') self.addCleanup(patcher.stop) self.pyrax = patcher.start() self.cs = Mock() self.cs.images.list.return_value = [mock_attr(name='image')] self.cs.flavors.list.return_value = [mock_attr(ram=512)] self.pyrax.connect_to_cloudservers.return_value = self.cs self.backend = backends.RackspaceBackend({ 'username': '******', 'api_key': '------', 'region': 'dfw', 'domainname': 'example.com', 'image': 'image', 'ram': 512 })
def test_add_instance(self): instance = mock_attr(pk=1) instance.node.pk = 2 instance.host.name = "host_name" instance.config_key = "/key" self.client.add_instance(instance) self.requests.post.assert_called_with( "https://127.0.0.1:1337/v1/instances", data={"node_id": "2", "config_key": "/key", "id": "1", "host_name": "host_name"}, cert="/cert.pem", )
def test_add_instance(self, Instance): Instance.create.return_value = testutils.mock_attr(data={}) self.client.post('/v1/instances', data={ 'node_id': '2', 'config_key': '/key', 'id': '1', 'host_name': 'host_name' }) Instance.create.assert_called_with({ 'node_id': '2', 'config_key': '/key', 'id': '1', 'host_name': 'host_name' })
def test_add_instance(self): instance = mock_attr(pk=1) instance.node.pk = 2 instance.host.name = 'host_name' instance.config_key = '/key' self.client.add_instance(instance) self.requests.post.assert_called_with( 'https://127.0.0.1:1337/v1/instances', data={ 'node_id': '2', 'config_key': '/key', 'id': '1', 'host_name': 'host_name' }, cert='/cert.pem')
def test_provision_host(self, put, run, upload_template, env): s = Mock() s.adminPass = '******' s.create_image.return_value = 'imageid' host = Mock() self.cs.images.get.return_value = mock_attr(status='ACTIVE') self.backend.provision_host(s, '0.0.0.0', host, True, 'p-name', 'p') eq_(env.host_string, '[email protected]') eq_(env.password, 'password') run.assert_has_calls([call('/bin/bash image-bootstrap.sh'), call('/bin/bash /root/host-bootstrap.sh')]) run.reset() self.backend.provision_host(s, '0.0.0.0', host, False, 'p-name', 'p') run.assert_called_with('/bin/bash /root/host-bootstrap.sh')
def test_create_host(self, provision_host, should_create_image, get_name): server = mock_attr(status='ACTIVE', accessIPv4='publicip', networks={'private': ['privateip']}) self.cs.servers.create.return_value = server host = Mock() should_create_image.return_value = (True, 'id') self.backend.use_public_network = True self.backend.create_host(host) provision_host.assert_called_with(server, 'publicip', host, True, 'p-name', 'p') should_create_image.return_value = (False, None) self.backend.use_public_network = False self.backend.create_host(host) provision_host.assert_called_with(server, 'privateip', host, False, 'p-name', 'p')
def test_get_class(importlib): importlib.import_module.return_value = testutils.mock_attr(klass='foo') eq_(utils.get_class('path.to.klass'), 'foo') importlib.import_module.assert_called_with('path.to')
def test_remove_node(self): node = mock_attr(pk=1) self.client.remove_node(node) self.requests.delete.assert_called_with( 'https://127.0.0.1:1337/v1/nodes/1', cert='/cert.pem')
def setUp(self): self.plugin = plugins.Plugin({ 'path': 'c/d', 'watch': ['e/f', 'g/h'] }, mock_attr(path='/a/b'))
def test_restart_instance(self, run_task): instance = mock_attr(pk=1) self.client.restart_instance(instance) run_task.assert_called_with('instances/1', 'restart')
def test_add_node(self): node = mock_attr(pk=1) self.client.add_node(node) self.requests.post.assert_called_with("https://127.0.0.1:1337/v1/nodes", data={"id": "1"}, cert="/cert.pem")
def test_mock_attr(): eq_(testutils.mock_attr(key='value').key, 'value')
def test_sync_env_config(self, get_key, set_dict): get_key.return_value = '/e' env = mock_attr(config={'key': 'value'}) self.cm.sync_env_config(env) set_dict.assert_called_with('/e/config', {'key': 'value'})
def test_monitored_paths(self): check_items_equal(self.plugin.monitored_paths, ['/a/b/e/f', '/a/b/g/h']) plugin = plugins.Plugin({}, mock_attr(path='/a/b')) eq_(plugin.monitored_paths, ['/a/b'])
def test_post(self): parser = Mock() parser.parse_args.return_value = {'key': 'value'} self.obj.create.return_value = testutils.mock_attr(data={'data': '1'}) self.assertEquals(self.list_resource.post(parser), ({'data': '1'}, 201)) self.obj.create.assert_called_with({'key': 'value'})
def test_remove_instance(self): instance = mock_attr(pk=1) self.client.remove_instance(instance) self.requests.delete.assert_called_with("https://127.0.0.1:1337/v1/instances/1", cert="/cert.pem")
def test_delete(self): delete_func = Mock() self.obj.return_value = testutils.mock_attr(delete=delete_func) self.assertEquals(self.resource.delete(_id='3'), ('', 204)) delete_func.assert_called_with()
def test_get(self): self.obj.return_value = testutils.mock_attr(data='foo') self.assertEquals(self.resource.get(_id='3'), 'foo') self.obj.assert_called_with('3')
def test_reload_instance(self, run_task): instance = mock_attr(pk=1) self.client.reload_instance(instance) run_task.assert_called_with("instances/1", "reload")
def test_remove_instance_from_hosts(self, get_key, delete): get_key.return_value = '/e' host = mock_attr(pk=2, environment=Mock(), group=None) instance = mock_attr(host=host, pk=3) self.cm.remove_instance(instance) delete.assert_called_with('/e/hosts/2/3')
def test_remove_instance(self): instance = mock_attr(pk=1) self.client.remove_instance(instance) self.requests.delete.assert_called_with( 'https://127.0.0.1:1337/v1/instances/1', cert='/cert.pem')
def test_get_key(self): env = mock_attr(pk=2, system=mock_attr(pk=1)) eq_(self.cm.get_key(env), '/1/envs/2')
def test_remove_node(self): node = mock_attr(pk=1) self.client.remove_node(node) self.requests.delete.assert_called_with("https://127.0.0.1:1337/v1/nodes/1", cert="/cert.pem")
def test_add_node(self, Node): Node.create.return_value = testutils.mock_attr(data={}) self.client.post('/v1/nodes', data={'id': '1'}) Node.create.assert_called_with({'id': '1'})