def test_should_stop_services_then_reboot_then_start_services(self, dump_plan, state_reboot, state_metalogic): components = create_component_pool_for_one_host(add_services=True) state_reboot.return_value = components state_metalogic.return_value = state_reboot.return_value reboot(uris=["host://foobar42"]) dump_plan_calls = dump_plan.call_args_list self.assertEqual(len(dump_plan_calls), 1, "More than one plan for reboot was serialized! (potential overwrite)") dump_plan_call = dump_plan_calls[0] dump_plan_call_name, dump_plan_args = dump_plan_call[0] actual_reboot_plan = dump_plan_args # Careful, there's no missing comma here (string concatenation) expected_actions = [ Action("update", "host://foobar42", "state", "rebooted", kwargs={"reboot_required": True, "upgrade_packages": False}, preconditions=[ TargetState("service://foobar42/bazservice", "state", "down"), TargetState("service://foobar42/barservice", "state", "down")]), Action("start", "service://foobar42/barservice", "state", "up", preconditions=[ TargetState("host://foobar42", "state", "rebooted")]), Action("start", "service://foobar42/bazservice", "state", "up", preconditions=[ TargetState("host://foobar42", "state", "rebooted")]), Action("stop", "service://foobar42/barservice", "state", "down"), Action("stop", "service://foobar42/bazservice", "state", "down") ] assert_actual_plan_matches_expected_actions(self, expected_actions, actual_reboot_plan)
def test_should_render_reboot_now(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPDATE_NEEDED, host_reboot_now=True) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(' R reboot required', info_matrix)
def test_should_stop_no_services_when_host_has_no_services(self, state): state.return_value = create_component_pool_for_one_host() stop_plan = yadtshell._reboot.create_plan_to_stop_all_services_on( ["host://foobar42"]) self.assertEqual(stop_plan.actions, ())
def test_should_render_missing_artefact_problems(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( missing_artefact=True) info_matrix = self._call_info_and_render_output_to_string() self.assert_in("config problem: missing artefact://foobar42/missing", info_matrix)
def test_should_update_host_when_host_is_not_uptodate(self, components, action_plan, apply_instructions, dump_action_plan, action): components.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPDATE_NEEDED, next_artefacts_present=True) compare_versions() action.assert_called_with('update', 'host://foobar42', 'state', 'uptodate')
def test_should_not_update_anything_when_hosts_are_uptodate(self, components, action_plan, apply_instructions, dump_action_plan): components.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPTODATE) compare_versions() dump_action_plan.assert_called_with('update', action_plan.return_value) action_plan.assert_called_with('start', set([]))
def test_should_stop_all_services_without_preconditions(self, state): state.return_value = create_component_pool_for_one_host(add_services=True) stop_plan = yadtshell._reboot.create_plan_to_stop_all_services_on(["host://foobar42"]) for stop_action in stop_plan.list_actions: self.assertEqual(stop_action.cmd, "stop") self.assertEqual(stop_action.preconditions, set([]))
def test_should_render_update_needed_when_host_is_not_uptodate(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPDATE_NEEDED) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(' u host uptodate', info_matrix)
def test_should_stop_all_services(self, state): state.return_value = create_component_pool_for_one_host(add_services=True) stop_plan = yadtshell._reboot.create_plan_to_stop_all_services_on(["host://foobar42"]) self.assertEqual(stop_plan.dump(), 'stop [2 items, workers *undefined*, 0 errors tolerated]:\n' ' stop the service://foobar42/barservice, set state to "down"\n' ' stop the service://foobar42/bazservice, set state to "down"\n')
def test_should_render_uptodate_when_host_is_uptodate(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPTODATE) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(' | host uptodate', info_matrix) self.assert_in('1/1 hosts uptodate', info_matrix)
def test_should_render_missing_artefact_problems(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( missing_artefact=True) info_matrix = self._call_info_and_render_output_to_string() self.assert_in( "config problem: missing artefact://foobar42/missing", info_matrix)
def test_should_stop_all_services_without_preconditions(self, state): state.return_value = create_component_pool_for_one_host( add_services=True) stop_plan = yadtshell._reboot.create_plan_to_stop_all_services_on( ["host://foobar42"]) for stop_action in stop_plan.list_actions: self.assertEqual(stop_action.cmd, "stop") self.assertEqual(stop_action.preconditions, set([]))
def test_should_render_stopped_services(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPTODATE, add_services=True, service_state=yadtshell.settings.DOWN) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(' O service barservice', info_matrix) self.assert_in(' O service bazservice', info_matrix)
def test_should_start_services_unconditionally_when_other_host_is_rebooted(self, state): components = create_component_pool_for_one_host(add_services=True) state.return_value = components start_plan = yadtshell._reboot.create_plan_to_start_services_after_rebooting(["service://foobar42/barservice", "service://foobar42/bazservice"], ["host://foobar43"], components) self.assertEqual(start_plan.dump(), 'start [2 items, workers *undefined*, 0 errors tolerated]:\n' ' start the service://foobar42/barservice, set state to "up"\n' ' start the service://foobar42/bazservice, set state to "up"\n')
def test_should_render_cache_in_red_when_it_is_old(self, component_pool, cache_age): cache_age.return_value = 900 component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPDATE_NEEDED) info_matrix = self._call_info_and_render_output_to_string() self.assert_in("queried ${BG_RED}${WHITE}${BOLD} 900 ${NORMAL} seconds ago", info_matrix)
def test_should_stop_services_then_reboot_then_start_services( self, dump_plan, state_reboot, state_metalogic): components = create_component_pool_for_one_host(add_services=True) state_reboot.return_value = components state_metalogic.return_value = state_reboot.return_value reboot(uris=["host://foobar42"]) dump_plan_calls = dump_plan.call_args_list self.assertEqual( len(dump_plan_calls), 1, "More than one plan for reboot was serialized! (potential overwrite)" ) dump_plan_call = dump_plan_calls[0] dump_plan_call_name, dump_plan_args = dump_plan_call[0] actual_reboot_plan = dump_plan_args # Careful, there's no missing comma here (string concatenation) expected_actions = [ Action("update", "host://foobar42", "state", "rebooted", kwargs={ "reboot_required": True, "upgrade_packages": False }, preconditions=[ TargetState("service://foobar42/bazservice", "state", "down"), TargetState("service://foobar42/barservice", "state", "down") ]), Action("start", "service://foobar42/barservice", "state", "up", preconditions=[ TargetState("host://foobar42", "state", "rebooted") ]), Action("start", "service://foobar42/bazservice", "state", "up", preconditions=[ TargetState("host://foobar42", "state", "rebooted") ]), Action("stop", "service://foobar42/barservice", "state", "down"), Action("stop", "service://foobar42/bazservice", "state", "down") ] assert_actual_plan_matches_expected_actions(self, expected_actions, actual_reboot_plan)
def test_should_stop_all_services(self, state): state.return_value = create_component_pool_for_one_host( add_services=True) stop_plan = yadtshell._reboot.create_plan_to_stop_all_services_on( ["host://foobar42"]) self.assertEqual( stop_plan.dump(), 'stop [2 items, workers *undefined*, 0 errors tolerated]:\n' ' stop the service://foobar42/barservice, set state to "down"\n' ' stop the service://foobar42/bazservice, set state to "down"\n' )
def test_should_render_readonly_services(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( add_readonly_services=True) info_matrix = self._call_info_and_render_output_to_string() rendered_ro_services = ''' foobar42 | readonly-service ro_up (needed by me you) O readonly-service ro_down (needed by something a_dog) ''' self.assert_in(rendered_ro_services, info_matrix)
def test_should_render_artefact_problems_when_state_is_not_up(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPDATE_NEEDED, artefact_state=yadtshell.settings.MISSING) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(''' problems ${RED}${BOLD} missing${NORMAL} artefact://foobar42/yit/0:0.0.1 ${RED}${BOLD} missing${NORMAL} artefact://foobar42/foo/0:0.0.0 ''', info_matrix)
def test_should_start_services_unconditionally_when_other_host_is_rebooted( self, state): components = create_component_pool_for_one_host(add_services=True) state.return_value = components start_plan = yadtshell._reboot.create_plan_to_start_services_after_rebooting( ["service://foobar42/barservice", "service://foobar42/bazservice"], ["host://foobar43"], components) self.assertEqual( start_plan.dump(), 'start [2 items, workers *undefined*, 0 errors tolerated]:\n' ' start the service://foobar42/barservice, set state to "up"\n' ' start the service://foobar42/bazservice, set state to "up"\n')
def test_should_render_host_locked_by_other(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_locked_by_other=True) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(''' ${BG_RED}${WHITE}${BOLD} host://foobar42 is locked by foobar Reason: yes we can (lock the host) ${NORMAL} ''', info_matrix) self.assert_in(' L host access', info_matrix)
def test_should_render_host_locked_by_other(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_locked_by_other=True) info_matrix = self._call_info_and_render_output_to_string() self.assert_in( ''' ${BG_RED}${WHITE}${BOLD} host://foobar42 is locked by foobar Reason: yes we can (lock the host) ${NORMAL} ''', info_matrix) self.assert_in(' L host access', info_matrix)
def test_should_render_host_locked_by_me(self, component_pool): component_pool.return_value = create_component_pool_for_one_host( host_locked_by_me=True) info_matrix = self._call_info_and_render_output_to_string() self.assert_in(''' ${BG_YELLOW}${BOLD} foobar42 is locked by me Reason: yes we can (lock the host) ${NORMAL} ''', info_matrix) self.assert_in(' l host access', info_matrix)
def test_should_render_readonly_services(self, component_pool): component_pool.return_value = create_component_pool_for_one_host(add_readonly_services=True) info_matrix = self._call_info_and_render_output_to_string() rendered_ro_services = ''' foobar42 | readonly-service ro_up (needed by me you) O readonly-service ro_down (needed by something a_dog) ''' self.assert_in(rendered_ro_services, info_matrix)
def test_should_render_colored_readonly_services(self, component_pool): yadtshell._info.calculate_info_view_settings = lambda *args: {'color': 'yes'} component_pool.return_value = create_component_pool_for_one_host(add_readonly_services=True) info_matrix = self._call_info_and_render_output_to_string() rendered_ro_services = ''' foobar42 ${BG_GREEN}${WHITE}${BOLD}|${NORMAL} readonly-service ro_up (needed by me you) ${BG_RED}${WHITE}${BOLD}O${NORMAL} readonly-service ro_down (needed by something a_dog) ''' self.assert_in(rendered_ro_services, info_matrix)
def test_should_render_matrix_for_one_host(self, component_pool, mock_time): mock_time.return_value = 1 component_pool.return_value = create_component_pool_for_one_host( host_state=yadtshell.settings.UPDATE_NEEDED) info_matrix = self._call_info_and_render_output_to_string() expected = ''' ${BOLD}yadt info | test${NORMAL} target status foobar42 yit 0:0.0.1 (next) ${REVERSE}foo${NORMAL} 0:0.0.${REVERSE}0${NORMAL} f o o b a r 4 2 u host uptodate | reboot required | host access legend: | up(todate),accessible O down ? unknown io? ignored (up,down,unknown) lL locked by me/other u update pending rR reboot needed (after update/due to new kernel) queried ${BG_GREEN}${WHITE}${BOLD} 0 ${NORMAL} seconds ago status: 0% 0% | 0/0 services up, 0/1 hosts uptodate ''' self.assert_in(expected, info_matrix) self.assertEqual(expected, info_matrix)
def test_should_stop_no_services_when_host_has_no_services(self, state): state.return_value = create_component_pool_for_one_host() stop_plan = yadtshell._reboot.create_plan_to_stop_all_services_on(["host://foobar42"]) self.assertEqual(stop_plan.actions, ())