Ejemplo n.º 1
0
def test_save():
    """
    Test saving beacons.
    """
    _beacon_conf_file = os.path.join(
        os.path.dirname(beacons.__opts__["conf_file"]),
        os.path.dirname(beacons.__opts__["default_include"]),
        "beacons.conf",
    )
    _beacons_data = {
        "ps": [{
            "processes": {
                "salt-master": "stopped",
                "apache2": "stopped"
            }
        }]
    }

    # Test that beacons contents are written to config file.
    _expected = {
        "comment": "Beacons saved to {}.".format(_beacon_conf_file),
        "result": True,
    }
    with patch("salt.utils.files.fopen",
               mock_open(read_data="")) as fopen_mock:
        with patch.object(beacons, "list_",
                          MagicMock(return_value=_beacons_data)):
            ret = beacons.save()
            assert ret == _expected

            _call = call(
                "beacons:\n  ps:\n  - processes:\n      apache2: stopped\n      salt-master: stopped\n"
            )
            write_calls = fopen_mock.filehandles[_beacon_conf_file][
                0].write._mock_mock_calls
            assert _call in write_calls

    _beacons_data = {}

    # Test that when beacons is empty then an empty config file is written.
    _expected = {
        "comment": "Beacons saved to {}.".format(_beacon_conf_file),
        "result": True,
    }
    with patch("salt.utils.files.fopen",
               mock_open(read_data="")) as fopen_mock:
        with patch.object(beacons, "list_",
                          MagicMock(return_value=_beacons_data)):
            ret = beacons.save()
            assert ret == _expected

            _call = call("")
            write_calls = fopen_mock.filehandles[_beacon_conf_file][
                0].write._mock_mock_calls
            assert _call in write_calls
Ejemplo n.º 2
0
    def test_save(self):
        """
        Test saving beacons.
        """
        comm1 = "Beacons saved to {}beacons.conf.".format(RUNTIME_VARS.TMP +
                                                          os.sep)
        with patch.dict(
                beacons.__opts__,
            {
                "conf_file": os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "foo"),
                "beacons": {},
                "default_include": RUNTIME_VARS.TMP + os.sep,
                "sock_dir": self.sock_dir,
            },
        ):

            mock = MagicMock(return_value=True)
            with patch.dict(beacons.__salt__, {"event.fire": mock}):
                _ret_value = {"complete": True, "beacons": {}}
                with patch.object(SaltEvent,
                                  "get_event",
                                  return_value=_ret_value):
                    self.assertDictEqual(beacons.save(), {
                        "comment": comm1,
                        "result": True
                    })
Ejemplo n.º 3
0
    def test_save(self):
        '''
        Test saving beacons.
        '''
        comm1 = 'Beacons saved to {0}beacons.conf.'.format(RUNTIME_VARS.TMP +
                                                           os.sep)
        with patch.dict(
                beacons.__opts__,
            {
                'conf_file': os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'foo'),
                'beacons': {},
                'default_include': RUNTIME_VARS.TMP + os.sep,
                'sock_dir': self.sock_dir
            }):

            mock = MagicMock(return_value=True)
            with patch.dict(beacons.__salt__, {'event.fire': mock}):
                _ret_value = {'complete': True, 'beacons': {}}
                with patch.object(SaltEvent,
                                  'get_event',
                                  return_value=_ret_value):
                    self.assertDictEqual(beacons.save(), {
                        'comment': comm1,
                        'result': True
                    })
Ejemplo n.º 4
0
    def test_save(self):
        '''
        Test saving beacons.
        '''
        comm1 = 'Beacons saved to {0}beacons.conf.'.format(TMP + os.sep)
        with patch.dict(beacons.__opts__, {'config_dir': '', 'beacons': {},
                                           'default_include': TMP + os.sep,
                                           'sock_dir': SOCK_DIR}):

            mock = MagicMock(return_value=True)
            with patch.dict(beacons.__salt__, {'event.fire': mock}):
                _ret_value = {'complete': True, 'beacons': {}}
                with patch.object(SaltEvent, 'get_event', return_value=_ret_value):
                    self.assertDictEqual(beacons.save(),
                                         {'comment': comm1, 'result': True})
Ejemplo n.º 5
0
class BeaconsTestCase(TestCase, LoaderModuleMockMixin):
    """
    Test cases for salt.modules.beacons
    '''

    @classmethod
    def setUpClass(cls):
        cls.sock_dir = os.path.join(RUNTIME_VARS.TMP, 'test-socks')

    def setup_loader_modules(self):
        return {beacons: {}}

    @skipIf(True, "SLOWTEST skip")
    def test_delete(self):
        """
        Test deleting a beacon.
        """
        comm1 = "Deleted beacon: ps."
        event_returns = [
            {
                "complete": True,
                "tag": "/salt/minion/minion_beacons_delete_complete",
                "beacons": {},
            },
        ]

        with patch.dict(beacons.__opts__, {'beacons': {'ps': [{'processes': {'salt-master': 'stopped', 'apache2': 'stopped'}}]}, 'sock_dir': self.sock_dir}):
            mock = MagicMock(return_value=True)
            with patch.dict(beacons.__salt__, {"event.fire": mock}):
                with patch.object(SaltEvent, "get_event", side_effect=event_returns):
                    self.assertDictEqual(
                        beacons.delete("ps"), {"comment": comm1, "result": True}
                    )

    @skipIf(True, "SLOWTEST skip")
    def test_add(self):
        """
        Test adding a beacon
        '''
        comm1 = 'Added beacon: ps.'
        event_returns = [{'complete': True,
                          'tag': '/salt/minion/minion_beacons_list_complete',
                          'beacons': {}},
                         {'complete': True,
                          'tag': '/salt/minion/minion_beacons_list_available_complete',
                          'beacons': ['ps']},
                         {'complete': True,
                          'valid': True,
                          'vcomment': '',
                          'tag': '/salt/minion/minion_beacons_list_complete'},
                         {'complete': True,
                          'tag': '/salt/minion/minion_beacon_add_complete',
                          'beacons': {'ps': [{'processes': {'salt-master': 'stopped', 'apache2': 'stopped'}}]}}]

        with patch.dict(beacons.__opts__, {'beacons': {}, 'sock_dir': self.sock_dir}):
            mock = MagicMock(return_value=True)
            with patch.dict(beacons.__salt__, {"event.fire": mock}):
                with patch.object(SaltEvent, "get_event", side_effect=event_returns):
                    self.assertDictEqual(
                        beacons.add(
                            "ps",
                            [
                                {
                                    "processes": {
                                        "salt-master": "stopped",
                                        "apache2": "stopped",
                                    }
                                }
                            ],
                        ),
                        {"comment": comm1, "result": True},
                    )

    @skipIf(True, "SLOWTEST skip")
    def test_save(self):
        """
        Test saving beacons.
        '''
        comm1 = 'Beacons saved to {0}beacons.conf.'.format(RUNTIME_VARS.TMP + os.sep)
        with patch.dict(beacons.__opts__, {'conf_file': os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'foo'),
                                           'beacons': {},
                                           'default_include': RUNTIME_VARS.TMP + os.sep,
                                           'sock_dir': self.sock_dir}):

            mock = MagicMock(return_value=True)
            with patch.dict(beacons.__salt__, {"event.fire": mock}):
                _ret_value = {"complete": True, "beacons": {}}
                with patch.object(SaltEvent, "get_event", return_value=_ret_value):
                    self.assertDictEqual(
                        beacons.save(), {"comment": comm1, "result": True}
                    )