示例#1
0
def test_exclude_parameter_is_not_passed_if_not_provided(fake_cmd):
    # Make sure we don't barf on existing behavior
    args = ("webserver_setup", "webserver2")
    kwargs_without_exclude = {
        "tgt_type": "glob",
        "highstate": True,
    }

    saltmod.state(*args, **kwargs_without_exclude)

    call = fake_cmd.call_args[1]
    assert "exclude" not in call["kwarg"]
示例#2
0
 def test_statemod_state(self):
     ''' Smoke test for for salt.states.statemod.state().  Ensures that we
         don't take an exception if optional parameters are not specified in
         __opts__ or __env__.
     '''
     args = ('webserver_setup', 'webserver2')
     kwargs = {
         'tgt_type': 'glob',
         'fail_minions': None,
         'pillar': None,
         'top': None,
         'batch': None,
         'orchestration_jid': None,
         'sls': 'vroom',
         'queue': False,
         'concurrent': False,
         'highstate': None,
         'expr_form': None,
         'ret': '',
         'ssh': False,
         'timeout': None, 'test': False,
         'allow_fail': 0,
         'saltenv': None,
         'expect_minions': False
     }
     ret = saltmod.state(*args, **kwargs)
     expected = {
         'comment': 'States ran successfully.',
         'changes': {},
         'name': 'webserver_setup',
         'result': True
     }
     self.assertEqual(ret, expected)
示例#3
0
 def test_statemod_state(self):
     """ Smoke test for for salt.states.statemod.state().  Ensures that we
         don't take an exception if optional parameters are not specified in
         __opts__ or __env__.
     """
     args = ("webserver_setup", "webserver2")
     kwargs = {
         "tgt_type": "glob",
         "fail_minions": None,
         "pillar": None,
         "top": None,
         "batch": None,
         "orchestration_jid": None,
         "sls": "vroom",
         "queue": False,
         "concurrent": False,
         "highstate": None,
         "expr_form": None,
         "ret": "",
         "ssh": False,
         "timeout": None,
         "test": False,
         "allow_fail": 0,
         "saltenv": None,
         "expect_minions": False,
     }
     ret = saltmod.state(*args, **kwargs)
     expected = {
         "comment": "States ran successfully.",
         "changes": {},
         "name": "webserver_setup",
         "result": True,
     }
     self.assertEqual(ret, expected)
示例#4
0
def test_exclude_parameter_gets_passed(exclude, fake_cmd):
    """
    Smoke test for for salt.states.statemod.state().  Ensures that we
    don't take an exception if optional parameters are not specified in
    __opts__ or __env__.
    """
    args = ("webserver_setup", "webserver2")
    expected_exclude = exclude
    kwargs = {
        "tgt_type": "glob",
        "exclude": expected_exclude,
        "highstate": True,
    }

    saltmod.state(*args, **kwargs)

    call = fake_cmd.call_args[1]
    assert call["kwarg"]["exclude"] == expected_exclude
示例#5
0
    def test_state(self):
        '''
        Test to invoke a state run on a given target
        '''
        name = 'state'
        tgt = 'minion1'

        comt = ('Passed invalid value for \'allow_fail\', must be an int')

        ret = {'name': name,
               'changes': {},
               'result': False,
               'comment': comt}

        test_ret = {'name': name,
                    'changes': {},
                    'result': True,
                    'comment': 'States ran successfully.'
                    }

        self.assertDictEqual(saltmod.state(name, tgt, allow_fail='a'), ret)

        comt = ('No highstate or sls specified, no execution made')
        ret.update({'comment': comt})
        self.assertDictEqual(saltmod.state(name, tgt), ret)

        comt = ("Must pass in boolean for value of 'concurrent'")
        ret.update({'comment': comt})
        self.assertDictEqual(saltmod.state(name, tgt, highstate=True,
                                           concurrent='a'), ret)

        ret.update({'comment': comt, 'result': None})
        with patch.dict(saltmod.__opts__, {'test': True}):
            self.assertDictEqual(saltmod.state(name, tgt, highstate=True), test_ret)

        ret.update({'comment': 'States ran successfully.', 'result': True})
        with patch.dict(saltmod.__opts__, {'test': False}):
            mock = MagicMock(return_value={})
            with patch.dict(saltmod.__salt__, {'saltutil.cmd': mock}):
                self.assertDictEqual(saltmod.state(name, tgt, highstate=True),
                                     ret)
