Ejemplo n.º 1
0
 def test_set_job(self):
     with patch.dict(cron.__grains__, {'os': None}):
         cron.set_job('DUMMY_USER', 1, 2, 3, 4, 5, '/bin/echo NOT A DROID',
                      'WERE YOU LOOKING FOR ME?')
         expected_call = call('DUMMY_USER', [
             '5 0 * * * /tmp/no_script.sh\n',
             '# Lines below here are managed by Salt, do not edit\n',
             '# WERE YOU LOOKING FOR ME?\n',
             '1 2 3 4 5 /bin/echo NOT A DROID\n'
         ])
         cron._write_cron_lines.call_args.assert_called_with(expected_call)
Ejemplo n.º 2
0
 def test_set_job(self):
     cron.__grains__ = __grains__
     with patch.dict(cron.__grains__, {'os': None}):
         cron.set_job('DUMMY_USER', 1, 2, 3, 4, 5,
                      '/bin/echo NOT A DROID',
                      'WERE YOU LOOKING FOR ME?')
         expected_call = call('DUMMY_USER',
                              ['5 0 * * * /tmp/no_script.sh\n',
                               '# Lines below here are managed by Salt, do not edit\n',
                               '# WERE YOU LOOKING FOR ME?\n',
                               '1 2 3 4 5 /bin/echo NOT A DROID\n'])
         cron._write_cron_lines.call_args.assert_called_with(expected_call)
Ejemplo n.º 3
0
 def test_set_job(self):
     with patch.dict(cron.__grains__, {'os': None}), \
             patch('salt.modules.cron._write_cron_lines',
                   new=MagicMock(return_value={'retcode': False})), \
                           patch('salt.modules.cron.raw_cron',
                                 new=MagicMock(return_value=STUB_SIMPLE_RAW_CRON)):
         cron.set_job('DUMMY_USER', 1, 2, 3, 4, 5,
                      '/bin/echo NOT A DROID',
                      'WERE YOU LOOKING FOR ME?')
         expected_call = call('DUMMY_USER',
                              ['5 0 * * * /tmp/no_script.sh\n',
                               '# Lines below here are managed by Salt, do not edit\n',
                               '# WERE YOU LOOKING FOR ME?\n',
                               '1 2 3 4 5 /bin/echo NOT A DROID\n'])
         cron._write_cron_lines.call_args.assert_called_with(expected_call)
Ejemplo n.º 4
0
    def test__need_changes_new(self):
        '''
        New behavior, identifier will get track of the managed lines!
        '''

        # when there are no identifiers,
        # we do not touch it
        set_crontab(L + '# SALT_CRON_IDENTIFIER:booh\n' '* * * * * ls\n')
        cron.set_job(
            user='******',
            minute='*',
            hour='*',
            daymonth='*',
            month='*',
            dayweek='*',
            cmd='ls',
            comment=None,
            identifier=None,
        )
        c1 = get_crontab()
        set_crontab(L + '* * * * * ls\n')
        self.assertEqual(
            c1, '# Lines below here are managed by Salt, do not edit\n'
            '# SALT_CRON_IDENTIFIER:booh\n'
            '* * * * * ls\n'
            '* * * * * ls')
        # whenever we have an identifier, hourray even without comment
        # we can match and edit the crontab in place
        # without cluttering the crontab with new cmds
        set_crontab(L + '# SALT_CRON_IDENTIFIER:bar\n' '* * * * * ls\n')
        cron.set_job(
            user='******',
            minute='*',
            hour='*',
            daymonth='*',
            month='*',
            dayweek='*',
            cmd='ls',
            comment=None,
            identifier='bar',
        )
        c5 = get_crontab()
        set_crontab(L + '* * * * * ls\n')
        self.assertEqual(
            c5, '# Lines below here are managed by Salt, do not edit\n'
            '# SALT_CRON_IDENTIFIER:bar\n'
            '* * * * * ls\n')
        # we can even change the other parameters as well
        # thx to the id
        set_crontab(L + '# SALT_CRON_IDENTIFIER:bar\n* * * * * ls\n')
        cron.set_job(
            user='******',
            minute='1',
            hour='2',
            daymonth='3',
            month='4',
            dayweek='5',
            cmd='foo',
            comment='moo',
            identifier='bar',
        )
        c6 = get_crontab()
        self.assertEqual(
            c6, '# Lines below here are managed by Salt, do not edit\n'
            '# moo SALT_CRON_IDENTIFIER:bar\n'
            '1 2 3 4 5 foo')
