# Path: mcloud/config.py
# class IConfig(Interface):
# class ConfigParseError(Exception):
# class UnknownServiceError(Exception):
# class OrderedDictYAMLLoader(yaml.Loader):
# class YamlConfig(IConfig):
#     def get_services(self):
#     def __init__(self, *args, **kwargs):
#     def construct_yaml_map(self, node):
#     def construct_mapping(self, node, deep=False):
#     def __init__(self, file=None, source=None, app_name=None, path=None, env=None):
#     def get_services(self):
#     def get_volumes(self):
#     def get_command_host(self, name=None):
#     def get_commands(self):
#     def get_service(self, name):
#     def load(self, process=True, client=None):
#     def export(self):
#     def filter_env(self, config):
#     def prepare(self, config):
#     def validate(self, config):
#     def process_command_build(self, service, config, path):
#     def process_other_settings_build(self, service, config, path):
#     def sanitize_path(self, path):
#     def process_volumes_build(self, service, config, path):
#     def process_env_build(self, service, config, path):
#     def process_image_build(self, service, config, path):
#     def process_local_config(self, config):
#     def process(self, config, path, app_name=None, client=None):
# 
# Path: mcloud/container.py
# class PrebuiltImageBuilder(IImageBuilder):
#     def __init__(self, image):
#         super(PrebuiltImageBuilder, self).__init__()
# 
#         self.image = image
# 
#     @defer.inlineCallbacks
#     def build_image(self, ticket_id, service):
# 
#         log.msg('[%s] Building image "%s".', ticket_id, self.image)
# 
#         name = self.image
#         tag = None
#         if ':' in name:
#             name, tag = name.split(':')
# 
#         images = yield service.client.images(name=name)
# 
#         if tag:
#             images = [x for x in images if self.image in x['RepoTags']]
# 
#         if not images:
#             log.msg('[%s] Image is not there. Pulling "%s" ...', ticket_id, self.image)
# 
#             yield service.client.pull(name, ticket_id, tag)
# 
#         log.msg('[%s] Image "%s" is ready to use.', ticket_id, self.image)
#         defer.returnValue(self.image)
# 
# class DockerfileImageBuilder(IImageBuilder):
#     def __init__(self, path):
#         super(DockerfileImageBuilder, self).__init__()
#         self.path = path
# 
#         self.image_id = None
# 
#     def create_archive(self):
#         d = defer.Deferred()
# 
#         def archive():
#             memfile = StringIO()
#             try:
#                 t = tarfile.open(mode='w', fileobj=memfile)
#                 t.add(self.path, arcname='.')
#                 d.callback(memfile.getvalue())
#             except OSError as e:
#                 d.errback(CanNotAccessPath('Can not access %s: %s' % (self.path, str(e))))
#             except Exception as e:
#                 d.errback(e)
#             finally:
#                 memfile.close()
# 
#         reactor.callLater(0, archive)
#         return d
# 
#     @defer.inlineCallbacks
#     def build_image(self, ticket_id, service):
#         archive = yield self.create_archive()
#         ret = yield service.client.build_image(archive, ticket_id=ticket_id)
#         defer.returnValue(ret)
# 
# class InlineDockerfileImageBuilder(VirtualFolderImageBuilder):
#     def __init__(self, source):
#         super(InlineDockerfileImageBuilder, self).__init__({
#             'Dockerfile': source
#         })


from collections import OrderedDict
from flexmock import flexmock
from mcloud.config import YamlConfig, Service, UnknownServiceError, ConfigParseError
from mcloud.container import PrebuiltImageBuilder, DockerfileImageBuilder, InlineDockerfileImageBuilder
import os
import pytest

def test_build_build_env_several():
    s = Service()
    c = YamlConfig(env='prod')

    c.process_env_build(s, {'env': {
        'foo1': 'bar1',
        'foo2': 'bar2',
        'foo3': 'bar3',
    }}, '/base/path')

    assert s.env == {
        'env': 'prod',
        'foo1': 'bar1',
        'foo2': 'bar2',
        'foo3': 'bar3',
    }


def test_build_image_image():
    s = Service()
    c = YamlConfig()

    c.process_image_build(s, {'image': 'foo/bar'}, '/base/path')

    assert isinstance(s.image_builder, PrebuiltImageBuilder)
    assert s.image_builder.image == 'foo/bar'


def test_build_image_dockerfile():