示例#6
0
    def test_state(self):
        '''
        Test to invoke a state run on a given target
        '''
        name = 'state'
        tgt = 'minion1'

        comt = ('Passed invalid value for \'allow_fail\', must be an int')

        ret = {'name': name,
               'changes': {},
               'result': False,
               'comment': comt}

        test_ret = {'name': name,
                    'changes': {},
                    'result': True,
                    'comment': 'States ran successfully.'
                    }

        self.assertDictEqual(saltmod.state(name, tgt, allow_fail='a'), ret)

        comt = ('No highstate or sls specified, no execution made')
        ret.update({'comment': comt})
        self.assertDictEqual(saltmod.state(name, tgt), ret)

        comt = ("Must pass in boolean for value of 'concurrent'")
        ret.update({'comment': comt})
        self.assertDictEqual(saltmod.state(name, tgt, highstate=True,
                                           concurrent='a'), ret)

        ret.update({'comment': comt, 'result': None})
        with patch.dict(saltmod.__opts__, {'test': True}):
            self.assertDictEqual(saltmod.state(name, tgt, highstate=True), test_ret)

        ret.update({'comment': 'States ran successfully. No changes made to silver.', 'result': True, '__jid__': '20170406104341210934'})
        with patch.dict(saltmod.__opts__, {'test': False}):
            mock = MagicMock(return_value={'silver': {'jid': '20170406104341210934', 'retcode': 0, 'ret': {'test_|-notify_me_|-this is a name_|-show_notification': {'comment': 'Notify me', 'name': 'this is a name', 'start_time': '10:43:41.487565', 'result': True, 'duration': 0.35, '__run_num__': 0, '__sls__': 'demo', 'changes': {}, '__id__': 'notify_me'}}, 'out': 'highstate'}})
            with patch.dict(saltmod.__salt__, {'saltutil.cmd': mock}):
                self.assertDictEqual(saltmod.state(name, tgt, highstate=True),
                                     ret)
示例#7
0
    def test_state_ssh(self):
        '''
        Test saltmod passes roster to saltutil.cmd
        '''
        origcmd = saltmod.__salt__['saltutil.cmd']
        cmd_kwargs = {}
        cmd_args = []

        def cmd_mock(*args, **kwargs):
            cmd_args.extend(args)
            cmd_kwargs.update(kwargs)
            return origcmd(*args, **kwargs)

        with patch.dict(saltmod.__salt__, {'saltutil.cmd': cmd_mock}):
            ret = saltmod.state('state.sls', tgt='*', ssh=True, highstate=True, roster='my_roster')
        assert 'roster' in cmd_kwargs
        assert cmd_kwargs['roster'] == 'my_roster'
示例#8
0
 def test_statemod_state(self):
     '''
         Smoke test for for salt.states.statemod.state(). Ensures that we
         don't take an exception if optional parameters are not specified in
         __opts__ or __env__.
     '''
     argv = []
     saltmod.__env__ = {}
     saltmod.__salt__ = {'saltutil.cmd': MagicMock()}
     saltmod.__opts__ = {
         'id': 'webserver2',
         'argv': argv,
         '__role': 'master',
         'cachedir': self.tmp_cachedir,
         'extension_modules': os.path.join(self.tmp_cachedir, 'extmods'),
     }
     args = ('webserver_setup', 'webserver2')
     kwargs = {
         'tgt_type': 'glob',
         'fail_minions': None,
         'pillar': None,
         'top': None,
         'batch': None,
         'orchestration_jid': None,
         'sls': 'vroom',
         'queue': False,
         'concurrent': False,
         'highstate': None,
         'expr_form': None,
         'ret': '',
         'ssh': False,
         'timeout': None,
         'test': False,
         'allow_fail': 0,
         'saltenv': None,
         'expect_minions': False
     }
     ret = saltmod.state(*args, **kwargs)
     expected = {
         'comment': 'States ran successfully.',
         'changes': {},
         'name': 'webserver_setup',
         'result': True
     }
     self.assertEqual(ret, expected)
示例#9
0
    def test_state_ssh(self):
        """
        Test saltmod state passes roster to saltutil.cmd
        """
        origcmd = saltmod.__salt__["saltutil.cmd"]
        cmd_kwargs = {}
        cmd_args = []

        def cmd_mock(*args, **kwargs):
            cmd_args.extend(args)
            cmd_kwargs.update(kwargs)
            return origcmd(*args, **kwargs)

        with patch.dict(saltmod.__salt__, {"saltutil.cmd": cmd_mock}):
            ret = saltmod.state(
                "state.sls", tgt="*", ssh=True, highstate=True, roster="my_roster"
            )
        assert "roster" in cmd_kwargs
        assert cmd_kwargs["roster"] == "my_roster"
