from __future__ import absolute_import
from __future__ import unicode_literals

from .. import unittest
from compose.cli.docker_client import docker_client
from compose.config.config import ServiceLoader
from compose.const import LABEL_PROJECT
from compose.progress_stream import stream_output
from compose.service import Service


class DockerClientTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.client = docker_client()

    def tearDown(self):
        for c in self.client.containers(
                all=True,
                filters={'label': '%s=composetest' % LABEL_PROJECT}):
            self.client.kill(c['Id'])
            self.client.remove_container(c['Id'])
        for i in self.client.images(
                filters={'label': 'com.docker.compose.test_image'}):
            self.client.remove_image(i)

    def create_service(self, name, **kwargs):
        if 'image' not in kwargs and 'build' not in kwargs:
            kwargs['image'] = 'busybox:latest'

        if 'command' not in kwargs:
            kwargs['command'] = ["top"]

        options = ServiceLoader(working_dir='.', filename=None, service_name=name, service_dict=kwargs).make_service_dict()

        labels = options.setdefault('labels', {})
        labels['com.docker.compose.test-name'] = self.id()

        return Service(
            project='composetest',
            client=self.client,
            **options
        )

    def check_build(self, *args, **kwargs):
        kwargs.setdefault('rm', True)
        build_output = self.client.build(*args, **kwargs)
        stream_output(build_output, open('/dev/null', 'w'))

@@ -1079,16 +1079,354 @@
 %22top%22%5D%0A%0A
+        links = kwargs.get('links', None)%0A        volumes_from = kwargs.get('volumes_from', None)%0A        net = kwargs.get('net', None)%0A%0A        workaround_options = %5B'links', 'volumes_from', 'net'%5D%0A        for key in workaround_options:%0A            try:%0A                del kwargs%5Bkey%5D%0A            except KeyError:%0A                pass%0A%0A
         
@@ -1648,24 +1648,205 @@
  self.id()%0A%0A
+        if links:%0A            options%5B'links'%5D = links%0A        if volumes_from:%0A            options%5B'volumes_from'%5D = volumes_from%0A        if net:%0A            options%5B'net'%5D = net%0A%0A
         retu

Work around some coupling of links, net & volume_from