示例#1
0
    def test_status_valid(self, mock_docker_client, mock_container,
                          mock_non_match_container):
        # @note when setting the container object using autospec=True, it could not
        # set the properties name and status of the mock object.
        # Thus we resort to using TestContainerSpec as the autospec where these are
        # settable. It should be noted that for the status test it was sufficient to use
        # @mock.patch('docker.models.containers.Container') directly but we are using
        # TestContainerSpec for consistency

        # arrange
        test_status = 'running'
        type(mock_container).status = mock.PropertyMock(
            return_value=test_status)
        type(mock_container).name = mock.PropertyMock(
            return_value=self.TEST_CONTAINER_NAME)
        type(mock_non_match_container).status = mock.PropertyMock(
            return_value='running')
        type(mock_non_match_container).name = mock.PropertyMock(
            return_value='blah')
        mock_docker_client.containers.list.return_value = [
            mock_non_match_container, mock_container
        ]
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act
        result = client.status(self.TEST_CONTAINER_NAME)

        # assert
        mock_docker_client.containers.list.assert_called_with(all=True)
        self.assertEqual(test_status, result)
示例#2
0
    def test_create_valid(self, mock_docker_client, mock_docker_api_client):
        # arrange
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        image = 'test_image'
        container_name = 'test_name'
        detach_bool = True
        env_dict = {'test_key_env': 'test_val_env'}
        ports_dict = {'test_key_ports': 'test_val_ports'}
        volume_dict = {
            'test_key_volume': {
                'bind': 'test_val_bind',
                'mode': 'test_val_mode'
            }
        }
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act
        client.create_container(image,
                                name=container_name,
                                detach=detach_bool,
                                environment=env_dict,
                                ports=ports_dict,
                                volumes=volume_dict)

        # assert
        mock_docker_client.api.create_container.assert_called_with(
            image,
            detach=detach_bool,
            environment=env_dict,
            name=container_name,
            ports=ports_dict,
            volumes=volume_dict)
示例#3
0
 def test_get_os_valid(self, mock_docker_client):
     # arrange
     os_type = 'TEST_OS'
     mock_docker_client.info.return_value = {'OSType': os_type}
     client = EdgeDockerClient.create_instance(mock_docker_client)
     result = client.get_os_type()
     mock_docker_client.info.assert_called_with()
     self.assertEqual(result, os_type.lower())
示例#4
0
    def test_stop_by_label_raises_exception(self, mock_docker_client):
        # arrange
        mock_docker_client.containers.list.side_effect = docker.errors.APIError(
            'list failure')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.stop_remove_by_label(self.TEST_LABEL)
示例#5
0
    def test_status_raises_exception(self, mock_docker_client):
        # arrange
        mock_docker_client.containers.list.side_effect = docker.errors.APIError(
            'list failure')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.status(self.TEST_CONTAINER_NAME)
示例#6
0
    def test_stop_invalid_container_raises_exception(self, mock_docker_client):
        # arrange
        mock_docker_client.containers.get.side_effect = docker.errors.NotFound(
            'invalid image')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.stop(self.TEST_CONTAINER_NAME)
示例#7
0
    def test_get_os_fails(self, mock_docker_client):
        # arrange
        mock_docker_client.info.side_effect = docker.errors.APIError(
            'info fails')
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeError):
            client.get_os_type()
 def test_get_os_type(self):
     with EdgeDockerClient() as client:
         exception_raised = False
         try:
             os_type = client.get_os_type()
         except EdgeDeploymentError:
             exception_raised = True
         self.assertFalse(exception_raised)
         permitted_os_types = ['windows', 'linux']
         self.assertIn(os_type, permitted_os_types)
