def test_get_stats_gets_the_client_stats(self): expected_stats = ['s1', 's2', 's3'] mock = Mock(spec=Client) mock.stats.return_value = expected_stats wrapper = DockerClientWrapper(mock) actual = wrapper.get_stats('c1', 3) self.assertEqual(expected_stats, [stat for time, stat in actual])
def test_get_containers_gets_the_client_containers(self): expected_containers = ['c1', 'c2', 'c3'] mock = Mock(spec=Client) mock.containers.return_value = expected_containers wrapper = DockerClientWrapper(mock) actual = wrapper.get_containers() self.assertEqual(expected_containers, actual)
def test_get_host_name_returns_na_when_no_host_name_found(self): expecte_host_name = 'N/A' mock = Mock(spec=Client) mock.info.return_value = {} wrapper = DockerClientWrapper(mock) actual = wrapper.get_host_name() self.assertEqual(expecte_host_name, actual)
def test_get_host_name_gets_the_client_host_name(self): expecte_host_name = 'host' mock = Mock(spec=Client) mock.info.return_value = {'Name': expecte_host_name} wrapper = DockerClientWrapper(mock) actual = wrapper.get_host_name() self.assertEqual(expecte_host_name, actual)
def test_get_inspect(self): expectedResult = 'result' mock = Mock() mock.inspect_container.return_value = expectedResult wrapper = DockerClientWrapper(mock) inspection = wrapper.get_inspection({'Id': 'c1'}) self.assertEqual(expectedResult, inspection)
def test_run_command(self): expectedResult = b'result' mock = Mock() mock.exec_create.return_value = 'exec1' mock.exec_start.return_value = expectedResult wrapper = DockerClientWrapper(mock) result = wrapper.run_command('c1', 'ls') self.assertEqual(expectedResult.decode('utf-8'), result)
def test_get_stats_gets_requested_number_of_stats_from_the_client(self): mock = Mock(spec=Client) mock.stats.return_value = map(lambda i: "s{0}".format(i), range(0, 100000)) expected = ["s0", "s1", "s2"] wrapper = DockerClientWrapper(mock) actual = wrapper.get_stats('c1', 3) self.assertEqual(expected, [stat for time, stat in actual])
def test_run_command_raise_docker_wrapper_error(self): expectedResult = b'result' mock = Mock() mock.exec_create.side_effect = ReadTimeoutError( 'pool', 'url', 'message') mock.exec_start.return_value = expectedResult wrapper = DockerClientWrapper(mock) self.assertRaises(DockerWrapperError, wrapper.run_command, 'c1', 'ls')
def test_get_stats_return_empty_list_on_timeout(self): mock = Mock() mock.stats.side_effect = ReadTimeoutError('pool', 'url', 'message') wrapper = DockerClientWrapper(mock) actual = wrapper.get_stats('c1', 3) self.assertEqual([], [stat for time, stat in actual])
def test_get_stats_return_empty_list_on_api_error(self): mock = Mock() mock.stats.side_effect = APIError("boom", "boom", "boom") wrapper = DockerClientWrapper(mock) actual = wrapper.get_stats('c1', 3) self.assertEqual([], [stat for time, stat in actual])