def setUp(self):
        super(TestBuildbotSchedulersGeneratedFromConfigAssignment, self).setUp()

        self.assignment_model = Mock()
        self.assignment_model.config = dumps({})

        self.assignment = Assignment(model=self.assignment_model)
class TestJobsConfiguration(DatabaseTestCase):
    def setUp(self):
        super(TestJobsConfiguration, self).setUp()

        self._mock_resolver()

        register_mock_jobs_and_commands()

        job_model = Mock()
        self.job = EchoJob(model=job_model)

        job_model.get_domain_object.return_value = self.job

        computer = MockBuildComputer()
        computer.id = 1

        project = MockProject()
        project.id = 1

        assignment_model = Mock()
        assignment_model.computer = computer
        assignment_model.job = self.job.model
        assignment_model.project = project,
        assignment_model.config = dumps({})
        self.assignment = Assignment(model=assignment_model)

    def _mock_resolver(self):
        self._original_resolver = urlresolvers.get_resolver

        resolver = Mock()
        self.prefix = get_script_prefix()
        self.mocked_uri = resolver.reverse.return_value="heureka"

        urlresolvers.get_resolver = lambda conf: resolver

    def _unmock_resolver(self):
        urlresolvers.get_resolver = self._original_resolver

    def test_unconfigured_job_retrieval(self):
        self.assert_raises(UnconfiguredCommandError, self.assignment.get_shell_commands)

    def test_loading_empty_configuration_still_raises_error(self):
        self.assert_raises(UnconfiguredCommandError, self.assignment.get_shell_commands)

    def test_configuration_propageted_to_command(self):
        text = 'bazaah!'
        self.assignment.model.config = dumps({
            'commands' : [
                {
                    'command' : 'cthulhubot-test-helper-echo',
                    'parameters' : {
                        'what' : text
                    }
                }
            ]})
        self.assert_equals([['echo', text]], self.assignment.get_shell_commands())

    def tearDown(self):
        self._unmock_resolver()
        super(TestJobsConfiguration, self).tearDown()
    def setUp(self):
        super(TestAssignment, self).setUp()
        register_mock_jobs_and_commands()

        self.computer = MockBuildComputer()
        self.computer.adapter = Mock()
        self.job = MultipleEchoJob()
        self.job.model = MockJob()
        self.job.model.get_domain_object = Mock()
        self.job.model.get_domain_object.return_value = self.job

        self.project = MockProject()

        self.assignment_model = JobAssignment(pk=1, project=self.project, computer=self.computer, job=self.job.model)
        self.assignment = Assignment(model=self.assignment_model)


        self._mock_resolver()
    def setUp(self):
        super(TestJobsConfiguration, self).setUp()

        self._mock_resolver()

        register_mock_jobs_and_commands()

        job_model = Mock()
        self.job = EchoJob(model=job_model)

        job_model.get_domain_object.return_value = self.job

        computer = MockBuildComputer()
        computer.id = 1

        project = MockProject()
        project.id = 1

        assignment_model = Mock()
        assignment_model.computer = computer
        assignment_model.job = self.job.model
        assignment_model.project = project,
        assignment_model.config = dumps({})
        self.assignment = Assignment(model=assignment_model)
