def test_stop_without_remove(self): container1 = FakeContainer(name='service1-testing-1234', network='the-network', status='running') container2 = FakeContainer(name='service2-testing-5678', network='the-network', status='exited') self.docker._existing_containers = [container1, container2] collection = ServiceCollection() class NewServiceBase(Service): name = "not used" image = "not used" class ServiceOne(NewServiceBase): name = "service1" image = "howareyou/image" class ServiceTwo(NewServiceBase): name = "service2" image = "howareyou/image" collection._base_class = NewServiceBase collection.load_definitions() collection.stop_all(DEFAULT_OPTIONS) assert container1.stopped assert container1.timeout == 1 assert container1.removed_at is None assert not container2.stopped assert self.docker._networks_removed == []
def test_stop_with_remove_and_order(self): container1 = FakeContainer(name='service1-testing-1234', network='the-network', status='running') container2 = FakeContainer(name='service2-testing-5678', network='the-network', status='running') container3 = FakeContainer(name='service3-testing-5678', network='the-network', status='running') self.docker._existing_containers = [container1, container2, container3] collection = ServiceCollection() class NewServiceBase(Service): name = "not used" image = "not used" class ServiceOne(NewServiceBase): name = "service1" image = "howareyou/image" class ServiceTwo(NewServiceBase): name = "service2" image = "howareyou/image" dependencies = ['service1'] class ServiceThree(NewServiceBase): name = "service3" image = "howareyou/image" dependencies = ['service2'] collection._base_class = NewServiceBase collection.load_definitions() options = Options(network=Network(name='the-network', id='the-network-id'), timeout=50, remove=True, run_dir='/etc', build=[]) collection.stop_all(options) assert container1.stopped assert container1.removed_at is not None assert container2.stopped assert container2.removed_at is not None assert container3.stopped assert container3.removed_at is not None assert container1.removed_at > container2.removed_at > container3.removed_at assert self.docker._networks_removed == ['the-network']
def test_stop_remove_container_on_failed(self): fake_context = FakeRunningContext() name = 'aservice' container = FakeContainer(name='{}-testing-5678'.format(name), network='the-network', status='running') _context = self class CrazyFakeService(FakeService): def ping(self): _context.docker._existing_containers = [container] raise ValueError("Blah") options = Options(network=Network(name='the-network', id='the-network-id'), timeout=0.01, remove=True, run_dir='/etc', build=[]) agent = ServiceAgent(CrazyFakeService(name=name), options, fake_context) agent.start_service() agent.join() assert container.stopped assert container.removed_at is not None # This is 0 because the service wasn't stopped by the user assert len(fake_context.stopped_services) == 0
def test_update_for_base_service(self): container1 = FakeContainer(name='service1-testing-1234', network='the-network', status='running') container2 = FakeContainer(name='service2-testing-5678', network='the-network', status='running') container3 = FakeContainer(name='service3-testing-5678', network='the-network', status='running') self.docker._existing_containers = [container1, container2, container3] collection = ServiceCollection() class NewServiceBase(Service): name = "not used" image = "not used" class ServiceOne(NewServiceBase): name = "service1" image = "howareyou/image" class ServiceTwo(NewServiceBase): name = "service2" image = "howareyou/image" dependencies = ['service1'] class ServiceThree(NewServiceBase): name = 'service3' image = 'howareyou/image' dependencies = ['service1', 'service2'] collection._base_class = NewServiceBase collection.load_definitions() collection.update_for_base_service('service2') assert collection.all_by_name == { 'service2': ServiceTwo(), 'service3': ServiceThree() } collection.stop_all(DEFAULT_OPTIONS) assert not container1.stopped assert container2.stopped assert container3.stopped
def test_stop_with_remove_and_exclude(self): container1 = FakeContainer(name='service1-testing-1234', network='the-network', status='running') container2 = FakeContainer(name='service2-testing-5678', network='the-network', status='running') self.docker._existing_containers = [container1, container2] collection = ServiceCollection() class NewServiceBase(Service): name = "not used" image = "not used" class ServiceOne(NewServiceBase): name = "service1" image = "howareyou/image" class ServiceTwo(NewServiceBase): name = "service2" image = "howareyou/image" collection._base_class = NewServiceBase collection.load_definitions() collection.exclude_for_stop(['service2']) options = Options(network=Network(name='the-network', id='the-network-id'), timeout=50, remove=True, run_dir='/etc', build=[]) collection.stop_all(options) assert container1.stopped assert container1.removed_at is not None # service2 was excluded assert not container2.stopped assert container2.removed_at is None # If excluded is not empty, network should not be removed assert self.docker._networks_removed == []
def test_stop_existing_container(self): fake_context = FakeRunningContext() fake_service = FakeService(exception_at_init=ValueError) container = FakeContainer(name='{}-testing-5678'.format( fake_service.name), network='the-network', status='running') self.docker._existing_containers = [container] agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, fake_context) agent.stop_service() agent.join() assert agent.status == AgentStatus.STOPPED assert container.stopped assert len(fake_context.stopped_services) == 1 assert fake_context.stopped_services[0] is fake_service