示例#9
0
    def test_remove_fails_raises_exception(self, mock_docker_client,
                                           mock_container):
        # arrange
        mock_container.remove.side_effect = docker.errors.APIError(
            'remove failure')
        mock_docker_client.containers.get.return_value = mock_container
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.remove(self.TEST_CONTAINER_NAME)
示例#10
0
    def test_create_raises_except_when_APIError_is_raised(
            self, mock_docker_client, mock_docker_api_client):
        # arrange
        mock_docker_api_client.create_container.side_effect = docker.errors.APIError(
            'image error')
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            self._create_common_invocation(client)
示例#11
0
    def test_remove_valid(self, mock_docker_client, mock_container):
        # arrange
        mock_docker_client.containers.get.return_value = mock_container
        client = EdgeDockerClient.create_instance(mock_docker_client)

        # act
        client.remove(self.TEST_CONTAINER_NAME)

        # assert
        mock_docker_client.containers.get.assert_called_with(
            self.TEST_CONTAINER_NAME)
        mock_container.remove.assert_called_with()
 def test_pull(self):
     with EdgeDockerClient() as client:
         exception_raised = False
         image_name = self.IMAGE_NAME
         try:
             local_sha_1 = client.get_local_image_sha_id(image_name)
             is_updated = client.pull(image_name, None, None)
             local_sha_2 = client.get_local_image_sha_id(image_name)
             if is_updated:
                 self.assertNotEqual(local_sha_1, local_sha_2)
             else:
                 self.assertEqual(local_sha_1, local_sha_2)
         except EdgeDeploymentError:
             exception_raised = True
         self.assertFalse(exception_raised)
示例#13
0
    def test_get_local_image_sha_id_fails(self, mock_docker_client,
                                          mock_docker_api_client):
        # arrange
        mock_docker_api_client.inspect_image.side_effect = docker.errors.APIError(
            'inspect fails')
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'

        # act
        result = client.get_local_image_sha_id(image)

        # assert
        mock_docker_api_client.inspect_image.assert_called_with(image)
        self.assertEqual(result, None)
示例#14
0
    def test_get_local_image_sha_id_valid(self, mock_docker_client,
                                          mock_docker_api_client):
        # arrange
        test_id = '1234'
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'

        # act
        result = client.get_local_image_sha_id(image)

        # assert
        mock_docker_api_client.inspect_image.assert_called_with(image)
        self.assertEqual(result, test_id)
示例#15
0
    def test_pull_raises_exception(self, mock_docker_client,
                                   mock_docker_api_client, mock_get_local_id):
        # arrange
        test_id = '1234'
        mock_get_local_id.return_value = None
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        mock_docker_client.images.pull.side_effect = docker.errors.APIError(
            'docker unavailable')
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'
        username = "******"
        password = "******"

        # act, assert
        with self.assertRaises(EdgeDeploymentError):
            client.pull(image, username, password)
示例#16
0
    def test_stop_by_label_valid(self, mock_docker_client, mock_container1,
                                 mock_container2):
        # @note when setting multiple container mocks autospec=True failed which is
        # why we resort to using TestContainerSpec as the autospec class

        # arrange
        mock_docker_client.containers.list.return_value = [
            mock_container1, mock_container2
        ]
        client = EdgeDockerClient.create_instance(mock_docker_client)
        filter_dict = {'label': self.TEST_LABEL}

        # act
        client.stop_remove_by_label(self.TEST_LABEL)

        # assert
        mock_docker_client.containers.list.assert_called_with(
            all=True, filters=filter_dict)
        mock_container1.stop.assert_called_with()
        mock_container2.stop.assert_called_with()
 def test_create(self):
     with EdgeDockerClient() as client:
         exception_raised = False
         try:
             self._reset_host_network(client)
             status = client.status(self.CONTAINER_NAME)
             if status is not None:
                 self._destroy_container(client)
             self._create_container(client)
             status = client.status(self.CONTAINER_NAME)
             self.assertEqual('created', status)
             client.start(self.CONTAINER_NAME)
             status = client.status(self.CONTAINER_NAME)
             self.assertEqual('running', status)
             time.sleep(5)
             self._destroy_container(client)
         except EdgeDeploymentError as ex:
             print(ex)
             exception_raised = True
         self.assertFalse(exception_raised)
