Exemplo n.º 1
0
class JobStateTests(TestCase):

    def setUp(self):
        self.job = make_job("A")
        self.job_state = JobState(self.job)

    def test_smoke(self):
        self.assertIsNotNone(self.job_state.result)
        self.assertIs(self.job_state.result.job, self.job)
        self.assertIs(self.job_state.result.outcome, JobResult.OUTCOME_NONE)
        self.assertEqual(self.job_state.readiness_inhibitor_list, [
            UndesiredJobReadinessInhibitor])

    def test_getting_job(self):
        self.assertIs(self.job_state.job, self.job)

    def test_setting_job_is_not_allowed(self):
        with self.assertRaises(AttributeError):
            self.job_state.job = None

    def test_setting_result(self):
        result = make_job_result(self.job)
        self.job_state.result = result
        self.assertIs(self.job_state.result, result)

    def test_setting_readiness_inhibitor_list(self):
        inhibitor = JobReadinessInhibitor(JobReadinessInhibitor.UNDESIRED)
        self.job_state.readiness_inhibitor_list = [inhibitor]
        self.assertEqual(self.job_state.readiness_inhibitor_list, [inhibitor])

    def test_can_start(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertTrue(self.job_state.can_start())
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor]
        self.assertFalse(self.job_state.can_start())

    def test_readiness_description(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertEqual(self.job_state.get_readiness_description(),
                         "job can be started")
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor]
        self.assertTrue(
            self.job_state.get_readiness_description().startswith(
                "job cannot be started: "))
Exemplo n.º 2
0
 def test_encode_resource_job(self):
     self.job_R = make_job("R", plugin="resource")
     result_R = JobResult({
         'job': self.job_R,
         'outcome': JobResult.OUTCOME_PASS,
         'io_log': ((0, 'stdout', "attr: value\n"),)
     })
     jobstate = JobState(self.job_R)
     jobstate.result = result_R
     jobstate_enc = jobstate._get_persistance_subset()
     # The inhibitor list is not saved
     with self.assertRaises(KeyError):
         jobstate_enc['_readiness_inhibitor_list']
     # Resource have to be re evealutated on startup, outcome of the job
     # must be reset to JobResult.OUTCOME_NONE
     self.assertEqual(jobstate_enc['_result'].outcome,
                      JobResult.OUTCOME_NONE)
Exemplo n.º 3
0
 def test_encode_resource_job(self):
     self.job_R = make_job("R", plugin="resource")
     result_R = JobResult({
         'job': self.job_R,
         'outcome': JobResult.OUTCOME_PASS,
         'io_log': ((0, 'stdout', "attr: value\n"), )
     })
     jobstate = JobState(self.job_R)
     jobstate.result = result_R
     jobstate_enc = jobstate._get_persistance_subset()
     # The inhibitor list is not saved
     with self.assertRaises(KeyError):
         jobstate_enc['_readiness_inhibitor_list']
     # Resource have to be re evealutated on startup, outcome of the job
     # must be reset to JobResult.OUTCOME_NONE
     self.assertEqual(jobstate_enc['_result'].outcome,
                      JobResult.OUTCOME_NONE)
Exemplo n.º 4
0
 def run(self):
     job = self._get_job()
     if job is None:
         print(_("There is no job called {!a}").format(self.job_id))
         print(
             _("See `plainbox special --list-jobs` for a list of choices"))
         return 126
     elif job.command is None:
         print(_("Selected job does not have a command"))
         return 125
     with TemporaryDirectory() as scratch, TemporaryDirectory() as iologs:
         runner = JobRunner(scratch, self.provider_list, iologs)
         job_state = JobState(job)
         ctrl = runner._get_ctrl_for_job(job)
         runner.log_leftovers = False
         runner.on_leftover_files.connect(self._on_leftover_files)
         return_code, record_path = runner._run_command(
             job, job_state, self.config, ctrl)
         self._display_script_outcome(job, return_code)
     return return_code
Exemplo n.º 5
0
 def setUp(self):
     self.job = make_job("A")
     self.job_state = JobState(self.job)
