summaryrefslogtreecommitdiff
path: root/src/tests/test_vyos_update_crontab.py
blob: 180871300e139fafc66ef0fc83ef2bb90a23f796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
#
# Copyright (C) 2018 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

import os
import tempfile
import unittest

from vyos import ConfigError
try:
    from src.conf_mode import vyos_update_crontab
except ModuleNotFoundError:  # for unittest.main()
    import sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
    from src.conf_mode import vyos_update_crontab


class TestUpdateCrontab(unittest.TestCase):

    def test_verify(self):
        tests = [
            {'name': 'one_task',
             'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': None
             },
            {'name': 'has_interval_and_spec',
             'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '0 * * * *', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': ConfigError
             },
            {'name': 'has_no_interval_and_spec',
             'tasks': [{'name': 'aaa', 'interval': '', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': ConfigError
             },
            {'name': 'invalid_interval',
             'tasks': [{'name': 'aaa', 'interval': '1y', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': ConfigError
             },
            {'name': 'invalid_interval_min',
             'tasks': [{'name': 'aaa', 'interval': '61m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': ConfigError
             },
            {'name': 'invalid_interval_hour',
             'tasks': [{'name': 'aaa', 'interval': '25h', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': ConfigError
             },
            {'name': 'invalid_interval_day',
             'tasks': [{'name': 'aaa', 'interval': '32d', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': ConfigError
             },
            {'name': 'no_executable',
             'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '', 'args': ''}],
             'expected': ConfigError
             },
            {'name': 'invalid_executable',
             'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/aaa', 'args': ''}],
             'expected': ConfigError
             }
        ]
        for t in tests:
            with self.subTest(msg=t['name'], tasks=t['tasks'], expected=t['expected']):
                if t['expected'] is not None:
                    with self.assertRaises(t['expected']):
                        vyos_update_crontab.verify(t['tasks'])
                else:
                    vyos_update_crontab.verify(t['tasks'])

    def test_generate(self):
        tests = [
            {'name': 'zero_task',
             'tasks': [],
             'expected': []
             },
            {'name': 'one_task',
             'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': [
                 '### Generated by vyos-update-crontab.py ###',
                 '*/60 * * * * root /bin/ls -l']
             },
            {'name': 'one_task_with_hour',
             'tasks': [{'name': 'aaa', 'interval': '10h', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': [
                 '### Generated by vyos-update-crontab.py ###',
                 '0 */10 * * * root /bin/ls -l']
             },
            {'name': 'one_task_with_day',
             'tasks': [{'name': 'aaa', 'interval': '10d', 'spec': '', 'executable': '/bin/ls', 'args': '-l'}],
             'expected': [
                 '### Generated by vyos-update-crontab.py ###',
                 '0 0 */10 * * root /bin/ls -l']
             },
            {'name': 'multiple_tasks',
             'tasks': [{'name': 'aaa', 'interval': '60m', 'spec': '', 'executable': '/bin/ls', 'args': '-l'},
                       {'name': 'bbb', 'interval': '', 'spec': '0 0 * * *', 'executable': '/bin/ls', 'args': '-ltr'}
                       ],
             'expected': [
                 '### Generated by vyos-update-crontab.py ###',
                 '*/60 * * * * root /bin/ls -l',
                 '0 0 * * * root /bin/ls -ltr']
             }
        ]
        for t in tests:
            with self.subTest(msg=t['name'], tasks=t['tasks'], expected=t['expected']):
                vyos_update_crontab.crontab_file = tempfile.mkstemp()[1]
                vyos_update_crontab.generate(t['tasks'])
                if len(t['expected']) > 0:
                    self.assertTrue(os.path.isfile(vyos_update_crontab.crontab_file))
                    with open(vyos_update_crontab.crontab_file) as f:
                        actual = f.read()
                        self.assertEqual(t['expected'], actual.splitlines())
                    os.remove(vyos_update_crontab.crontab_file)
                else:
                    self.assertFalse(os.path.isfile(vyos_update_crontab.crontab_file))


if __name__ == "__main__":
    unittest.main()