Пример #1
0
    def test_scheduler_before_connect(self):
        """
        Tests that the 'scheduler_before_connect' option causes the scheduler to be initialized before connect.
        """
        with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.minion.Minion.sync_connect_master",
            MagicMock(side_effect=RuntimeError("stop execution")),
        ), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
        ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
        ):
            mock_opts = self.get_config("minion", from_scratch=True)
            mock_opts["scheduler_before_connect"] = True
            io_loop = salt.ext.tornado.ioloop.IOLoop()
            io_loop.make_current()
            minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
            try:
                try:
                    minion.tune_in(start=True)
                except RuntimeError:
                    pass

                # Make sure the scheduler is initialized but the beacons are not
                self.assertTrue("schedule" in minion.periodic_callbacks)
                self.assertTrue("beacons" not in minion.periodic_callbacks)
            finally:
                minion.destroy()
Пример #2
0
def test_minion_module_refresh(tmp_path):
    """
    Tests that the 'module_refresh' just return in case there is no 'schedule'
    because destroy method was already called.
    """
    with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
    ):
        try:
            mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
            mock_opts["cachedir"] = str(tmp_path)
            minion = salt.minion.Minion(
                mock_opts,
                io_loop=salt.ext.tornado.ioloop.IOLoop(),
            )
            minion.schedule = salt.utils.schedule.Schedule(mock_opts, {},
                                                           returners={})
            assert hasattr(minion, "schedule")
            minion.destroy()
            assert not hasattr(minion, "schedule")
            assert not minion.module_refresh()
        finally:
            minion.destroy()
Пример #3
0
    def test_handle_decoded_payload_jid_queue_addition(self):
        """
        Tests that the _handle_decoded_payload function adds a jid to the minion's jid_queue when the new
        jid isn't already present in the jid_queue.
        """
        with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
        ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
        ):
            mock_jid = 11111
            mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
            mock_data = {"fun": "foo.bar", "jid": mock_jid}
            mock_jid_queue = [123, 456]
            minion = salt.minion.Minion(
                mock_opts,
                jid_queue=copy.copy(mock_jid_queue),
                io_loop=salt.ext.tornado.ioloop.IOLoop(),
            )
            try:

                # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
                # This can help debug any test failures if the _handle_decoded_payload call fails.
                self.assertEqual(minion.jid_queue, mock_jid_queue)

                # Call the _handle_decoded_payload function and update the mock_jid_queue to include the new
                # mock_jid. The mock_jid should have been added to the jid_queue since the mock_jid wasn't
                # previously included. The minion's jid_queue attribute and the mock_jid_queue should be equal.
                minion._handle_decoded_payload(mock_data).result()
                mock_jid_queue.append(mock_jid)
                self.assertEqual(minion.jid_queue, mock_jid_queue)
            finally:
                minion.destroy()
Пример #4
0
    def test_handle_decoded_payload_jid_queue_reduced_minion_jid_queue_hwm(
            self):
        '''
        Tests that the _handle_decoded_payload function removes a jid from the minion's jid_queue when the
        minion's jid_queue high water mark (minion_jid_queue_hwm) is hit.
        '''
        with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
                patch('salt.utils.process.SignalHandlingMultiprocessingProcess.start', MagicMock(return_value=True)), \
                patch('salt.utils.process.SignalHandlingMultiprocessingProcess.join', MagicMock(return_value=True)):
            mock_opts = salt.config.DEFAULT_MINION_OPTS
            mock_opts['minion_jid_queue_hwm'] = 2
            mock_data = {'fun': 'foo.bar', 'jid': 789}
            mock_jid_queue = [123, 456]
            try:
                minion = salt.minion.Minion(
                    mock_opts,
                    jid_queue=copy.copy(mock_jid_queue),
                    io_loop=tornado.ioloop.IOLoop())

                # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
                # This can help debug any test failures if the _handle_decoded_payload call fails.
                self.assertEqual(minion.jid_queue, mock_jid_queue)

                # Call the _handle_decoded_payload function and check that the queue is smaller by one item
                # and contains the new jid
                minion._handle_decoded_payload(mock_data)
                self.assertEqual(len(minion.jid_queue), 2)
                self.assertEqual(minion.jid_queue, [456, 789])
            finally:
                minion.destroy()