示例#18
0
    def test_pull_image_exists_locally_with_newer_image_no_credentials_valid(
            self, mock_docker_client, mock_docker_api_client,
            mock_get_local_id):
        # arrange
        test_id = '1234'
        mock_get_local_id.return_value = '1000'
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'
        auth_dict = None

        # act
        result = client.pull(image, None, None)

        # assert
        mock_get_local_id.assert_called_with(image)
        mock_docker_api_client.inspect_image.assert_called_with(image)
        mock_docker_client.images.pull.assert_called_with(
            image, auth_config=auth_dict)
        self.assertTrue(result)
示例#19
0
    def test_pull_image_exists_locally_with_no_newer_image_valid(
            self, mock_docker_client, mock_docker_api_client,
            mock_get_local_id):
        # arrange
        test_id = '1234'
        mock_get_local_id.return_value = test_id
        mock_docker_api_client.inspect_image.return_value = {'Id': test_id}
        type(mock_docker_client).api = mock.PropertyMock(
            return_value=mock_docker_api_client)
        client = EdgeDockerClient.create_instance(mock_docker_client)
        image = 'test_image'
        username = "******"
        password = "******"
        auth_dict = {'username': username, 'password': password}

        # act
        result = client.pull(image, username, password)

        # assert
        mock_get_local_id.assert_called_with(image)
        mock_docker_api_client.inspect_image.assert_called_with(image)
        mock_docker_client.images.pull.assert_called_with(
            image, auth_config=auth_dict)
        self.assertFalse(result)
示例#20
0
import os
import pytest
import platform
import re
import subprocess
import shutil
import time
from click.testing import CliRunner
from iotedgehubdev import cli
from iotedgehubdev import configs
from iotedgehubdev.edgedockerclient import EdgeDockerClient
from iotedgehubdev.hostplatform import HostPlatform

workingdirectory = os.getcwd()
docker_client = EdgeDockerClient()

tests_dir = os.path.join(workingdirectory, 'tests')
test_config_dir = os.path.join(tests_dir, 'assets', 'config')
test_certs_dir = os.path.join(tests_dir, 'assets', 'certs')
test_resources_dir = os.path.join(tests_dir, 'test_compose_resources')
test_temp_cert_dir = os.path.join(tests_dir, 'cert_temp')
test_ca_file = 'iotedgehubdev-test-only.root.ca.cert.pem'
test_ca_key_file = 'iotedgehubdev-test-only.root.ca.key.pem'

VALID_IOTHUBCONNECTIONSTRING = os.environ['IOTHUB_CONNECTION_STRING']
VALID_DEVICECONNECTIONSTRING = os.environ[platform.system().upper() + '_DEVICE_CONNECTION_STRING']
VALID_CONTAINERREGISTRYSERVER = os.environ['CONTAINER_REGISTRY_SERVER']
VALID_CONTAINERREGISTRYUSERNAME = os.environ['CONTAINER_REGISTRY_USERNAME']
VALID_CONTAINERREGISTRYPASSWORD = os.environ['CONTAINER_REGISTRY_PASSWORD']
VALID_TEST_CA_KEY_PASSPHASE = os.environ['TEST_CA_KEY_PASSPHASE']
示例#21
0
import pytest
import re
import subprocess
import time
from click.testing import CliRunner
from dotenv import load_dotenv
from iotedgehubdev import cli
from iotedgehubdev import configs
from iotedgehubdev.edgedockerclient import EdgeDockerClient
from iotedgehubdev.hostplatform import HostPlatform

workingdirectory = os.getcwd()
filename = os.path.join(workingdirectory, '.env')
if os.path.exists(filename):
    load_dotenv(filename)
docker_client = EdgeDockerClient()

VALID_DEVICECONNECTIONSTRING = os.environ[platform.system().upper() +
                                          '_DEVICE_CONNECTION_STRING']
VALID_IOTHUBCONNECTIONSTRING = os.environ['IOTHUB_CONNECTION_STRING']

device_id = re.findall(".*DeviceId=(.*);SharedAccessKey.*",
                       VALID_DEVICECONNECTIONSTRING)[0]
iothub_name = re.findall(".*HostName=(.*);DeviceId.*",
                         VALID_DEVICECONNECTIONSTRING)[0].split('.')[0]


@pytest.fixture
def runner():
    return CliRunner()