def test_add_node_to_pool_with_none_params(self, mocked_tsuru_request): mocked_tsuru_request.return_value = (200, None) pool_handler = plugin.TsuruPool("foobar") pool_handler.add_node_to_pool('127.0.0.1', '4243', 'http', None) node_add_dict = {'address': 'http://127.0.0.1:4243', 'pool': 'foobar'} mocked_tsuru_request.assert_called_once_with( "POST", "/docker/node?register=true", node_add_dict)
def setUp(self, users_mock): users_mock.return_value = {"Email": "myuser"} os.environ["TSURU_TARGET"] = "https://cloud.tsuru.io/" os.environ["TSURU_TOKEN"] = "abc123" self.patcher = patch('urllib2.urlopen') self.urlopen_mock = self.patcher.start() self.pool_handler = plugin.TsuruPool("foobar")
def test_move_node_containers_success(self, stdout, stderr, sleep): fake_buffer = [ 'garbage in first chunk\ {"Message":"Moving 2 units..."}\n' '{"Message":"moving unit: abcd1234"}\n', '{"Message":"moving unit: xyzabcd234"}\n', '{ "Message":"Container moved successfully" }' ] fake_buffer = "".join([x for x in fake_buffer]) self.urlopen_mock.return_value = FakeURLopenResponse(fake_buffer, 200) pool_handler = plugin.TsuruPool("foobar") stdout_calls = [ call('Moving 2 units...\n'), call('moving unit: abcd1234\n'), call('moving unit: xyzabcd234\n'), call('Container moved successfully\n') ] move_return_value = pool_handler.move_node_containers( 'http://10.10.1.2:123', 'https://1.2.3.4') self.assertEqual(stdout.write.call_args_list, stdout_calls) self.assertEqual(move_return_value, True) sleep.assert_has_calls([])
def test_get_node_metadata_return_none_on_key_error(self): fake_response_iaas = [] self.urlopen_mock.return_value = FakeURLopenResponse( fake_response_iaas, 200) pool_handler = plugin.TsuruPool("foobar") self.assertEqual(pool_handler.get_node_metadata('http://10.20.20.2'), None)
def test_get_machine_metadata_from_iaas_return_none_on_key_error(self): fake_response_iaas = '[]' self.urlopen_mock.return_value = FakeURLopenResponse( fake_response_iaas, 200) pool_handler = plugin.TsuruPool("foobar") self.assertEqual( pool_handler.get_machine_metadata_from_iaas('http://127.0.0.1'), None)
def test_create_new_node(self): self.urlopen_mock.return_value = FakeURLopenResponse(None, 200) pool_handler = plugin.TsuruPool("foobar") pool_handler.get_nodes = Mock() pool_handler.get_nodes.side_effect = [[ '192.168.1.1', 'http://10.1.1.1:2723', '10.10.10.1' ], ['192.168.1.1', '10.2.3.2', '10.10.10.1', 'http://10.1.1.1:2723']] return_new_node = pool_handler.create_new_node("my_template") self.assertEqual(return_new_node, '10.2.3.2')
def test_get_machine_metadata_from_iaas_return_none(self): fake_response_iaas = '''[{"Id": "abc", "Address": "10.10.2.1", "CreationParams": {"test": "abc"}}, {"Id": "def", "Address": "10.20.1.2", "CreationParams": {"test": "cde"}}]''' self.urlopen_mock.return_value = FakeURLopenResponse( fake_response_iaas, 200) pool_handler = plugin.TsuruPool("foobar") self.assertEqual( pool_handler.get_machine_metadata_from_iaas('http://10.10.2.2'), None)
def test_get_node_metadata_return_none(self): fake_response_iaas = '''{"machines":[{"Id": "abc", "Address": "10.10.2.1", "CreationParams": {"test": "abc"}}], "nodes":[{"Address": "http://10.20.1.2:4243", "Metadata": {"test": "cde"}}]}''' self.urlopen_mock.return_value = FakeURLopenResponse( fake_response_iaas, 200) pool_handler = plugin.TsuruPool("foobar") self.assertEqual(pool_handler.get_node_metadata('http://10.20.20.2'), None)
def test_move_node_containers_empty_return_stream(self, stdout, stderr, sleep): fake_buffer = '' self.urlopen_mock.return_value = FakeURLopenResponse(fake_buffer, 200) with self.assertRaises(MoveNodeContainersError): pool_handler = plugin.TsuruPool("foobar") pool_handler.move_node_containers('http://10.10.1.2:123', 'https://1.2.3.4') sleep.assert_has_calls([]) stdout.write.assert_has_calls([]) stderr.write.assert_has_calls([])
def test_move_node_containers_fail(self, stdout, stderr, sleep): fake_buffer_docker_connection_error = [ 'garbage in first chunk {"Message":"Moving 2 units..."}\n' '{"Message":"Error moving unit: abcd1234"}\n' '{"Message":"Error moving container: Error moving' ' unit: cannot connect to Docker endpoint"}\n' '{"Message":"Error moving unit: xyzabcd234"}\n', '{"Message":"Moving 2 units..."}\n' '{"Message":"Error moving unit: abcd1234"}\n' '{"Message":"Error moving container: Error moving unit:' ' cannot connect to Docker endpoint"}\n' '{"Message":"Error moving unit: xyzabcd234"}\n', '{"Message": "Moving unit abcd1234"}\n' '{"Message": "Moving unit xyzabc234"}\n' '{"Message": "Error moving unit: 0oi99222"}\n' ] docker_connection_error = [ FakeURLopenResponse(fake_buffer_docker_connection_error[0], 200), FakeURLopenResponse(fake_buffer_docker_connection_error[1], 200), FakeURLopenResponse(fake_buffer_docker_connection_error[2], 200) ] self.urlopen_mock.side_effect = docker_connection_error with self.assertRaises(MoveNodeContainersError): pool_handler = plugin.TsuruPool("foobar") pool_handler.move_node_containers('http://1.2.3.4:123', 'http://5.6.7.8:234', 0, 2) stderr_calls = [] for message_block in fake_buffer_docker_connection_error: for line in message_block.split('\n'): if line is not '' and 'Error' in line: message = json.loads(line)['Message'] stderr_calls.append(call(str(message + '\n'))) stderr_calls.append( call('Error: Max retry reached for moving on 3 attempts.')) stdout_calls = [ call('Moving 2 units...\n'), call('Retrying move containers from http://1.2.3.4:123 to' ' http://5.6.7.8:234. Waiting for 180 seconds...'), call('Moving 2 units...\n'), call('Retrying move containers from http://1.2.3.4:123 to' ' http://5.6.7.8:234. Waiting for 180 seconds...'), call('Moving unit abcd1234\n'), call('Moving unit xyzabc234\n') ] self.assertEqual(stdout.write.call_args_list, stdout_calls) self.assertEqual(stderr.write.call_args_list, stderr_calls) sleep.assert_has_calls([call(180), call(180)])
def test_remove_machine_from_iaas(self): fake_response_iaas = '''[{"Id": "abc", "Address": "10.10.2.1", "CreationParams": {"test": "abc"}}, {"Id": "def", "Address": "10.20.1.2", "CreationParams": {"test": "cde"}}]''' self.urlopen_mock.side_effect = [ FakeURLopenResponse(fake_response_iaas, 200), FakeURLopenResponse(None, 200) ] pool_handler = plugin.TsuruPool("foobar") self.assertEqual( pool_handler.remove_machine_from_iaas("http://10.20.1.2"), True)
def test_remove_machine_from_iaas_with_node_machine_not_found_error(self): fake_response_iaas = '''[{"Id": "abc", "Address": "10.10.2.1", "CreationParams": {"test": "abc"}}, {"Id": "def", "Address": "10.20.1.2", "CreationParams": {"test": "cde"}}]''' self.urlopen_mock.side_effect = [ FakeURLopenResponse(fake_response_iaas, 200), FakeURLopenResponse(None, 200) ] pool_handler = plugin.TsuruPool("foobar") self.assertRaisesRegexp(RemoveMachineFromIaaSError, 'machine 10.20.1.20 not found on IaaS', pool_handler.remove_machine_from_iaas, '10.20.1.20')
def test_remove_node_from_pool(self): http_error = urllib2.HTTPError(None, 500, None, None, StringIO(u"No such node in storage")) self.urlopen_mock.side_effect = [ FakeURLopenResponse(None, 200), http_error ] pool_handler = plugin.TsuruPool("foobar") return_remove_node = pool_handler.remove_node_from_pool( 'http://127.0.0.1:4243') self.assertEqual(return_remove_node, True) self.assertRaisesRegexp(Exception, 'No such node in storage', pool_handler.remove_node_from_pool, 'http://127.0.0.1:4243')
def test_add_node_to_pool(self, mocked_tsuru_request): mocked_tsuru_request.return_value = (200, None) pool_handler = plugin.TsuruPool("foobar") extra_params = {'bla': 'ble', 'xxx': 'yyy'} pool_handler.add_node_to_pool('127.0.0.1', '4243', 'http', extra_params) node_add_dict = dict( { 'address': 'http://127.0.0.1:4243', 'pool': 'foobar' }, **extra_params) mocked_tsuru_request.assert_called_once_with( "POST", "/docker/node?register=true", node_add_dict)
def test_move_node_containers_invalid_host(self, stdout, stderr, sleep): pool_handler = plugin.TsuruPool("foobar") self.assertRaisesRegexp(MoveNodeContainersError, 'node address .+ are invalids', pool_handler.move_node_containers, 'http://10.10.1.2:123', '1.2.3.4:432')
def test_return_machines_templates(self): machines_templates_json = ''' [ { "Name": "template_red", "IaaSName": "cloudstack_prod", "Data": [ { "Name": "pool", "Value": "foobar" }, { "Name": "projectid", "Value": "222f0798-e472-4216-a8ed-ce1950f419e8" }, { "Name": "displayname", "Value": "test_a" }, { "Name": "networkids", "Value": "513ef8b6-bd98-4e6b-89a6-6ca8a859fbb4" } ] }, { "Name": "template_blue", "IaaSName": "cloudstack_prod", "Data": [ { "Name": "pool", "Value": "infra" }, { "Name": "projectid", "Value": "222f0798-e472-4216-a8ed-ce1950f419e8" }, { "Name": "displayname", "Value": "test_infra" }, { "Name": "networkids", "Value": "97d7ad56-62b4-4d43-805a-2aee42619ac6" } ] }, { "Name": "template_yellow", "IaaSName": "cloudstack_dev", "Data": [ { "Name": "pool", "Value": "foobar" }, { "Name": "projectid", "Value": "222f0798-e472-4216-a8ed-ce1950f419e8" }, { "Name": "displayname", "Value": "docker_xxx" }, { "Name": "networkids", "Value": "97d7ad56-62b4-4d43-805a-2aee42619ac6" } ] }, { "Name": "template_green", "IaaSName": "cloudstack_dev", "Data": [ { "Name": "pool", "Value": "xxx_foobar" }, { "Name": "projectid", "Value": "222f0798-e472-4216-a8ed-ce1950f419e8" }, { "Name": "displayname", "Value": "docker_xxx" }, { "Name": "networkids", "Value": "97d7ad56-62b4-4d43-805a-2aee42619ac6" } ] } ] ''' self.urlopen_mock.side_effect = [ FakeURLopenResponse(machines_templates_json, 200), FakeURLopenResponse(None, 500) ] pool_handler = plugin.TsuruPool("foobar") self.assertListEqual(pool_handler.get_machines_templates(), ['template_red', 'template_yellow']) self.assertRaisesRegexp(Exception, 'Error getting machines templates', pool_handler.get_machines_templates)
def test_get_nodes_from_pool(self): docker_nodes_json = ''' { "machines": [ { "Id": "f04388e3-02e0-46ec-93c8-9e5ba095eeb8", "Iaas": "cloudstack", "Status": "running", "Address": "10.10.34.221", "CreationParams": { "displayname": "machine_a", "pool": "foobar" } }, { "Id": "c56ba117-cac2-4aba-b3c9-bc273ca79db0", "Iaas": "cloudstack", "Status": "running", "Address": "10.20.42.42", "CreationParams": { "displayname": "machine_b", "pool": "bilbo" } }, { "Id": "c059bfb9-7323-41a4-96dc-a44b8c5d97da", "Iaas": "cloudstack", "Status": "running", "Address": "10.30.33.182", "CreationParams": { "displayname": "machine_c" } } ], "nodes": [ { "Address": "http://10.2.25.169:4243", "Metadata": { "pool": "bilbo" }, "Status": "waiting" }, { "Address": "http://10.23.26.76:4243", "Metadata": { "pool": "foobar" }, "Status": "waiting" }, { "Address": "http://10.25.23.138:4243", "Metadata": { "LastSuccess": "2015-02-04T11:47:54-02:00", "pool": "foobar" }, "Status": "ready" } ] } ''' docker_nodes_null = '{ "machines": null, "nodes": null }' self.urlopen_mock.side_effect = [ FakeURLopenResponse(docker_nodes_json), FakeURLopenResponse(docker_nodes_null) ] pool_handler = plugin.TsuruPool("foobar") self.assertListEqual( pool_handler.get_nodes(), ['http://10.23.26.76:4243', 'http://10.25.23.138:4243']) self.assertListEqual(pool_handler.get_nodes(), [])