Ejemplo n.º 5
0
 def test__issue10959(self):
     '''
     handle multi old style crontabs
     https://github.com/saltstack/salt/issues/10959
     '''
     with patch('salt.modules.cron.raw_cron',
                new=MagicMock(side_effect=get_crontab)):
         set_crontab(
             '# Lines below here are managed by Salt, do not edit\n'
             '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
             '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
             '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
             '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
             '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
             # as managed per salt, the last lines will be merged together !
             '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
             '* * * * * samecmd\n'
             '* * * * * otheridcmd\n'
             '* * * * * otheridcmd\n'
             '# SALT_CRON_IDENTIFIER:NO ID SET\n0 * * * * samecmd1\n'
             '1 * * * * samecmd1\n'
             '0 * * * * otheridcmd1\n'
             '1 * * * * otheridcmd1\n'
             # special case here, none id managed line with same command
             # as a later id managed line will become managed
             '# SALT_CRON_IDENTIFIER:1\n0 * * * * otheridcmd1\n'
             '# SALT_CRON_IDENTIFIER:2\n0 * * * * otheridcmd1\n')
         crons1 = cron.list_tab('root')
         # the filtering is done on save, we reflect in listing
         # the same that we have in a file, no matter what we
         # have
         self.assertEqual(
             crons1, {
                 'crons': [{
                     'cmd': 'ls',
                     'comment': 'uoo',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'too',
                     'comment': 'uuoo',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'zoo',
                     'comment': 'uuuoo',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'yoo',
                     'comment': '',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'xoo',
                     'comment': '',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'samecmd',
                     'comment': '',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'samecmd',
                     'comment': None,
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': None,
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'otheridcmd',
                     'comment': None,
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': None,
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'otheridcmd',
                     'comment': None,
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': None,
                     'minute': '*',
                     'month': '*'
                 }, {
                     'cmd': 'samecmd1',
                     'comment': '',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': 'NO ID SET',
                     'minute': '0',
                     'month': '*'
                 }, {
                     'cmd': 'samecmd1',
                     'comment': None,
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': None,
                     'minute': '1',
                     'month': '*'
                 }, {
                     'cmd': 'otheridcmd1',
                     'comment': None,
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': None,
                     'minute': '0',
                     'month': '*'
                 }, {
                     'cmd': 'otheridcmd1',
                     'comment': None,
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': None,
                     'minute': '1',
                     'month': '*'
                 }, {
                     'cmd': 'otheridcmd1',
                     'comment': '',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': '1',
                     'minute': '0',
                     'month': '*'
                 }, {
                     'cmd': 'otheridcmd1',
                     'comment': '',
                     'daymonth': '*',
                     'dayweek': '*',
                     'hour': '*',
                     'identifier': '2',
                     'minute': '0',
                     'month': '*'
                 }],
                 'env': [],
                 'pre': [],
                 'special': []
             })
         # so yood so far, no problem for now, trying to save the
         # multilines without id crons now
         inc_tests = [
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n'
              '0 * * * * samecmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '0 * * * * otheridcmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '1 * * * * otheridcmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '# SALT_CRON_IDENTIFIER:1\n0 * * * * otheridcmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '# SALT_CRON_IDENTIFIER:1\n0 * * * * otheridcmd1\n'
              '# SALT_CRON_IDENTIFIER:2\n0 * * * * otheridcmd1')
         ]
         set_crontab('')
         for idx, cr in enumerate(crons1['crons']):
             cron.set_job('root', **cr)
             self.assertEqual(get_crontab(), inc_tests[idx],
                              ("idx {0}\n'{1}'\n != \n'{2}'\n\n\n"
                               "\'{1}\' != \'{2}\'").format(
                                   idx, get_crontab(), inc_tests[idx]))
Ejemplo n.º 6
0
 def test__need_changes_old(self):
     '''
     old behavior; ID has no special action
     - If an id is found, it will be added as a new crontab
       even if there is a cmd that looks like this one
     - no comment, delete the cmd and readd it
     - comment: idem
     '''
     with patch('salt.modules.cron.raw_cron',
                new=MagicMock(side_effect=get_crontab)):
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='ls',
             comment=None,
             identifier=cron.SALT_CRON_NO_IDENTIFIER,
         )
         c1 = get_crontab()
         set_crontab(L + '* * * * * ls\n')
         self.assertEqual(
             c1, '# Lines below here are managed by Salt, do not edit\n'
             '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='ls',
             comment='foo',
             identifier=cron.SALT_CRON_NO_IDENTIFIER,
         )
         c2 = get_crontab()
         self.assertEqual(
             c2, '# Lines below here are managed by Salt, do not edit\n'
             '# foo\n* * * * * ls')
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='lsa',
             comment='foo',
             identifier='bar',
         )
         c3 = get_crontab()
         self.assertEqual(
             c3, '# Lines below here are managed by Salt, do not edit\n'
             '* * * * * ls\n'
             '# foo SALT_CRON_IDENTIFIER:bar\n'
             '* * * * * lsa')
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='foo',
             comment='foo',
             identifier='bar',
         )
         c4 = get_crontab()
         self.assertEqual(
             c4, '# Lines below here are managed by Salt, do not edit\n'
             '* * * * * ls\n'
             '# foo SALT_CRON_IDENTIFIER:bar\n'
             '* * * * * foo')
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='ls',
             comment='foo',
             identifier='bbar',
         )
         c4 = get_crontab()
         self.assertEqual(
             c4, '# Lines below here are managed by Salt, do not edit\n'
             '# foo SALT_CRON_IDENTIFIER:bbar\n'
             '* * * * * ls')