示例#10
0
    def test_state(self):
        '''
        Test to invoke a state run on a given target
        '''
        name = 'state'
        tgt = 'minion1'

        comt = ('Passed invalid value for \'allow_fail\', must be an int')

        ret = {'name': name,
               'changes': {},
               'result': False,
               'comment': comt}

        test_ret = {'name': name,
                    'changes': {},
                    'result': True,
                    'comment': 'States ran successfully.'
                    }

        test_batch_return = {
            'minion1': {
                'ret': {
                    'test_|-notify_me_|-this is a name_|-show_notification': {
                        'comment': 'Notify me',
                        'name': 'this is a name',
                        'start_time': '10:43:41.487565',
                        'result': True,
                        'duration': 0.35,
                        '__run_num__': 0,
                        '__sls__': 'demo',
                        'changes': {},
                        '__id__': 'notify_me'
                    },
                    'retcode': 0
                },
                'out': 'highstate'
            },
            'minion2': {
                'ret': {
                    'test_|-notify_me_|-this is a name_|-show_notification': {
                        'comment': 'Notify me',
                        'name': 'this is a name',
                        'start_time': '10:43:41.487565',
                        'result': True,
                        'duration': 0.35,
                        '__run_num__': 0,
                        '__sls__': 'demo',
                        'changes': {},
                        '__id__': 'notify_me'
                    },
                    'retcode': 0
                },
                'out': 'highstate'
            },
            'minion3': {
                'ret': {
                    'test_|-notify_me_|-this is a name_|-show_notification': {
                        'comment': 'Notify me',
                        'name': 'this is a name',
                        'start_time': '10:43:41.487565',
                        'result': True,
                        'duration': 0.35,
                        '__run_num__': 0,
                        '__sls__': 'demo',
                        'changes': {},
                        '__id__': 'notify_me'
                    },
                    'retcode': 0
                },
                'out': 'highstate'
            }
        }

        self.assertDictEqual(saltmod.state(name, tgt, allow_fail='a'), ret)

        comt = ('No highstate or sls specified, no execution made')
        ret.update({'comment': comt})
        self.assertDictEqual(saltmod.state(name, tgt), ret)

        comt = ("Must pass in boolean for value of 'concurrent'")
        ret.update({'comment': comt})
        self.assertDictEqual(saltmod.state(name, tgt, highstate=True,
                                           concurrent='a'), ret)

        ret.update({'comment': comt, 'result': None})
        with patch.dict(saltmod.__opts__, {'test': True}):
            self.assertDictEqual(saltmod.state(name, tgt, highstate=True), test_ret)

        ret.update({'comment': 'States ran successfully. No changes made to silver.', 'result': True, '__jid__': '20170406104341210934'})
        with patch.dict(saltmod.__opts__, {'test': False}):
            mock = MagicMock(return_value={'silver': {'jid': '20170406104341210934', 'retcode': 0, 'ret': {'test_|-notify_me_|-this is a name_|-show_notification': {'comment': 'Notify me', 'name': 'this is a name', 'start_time': '10:43:41.487565', 'result': True, 'duration': 0.35, '__run_num__': 0, '__sls__': 'demo', 'changes': {}, '__id__': 'notify_me'}}, 'out': 'highstate'}})
            with patch.dict(saltmod.__salt__, {'saltutil.cmd': mock}):
                self.assertDictEqual(saltmod.state(name, tgt, highstate=True), ret)

        ret.update({'comment': 'States ran successfully. No changes made to minion1, minion3, minion2.'})
        del ret['__jid__']
        with patch.dict(saltmod.__opts__, {'test': False}):
            with patch.dict(saltmod.__salt__, {'saltutil.cmd': MagicMock(return_value=test_batch_return)}):
                state_run = saltmod.state(name, tgt, highstate=True)

                # Test return without checking the comment contents. Comments are tested later.
                comment = state_run.pop('comment')
                ret.pop('comment')
                self.assertDictEqual(state_run, ret)

                # Check the comment contents in a non-order specific way (ordering fails sometimes on PY3)
                self.assertIn('States ran successfully. No changes made to', comment)
                for minion in ['minion1', 'minion2', 'minion3']:
                    self.assertIn(minion, comment)