Пример #5
0
def test_minion_module_refresh_beacons_refresh(tmp_path):
    """
    Tests that 'module_refresh' calls beacons_refresh and that the
    minion object has a beacons attribute with beacons.
    """
    with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
    ):
        try:
            mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
            mock_opts["cachedir"] = str(tmp_path)
            minion = salt.minion.Minion(
                mock_opts,
                io_loop=salt.ext.tornado.ioloop.IOLoop(),
            )
            minion.schedule = salt.utils.schedule.Schedule(mock_opts, {},
                                                           returners={})
            assert not hasattr(minion, "beacons")
            minion.module_refresh()
            assert hasattr(minion, "beacons")
            assert hasattr(minion.beacons, "beacons")
            assert "service.beacon" in minion.beacons.beacons
            minion.destroy()
        finally:
            minion.destroy()
Пример #6
0
    def test_handle_decoded_payload_jid_queue_reduced_minion_jid_queue_hwm(self):
        '''
        Tests that the _handle_decoded_payload function removes a jid from the minion's jid_queue when the
        minion's jid_queue high water mark (minion_jid_queue_hwm) is hit.
        '''
        mock_opts = {'cachedir': '',
                     'extension_modules': '',
                     'minion_jid_queue_hwm': 2}
        mock_data = {'fun': 'foo.bar',
                     'jid': 789}
        mock_jid_queue = [123, 456]
        try:
            minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=tornado.ioloop.IOLoop())

            # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
            # This can help debug any test failures if the _handle_decoded_payload call fails.
            self.assertEqual(minion.jid_queue, mock_jid_queue)

            # Call the _handle_decoded_payload function and check that the queue is smaller by one item
            # and contains the new jid
            minion._handle_decoded_payload(mock_data)
            self.assertEqual(len(minion.jid_queue), 2)
            self.assertEqual(minion.jid_queue, [456, 789])
        finally:
            minion.destroy()
Пример #7
0
def test_beacons_before_connect():
    """
    Tests that the 'beacons_before_connect' option causes the beacons to be initialized before connect.
    """
    with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.minion.Minion.sync_connect_master",
            MagicMock(side_effect=RuntimeError("stop execution")),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
    ):
        mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
        mock_opts["beacons_before_connect"] = True
        io_loop = salt.ext.tornado.ioloop.IOLoop()
        io_loop.make_current()
        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
        try:

            try:
                minion.tune_in(start=True)
            except RuntimeError:
                pass

            # Make sure beacons are initialized but the sheduler is not
            assert "beacons" in minion.periodic_callbacks
            assert "schedule" not in minion.periodic_callbacks
        finally:
            minion.destroy()
Пример #8
0
    def test_handle_decoded_payload_jid_queue_reduced_minion_jid_queue_hwm(self):
        """
        Tests that the _handle_decoded_payload function removes a jid from the minion's jid_queue when the
        minion's jid_queue high water mark (minion_jid_queue_hwm) is hit.
        """
        with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
        ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
        ):
            mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
            mock_opts["minion_jid_queue_hwm"] = 2
            mock_data = {"fun": "foo.bar", "jid": 789}
            mock_jid_queue = [123, 456]
            minion = salt.minion.Minion(
                mock_opts,
                jid_queue=copy.copy(mock_jid_queue),
                io_loop=salt.ext.tornado.ioloop.IOLoop(),
            )
            try:

                # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
                # This can help debug any test failures if the _handle_decoded_payload call fails.
                self.assertEqual(minion.jid_queue, mock_jid_queue)

                # Call the _handle_decoded_payload function and check that the queue is smaller by one item
                # and contains the new jid
                minion._handle_decoded_payload(mock_data).result()
                self.assertEqual(len(minion.jid_queue), 2)
                self.assertEqual(minion.jid_queue, [456, 789])
            finally:
                minion.destroy()
