def test_start_new_if_always_start_new(self): service = FakeService() service.always_start_new = True options = Options(network=Network(name='the-network', id='the-network-id'), timeout=1, remove=True, run_dir='/etc', build=[]) agent = ServiceAgent(service, options, None) restarted = False def start(): nonlocal restarted restarted = True self.docker._existing_containers = [ Bunch(status='exited', start=start, network='the-network', attrs={'Config': { 'Env': [] }}, name="{}-testing-123".format(service.name)) ] agent.run_image() assert len(self.docker._services_started) == 1 assert not restarted
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_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_service_failed_on_failed_ping(self): fake_context = FakeRunningContext() fake_service = FakeService(fail_ping=True) # Using options with low timeout so that test doesn't hang options = Options(network=Network(name='the-network', id='the-network-id'), timeout=0.1, remove=True, run_dir='/etc', build=[]) agent = ServiceAgent(fake_service, options, fake_context) agent.start_service() agent.join() assert fake_service.ping_count > 0 assert fake_context.started_services == [] assert len(fake_context.failed_services) == 1 assert fake_context.failed_services[0].name == 'service1'
def start_services(maindir, exclude, network_name, timeout): if types.group_name is None: raise MinibossException( "Group name is not set; set it with miniboss.group_name in the main script" ) Context.load_from(maindir) collection = ServiceCollection() collection.load_definitions() collection.exclude_for_start(exclude) network_name = network_name or "miniboss-{}".format(types.group_name) options = Options(network=Network(name=network_name, id=''), timeout=timeout, remove=False, run_dir=maindir, build=[]) service_names = collection.start_all(options) logger.info("Started services: %s", ", ".join(service_names)) Context.save_to(maindir)
def stop_services(maindir, exclude, network_name, remove, timeout): if types.group_name is None: raise MinibossException( "Group name is not set; set it with miniboss.group_name in the main script" ) logger.info("Stopping services (excluded: %s)", "none" if not exclude else ",".join(exclude)) network_name = network_name or "miniboss-{}".format(types.group_name) options = Options(network=Network(name=network_name, id=''), timeout=timeout, remove=remove, run_dir=maindir, build=[]) collection = ServiceCollection() collection.load_definitions() collection.exclude_for_stop(exclude) collection.stop_all(options) if remove: Context.remove_file(maindir)
def test_no_pre_ping_or_init_if_running(self): service = FakeService() fake_context = FakeRunningContext() options = Options(network=Network(name='the-network', id='the-network-id'), timeout=1, remove=True, run_dir='/etc', build=[]) agent = ServiceAgent(service, options, fake_context) self.docker._existing_containers = [ Bunch(status='running', network='the-network', name="{}-testing-123".format(service.name)) ] agent.start_service() agent.join() assert service.ping_count == 0 assert not service.init_called assert not service.pre_start_called
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 reload_service(maindir, service, network_name, remove, timeout): if types.group_name is None: raise MinibossException( "Group name is not set; set it with miniboss.group_name in the main script" ) network_name = network_name or "miniboss-{}".format(types.group_name) options = Options(network=Network(name=network_name, id=''), timeout=timeout, remove=remove, run_dir=maindir, build=[service]) stop_collection = ServiceCollection() stop_collection.load_definitions() stop_collection.check_can_be_built(service) stop_collection.update_for_base_service(service) stop_collection.stop_all(options) # We don't need to do this earlier, as the context is not used by the stop # functionality Context.load_from(maindir) start_collection = ServiceCollection() start_collection.load_definitions() start_collection.start_all(options) Context.save_to(maindir)