Exemplo n.º 6
0
class JobStateTests(TestCase):
    def setUp(self):
        self.job = make_job("A")
        self.job_state = JobState(self.job)

    def test_smoke(self):
        self.assertIsNotNone(self.job_state.result)
        self.assertIs(self.job_state.result.outcome, IJobResult.OUTCOME_NONE)
        self.assertEqual(self.job_state.readiness_inhibitor_list,
                         [UndesiredJobReadinessInhibitor])

    def test_getting_job(self):
        self.assertIs(self.job_state.job, self.job)

    @expectedFailure
    def test_setting_job_is_not_allowed(self):
        #FIXME: We want this test to come back at some point so I didn't
        #delete it, but at the moment we need it to always pass because
        #a JobState's job attribute needs to be writable.
        with self.assertRaises(AttributeError):
            self.job_state.job = None

    def test_setting_result(self):
        result = make_job_result()
        self.job_state.result = result
        self.assertIs(self.job_state.result, result)

    def test_setting_result_fires_signal(self):
        """
        verify that assigning state.result fires the on_result_changed signal
        """
        # Remember both new and old result for verification
        new_result = make_job_result()
        old_result = self.job_state.result

        def changed_callback(old, new):
            # Verify that new and old are correct and not swapped
            self.assertIs(new, new_result)
            self.assertIs(old, old_result)
            # Set a flag that we verify below in case this never gets called
            self.on_changed_fired = True

        # Connect the signal handler
        self.job_state.on_result_changed.connect(changed_callback)
        # Assign the new result
        self.job_state.result = new_result
        # Ensure that the signal was fired and called our callback
        self.assertTrue(self.on_changed_fired)

    def test_setting_result_fires_signal_only_when_real_change_happens(self):
        """
        verify that assigning state.result does NOT fire the signal when the
        new result is the same
        """
        # Assume we never get called and reset the flag
        self.on_changed_fired = False

        def changed_callback(old, new):
            # Set the flag in case we do get called
            self.on_changed_fired = True

        # Connect the signal handler
        self.job_state.on_result_changed.connect(changed_callback)
        # Assign the same result again
        self.job_state.result = self.job_state.result
        # Ensure that the signal was NOT fired
        self.assertFalse(self.on_changed_fired)

    def test_setting_readiness_inhibitor_list(self):
        inhibitor = JobReadinessInhibitor(JobReadinessInhibitor.UNDESIRED)
        self.job_state.readiness_inhibitor_list = [inhibitor]
        self.assertEqual(self.job_state.readiness_inhibitor_list, [inhibitor])

    def test_can_start(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertTrue(self.job_state.can_start())
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor
        ]
        self.assertFalse(self.job_state.can_start())

    def test_readiness_description(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertEqual(self.job_state.get_readiness_description(),
                         "job can be started")
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor
        ]
        self.assertTrue(self.job_state.get_readiness_description().startswith(
            "job cannot be started: "))
