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_container_does_not_exist(self): fake_context = FakeRunningContext() fake_service = FakeService(exception_at_init=ValueError) agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, fake_context) agent.stop_service() agent.join() assert agent.status == AgentStatus.STOPPED
def test_fail_if_action_not_set(self): service = Bunch(name='service1', dependencies=[], dependants=[]) fake_context = FakeRunningContext() agent = ServiceAgent(service, DEFAULT_OPTIONS, fake_context) with pytest.raises(ServiceAgentException): agent.run() assert len(fake_context.failed_services) == 1 assert fake_context.failed_services[0] is service
def test_pre_start_before_run(self): fake_context = FakeRunningContext() fake_service = FakeService() assert not fake_service.pre_start_called agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, fake_context) agent.start_service() agent.join() assert fake_service.pre_start_called
def test_build_on_start(self): fake_context = FakeRunningContext() fake_service = FakeService() fake_service.build_from = "the/service/dir" options = attr.evolve(DEFAULT_OPTIONS, build=[fake_service.name]) agent = ServiceAgent(fake_service, options, fake_context) agent.start_service() agent.join() assert len(self.docker._images_built) == 1
def test_if_build_from_and_latest(self): fake_context = FakeRunningContext() fake_service = FakeService() fake_service.image = "service:latest" fake_service.build_from = "the/service/dir" agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, fake_context) agent.start_service() agent.join() assert len(self.docker._images_built) == 1
def test_call_collection_failed_on_error(self): fake_context = FakeRunningContext() fake_service = FakeService(exception_at_init=ValueError) agent = ServiceAgent(fake_service, DEFAULT_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 test_build_image_dockerfile(self): fake_service = FakeService(name='myservice') fake_service.dockerfile = 'Dockerfile.other' fake_service.build_from = "the/service/dir" agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, FakeRunningContext()) agent.build_image() assert len(self.docker._images_built) == 1 _, dockerfile, _ = self.docker._images_built[0] assert dockerfile == 'Dockerfile.other'
def test_ping_and_init_after_run(self): fake_context = FakeRunningContext() fake_service = FakeService() agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, fake_context) agent.start_service() agent.join() assert len(fake_context.started_services) == 1 assert fake_context.started_services[0].name == 'service1' assert fake_service.ping_count == 1 assert fake_service.init_called
def test_repeat_ping_and_timeout(self, mock_time): mock_time.monotonic.side_effect = [0, 0.2, 0.6, 0.8, 1] fake_context = FakeRunningContext() fake_service = FakeService(fail_ping=True) agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, fake_context) agent.start_service() agent.join() assert fake_service.ping_count == 3 assert mock_time.sleep.call_count == 3 assert agent.status == AgentStatus.FAILED assert len(fake_context.failed_services) == 1 assert fake_context.failed_services[0] is fake_service
def test_agent_status_change_happy_path(self): class ServiceAgentTestSubclass(ServiceAgent): def ping(self): assert self.status == 'in-progress' return super().ping() agent = ServiceAgentTestSubclass(FakeService(), DEFAULT_OPTIONS, FakeRunningContext()) assert agent.status == 'null' agent.start_service() agent.join() assert agent.status == 'started'
def test_agent_status_change_sad_path(self): class ServiceAgentTestSubclass(ServiceAgent): def ping(self): assert self.status == 'in-progress' raise ValueError("I failed miserably") agent = ServiceAgentTestSubclass(FakeService(), DEFAULT_OPTIONS, FakeRunningContext()) assert agent.status == 'null' agent.start_service() agent.join() assert agent.status == 'failed'
def test_build_image(self, mock_datetime): now = datetime.now() mock_datetime.now.return_value = now fake_service = FakeService(name='myservice') fake_service.build_from = "the/service/dir" agent = ServiceAgent(fake_service, DEFAULT_OPTIONS, FakeRunningContext()) retval = agent.build_image() assert len(self.docker._images_built) == 1 build_dir, dockerfile, image_tag = self.docker._images_built[0] assert build_dir == "/etc/the/service/dir" assert dockerfile == 'Dockerfile' assert image_tag == now.strftime("myservice-%Y-%m-%d-%H%M") assert retval == image_tag assert RunCondition.BUILD_IMAGE in agent.run_condition.actions
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
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 test_yes_ping_no_init_if_started(self): service = FakeService() fake_context = FakeRunningContext() agent = ServiceAgent(service, DEFAULT_OPTIONS, fake_context) self.docker._existing_containers = [ Bunch(status='exited', network='the-network', id='longass-container-id', image=Bunch(tags=[service.image]), attrs={'Config': { 'Env': [] }}, name="{}-testing-123".format(service.name)) ] agent.start_service() agent.join() assert service.ping_count == 1 assert not service.init_called assert self.docker._containers_ran == ['longass-container-id']
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