def test_raise_exception_on_same_name(self): services = [ Bunch(name="hello", image="hello"), Bunch(name="hello", image="goodbye") ] with pytest.raises(ServiceLoadError): connect_services(services)
def test_exception_on_invalid_dependency(self): services = [ Bunch(name="hello", image="hello", dependencies=[]), Bunch(name="goodbye", image="goodbye", dependencies=["not_hello"]) ] with pytest.raises(ServiceLoadError): connect_services(services)
def test_can_stop(self): services = connect_services([ Bunch(name='service1', dependencies=[]), Bunch(name='service2', dependencies=['service1']) ]) agent = ServiceAgent(services['service1'], DEFAULT_OPTIONS, None) assert agent.can_stop is False agent.process_service_stopped(services['service2']) assert agent.can_stop is True
def test_can_start(self): services = connect_services([ Bunch(name='service1', dependencies=[]), Bunch(name='service2', dependencies=['service1']) ]) agent = ServiceAgent(services['service2'], DEFAULT_OPTIONS, None) assert agent.can_start is False agent.process_service_started(services['service1']) assert agent.can_start is True agent.status = AgentStatus.IN_PROGRESS assert agent.can_start is False
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_start_existing_if_differing_env_value_type_but_not_string(self): service = FakeService() service.env = {'KEY': 12345} agent = ServiceAgent(service, DEFAULT_OPTIONS, None) self.docker._existing_containers = [ Bunch(status='exited', network='the-network', id='longass-container-id', image=Bunch(tags=[service.image]), attrs={'Config': { 'Env': ['KEY=12345'] }}, name="{}-testing-123".format(service.name)) ] agent.run_image() assert len(self.docker._services_started) == 0
def test_start_old_container_if_exists(self): service = FakeService() agent = ServiceAgent(service, DEFAULT_OPTIONS, None) 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.run_image() assert len(self.docker._services_started) == 0 assert self.docker._containers_ran == ['longass-container-id']
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_action_property(self): service = Bunch(name='service1', dependencies=[], dependants=[]) agent = ServiceAgent(service, DEFAULT_OPTIONS, None) assert agent.action is None with pytest.raises(ServiceAgentException): agent.action = 'blah' agent.action = 'start' assert agent.action == 'start'
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_drop_event(self): envelope = Bunch(delivery_tag=12345) mock_channel = MockChannel() event = Event(channel=mock_channel, body=EVENT_PAYLOAD, envelope=envelope) helpers.aio_run(event.drop()) assert len(mock_channel.rejected) == 1 assert mock_channel.rejected[0] == (12345, False)
def test_no_matching_remote(self, mock_git, mock_error_prompt): repo = mock_git.Repo.return_value repo.active_branch = 'the-branch' remote = mock.MagicMock() remote.refs = [Bunch(name='', commit='')] repo.remotes = [remote] with pytest.raises(SystemExit): head = kubedeploy.head_of('/blah') mock_error_prompt.assert_called_once_with( 'No remote tracking branch matching "the-branch" found')
def test_matching_remote(self, mock_git, mock_error_prompt): repo = mock_git.Repo.return_value repo.active_branch = 'the-branch' remote = mock.MagicMock() remote.name = 'origin' remote.refs = [ Bunch(name='origin/the-branch', commit='githash-remote') ] repo.remotes = [remote] head = kubedeploy.head_of('/blah') assert head == repo.git.rev_parse.return_value
def test_all_good(self): services = [ Bunch(name="hello", image="hello", dependencies=[]), Bunch(name="goodbye", image="goodbye", dependencies=["hello"]), Bunch(name="howareyou", image="howareyou", dependencies=["hello", "goodbye"]) ] by_name = connect_services(services) assert len(by_name) == 3 hello = by_name['hello'] assert hello.dependencies == [] assert len(hello.dependants) == 2 assert by_name['goodbye'] in hello.dependants assert by_name['howareyou'] in hello.dependants howareyou = by_name['howareyou'] assert len(howareyou.dependencies) == 2 assert hello in howareyou.dependencies assert by_name['goodbye'] in howareyou.dependencies assert howareyou.dependants == []
def test_start_new_container_if_old_has_different_tag(self): service = FakeService() agent = ServiceAgent(service, DEFAULT_OPTIONS, None) self.docker._existing_containers = [ Bunch(status='exited', network='the-network', id='longass-container-id', image=Bunch(tags=['different-tag']), attrs={'Config': { 'Env': [] }}, name="{}-miniboss-123".format(service.name)) ] agent.run_image() assert len(self.docker._services_started) == 1 prefix, service, network = self.docker._services_started[0] assert prefix == "service1-testing" assert service.name == 'service1' assert service.image == 'not/used' assert network.name == 'the-network' assert self.docker._containers_ran == []
def test_ack_event(self): envelope = Bunch(delivery_tag=12345) mock_channel = MockChannel() event = Event(channel=mock_channel, body=EVENT_PAYLOAD, envelope=envelope) event.validate() assert isinstance(event.payload, EventPayload) helpers.aio_run(event.ack()) assert len(mock_channel.acked) == 1 assert mock_channel.acked[0] == 12345
def test_skip_if_running_on_same_network(self): service = FakeService() agent = ServiceAgent(service, DEFAULT_OPTIONS, None) self.docker._existing_containers = [ Bunch(status='running', name="{}-testing-123".format(service.name), network='the-network') ] agent.run_image() assert len(self.docker._services_started) == 0 assert len(self.docker._existing_queried) == 1 assert self.docker._existing_queried[0] == ("service1-testing", Network( name="the-network", id="the-network-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