Пример #9
0
    def test_handle_decoded_payload_jid_queue_addition(self):
        '''
        Tests that the _handle_decoded_payload function adds a jid to the minion's jid_queue when the new
        jid isn't already present in the jid_queue.
        '''
        mock_jid = 11111
        mock_opts = {'cachedir': '',
                     'extension_modules': '',
                     'minion_jid_queue_hwm': 100}
        mock_data = {'fun': 'foo.bar',
                     'jid': mock_jid}
        mock_jid_queue = [123, 456]
        try:
            minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=tornado.ioloop.IOLoop())

            # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
            # This can help debug any test failures if the _handle_decoded_payload call fails.
            self.assertEqual(minion.jid_queue, mock_jid_queue)

            # Call the _handle_decoded_payload function and update the mock_jid_queue to include the new
            # mock_jid. The mock_jid should have been added to the jid_queue since the mock_jid wasn't
            # previously included. The minion's jid_queue attribute and the mock_jid_queue should be equal.
            minion._handle_decoded_payload(mock_data)
            mock_jid_queue.append(mock_jid)
            self.assertEqual(minion.jid_queue, mock_jid_queue)
        finally:
            minion.destroy()
Пример #10
0
    def test_when_ping_interval_is_set_the_callback_should_be_added_to_periodic_callbacks(
        self,
    ):
        with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.minion.Minion.sync_connect_master",
            MagicMock(side_effect=RuntimeError("stop execution")),
        ), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
        ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
        ):
            mock_opts = self.get_config("minion", from_scratch=True)
            mock_opts["ping_interval"] = 10
            io_loop = salt.ext.tornado.ioloop.IOLoop()
            io_loop.make_current()
            minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
            try:
                try:
                    minion.connected = MagicMock(side_effect=(False, True))
                    minion._fire_master_minion_start = MagicMock()
                    minion.tune_in(start=False)
                except RuntimeError:
                    pass

                # Make sure the scheduler is initialized but the beacons are not
                self.assertTrue("ping" in minion.periodic_callbacks)
            finally:
                minion.destroy()
Пример #11
0
    def test_handle_decoded_payload_jid_queue_reduced_minion_jid_queue_hwm(self):
        '''
        Tests that the _handle_decoded_payload function removes a jid from the minion's jid_queue when the
        minion's jid_queue high water mark (minion_jid_queue_hwm) is hit.
        '''
        mock_opts = {'cachedir': '',
                     'extension_modules': '',
                     'minion_jid_queue_hwm': 2}
        mock_data = {'fun': 'foo.bar',
                     'jid': 789}
        mock_jid_queue = [123, 456]
        try:
            minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=tornado.ioloop.IOLoop())

            # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
            # This can help debug any test failures if the _handle_decoded_payload call fails.
            self.assertEqual(minion.jid_queue, mock_jid_queue)

            # Call the _handle_decoded_payload function and check that the queue is smaller by one item
            # and contains the new jid
            minion._handle_decoded_payload(mock_data)
            self.assertEqual(len(minion.jid_queue), 2)
            self.assertEqual(minion.jid_queue, [456, 789])
        finally:
            minion.destroy()
Пример #12
0
    def test_handle_decoded_payload_jid_queue_addition(self):
        '''
        Tests that the _handle_decoded_payload function adds a jid to the minion's jid_queue when the new
        jid isn't already present in the jid_queue.
        '''
        mock_jid = 11111
        mock_opts = {'cachedir': '',
                     'extension_modules': '',
                     'minion_jid_queue_hwm': 100}
        mock_data = {'fun': 'foo.bar',
                     'jid': mock_jid}
        mock_jid_queue = [123, 456]
        try:
            minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=tornado.ioloop.IOLoop())

            # Assert that the minion's jid_queue attribute matches the mock_jid_queue as a baseline
            # This can help debug any test failures if the _handle_decoded_payload call fails.
            self.assertEqual(minion.jid_queue, mock_jid_queue)

            # Call the _handle_decoded_payload function and update the mock_jid_queue to include the new
            # mock_jid. The mock_jid should have been added to the jid_queue since the mock_jid wasn't
            # previously included. The minion's jid_queue attribute and the mock_jid_queue should be equal.
            minion._handle_decoded_payload(mock_data)
            mock_jid_queue.append(mock_jid)
            self.assertEqual(minion.jid_queue, mock_jid_queue)
        finally:
            minion.destroy()