示例#11
0
    def test_state(self):
        """
        Test to invoke a state run on a given target
        """
        name = "state"
        tgt = "minion1"

        comt = "Passed invalid value for 'allow_fail', must be an int"

        ret = {"name": name, "changes": {}, "result": False, "comment": comt}

        test_ret = {
            "name": name,
            "changes": {},
            "result": True,
            "comment": "States ran successfully.",
        }

        test_batch_return = {
            "minion1": {
                "ret": {
                    "test_|-notify_me_|-this is a name_|-show_notification": {
                        "comment": "Notify me",
                        "name": "this is a name",
                        "start_time": "10:43:41.487565",
                        "result": True,
                        "duration": 0.35,
                        "__run_num__": 0,
                        "__sls__": "demo",
                        "changes": {},
                        "__id__": "notify_me",
                    },
                    "retcode": 0,
                },
                "out": "highstate",
            },
            "minion2": {
                "ret": {
                    "test_|-notify_me_|-this is a name_|-show_notification": {
                        "comment": "Notify me",
                        "name": "this is a name",
                        "start_time": "10:43:41.487565",
                        "result": True,
                        "duration": 0.35,
                        "__run_num__": 0,
                        "__sls__": "demo",
                        "changes": {},
                        "__id__": "notify_me",
                    },
                    "retcode": 0,
                },
                "out": "highstate",
            },
            "minion3": {
                "ret": {
                    "test_|-notify_me_|-this is a name_|-show_notification": {
                        "comment": "Notify me",
                        "name": "this is a name",
                        "start_time": "10:43:41.487565",
                        "result": True,
                        "duration": 0.35,
                        "__run_num__": 0,
                        "__sls__": "demo",
                        "changes": {},
                        "__id__": "notify_me",
                    },
                    "retcode": 0,
                },
                "out": "highstate",
            },
        }

        self.assertDictEqual(saltmod.state(name, tgt, allow_fail="a"), ret)

        comt = "No highstate or sls specified, no execution made"
        ret.update({"comment": comt})
        self.assertDictEqual(saltmod.state(name, tgt), ret)

        comt = "Must pass in boolean for value of 'concurrent'"
        ret.update({"comment": comt})
        self.assertDictEqual(
            saltmod.state(name, tgt, highstate=True, concurrent="a"), ret)

        ret.update({"comment": comt, "result": None})
        with patch.dict(saltmod.__opts__, {"test": True}):
            self.assertDictEqual(saltmod.state(name, tgt, highstate=True),
                                 test_ret)

        ret.update({
            "comment": "States ran successfully. No changes made to silver.",
            "result": True,
            "__jid__": "20170406104341210934",
        })
        with patch.dict(saltmod.__opts__, {"test": False}):
            mock = MagicMock(
                return_value={
                    "silver": {
                        "jid": "20170406104341210934",
                        "retcode": 0,
                        "ret": {
                            "test_|-notify_me_|-this is a name_|-show_notification":
                            {
                                "comment": "Notify me",
                                "name": "this is a name",
                                "start_time": "10:43:41.487565",
                                "result": True,
                                "duration": 0.35,
                                "__run_num__": 0,
                                "__sls__": "demo",
                                "changes": {},
                                "__id__": "notify_me",
                            }
                        },
                        "out": "highstate",
                    }
                })
            with patch.dict(saltmod.__salt__, {"saltutil.cmd": mock}):
                self.assertDictEqual(saltmod.state(name, tgt, highstate=True),
                                     ret)

        ret.update({
            "comment":
            "States ran successfully. No changes made to minion1, minion3, minion2."
        })
        del ret["__jid__"]
        with patch.dict(saltmod.__opts__, {"test": False}):
            with patch.dict(
                    saltmod.__salt__,
                {"saltutil.cmd": MagicMock(return_value=test_batch_return)},
            ):
                state_run = saltmod.state(name, tgt, highstate=True)

                # Test return without checking the comment contents. Comments are tested later.
                comment = state_run.pop("comment")
                ret.pop("comment")
                self.assertDictEqual(state_run, ret)

                # Check the comment contents in a non-order specific way (ordering fails sometimes on PY3)
                self.assertIn("States ran successfully. No changes made to",
                              comment)
                for minion in ["minion1", "minion2", "minion3"]:
                    self.assertIn(minion, comment)