Ejemplo n.º 7
0
    def test__need_changes_new(self):
        '''
        New behavior, identifier will get track of the managed lines!
        '''

        # when there are no identifiers,
        # we do not touch it
        set_crontab(
            L + '# SALT_CRON_IDENTIFIER:booh\n'
            '* * * * * ls\n')
        cron.set_job(
            user='******',
            minute='*',
            hour='*',
            daymonth='*',
            month='*',
            dayweek='*',
            cmd='ls',
            comment=None,
            identifier=None,
        )
        c1 = get_crontab()
        set_crontab(L + '* * * * * ls\n')
        self.assertEqual(
            c1,
            '# Lines below here are managed by Salt, do not edit\n'
            '# SALT_CRON_IDENTIFIER:booh\n'
            '* * * * * ls\n'
            '* * * * * ls'
        )
        # whenever we have an identifier, hourray even without comment
        # we can match and edit the crontab in place
        # without cluttering the crontab with new cmds
        set_crontab(
            L + '# SALT_CRON_IDENTIFIER:bar\n'
            '* * * * * ls\n')
        cron.set_job(
            user='******',
            minute='*',
            hour='*',
            daymonth='*',
            month='*',
            dayweek='*',
            cmd='ls',
            comment=None,
            identifier='bar',
        )
        c5 = get_crontab()
        set_crontab(L + '* * * * * ls\n')
        self.assertEqual(
            c5,
            '# Lines below here are managed by Salt, do not edit\n'
            '# SALT_CRON_IDENTIFIER:bar\n'
            '* * * * * ls\n'
        )
        # we can even change the other parameters as well
        # thx to the id
        set_crontab(
            L + '# SALT_CRON_IDENTIFIER:bar\n* * * * * ls\n')
        cron.set_job(
            user='******',
            minute='1',
            hour='2',
            daymonth='3',
            month='4',
            dayweek='5',
            cmd='foo',
            comment='moo',
            identifier='bar',
        )
        c6 = get_crontab()
        self.assertEqual(
            c6,
            '# Lines below here are managed by Salt, do not edit\n'
            '# moo SALT_CRON_IDENTIFIER:bar\n'
            '1 2 3 4 5 foo'
        )