Пример #13
0
    def test_beacons_before_connect(self):
        '''
        Tests that the 'beacons_before_connect' option causes the beacons to be initialized before connect.
        '''
        with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
                patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \
                patch('salt.utils.process.SignalHandlingProcess.start', MagicMock(return_value=True)), \
                patch('salt.utils.process.SignalHandlingProcess.join', MagicMock(return_value=True)):
            mock_opts = self.get_config('minion', from_scratch=True)
            mock_opts['beacons_before_connect'] = True
            io_loop = tornado.ioloop.IOLoop()
            io_loop.make_current()
            minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
            try:

                try:
                    minion.tune_in(start=True)
                except RuntimeError:
                    pass

                # Make sure beacons are initialized but the sheduler is not
                self.assertTrue('beacons' in minion.periodic_callbacks)
                self.assertTrue('schedule' not in minion.periodic_callbacks)
            finally:
                minion.destroy()
Пример #14
0
def test_minion_manage_schedule():
    """
    Tests that the manage_schedule will call the add function, adding
    schedule data into opts.
    """
    with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.minion.Minion.sync_connect_master",
            MagicMock(side_effect=RuntimeError("stop execution")),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
    ):
        mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
        io_loop = salt.ext.tornado.ioloop.IOLoop()
        io_loop.make_current()

        with patch("salt.utils.schedule.clean_proc_dir",
                   MagicMock(return_value=None)):
            try:
                mock_functions = {"test.ping": None}

                minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
                minion.schedule = salt.utils.schedule.Schedule(
                    mock_opts,
                    mock_functions,
                    returners={},
                    new_instance=True,
                )

                minion.opts["foo"] = "bar"
                schedule_data = {
                    "test_job": {
                        "function": "test.ping",
                        "return_job": False,
                        "jid_include": True,
                        "maxrunning": 2,
                        "seconds": 10,
                    }
                }

                data = {
                    "name": "test-item",
                    "schedule": schedule_data,
                    "func": "add",
                    "persist": False,
                }
                tag = "manage_schedule"

                minion.manage_schedule(tag, data)
                assert "test_job" in minion.opts["schedule"]
            finally:
                del minion.schedule
                minion.destroy()
                del minion
Пример #15
0
def test_process_count_max():
    """
    Tests that the _handle_decoded_payload function does not spawn more than the configured amount of processes,
    as per process_count_max.
    """
    with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
    ), patch("salt.utils.minion.running", MagicMock(return_value=[])), patch(
            "salt.ext.tornado.gen.sleep",
            MagicMock(return_value=salt.ext.tornado.concurrent.Future()),
    ):
        process_count_max = 10
        mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
        mock_opts["__role"] = "minion"
        mock_opts["minion_jid_queue_hwm"] = 100
        mock_opts["process_count_max"] = process_count_max

        io_loop = salt.ext.tornado.ioloop.IOLoop()
        minion = salt.minion.Minion(mock_opts, jid_queue=[], io_loop=io_loop)
        try:

            # mock gen.sleep to throw a special Exception when called, so that we detect it
            class SleepCalledException(Exception):
                """Thrown when sleep is called"""

            salt.ext.tornado.gen.sleep.return_value.set_exception(
                SleepCalledException())

            # up until process_count_max: gen.sleep does not get called, processes are started normally
            for i in range(process_count_max):
                mock_data = {"fun": "foo.bar", "jid": i}
                io_loop.run_sync(lambda data=mock_data: minion.
                                 _handle_decoded_payload(data))
                assert (salt.utils.process.SignalHandlingProcess.start.
                        call_count == i + 1)
                assert len(minion.jid_queue) == i + 1
                salt.utils.minion.running.return_value += [i]

            # above process_count_max: gen.sleep does get called, JIDs are created but no new processes are started
            mock_data = {"fun": "foo.bar", "jid": process_count_max + 1}

            pytest.raises(
                SleepCalledException,
                lambda: io_loop.run_sync(lambda: minion.
                                         _handle_decoded_payload(mock_data)),
            )
            assert (salt.utils.process.SignalHandlingProcess.start.call_count
                    == process_count_max)
            assert len(minion.jid_queue) == process_count_max + 1
        finally:
            minion.destroy()
