def test_destroy_container_bad_return_code(self): mock_conn = self.make_request('DELETE', 'containers/XXX') response = FakeResponse(400) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(False, client.destroy_container('XXX')) self.mox.VerifyAll()
def test_destroy_container(self): mock_conn = self.make_request('DELETE', 'containers/XXX') response = FakeResponse(204, headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(True, client.destroy_container('XXX')) self.mox.VerifyAll()
def test_inspect_image_bad_return_code(self): mock_conn = self.make_request('GET', 'images/XXX/json') response = FakeResponse(404) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) image = client.inspect_image('XXX') self.assertIsNone(image) self.mox.VerifyAll()
def test_stop_container_bad_return_code(self): mock_conn = self.make_request('POST', 'containers/XXX/stop', ('t', '5')) response = FakeResponse(400) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(False, client.stop_container('XXX')) self.mox.VerifyAll()
def test_commit_container_bad_return_code(self): mock_conn = self.make_request('POST', 'commit', ('container', 'XXX'), ('repo', 'ping')) response = FakeResponse(400, headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(False, client.commit_container('XXX', 'ping')) self.mox.VerifyAll()
def test_inspect_container_bad_return_code(self): mock_conn = self.make_request('GET', 'containers/XXX/json') response = FakeResponse(404, data='inspect: No such container: XXX', headers={'Content-Type': 'text/plain'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) container = client.inspect_container('XXX') self.assertEqual({}, container) self.mox.VerifyAll()
def test_inspect_container(self): mock_conn = self.make_request('GET', 'containers/XXX/json') response = FakeResponse(200, data='{"id": "XXX"}', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) container = client.inspect_container('XXX') self.assertEqual({'id': 'XXX'}, container) self.mox.VerifyAll()
def test_start_container(self): mock_conn = self.make_request('POST', 'containers/XXX/start', body='{}') response = FakeResponse(200, headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(True, client.start_container('XXX')) self.mox.VerifyAll()
def test_list_containers(self): mock_conn = self.make_request('GET', 'containers/ps', ('all', '1')) response = FakeResponse(200, data='[]', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) containers = client.list_containers() self.assertEqual([], containers) self.mox.VerifyAll()
def test_load_repository(self): data = ["hello", "world"] url = 'images/load' headers = {'Content-Type': 'application/json'} mock_conn = self.make_request('POST', url, body=data, headers=headers) response = FakeResponse(200, data) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) client.load_repository('XXX', data) self.mox.VerifyAll()
def test_get_container_logs_bad_return_code(self): url = 'containers/XXX/attach' mock_conn = self.make_request('POST', url, ('logs', '1'), ('stream', '0'), ('stdout', '1'), ('stderr', '1')) response = FakeResponse(404) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) logs = client.get_container_logs('XXX') self.assertIsNone(logs) self.mox.VerifyAll()
def test_destroy_container_bad_return_code(self): mock_conn = self.mox.CreateMockAnything() mock_conn.request('DELETE', '/v1.7/containers/XXX', headers={'Content-Type': 'application/json'}) response = FakeResponse(400) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(False, client.destroy_container('XXX')) self.mox.VerifyAll()
def test_pull_repository_bad_return_code(self): mock_conn = self.mox.CreateMockAnything() mock_conn.request('POST', '/v1.7/images/create?fromImage=ping', headers={'Content-Type': 'application/json'}) response = FakeResponse(400, headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(False, client.pull_repository('ping')) self.mox.VerifyAll()
def test_inspect_image_bad_return_code(self): mock_conn = self.mox.CreateMockAnything() mock_conn.request('GET', '/v1.7/images/XXX/json', headers={'Content-Type': 'application/json'}) response = FakeResponse(404) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) image = client.inspect_image('XXX') self.assertIsNone(image) self.mox.VerifyAll()
def test_commit_container(self): mock_conn = self.mox.CreateMockAnything() mock_conn.request('POST', '/v1.7/commit?container=XXX&repo=ping', headers={'Content-Type': 'application/json'}) response = FakeResponse(201, headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(True, client.commit_container('XXX', 'ping')) self.mox.VerifyAll()
def test_get_container_logs_bad_return_code(self): mock_conn = self.mox.CreateMockAnything() url = '/v1.7/containers/XXX/attach?logs=1&stream=0&stdout=1&stderr=1' mock_conn.request('POST', url, headers={'Content-Type': 'application/json'}) response = FakeResponse(404) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) logs = client.get_container_logs('XXX') self.assertIsNone(logs) self.mox.VerifyAll()
def test_get_container_logs(self): url = 'containers/XXX/attach' mock_conn = self.make_request('POST', url, ('logs', '1'), ('stream', '0'), ('stdout', '1'), ('stderr', '1')) response = FakeResponse(200, data='ping pong', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) logs = client.get_container_logs('XXX') self.assertEqual('ping pong', logs) self.mox.VerifyAll()
def test_create_container_bad_return_code(self): expected_uuid = uuid.uuid4() mock_conn = self.make_request('POST', 'containers/create', ('name', expected_uuid), body=mox.IgnoreArg()) response = FakeResponse(400) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) container_id = client.create_container({}, expected_uuid) self.assertIsNone(container_id) self.mox.VerifyAll()
def test_create_container_with_args(self): mock_conn = self.mox.CreateMockAnything() expected_uuid = uuid.uuid4() expected_body = jsonutils.dumps({ 'Hostname': 'marco', 'User': '', 'Memory': 512, 'MemorySwap': 0, 'AttachStdin': False, 'AttachStdout': False, 'AttachStderr': False, 'PortSpecs': [], 'Tty': True, 'OpenStdin': True, 'StdinOnce': False, 'Env': None, 'Cmd': [], 'Dns': None, 'Image': 'example', 'Volumes': {}, 'VolumesFrom': '', }) mock_conn.request( 'POST', '/v1.7/containers/create?name={0}'.format(expected_uuid), body=expected_body, headers={'Content-Type': 'application/json'}) response = FakeResponse(201, data='{"id": "XXX"}', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) args = { 'Hostname': 'marco', 'Memory': 512, 'Image': 'example', } container_id = client.create_container(args, expected_uuid) self.assertEqual('XXX', container_id) self.mox.VerifyAll()
def test_list_containers(self): mock_conn = self.mox.CreateMockAnything() mock_conn.request('GET', '/v1.7/containers/ps?all=1', headers={'Content-Type': 'application/json'}) response = FakeResponse(200, data='[]', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) containers = client.list_containers() self.assertEqual([], containers) self.mox.VerifyAll()
def test_inspect_image(self): mock_conn = self.mox.CreateMockAnything() mock_conn.request('GET', '/v1.7/images/XXX/json', headers={'Content-Type': 'application/json'}) response = FakeResponse(200, data='{"name": "XXX"}', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) image = client.inspect_image('XXX') self.assertEqual({'name': 'XXX'}, image) self.mox.VerifyAll()
def test_push_repository_bad_return_code(self): mock_conn = self.mox.CreateMockAnything() body = ('{"username":"******","password":"******",' '"auth":"","email":"*****@*****.**"}') mock_conn.request('POST', '/v1.7/images/ping/push', headers={'Content-Type': 'application/json'}, body=body) response = FakeResponse(400, headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) self.assertEqual(False, client.push_repository('ping')) self.mox.VerifyAll()
def test_create_container_bad_return_code(self): mock_conn = self.mox.CreateMockAnything() expected_uuid = uuid.uuid4() mock_conn.request( 'POST', '/v1.7/containers/create?name={0}'.format(expected_uuid), body=mox.IgnoreArg(), headers={'Content-Type': 'application/json'}) response = FakeResponse(400) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) container_id = client.create_container({}, expected_uuid) self.assertIsNone(container_id) self.mox.VerifyAll()
def test_create_container_no_id_in_response(self): expected_uuid = uuid.uuid4() mock_conn = self.make_request('POST', 'containers/create', ('name', expected_uuid), body=mox.IgnoreArg()) response = FakeResponse(201, data='{"ping": "pong"}', headers={'Content-Type': 'application/json'}) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) container_id = client.create_container({}, expected_uuid) self.assertIsNone(container_id) self.mox.VerifyAll()
def test_get_image(self): image_id = 'XXX' data = ["hello world"] url = 'images/{0}/get'.format(image_id) headers = {'Content-Type': 'application/json'} mock_conn = self.make_request('GET', url, headers=headers) response = FakeResponse(201, data) mock_conn.getresponse().AndReturn(response) self.mox.ReplayAll() client = docker_client.DockerHTTPClient(mock_conn) image = client.get_image(image_id) self.assertIsInstance(image, collections.Iterable) # Only calling the generator will trigger the GET request. next(image) self.mox.VerifyAll()
def docker(self): if self._docker is None: self._docker = docker_client.DockerHTTPClient(CONF.docker.host_url) return self._docker
def docker(self): if self._docker is None: self._docker = docker_client.DockerHTTPClient() return self._docker