Exemplo n.º 7
0
class JobStateTests(TestCase):
    def setUp(self):
        self.job = make_job("A")
        self.job_state = JobState(self.job)

    def test_smoke(self):
        self.assertIsNotNone(self.job_state.result)
        self.assertIs(self.job_state.result.outcome, IJobResult.OUTCOME_NONE)
        self.assertEqual(self.job_state.result_history, ())
        self.assertEqual(self.job_state.readiness_inhibitor_list,
                         [UndesiredJobReadinessInhibitor])
        self.assertEqual(self.job_state.effective_category_id,
                         self.job.category_id)
        self.assertEqual(self.job_state.effective_certification_status,
                         self.job.certification_status)
        self.assertEqual(self.job_state.effective_auto_retry,
                         self.job.auto_retry)
        self.assertIsNone(self.job_state.via_job)

    def test_getting_job(self):
        self.assertIs(self.job_state.job, self.job)

    @expectedFailure
    def test_setting_job_is_not_allowed(self):
        # FIXME: We want this test to come back at some point so I didn't
        # delete it, but at the moment we need it to always pass because
        # a JobState's job attribute needs to be writable.
        with self.assertRaises(AttributeError):
            self.job_state.job = None

    def test_setting_result(self):
        result = make_job_result()
        self.job_state.result = result
        self.assertIs(self.job_state.result, result)

    def test_result_history_keeps_track_of_result_changes(self):
        # XXX: this example will fail if subsequent results are identical
        self.assertEqual(self.job_state.result_history, ())
        result1 = make_job_result(outcome='fail')
        self.job_state.result = result1
        self.assertEqual(self.job_state.result_history, (result1, ))
        result2 = make_job_result(outcome='pass')
        self.job_state.result = result2
        self.assertEqual(self.job_state.result_history, (result1, result2))

    def test_setting_result_fires_signal(self):
        """
        verify that assigning state.result fires the on_result_changed signal
        """
        # Remember both new and old result for verification
        new_result = make_job_result()
        old_result = self.job_state.result

        def changed_callback(old, new):
            # Verify that new and old are correct and not swapped
            self.assertIs(new, new_result)
            self.assertIs(old, old_result)
            # Set a flag that we verify below in case this never gets called
            self.on_changed_fired = True

        # Connect the signal handler
        self.job_state.on_result_changed.connect(changed_callback)
        # Assign the new result
        self.job_state.result = new_result
        # Ensure that the signal was fired and called our callback
        self.assertTrue(self.on_changed_fired)

    def test_setting_result_fires_signal_only_when_real_change_happens(self):
        """
        verify that assigning state.result does NOT fire the signal when the
        new result is the same
        """
        # Assume we never get called and reset the flag
        self.on_changed_fired = False

        def changed_callback(old, new):
            # Set the flag in case we do get called
            self.on_changed_fired = True

        # Connect the signal handler
        self.job_state.on_result_changed.connect(changed_callback)
        # Assign the same result again
        self.job_state.result = self.job_state.result
        # Ensure that the signal was NOT fired
        self.assertFalse(self.on_changed_fired)

    def test_setting_readiness_inhibitor_list(self):
        inhibitor = JobReadinessInhibitor(InhibitionCause.UNDESIRED)
        self.job_state.readiness_inhibitor_list = [inhibitor]
        self.assertEqual(self.job_state.readiness_inhibitor_list, [inhibitor])

    def test_can_start(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertTrue(self.job_state.can_start())
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor
        ]
        self.assertFalse(self.job_state.can_start())

    def test_readiness_description(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertEqual(self.job_state.get_readiness_description(),
                         "job can be started")
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor
        ]
        self.assertTrue(self.job_state.get_readiness_description().startswith(
            "job cannot be started: "))

    def test_setting_effective_category_id(self):
        self.job_state.effective_category_id = 'value'
        self.assertEqual(self.job_state.effective_category_id, 'value')

    def test_setting_effective_cert_certification_status(self):
        self.job_state.effective_certification_status = 'value'
        self.assertEqual(self.job_state.effective_certification_status,
                         'value')

    def test_setting_via_job__TypeError(self):
        with self.assertRaises(TypeError):
            self.job_state.via_job = 'value'

    def test_setting_via_job(self):
        parent = make_job("parent")
        self.job_state.via_job = parent
        self.assertIs(self.job_state.via_job, parent)

    def test_resetting_via_job(self):
        parent = make_job("parent")
        self.job_state.via_job = parent
        self.assertIs(self.job_state.via_job, parent)
        self.job_state.via_job = None
        self.assertIs(self.job_state.via_job, None)
Exemplo n.º 8
0
 def setUp(self):
     self.job = make_job("A")
     self.job_state = JobState(self.job)