Пример #16
0
    def test_process_count_max(self):
        '''
        Tests that the _handle_decoded_payload function does not spawn more than the configured amount of processes,
        as per process_count_max.
        '''
        with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
                patch('salt.utils.process.SignalHandlingMultiprocessingProcess.start', MagicMock(return_value=True)), \
                patch('salt.utils.process.SignalHandlingMultiprocessingProcess.join', MagicMock(return_value=True)), \
                patch('salt.utils.minion.running', MagicMock(return_value=[])), \
                patch('tornado.gen.sleep', MagicMock(return_value=tornado.concurrent.Future())):
            process_count_max = 10
            mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
            mock_opts['__role'] = 'minion'
            mock_opts['minion_jid_queue_hwm'] = 100
            mock_opts["process_count_max"] = process_count_max

            io_loop = tornado.ioloop.IOLoop()
            minion = salt.minion.Minion(mock_opts,
                                        jid_queue=[],
                                        io_loop=io_loop)
            try:

                # mock gen.sleep to throw a special Exception when called, so that we detect it
                class SleepCalledException(Exception):
                    """Thrown when sleep is called"""
                    pass

                tornado.gen.sleep.return_value.set_exception(
                    SleepCalledException())

                # up until process_count_max: gen.sleep does not get called, processes are started normally
                for i in range(process_count_max):
                    mock_data = {'fun': 'foo.bar', 'jid': i}
                    io_loop.run_sync(lambda data=mock_data: minion.
                                     _handle_decoded_payload(data))
                    self.assertEqual(
                        salt.utils.process.
                        SignalHandlingMultiprocessingProcess.start.call_count,
                        i + 1)
                    self.assertEqual(len(minion.jid_queue), i + 1)
                    salt.utils.minion.running.return_value += [i]

                # above process_count_max: gen.sleep does get called, JIDs are created but no new processes are started
                mock_data = {'fun': 'foo.bar', 'jid': process_count_max + 1}

                self.assertRaises(
                    SleepCalledException, lambda: io_loop.run_sync(
                        lambda: minion._handle_decoded_payload(mock_data)))
                self.assertEqual(
                    salt.utils.process.SignalHandlingMultiprocessingProcess.
                    start.call_count, process_count_max)
                self.assertEqual(len(minion.jid_queue), process_count_max + 1)
            finally:
                minion.destroy()
Пример #17
0
    def test_when_not_passed_start_event_grains(self):
        mock_opts = self.get_config("minion", from_scratch=True)
        io_loop = salt.ext.tornado.ioloop.IOLoop()
        io_loop.make_current()
        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
        try:
            minion.tok = MagicMock()
            minion._send_req_sync = MagicMock()
            minion._fire_master("Minion has started", "minion_start")
            load = minion._send_req_sync.call_args[0][0]

            self.assertTrue("grains" not in load)
        finally:
            minion.destroy()
Пример #18
0
def test_when_not_passed_start_event_grains():
    mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
    io_loop = salt.ext.tornado.ioloop.IOLoop()
    io_loop.make_current()
    minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
    try:
        minion.tok = MagicMock()
        minion._send_req_sync = MagicMock()
        minion._fire_master("Minion has started", "minion_start")
        load = minion._send_req_sync.call_args[0][0]

        assert "grains" not in load
    finally:
        minion.destroy()
Пример #19
0
def test_when_other_events_fired_and_start_event_grains_are_set():
    mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
    mock_opts["start_event_grains"] = ["os"]
    io_loop = salt.ext.tornado.ioloop.IOLoop()
    io_loop.make_current()
    minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
    try:
        minion.tok = MagicMock()
        minion._send_req_sync = MagicMock()
        minion._fire_master("Custm_event_fired", "custom_event")
        load = minion._send_req_sync.call_args[0][0]

        assert "grains" not in load
    finally:
        minion.destroy()
Пример #20
0
    def test_when_other_events_fired_and_start_event_grains_are_set(self):
        mock_opts = self.get_config("minion", from_scratch=True)
        mock_opts["start_event_grains"] = ["os"]
        io_loop = salt.ext.tornado.ioloop.IOLoop()
        io_loop.make_current()
        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
        try:
            minion.tok = MagicMock()
            minion._send_req_sync = MagicMock()
            minion._fire_master("Custm_event_fired", "custom_event")
            load = minion._send_req_sync.call_args[0][0]

            self.assertTrue("grains" not in load)
        finally:
            minion.destroy()
