def test_rename_container(self): response_run = DockerEngine().run_container_operation( operation='run', data={ 'image': 'mongo:4.2.3', 'detach': True }) self.assertNotEqual(response_run, None, 'Background image run failed') self.assertEqual(response_run.status, 'created', 'Container not created') container = DockerEngine().get_container_by_id(response_run.short_id) self.assertNotEqual(container, None, 'Container does not exist') response = DockerEngine().run_operation_in_object( object=container, operation='rename', data={'name': 'testing-name'}) self.assertEqual(response, None, 'Container not reloaded') container = DockerEngine().get_container_by_id(response_run.short_id) self.assertEqual(container.name, 'testing-name', 'Wrong name') response = DockerEngine().run_operation_in_object(object=container, operation='stop', data={}) self.assertEqual(response, None, 'Container not stopped')
def test_pause_unpause_container(self): response = DockerEngine().run_container_operation(operation='run', data={ 'image': 'mongo:4.2.3', 'detach': True }) self.assertNotEqual(response, None, 'Background image run failed') self.assertEqual(response.status, 'created', 'Container not created') container = DockerEngine().get_container_by_id(response.short_id) self.assertNotEqual(container, None, 'Container does not exist') response = DockerEngine().run_operation_in_object(object=container, operation='pause', data={}) self.assertEqual(response, None, 'Container not paused') response = DockerEngine().run_operation_in_object(object=container, operation='unpause', data={}) self.assertEqual(response, None, 'Container not unpaused') response = DockerEngine().run_operation_in_object(object=container, operation='stop', data={}) self.assertEqual(response, None, 'Container not stopped')
def test_run_playbook(self): self.playbook = 'test-alpine-ssh' self.hosts = ['alpine'] ips = '172.17.0.' response_list = DockerEngine().run_container_operation( operation='list', data={'all': False}) if len(response_list) > 0: ips = ips + str(2 + len(response_list)) else: ips = ips + '2' h = Host().insert({'name': self.hosts[0], 'ips': [ips]}) self.assertEqual(h, True, 'Host not added') p = Playbook().insert({ 'name': self.playbook, 'playbook': { 'hosts': self.hosts[0], 'remote_user': '******', 'tasks': [{ 'name': 'Test debug msg', 'debug': { 'msg': 'This works!' } }] } }) self.assertEqual(p, True, 'Playbook not added') container = DockerEngine().run_container_operation( operation='run', data={ 'image': 'sickp/alpine-sshd', 'detach': True }) self.assertNotEqual(container, False, 'Cointainer not running') self.container_id = container.short_id response = AnsibleEngine().run_playbook(hosts=self.hosts, playbook=self.playbook, passwords={ 'conn_pass': '******', 'become_pass': '******' }) self.assertNotEqual(response, False, 'Playbook did not run') self.assertNotEqual(response.find('PLAY [alpine]'), -1) c = DockerEngine().get_container_by_id(self.container_id) DockerEngine().run_operation_in_object(object=c, operation='stop', data={})
def test_history_image(self): image = DockerEngine().run_image_operation(operation='get', data={'name': 'alpine'}) self.assertNotEqual(image, False, 'Image not exists') response = DockerEngine().run_operation_in_object(object=image, operation='history', data={}) self.assertIsInstance(response, list, 'Wrong history')
def test_reload_image(self): image = DockerEngine().run_image_operation( operation='pull', data={'repository': 'alpine:latest'}) self.assertNotEqual(image, False, 'Image not exists') response = DockerEngine().run_operation_in_object(object=image, operation='reload', data={}) self.assertNotEqual(response, False, 'Image not reloaded')
def test_get_image(self): response = DockerEngine().run_image_operation( operation='pull', data={'repository': 'alpine:latest'}) self.assertNotEqual(response, None, 'Pull image failed') response = DockerEngine().run_image_operation(operation='get', data={'name': 'alpine'}) self.assertNotEqual(response, None, 'Pull get failed') self.assertNotEqual(response.short_id, None, 'Wrong ID') self.assertEqual(response.tags, ['alpine:latest'], 'Wrong tag')
def test_pull_and_remove_image(self): response = DockerEngine().run_image_operation( operation='pull', data={'repository': 'alpine:latest'}) self.assertNotEqual(response, None, 'Pull image failed') response = DockerEngine().run_image_operation(operation='remove', data={ 'image': 'alpine', 'force': True }) self.assertEqual(response, None, 'Remove image failed')
def post(): payload = validate_or_abort(BaseSingleImageProps, request.get_json()) image = DockerEngine().get_image_by_name(payload['name']) if image: response = DockerEngine().run_operation_in_object( object=image, operation=payload['operation'], data=payload['data']) return parse_data(None, response) if response \ else response_by_success(False) return response_by_success(False)
def post(): payload = validate_or_abort(BaseSingleContainerProps, request.get_json()) container = DockerEngine().get_container_by_id(payload['container_id']) if container: response = DockerEngine().run_operation_in_object( object=container, operation=payload['operation'], data=payload['data']) if response is not False: return parse_data(None, response) return response_by_success(False)
def post(): payload = validate_or_abort(BaseImageProps, request.get_json()) data = payload['data'] if payload['operation'] == 'get': data = validate_or_abort(ImageGetProps, data) if payload['operation'] == 'list': data = validate_or_abort(ImageListProps, data) if payload['operation'] == 'pull': data = validate_or_abort(ImagePullProps, data) if payload['operation'] == 'remove': data = validate_or_abort(ImageRemoveProps, data) data['force'] = True if payload['operation'] == 'search': data = validate_or_abort(ImageSearchProps, data) if payload['operation'] == 'prune': data = validate_or_abort(ImagePruneProps, data) response = DockerEngine().run_image_operation( operation=payload['operation'], data=data) schema = ImageObj if response else None if response and payload['operation'] == 'search': schema = DockerHubImage return parse_data(schema, response) if response \ else response_by_success(False)
def test_list_running_containers(self): response = DockerEngine().run_container_operation(operation='run', data={ 'image': 'mongo:4.2.3', 'detach': True }) self.assertNotEqual(response, None, 'Background container1 run failed') self.assertEqual(response.status, 'created', 'Container1 not created') response2 = DockerEngine().run_container_operation(operation='run', data={ 'image': 'mongo:4.2.3', 'detach': True }) self.assertNotEqual(response2, None, 'Background container2 run failed') self.assertEqual(response2.status, 'created', 'Container2 not created') response_list = DockerEngine().run_container_operation( operation='list', data={'all': False}) self.assertGreaterEqual(len(response_list), 2, 'Not all containers listed') response_list = DockerEngine().run_container_operation( operation='list', data={ 'all': False, 'filters': { 'id': response.short_id } }) self.assertEqual(len(response_list), 1, 'Wrong number of container listed') self.assertEqual(response_list[0].short_id, response.short_id, 'Wrong container listed') container = DockerEngine().get_container_by_id(response.short_id) self.assertNotEqual(container, None, 'Container1 does not exist') response = DockerEngine().run_operation_in_object(object=container, operation='stop', data={}) self.assertEqual(response, None, 'Container not stopped') container = DockerEngine().get_container_by_id(response2.short_id) self.assertNotEqual(container, None, 'Container2 does not exist') response = DockerEngine().run_operation_in_object(object=container, operation='stop', data={}) self.assertEqual(response, None, 'Container not stopped')
def get(): mongo = MongoEngine().status() docker = DockerEngine().status() hc = mongo['is_up'] and docker['is_up'] code = 200 if hc else 500 return { 'ok': hc }, code
def get(): user = Login().get_username(request.headers['x-access-token']) is_admin = User().is_admin(user) return { 'mongo': MongoEngine().status(is_admin), 'docker': DockerEngine().status() }, 200
def test_get_running_container(self): response = DockerEngine().run_container_operation(operation='run', data={ 'image': 'mongo:4.2.3', 'detach': True }) self.assertNotEqual(response, None, 'Background container run failed') self.assertEqual(response.status, 'created', 'Container not created') response_get = DockerEngine().run_container_operation( operation='get', data={'container_id': response.short_id}) self.assertEqual(response_get.short_id, response.short_id, 'Image not found') container = DockerEngine().get_container_by_id(response.short_id) self.assertNotEqual(container, None, 'Container does not exist') response = DockerEngine().run_operation_in_object(object=container, operation='stop', data={}) self.assertEqual(response, None, 'Container not stopped')
def post(): payload = validate_or_abort(BaseContainersProps, request.get_json()) data = payload['data'] if payload['operation'] == 'run': data = validate_or_abort(ContainersRunProps, data) data['detach'] = True if payload['operation'] == 'get': data = validate_or_abort(ContainersGetProps, data) if payload['operation'] == 'list': data = validate_or_abort(ContainersListProps, data) if payload['operation'] == 'prune': data = validate_or_abort(ContainersPruneProps, data) response = DockerEngine().run_container_operation( operation=payload['operation'], data=data) schema = ContainerObj if response else None return parse_data(schema, response) if response \ else response_by_success(False)
def test_get_client(self): client = DockerEngine().get_client() self.assertNotEqual(client, None, 'Engine does not exist')
def test_prune_containers(self): response = DockerEngine().run_container_operation(operation='prune', data={}) self.assertNotEqual(response, False, 'Prune all failed')
def test_search_image(self): response = DockerEngine().run_image_operation(operation='search', data={'term': 'alpine'}) self.assertNotEqual(response, None, 'Search images failed') self.assertIsInstance(response, list, 'Response is not a list')
def test_prune_images(self): response = DockerEngine().run_image_operation( operation='prune', data={'filters': { 'dangling': True }}) self.assertNotEqual(response, None, 'Prune image failed')
def test_run_hello_world_container(self): response = DockerEngine().run_container_operation( operation='run', data={'image': 'hello-world'}) self.assertNotEqual(response.decode().find('Hello from Docker!'), -1, 'Hello world run failed')
def test_run_playbook(self): ips = '172.17.0.' response_list = DockerEngine().run_container_operation( operation='list', data={ 'all': False } ) if len(response_list) > 0: ips = ips + str(2 + len(response_list)) else: ips = ips + '2' h = Host().insert({ 'name': self.hosts[0], 'ips': ips }) self.assertEqual(h, True, 'Host not added') p = Playbook().insert({ 'name': self.playbook, 'playbook': { 'hosts': self.hosts[0], 'remote_user': '******', 'tasks': [ { 'name': 'Test debug msg', 'debug': { 'msg': 'This works!' } } ] } }) self.assertEqual(p, True, 'Playbook not added') container = DockerEngine().run_container_operation( operation='run', data={ 'image': 'sickp/alpine-sshd', 'detach': True } ) self.assertNotEqual(container, False, 'Cointainer not running') self.container_id = container.short_id # Run the playbook response = self.app.post( self.path, headers=self.headers, data=json.dumps({ 'hosts': self.hosts, 'playbook': self.playbook, 'passwords': { 'conn_pass': '******', 'become_pass': '******' } }) ) self.assertEqual(response.status_code, 200, 'Playbook failed running') self.assertNotEqual(json.loads(response.data)['result'] .find('PLAY [alpine]'), -1) # Stop the container c = DockerEngine().get_container_by_id(self.container_id) DockerEngine().run_operation_in_object( object=c, operation='stop', data={} )