Ejemplo n.º 8
0
 def test__issue10959(self):
     '''
     handle multi old style crontabs
     https://github.com/saltstack/salt/issues/10959
     '''
     with patch(
         'salt.modules.cron.raw_cron',
         new=MagicMock(side_effect=get_crontab)
     ):
         set_crontab(
             '# Lines below here are managed by Salt, do not edit\n'
             '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
             '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
             '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
             '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
             '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
             # as managed per salt, the last lines will be merged together !
             '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
             '* * * * * samecmd\n'
             '* * * * * otheridcmd\n'
             '* * * * * otheridcmd\n'
             '# SALT_CRON_IDENTIFIER:NO ID SET\n0 * * * * samecmd1\n'
             '1 * * * * samecmd1\n'
             '0 * * * * otheridcmd1\n'
             '1 * * * * otheridcmd1\n'
             # special case here, none id managed line with same command
             # as a later id managed line will become managed
             '# SALT_CRON_IDENTIFIER:1\n0 * * * * otheridcmd1\n'
             '# SALT_CRON_IDENTIFIER:2\n0 * * * * otheridcmd1\n'
         )
         crons1 = cron.list_tab('root')
         # the filtering is done on save, we reflect in listing
         # the same that we have in a file, no matter what we
         # have
         self.assertEqual(crons1, {
             'crons': [
                 {'cmd': 'ls', 'comment': 'uoo', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '*', 'month': '*'},
                 {'cmd': 'too', 'comment': 'uuoo', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '*', 'month': '*'},
                 {'cmd': 'zoo', 'comment': 'uuuoo', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '*', 'month': '*'},
                 {'cmd': 'yoo', 'comment': '', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '*', 'month': '*'},
                 {'cmd': 'xoo', 'comment': '', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '*', 'month': '*'},
                 {'cmd': 'samecmd', 'comment': '', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '*', 'month': '*'},
                 {'cmd': 'samecmd', 'comment': None, 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': None,
                  'minute': '*', 'month': '*'},
                 {'cmd': 'otheridcmd', 'comment': None, 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': None,
                  'minute': '*', 'month': '*'},
                 {'cmd': 'otheridcmd', 'comment': None, 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': None,
                  'minute': '*', 'month': '*'},
                 {'cmd': 'samecmd1', 'comment': '', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': 'NO ID SET',
                  'minute': '0', 'month': '*'},
                 {'cmd': 'samecmd1', 'comment': None, 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': None,
                  'minute': '1', 'month': '*'},
                 {'cmd': 'otheridcmd1', 'comment': None, 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': None,
                  'minute': '0', 'month': '*'},
                 {'cmd': 'otheridcmd1', 'comment': None, 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': None,
                  'minute': '1', 'month': '*'},
                 {'cmd': 'otheridcmd1', 'comment': '', 'daymonth': '*',
                  'dayweek': '*', 'hour': '*', 'identifier': '1',
                  'minute': '0', 'month': '*'},
                 {'cmd': 'otheridcmd1',
                  'comment': '', 'daymonth': '*', 'dayweek': '*',
                  'hour': '*', 'identifier': '2', 'minute': '0',
                  'month': '*'}
             ],
             'env': [],
             'pre': [],
             'special': []})
         # so yood so far, no problem for now, trying to save the
         # multilines without id crons now
         inc_tests = [
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n'
              '0 * * * * samecmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '0 * * * * otheridcmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '1 * * * * otheridcmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '# SALT_CRON_IDENTIFIER:1\n0 * * * * otheridcmd1'),
             #
             ('# Lines below here are managed by Salt, do not edit\n'
              '# uoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * ls\n'
              '# uuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * too\n'
              '# uuuoo SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * zoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * yoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * xoo\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n* * * * * samecmd\n'
              '* * * * * otheridcmd\n'
              '# SALT_CRON_IDENTIFIER:NO ID SET\n1 * * * * samecmd1\n'
              '# SALT_CRON_IDENTIFIER:1\n0 * * * * otheridcmd1\n'
              '# SALT_CRON_IDENTIFIER:2\n0 * * * * otheridcmd1')
         ]
         set_crontab('')
         for idx, cr in enumerate(crons1['crons']):
             cron.set_job('root', **cr)
             self.assertEqual(
                 get_crontab(),
                 inc_tests[idx], (
                     "idx {0}\n '{1}'\n != \n'{2}'\n\n\n"
                     "{1!r} != {2!r}"
                 ).format(
                     idx, get_crontab(), inc_tests[idx]))
Ejemplo n.º 9
0
 def test__need_changes_old(self):
     '''
     old behavior; ID has no special action
     - If an id is found, it will be added as a new crontab
       even if there is a cmd that looks like this one
     - no comment, delete the cmd and readd it
     - comment: idem
     '''
     with patch(
         'salt.modules.cron.raw_cron',
         new=MagicMock(side_effect=get_crontab)
     ):
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='ls',
             comment=None,
             identifier=cron.SALT_CRON_NO_IDENTIFIER,
         )
         c1 = get_crontab()
         set_crontab(L + '* * * * * ls\n')
         self.assertEqual(
             c1,
             '# Lines below here are managed by Salt, do not edit\n'
             '* * * * * ls\n'
         )
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='ls',
             comment='foo',
             identifier=cron.SALT_CRON_NO_IDENTIFIER,
         )
         c2 = get_crontab()
         self.assertEqual(
             c2,
             '# Lines below here are managed by Salt, do not edit\n'
             '# foo\n* * * * * ls'
         )
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='lsa',
             comment='foo',
             identifier='bar',
         )
         c3 = get_crontab()
         self.assertEqual(
             c3,
             '# Lines below here are managed by Salt, do not edit\n'
             '* * * * * ls\n'
             '# foo SALT_CRON_IDENTIFIER:bar\n'
             '* * * * * lsa'
         )
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='foo',
             comment='foo',
             identifier='bar',
         )
         c4 = get_crontab()
         self.assertEqual(
             c4,
             '# Lines below here are managed by Salt, do not edit\n'
             '* * * * * ls\n'
             '# foo SALT_CRON_IDENTIFIER:bar\n'
             '* * * * * foo'
         )
         set_crontab(L + '* * * * * ls\n')
         cron.set_job(
             user='******',
             minute='*',
             hour='*',
             daymonth='*',
             month='*',
             dayweek='*',
             cmd='ls',
             comment='foo',
             identifier='bbar',
         )
         c4 = get_crontab()
         self.assertEqual(
             c4,
             '# Lines below here are managed by Salt, do not edit\n'
             '# foo SALT_CRON_IDENTIFIER:bbar\n'
             '* * * * * ls'
         )