-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Aiodocker helper * Aiodocker tests * deleting used containers * close docker removed
- Loading branch information
Showing
10 changed files
with
192 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pylibinjection | |
jinja2 | ||
pycodestyle | ||
geoip2 | ||
aiodocker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import asyncio | ||
import unittest | ||
|
||
from tanner.utils.aiodocker_helper import AIODockerHelper | ||
|
||
|
||
class TestAioDockerHelper(unittest.TestCase): | ||
def setUp(self): | ||
self.loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(self.loop) | ||
self.handler = AIODockerHelper() | ||
self.image = None | ||
self.expected_result = None | ||
self.returned_result = None | ||
|
||
def test_setup_host_image(self): | ||
self.image = 'busybox:latest' | ||
|
||
async def test(): | ||
await self.handler.setup_host_image() | ||
self.returned_result = await self.handler.docker_client.images.list(filter=self.image) | ||
|
||
self.loop.run_until_complete(test()) | ||
self.expected_result = 1 | ||
self.assertEqual(len(self.returned_result), self.expected_result) | ||
|
||
def test_get_container(self): | ||
container_name = 'attacker_container' | ||
|
||
async def test(): | ||
await self.handler.create_container(container_name) | ||
self.returned_result = await self.handler.get_container(container_name) | ||
await self.handler.delete_container(container_name) | ||
|
||
self.loop.run_until_complete(test()) | ||
self.assertTrue(self.returned_result._id) | ||
|
||
def test_create_container(self): | ||
container_name = 'attacker' | ||
|
||
async def test(): | ||
container = await self.handler.create_container(container_name=container_name) | ||
await container.start() | ||
self.returned_result = await container.show() | ||
await self.handler.delete_container(container_name) | ||
|
||
self.loop.run_until_complete(test()) | ||
self.assertTrue(self.returned_result["State"]["Running"]) | ||
|
||
def test_execute_cmd(self): | ||
cmd = ["sh", "-c", "echo 'Hello!'"] | ||
|
||
async def test(): | ||
self.returned_result = await self.handler.execute_cmd(cmd) | ||
|
||
self.loop.run_until_complete(test()) | ||
self.expected_result = 'Hello!' | ||
self.assertIn(self.expected_result, self.returned_result) | ||
|
||
def test_delete_container(self): | ||
container_name = 'attacker_z' | ||
|
||
async def test(): | ||
await self.handler.create_container(container_name) | ||
await self.handler.delete_container(container_name) | ||
self.returned_result = await self.handler.get_container(container_name) | ||
|
||
self.loop.run_until_complete(test()) | ||
self.assertEqual(self.returned_result, None) | ||
|
||
def tearDown(self): | ||
self.loop.run_until_complete(self.handler.docker_client.close()) | ||
self.loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import aiodocker | ||
import logging | ||
|
||
from tanner.config import TannerConfig | ||
|
||
|
||
class AIODockerHelper: | ||
def __init__(self): | ||
|
||
self.logger = logging.getLogger('tanner.aiodocker_helper.AIODockerHelper') | ||
|
||
self.docker_client = aiodocker.Docker() | ||
self.host_image = TannerConfig.get('DOCKER', 'host_image') | ||
|
||
async def setup_host_image(self, remote_path=None, tag=None): | ||
|
||
try: | ||
if remote_path and tag is not None: | ||
params = {"tag": tag, "remote": remote_path} | ||
await self.docker_client.images.build(**params) | ||
|
||
image = await self.docker_client.images.list(filter=self.host_image) | ||
if not image: | ||
await self.docker_client.images.pull(self.host_image) | ||
|
||
except aiodocker.exceptions.DockerError as docker_error: | ||
self.logger.exception('Error while pulling %s image %s', self.host_image, docker_error) | ||
|
||
async def get_container(self, container_name): | ||
container = None | ||
try: | ||
container = await self.docker_client.containers.get(container=container_name) | ||
|
||
except aiodocker.exceptions.DockerError as server_error: | ||
self.logger.exception('Error while fetching %s container %s', container_name, server_error) | ||
return container | ||
|
||
async def create_container(self, container_name, cmd=None, image=None): | ||
await self.setup_host_image() | ||
container = None | ||
if image is None: | ||
image = self.host_image | ||
|
||
config = { | ||
"Cmd": cmd, | ||
"Image": image, | ||
} | ||
try: | ||
container = await self.docker_client.containers.create_or_replace(config=config, name=container_name) | ||
|
||
except (aiodocker.exceptions.DockerError or aiodocker.exceptions.DockerContainerError) as docker_error: | ||
self.logger.exception('Error while creating a container %s', docker_error) | ||
return container | ||
|
||
async def execute_cmd(self, cmd, image=None): | ||
execute_result = None | ||
try: | ||
if image is None: | ||
image = self.host_image | ||
|
||
config = {"Cmd": cmd, "Image": image} | ||
container = await self.docker_client.containers.run(config=config) | ||
|
||
await container.wait() | ||
result_exists = await container.log(stdout=True, stderr=True) | ||
if result_exists: | ||
execute_result = ''.join(result_exists) | ||
|
||
# Deleting the used container | ||
await container.delete(force=True) | ||
|
||
except (aiodocker.exceptions.DockerError or aiodocker.exceptions.DockerContainerError) as server_error: | ||
self.logger.error('Error while executing command %s in container %s', cmd, server_error) | ||
return execute_result | ||
|
||
async def delete_container(self, container_name): | ||
container = await self.get_container(container_name) | ||
try: | ||
if container: | ||
await container.delete(force=True) | ||
except aiodocker.exceptions.DockerError as server_error: | ||
self.logger.exception('Error while removing %s container %s', container_name, server_error) |
This file was deleted.
Oops, something went wrong.