class TestBuildbotSchedulersGeneratedFromConfigAssignment(UnitTestCase):
    def setUp(self):
        super(TestBuildbotSchedulersGeneratedFromConfigAssignment, self).setUp()

        self.assignment_model = Mock()
        self.assignment_model.config = dumps({})

        self.assignment = Assignment(model=self.assignment_model)

    def _update_config(self, config):
        conf = loads(self.assignment_model.config)
        conf.update(config)
        self.assignment_model.config = dumps(conf)

    def test_single_scheduler_generated_by_default(self):
        self.assert_equals(1, len(self.assignment.get_schedulers()))

    def test_after_push_generated_by_default_when_no_branch_given(self):
        self.assert_equals(AnyBranchScheduler, self.assignment.get_schedulers()[0].__class__)

    def test_all_consuming_scheduler_generated_by_default(self):
        raise self.SkipTest("TODO: Don't know how to check for all branches with new filters...")
        self.assert_equals(None, self.assignment.get_schedulers()[0].branches)

    def test_no_branch_after_push_means_any_branch(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'after_push',
                    'parameters' : {
                        'treeStableTimer' : 1,
                        'branch' : ""
                    }
                }
            ]
        })
        self.assert_equals(AnyBranchScheduler, self.assignment.get_schedulers()[0].__class__)

    def test_branched_scheduler_means_after_push(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'after_push',
                    'parameters' : {
                        'treeStableTimer' : 1,
                        'branch' : "mastah"
                    }
                }
            ]
        })
        self.assert_equals(Scheduler, self.assignment.get_schedulers()[0].__class__)

    def test_default_scheduler_replaced_by_configured_one(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'after_push',
                    'parameters' : {
                        'treeStableTimer' : 1,
                        'branch' : "mastah"
                    }
                }
            ]
        })
        self.assert_equals(1, len(self.assignment.get_schedulers()))

    def test_branch_name_propagated_to_push_scheduler(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'after_push',
                    'parameters' : {
                        'treeStableTimer' : 1,
                        'branch' : "mastah"
                    }
                }
            ]
        })
        self.assert_equals("mastah", self.assignment.get_schedulers()[0].branch)

    def test_periodic_builder_propagated_with_proper_timer(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'periodic',
                    'parameters' : {
                        'periodicBuildTimer' : 249,
                    }
                }
            ]
        })
        self.assert_equals(249, self.assignment.get_schedulers()[0].periodicBuildTimer)

    def test_all_branches_are_default_for_configured_schedulers(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'periodic',
                    'parameters' : {
                        'periodicBuildTimer' : 249,
                    }
                }
            ]
        })
        self.assert_equals(None, self.assignment.get_schedulers()[0].branch)

    def test_multiple_schedulers_accepted(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'periodic',
                    'parameters' : {
                        'periodicBuildTimer' : 249,
                    }
                },
                {
                    'identifier' : 'after_push',
                    'parameters' : {
                        'treeStableTimer' : 100,
                        'branch' : 'mastah'
                    }
                }
            ]
        })
        self.assert_equals(2, len(self.assignment.get_schedulers()))

    def test_multiple_schedulers_propagates_parameters_properly(self):
        self._update_config({
            'schedule' : [
                {
                    'identifier' : 'periodic',
                    'parameters' : {
                        'periodicBuildTimer' : 249,
                    }
                },
                {
                    'identifier' : 'after_push',
                    'parameters' : {
                        'treeStableTimer' : 100,
                        'branch' : 'mastah'
                    }
                }
            ]
        })
        self.assert_equals(249, self.assignment.get_schedulers()[0].periodicBuildTimer)
        self.assert_equals(100, self.assignment.get_schedulers()[1].treeStableTimer)
class TestAssignment(UnitTestCase):
    def setUp(self):
        super(TestAssignment, self).setUp()
        register_mock_jobs_and_commands()

        self.computer = MockBuildComputer()
        self.computer.adapter = Mock()
        self.job = MultipleEchoJob()
        self.job.model = MockJob()
        self.job.model.get_domain_object = Mock()
        self.job.model.get_domain_object.return_value = self.job

        self.project = MockProject()

        self.assignment_model = JobAssignment(pk=1, project=self.project, computer=self.computer, job=self.job.model)
        self.assignment = Assignment(model=self.assignment_model)


        self._mock_resolver()

    def _mock_resolver(self):
        self._original_resolver = urlresolvers.get_resolver

        resolver = Mock()
        self.prefix = get_script_prefix()
        self.mocked_uri = resolver.reverse.return_value="heureka"

        urlresolvers.get_resolver = lambda conf: resolver

    def _unmock_resolver(self):
        urlresolvers.get_resolver = self._original_resolver
        self._original_resolver = None

    def test_job_mocked_properly(self):
        self.assert_equals(self.job, self.assignment.job)

    def test_url_retrieving(self):
        self.assert_equals(self.prefix+self.mocked_uri, self.assignment.get_absolute_url())

    def test_configuration_to_factory_propagated_properly(self):
        self.assignment.model.config = dumps({
            "commands" : [{}, {}, {}],
        })
        self.assert_equals(["echo", "first"], self.assignment.get_factory().steps[0][1]['command'])

    def test_factory_generated_even_when_parameters_not_given(self):
        self.assignment.model.config = dumps({
            "commands" : [{'command' : 'cthulhubot-test-helper-echo'}, {}, {}],
        })
        self.assert_equals(["echo", "first"], self.assignment.get_factory().steps[0][1]['command'])

    def test_error_raised_when_command_identifier_not_given_for_parameters(self):
        self.assignment.model.config = dumps({
            "commands" : [
                {
                    'parameters' : {
                        'what' : 'first'
                    }
                },
                {},
                {}
            ],
        })
        self.assert_raises(ValueError, self.assignment.get_factory)

    def test_error_raised_when_bad_command_identifier_given(self):
        self.assignment.model.config = dumps({
            "commands" : [
                {
                    'command' : 'xxx-bad-command-identifier-for-this-position',
                },
                {},
                {}
            ],
        })
        self.assert_raises(ValueError, self.assignment.get_factory)

    def tearDown(self):
        super(TestAssignment, self).tearDown()

        self._unmock_resolver()