# Path: mcloud/config.py
# class IConfig(Interface):
# class ConfigParseError(Exception):
# class UnknownServiceError(Exception):
# class OrderedDictYAMLLoader(yaml.Loader):
# class YamlConfig(IConfig):
#     def get_services(self):
#     def __init__(self, *args, **kwargs):
#     def construct_yaml_map(self, node):
#     def construct_mapping(self, node, deep=False):
#     def __init__(self, file=None, source=None, app_name=None, path=None, env=None):
#     def get_services(self):
#     def get_volumes(self):
#     def get_command_host(self, name=None):
#     def get_commands(self):
#     def get_service(self, name):
#     def load(self, process=True, client=None):
#     def export(self):
#     def filter_env(self, config):
#     def prepare(self, config):
#     def validate(self, config):
#     def process_command_build(self, service, config, path):
#     def process_other_settings_build(self, service, config, path):
#     def sanitize_path(self, path):
#     def process_volumes_build(self, service, config, path):
#     def process_env_build(self, service, config, path):
#     def process_image_build(self, service, config, path):
#     def process_local_config(self, config):
#     def process(self, config, path, app_name=None, client=None):
# 
# Path: mcloud/container.py
# class PrebuiltImageBuilder(IImageBuilder):
#     def __init__(self, image):
#         super(PrebuiltImageBuilder, self).__init__()
# 
#         self.image = image
# 
#     @defer.inlineCallbacks
#     def build_image(self, ticket_id, service):
# 
#         log.msg('[%s] Building image "%s".', ticket_id, self.image)
# 
#         name = self.image
#         tag = None
#         if ':' in name:
#             name, tag = name.split(':')
# 
#         images = yield service.client.images(name=name)
# 
#         if tag:
#             images = [x for x in images if self.image in x['RepoTags']]
# 
#         if not images:
#             log.msg('[%s] Image is not there. Pulling "%s" ...', ticket_id, self.image)
# 
#             yield service.client.pull(name, ticket_id, tag)
# 
#         log.msg('[%s] Image "%s" is ready to use.', ticket_id, self.image)
#         defer.returnValue(self.image)
# 
# class DockerfileImageBuilder(IImageBuilder):
#     def __init__(self, path):
#         super(DockerfileImageBuilder, self).__init__()
#         self.path = path
# 
#         self.image_id = None
# 
#     def create_archive(self):
#         d = defer.Deferred()
# 
#         def archive():
#             memfile = StringIO()
#             try:
#                 t = tarfile.open(mode='w', fileobj=memfile)
#                 t.add(self.path, arcname='.')
#                 d.callback(memfile.getvalue())
#             except OSError as e:
#                 d.errback(CanNotAccessPath('Can not access %s: %s' % (self.path, str(e))))
#             except Exception as e:
#                 d.errback(e)
#             finally:
#                 memfile.close()
# 
#         reactor.callLater(0, archive)
#         return d
# 
#     @defer.inlineCallbacks
#     def build_image(self, ticket_id, service):
#         archive = yield self.create_archive()
#         ret = yield service.client.build_image(archive, ticket_id=ticket_id)
#         defer.returnValue(ret)
# 
# class InlineDockerfileImageBuilder(VirtualFolderImageBuilder):
#     def __init__(self, source):
#         super(InlineDockerfileImageBuilder, self).__init__({
#             'Dockerfile': source
#         })


# Path: tests/test_config.py
from collections import OrderedDict
from flexmock import flexmock
from mcloud.config import YamlConfig, Service, UnknownServiceError, ConfigParseError
from mcloud.container import PrebuiltImageBuilder, DockerfileImageBuilder, InlineDockerfileImageBuilder
import os
import pytest


def test_build_build_env_several():
    s = Service()
    c = YamlConfig(env='prod')

    c.process_env_build(s, {'env': {
        'foo1': 'bar1',
        'foo2': 'bar2',
        'foo3': 'bar3',
    }}, '/base/path')

    assert s.env == {
        'env': 'prod',
        'foo1': 'bar1',
        'foo2': 'bar2',
        'foo3': 'bar3',
    }


def test_build_image_image():
    s = Service()
    c = YamlConfig()

    c.process_image_build(s, {'image': 'foo/bar'}, '/base/path')

    assert isinstance(s.image_builder, PrebuiltImageBuilder)
    assert s.image_builder.image == 'foo/bar'


def test_build_image_dockerfile():

    s = Service()