Пример #21
0
    def test_when_passed_start_event_grains(self):
        mock_opts = self.get_config('minion', from_scratch=True)
        mock_opts['start_event_grains'] = ["os"]
        io_loop = tornado.ioloop.IOLoop()
        io_loop.make_current()
        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
        try:
            minion.tok = MagicMock()
            minion._send_req_sync = MagicMock()
            minion._fire_master('Minion has started', 'minion_start')
            load = minion._send_req_sync.call_args[0][0]

            self.assertTrue('grains' in load)
            self.assertTrue('os' in load['grains'])
        finally:
            minion.destroy()
Пример #22
0
    def test_when_passed_start_event_grains(self):
        mock_opts = self.get_config("minion", from_scratch=True)
        # provide mock opts an os grain since we'll look for it later.
        mock_opts["grains"]["os"] = "linux"
        mock_opts["start_event_grains"] = ["os"]
        io_loop = salt.ext.tornado.ioloop.IOLoop()
        io_loop.make_current()
        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
        try:
            minion.tok = MagicMock()
            minion._send_req_sync = MagicMock()
            minion._fire_master("Minion has started",
                                "minion_start",
                                include_startup_grains=True)
            load = minion._send_req_sync.call_args[0][0]

            self.assertTrue("grains" in load)
            self.assertTrue("os" in load["grains"])
        finally:
            minion.destroy()
Пример #23
0
    def test_gen_modules_executors(self):
        """
        Ensure gen_modules is called with the correct arguments #54429
        """
        mock_opts = self.get_config("minion", from_scratch=True)
        io_loop = salt.ext.tornado.ioloop.IOLoop()
        io_loop.make_current()
        minion = salt.minion.Minion(mock_opts, io_loop=io_loop)

        class MockPillarCompiler(object):
            def compile_pillar(self):
                return {}

        try:
            with patch("salt.pillar.get_pillar", return_value=MockPillarCompiler()):
                with patch("salt.loader.executors") as execmock:
                    minion.gen_modules()
            assert execmock.called_with(minion.opts, minion.functions)
        finally:
            minion.destroy()
Пример #24
0
def test_when_passed_start_event_grains():
    mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
    # provide mock opts an os grain since we'll look for it later.
    mock_opts["grains"]["os"] = "linux"
    mock_opts["start_event_grains"] = ["os"]
    io_loop = salt.ext.tornado.ioloop.IOLoop()
    io_loop.make_current()
    minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
    try:
        minion.tok = MagicMock()
        minion._send_req_sync = MagicMock()
        minion._fire_master("Minion has started",
                            "minion_start",
                            include_startup_grains=True)
        load = minion._send_req_sync.call_args[0][0]

        assert "grains" in load
        assert "os" in load["grains"]
    finally:
        minion.destroy()
Пример #25
0
    def test_handle_decoded_payload_jid_match_in_jid_queue(self):
        '''
        Tests that the _handle_decoded_payload function returns when a jid is given that is already present
        in the jid_queue.

        Note: This test doesn't contain all of the patch decorators above the function like the other tests
        for _handle_decoded_payload below. This is essential to this test as the call to the function must
        return None BEFORE any of the processes are spun up because we should be avoiding firing duplicate
        jobs.
        '''
        mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
        mock_data = {'fun': 'foo.bar',
                     'jid': 123}
        mock_jid_queue = [123]
        minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=tornado.ioloop.IOLoop())
        try:
            ret = minion._handle_decoded_payload(mock_data).result()
            self.assertEqual(minion.jid_queue, mock_jid_queue)
            self.assertIsNone(ret)
        finally:
            minion.destroy()
Пример #26
0
def test_minion_grains_refresh_pre_exec_true():
    """
    Minion refreshes grains when grains_refresh_pre_exec is True
    """
    mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
    mock_opts["multiprocessing"] = False
    mock_opts["grains_refresh_pre_exec"] = True
    mock_data = {"fun": "foo.bar", "jid": 123}
    with patch("salt.loader.grains") as grainsfunc, patch(
            "salt.minion.Minion._target", MagicMock(return_value=True)):
        minion = salt.minion.Minion(
            mock_opts,
            jid_queue=None,
            io_loop=salt.ext.tornado.ioloop.IOLoop(),
            load_grains=False,
        )
        try:
            ret = minion._handle_decoded_payload(mock_data).result()
            grainsfunc.assert_called()
        finally:
            minion.destroy()
