def test_start_all_create_network(self):
        collection = ServiceCollection()

        class NewServiceBase(Service):
            name = "not used"
            image = "not used"

        class ServiceTwo(NewServiceBase):
            name = "goodbye"
            image = "goodbye/image"

        collection._base_class = NewServiceBase
        collection.load_definitions()
        collection.start_all(DEFAULT_OPTIONS)
        assert self.docker._networks_created == ["the-network"]
    def test_continue_if_start_failed(self):
        """If a service fails, those that don't depend on it should still be started"""
        collection = ServiceCollection()

        class NewServiceBase(Service):
            name = "not used"
            image = "not used"

        class FirstService(NewServiceBase):
            name = "first-service"
            image = "howareyou/image"

            def ping(self):
                raise ValueError("I failed miserably")

        class SecondService(NewServiceBase):
            name = "second-service"
            image = "howareyou/image"

            def ping(self):
                time.sleep(0.5)
                return True

        collection._base_class = NewServiceBase
        collection.load_definitions()
        started = collection.start_all(DEFAULT_OPTIONS)
        assert started == ["second-service"]
    def test_start_all_with_build(self):
        collection = ServiceCollection()

        class NewServiceBase(Service):
            name = "not used"
            image = "not used"

        collection._base_class = NewServiceBase

        class ServiceTwo(NewServiceBase):
            name = "goodbye"
            image = "goodbye/image"
            build_from = "goodbye/dir"
            dockerfile = "Dockerfile.alt"

        collection.load_definitions()
        options = attr.evolve(DEFAULT_OPTIONS, build=['goodbye'])
        retval = collection.start_all(options)
        assert len(self.docker._images_built) == 1
        build_dir, dockerfile, image_tag = self.docker._images_built[0]
        assert build_dir == "/etc/goodbye/dir"
        assert dockerfile == 'Dockerfile.alt'
        assert image_tag.startswith("goodbye-")
        service = collection.all_by_name['goodbye']
        assert service.image == image_tag
    def test_start_all(self):
        # This test does not fake threading, which is somehow dangerous, but the
        # aim is to make sure that the error handling etc. works also when there
        # is an exception in the service agent thread, and the
        # collection.start_all method does not hang.
        collection = ServiceCollection()

        class NewServiceBase(Service):
            name = "not used"
            image = "not used"

        collection._base_class = NewServiceBase

        class ServiceOne(NewServiceBase):
            name = "hello"
            image = "hello/image"
            dependencies = ["howareyou"]

        class ServiceTwo(NewServiceBase):
            name = "goodbye"
            image = "goodbye/image"
            dependencies = ["hello"]

        class ServiceThree(NewServiceBase):
            name = "howareyou"
            image = "howareyou/image"

        collection.load_definitions()
        retval = collection.start_all(DEFAULT_OPTIONS)
        assert set(retval) == {"hello", "goodbye", "howareyou"}
        assert len(self.docker._services_started) == 3
        # The one without dependencies should have been started first
        name_prefix, service, network_name = self.docker._services_started[0]
        assert service.image == 'howareyou/image'
        assert name_prefix == "howareyou-testing"
    def test_stop_on_fail(self):
        collection = ServiceCollection()

        class NewServiceBase(Service):
            name = "not used"
            image = "not used"

        class TheService(NewServiceBase):
            name = "howareyou"
            image = "howareyou/image"

            def ping(self):
                raise ValueError("I failed miserably")

        collection._base_class = NewServiceBase
        collection.load_definitions()
        started = collection.start_all(DEFAULT_OPTIONS)
        assert started == []
    def test_dont_return_failed_services(self):
        collection = ServiceCollection()

        class NewServiceBase(Service):
            name = "not used"
            image = "not used"

        class TheFirstService(NewServiceBase):
            name = "howareyou"
            image = "howareyou/image"

        class TheService(NewServiceBase):
            name = "imok"
            image = "howareyou/image"
            dependencies = ["howareyou"]

            def ping(self):
                raise ValueError("I failed miserably")

        collection._base_class = NewServiceBase
        collection.load_definitions()
        started = collection.start_all(DEFAULT_OPTIONS)
        assert started == ["howareyou"]