Exemplo n.º 9
0
class JobStateTests(TestCase):
    def setUp(self):
        self.job = make_job("A")
        self.job_state = JobState(self.job)

    def test_smoke(self):
        self.assertIsNotNone(self.job_state.result)
        self.assertIs(self.job_state.result.outcome, IJobResult.OUTCOME_NONE)
        self.assertEqual(self.job_state.readiness_inhibitor_list, [UndesiredJobReadinessInhibitor])

    def test_getting_job(self):
        self.assertIs(self.job_state.job, self.job)

    @expectedFailure
    def test_setting_job_is_not_allowed(self):
        # FIXME: We want this test to come back at some point so I didn't
        # delete it, but at the moment we need it to always pass because
        # a JobState's job attribute needs to be writable.
        with self.assertRaises(AttributeError):
            self.job_state.job = None

    def test_setting_result(self):
        result = make_job_result()
        self.job_state.result = result
        self.assertIs(self.job_state.result, result)

    def test_setting_result_fires_signal(self):
        """
        verify that assigning state.result fires the on_result_changed signal
        """
        # Remember both new and old result for verification
        new_result = make_job_result()
        old_result = self.job_state.result

        def changed_callback(old, new):
            # Verify that new and old are correct and not swapped
            self.assertIs(new, new_result)
            self.assertIs(old, old_result)
            # Set a flag that we verify below in case this never gets called
            self.on_changed_fired = True

        # Connect the signal handler
        self.job_state.on_result_changed.connect(changed_callback)
        # Assign the new result
        self.job_state.result = new_result
        # Ensure that the signal was fired and called our callback
        self.assertTrue(self.on_changed_fired)

    def test_setting_result_fires_signal_only_when_real_change_happens(self):
        """
        verify that assigning state.result does NOT fire the signal when the
        new result is the same
        """
        # Assume we never get called and reset the flag
        self.on_changed_fired = False

        def changed_callback(old, new):
            # Set the flag in case we do get called
            self.on_changed_fired = True

        # Connect the signal handler
        self.job_state.on_result_changed.connect(changed_callback)
        # Assign the same result again
        self.job_state.result = self.job_state.result
        # Ensure that the signal was NOT fired
        self.assertFalse(self.on_changed_fired)

    def test_setting_readiness_inhibitor_list(self):
        inhibitor = JobReadinessInhibitor(JobReadinessInhibitor.UNDESIRED)
        self.job_state.readiness_inhibitor_list = [inhibitor]
        self.assertEqual(self.job_state.readiness_inhibitor_list, [inhibitor])

    def test_can_start(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertTrue(self.job_state.can_start())
        self.job_state.readiness_inhibitor_list = [UndesiredJobReadinessInhibitor]
        self.assertFalse(self.job_state.can_start())

    def test_readiness_description(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertEqual(self.job_state.get_readiness_description(), "job can be started")
        self.job_state.readiness_inhibitor_list = [UndesiredJobReadinessInhibitor]
        self.assertTrue(self.job_state.get_readiness_description().startswith("job cannot be started: "))
Exemplo n.º 10
0
class JobStateTests(TestCase):

    def setUp(self):
        self.job = make_job("A")
        self.job_state = JobState(self.job)

    def test_smoke(self):
        self.assertIsNotNone(self.job_state.result)
        self.assertIs(self.job_state.result.job, self.job)
        self.assertIs(self.job_state.result.outcome, JobResult.OUTCOME_NONE)
        self.assertEqual(self.job_state.readiness_inhibitor_list, [
            UndesiredJobReadinessInhibitor])

    def test_getting_job(self):
        self.assertIs(self.job_state.job, self.job)

    def test_setting_job_is_not_allowed(self):
        with self.assertRaises(AttributeError):
            self.job_state.job = None

    def test_setting_result(self):
        result = make_job_result(self.job)
        self.job_state.result = result
        self.assertIs(self.job_state.result, result)

    def test_setting_readiness_inhibitor_list(self):
        inhibitor = JobReadinessInhibitor(JobReadinessInhibitor.UNDESIRED)
        self.job_state.readiness_inhibitor_list = [inhibitor]
        self.assertEqual(self.job_state.readiness_inhibitor_list, [inhibitor])

    def test_can_start(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertTrue(self.job_state.can_start())
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor]
        self.assertFalse(self.job_state.can_start())

    def test_readiness_description(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertEqual(self.job_state.get_readiness_description(),
                         "job can be started")
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor]
        self.assertTrue(
            self.job_state.get_readiness_description().startswith(
                "job cannot be started: "))

    def test_encode_resource_job(self):
        self.job_R = make_job("R", plugin="resource")
        result_R = JobResult({
            'job': self.job_R,
            'outcome': JobResult.OUTCOME_PASS,
            'io_log': ((0, 'stdout', "attr: value\n"),)
        })
        jobstate = JobState(self.job_R)
        jobstate.result = result_R
        jobstate_enc = jobstate._get_persistance_subset()
        # The inhibitor list is not saved
        with self.assertRaises(KeyError):
            jobstate_enc['_readiness_inhibitor_list']
        # Resource have to be re evealutated on startup, outcome of the job
        # must be reset to JobResult.OUTCOME_NONE
        self.assertEqual(jobstate_enc['_result'].outcome,
                         JobResult.OUTCOME_NONE)

    def test_encode_normal_job(self):
        result = JobResult({
            'job': self.job,
            'outcome': JobResult.OUTCOME_PASS,
        })
        self.job_state.result = result
        jobstate_enc = self.job_state._get_persistance_subset()
        # The inhibitor list is not saved
        with self.assertRaises(KeyError):
            jobstate_enc['_readiness_inhibitor_list']
        # Normal jobs should keep their outcome value
        self.assertEqual(jobstate_enc['_result'].outcome,
                         JobResult.OUTCOME_PASS)

    def test_decode(self):
        raw_json = """{
            "_class_id": "JOB_STATE",
            "_job": {
                "_class_id": "JOB_DEFINITION",
                "data": {
                    "name": "X",
                    "plugin": "dummy"
                }
            },
            "_result": {
                "_class_id": "JOB_RESULT",
                "data": {
                    "comments": null,
                    "job": {
                        "_class_id": "JOB_DEFINITION",
                        "data": {
                            "name": "X",
                            "plugin": "dummy"
                        }
                    },
                    "outcome": "pass",
                    "return_code": null
                }
            }
        }"""
        job_dec = json.loads(
            raw_json, object_hook=SessionStateEncoder().dict_to_object)
        self.assertIsInstance(job_dec, JobState)
        self.assertEqual(
            repr(job_dec._result), (
                "<JobResult job:<JobDefinition name:'X'"
                " plugin:'dummy'> outcome:'pass'>"))
Exemplo n.º 11
0
class JobStateTests(TestCase):
    def setUp(self):
        self.job = make_job("A")
        self.job_state = JobState(self.job)

    def test_smoke(self):
        self.assertIsNotNone(self.job_state.result)
        self.assertIs(self.job_state.result.job, self.job)
        self.assertIs(self.job_state.result.outcome, JobResult.OUTCOME_NONE)
        self.assertEqual(self.job_state.readiness_inhibitor_list,
                         [UndesiredJobReadinessInhibitor])

    def test_getting_job(self):
        self.assertIs(self.job_state.job, self.job)

    def test_setting_job_is_not_allowed(self):
        with self.assertRaises(AttributeError):
            self.job_state.job = None

    def test_setting_result(self):
        result = make_job_result(self.job)
        self.job_state.result = result
        self.assertIs(self.job_state.result, result)

    def test_setting_readiness_inhibitor_list(self):
        inhibitor = JobReadinessInhibitor(JobReadinessInhibitor.UNDESIRED)
        self.job_state.readiness_inhibitor_list = [inhibitor]
        self.assertEqual(self.job_state.readiness_inhibitor_list, [inhibitor])

    def test_can_start(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertTrue(self.job_state.can_start())
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor
        ]
        self.assertFalse(self.job_state.can_start())

    def test_readiness_description(self):
        self.job_state.readiness_inhibitor_list = []
        self.assertEqual(self.job_state.get_readiness_description(),
                         "job can be started")
        self.job_state.readiness_inhibitor_list = [
            UndesiredJobReadinessInhibitor
        ]
        self.assertTrue(self.job_state.get_readiness_description().startswith(
            "job cannot be started: "))

    def test_encode_resource_job(self):
        self.job_R = make_job("R", plugin="resource")
        result_R = JobResult({
            'job': self.job_R,
            'outcome': JobResult.OUTCOME_PASS,
            'io_log': ((0, 'stdout', "attr: value\n"), )
        })
        jobstate = JobState(self.job_R)
        jobstate.result = result_R
        jobstate_enc = jobstate._get_persistance_subset()
        # The inhibitor list is not saved
        with self.assertRaises(KeyError):
            jobstate_enc['_readiness_inhibitor_list']
        # Resource have to be re evealutated on startup, outcome of the job
        # must be reset to JobResult.OUTCOME_NONE
        self.assertEqual(jobstate_enc['_result'].outcome,
                         JobResult.OUTCOME_NONE)

    def test_encode_normal_job(self):
        result = JobResult({
            'job': self.job,
            'outcome': JobResult.OUTCOME_PASS,
        })
        self.job_state.result = result
        jobstate_enc = self.job_state._get_persistance_subset()
        # The inhibitor list is not saved
        with self.assertRaises(KeyError):
            jobstate_enc['_readiness_inhibitor_list']
        # Normal jobs should keep their outcome value
        self.assertEqual(jobstate_enc['_result'].outcome,
                         JobResult.OUTCOME_PASS)

    def test_decode(self):
        raw_json = """{
            "_class_id": "JOB_STATE",
            "_job": {
                "_class_id": "JOB_DEFINITION",
                "data": {
                    "name": "X",
                    "plugin": "dummy"
                }
            },
            "_result": {
                "_class_id": "JOB_RESULT",
                "data": {
                    "comments": null,
                    "job": {
                        "_class_id": "JOB_DEFINITION",
                        "data": {
                            "name": "X",
                            "plugin": "dummy"
                        }
                    },
                    "outcome": "pass",
                    "return_code": null
                }
            }
        }"""
        job_dec = json.loads(raw_json,
                             object_hook=SessionStateEncoder().dict_to_object)
        self.assertIsInstance(job_dec, JobState)
        self.assertEqual(repr(job_dec._result),
                         ("<JobResult job:<JobDefinition name:'X'"
                          " plugin:'dummy'> outcome:'pass'>"))