Пример #27
0
    def test_scheduler_before_connect(self):
        '''
        Tests that the 'scheduler_before_connect' option causes the scheduler to be initialized before connect.
        '''
        with patch('salt.minion.Minion.ctx', MagicMock(return_value={})), \
                patch('salt.minion.Minion.sync_connect_master', MagicMock(side_effect=RuntimeError('stop execution'))), \
                patch('salt.utils.process.SignalHandlingMultiprocessingProcess.start', MagicMock(return_value=True)), \
                patch('salt.utils.process.SignalHandlingMultiprocessingProcess.join', MagicMock(return_value=True)):
            mock_opts = copy.copy(salt.config.DEFAULT_MINION_OPTS)
            mock_opts['scheduler_before_connect'] = True
            minion = salt.minion.Minion(mock_opts, io_loop=tornado.ioloop.IOLoop())
            try:
                try:
                    minion.tune_in(start=True)
                except RuntimeError:
                    pass

                # Make sure the scheduler is initialized but the beacons are not
                self.assertTrue('schedule' in minion.periodic_callbacks)
                self.assertTrue('beacons' not in minion.periodic_callbacks)
            finally:
                minion.destroy()
Пример #28
0
    def test_handle_decoded_payload_jid_match_in_jid_queue(self):
        '''
        Tests that the _handle_decoded_payload function returns when a jid is given that is already present
        in the jid_queue.

        Note: This test doesn't contain all of the patch decorators above the function like the other tests
        for _handle_decoded_payload below. This is essential to this test as the call to the function must
        return None BEFORE any of the processes are spun up because we should be avoiding firing duplicate
        jobs.
        '''
        mock_opts = {'cachedir': '',
                     'extension_modules': ''}
        mock_data = {'fun': 'foo.bar',
                     'jid': 123}
        mock_jid_queue = [123]
        try:
            minion = salt.minion.Minion(mock_opts, jid_queue=copy.copy(mock_jid_queue), io_loop=tornado.ioloop.IOLoop())
            ret = minion._handle_decoded_payload(mock_data)
            self.assertEqual(minion.jid_queue, mock_jid_queue)
            self.assertIsNone(ret)
        finally:
            minion.destroy()
Пример #29
0
def test_minion_manage_beacons():
    """
    Tests that the manage_beacons will call the add function, adding
    beacon data into opts.
    """
    with patch("salt.minion.Minion.ctx", MagicMock(return_value={})), patch(
            "salt.minion.Minion.sync_connect_master",
            MagicMock(side_effect=RuntimeError("stop execution")),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.start",
            MagicMock(return_value=True),
    ), patch(
            "salt.utils.process.SignalHandlingProcess.join",
            MagicMock(return_value=True),
    ):
        try:
            mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
            mock_opts["beacons"] = {}

            io_loop = salt.ext.tornado.ioloop.IOLoop()
            io_loop.make_current()

            mock_functions = {"test.ping": None}
            minion = salt.minion.Minion(mock_opts, io_loop=io_loop)
            minion.beacons = salt.beacons.Beacon(mock_opts, mock_functions)

            bdata = [{"salt-master": "stopped"}, {"apache2": "stopped"}]
            data = {"name": "ps", "beacon_data": bdata, "func": "add"}

            tag = "manage_beacons"
            log.debug("==== minion.opts %s ====", minion.opts)

            minion.manage_beacons(tag, data)
            assert "ps" in minion.opts["beacons"]
            assert minion.opts["beacons"]["ps"] == bdata
        finally:
            minion.destroy()
Пример #30
0
def test_handle_decoded_payload_jid_match_in_jid_queue():
    """
    Tests that the _handle_decoded_payload function returns when a jid is given that is already present
    in the jid_queue.

    Note: This test doesn't contain all of the patch decorators above the function like the other tests
    for _handle_decoded_payload below. This is essential to this test as the call to the function must
    return None BEFORE any of the processes are spun up because we should be avoiding firing duplicate
    jobs.
    """
    mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
    mock_data = {"fun": "foo.bar", "jid": 123}
    mock_jid_queue = [123]
    minion = salt.minion.Minion(
        mock_opts,
        jid_queue=copy.copy(mock_jid_queue),
        io_loop=salt.ext.tornado.ioloop.IOLoop(),
    )
    try:
        ret = minion._handle_decoded_payload(mock_data).result()
        assert minion.jid_queue == mock_jid_queue
        assert ret is None
    finally:
        minion.destroy()