summaryrefslogtreecommitdiff
path: root/smoketest/bin/vyos-configtest
blob: 3e42b03807ffd39913f94824382bfd749a3f7155 (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
#!/usr/bin/env python3
#
# Copyright (C) 2020 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 sys
import time
import logging
import unittest

from vyos.configsession import ConfigSession, ConfigSessionError
from vyos import ConfigError

config_dir = '/usr/libexec/vyos/tests/config'
save_config = '/tmp/vyos-configtest-save'

class DynamicClassBase(unittest.TestCase):
    def setUp(self):
        self._start_time = time.time()
        self.session = ConfigSession(os.getpid())
        self.session.save_config(save_config)

    def tearDown(self):
        self.session.migrate_and_load_config(save_config)
        self.session.commit()
        log.info(f" time: {time.time() - self._start_time:.3f}")
        del self.session
        try:
            os.remove(save_config)
        except OSError:
            pass

def make_test_function(filename):
    def test_config_load(self):
        config_path = os.path.join(config_dir, filename)
        self.session.migrate_and_load_config(config_path)
        try:
            self.session.commit()
        except (ConfigError, ConfigSessionError):
            self.session.discard()
            self.fail()
    return test_config_load

def class_name_from_func_name(s):
    res = ''.join(str.capitalize(x) for x in s.split('_'))
    return res

if __name__ == '__main__':
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG,
                        format='%(message)s')
    log = logging.getLogger("TestConfigLog")

    start_time = time.time()
    log.info("Generating tests")

    (_, _, config_list) = next(iter(os.walk(config_dir)))
    config_list.sort()

    for config in config_list:
        test_func = make_test_function(config)

        func_name = config.replace('-', '_')
        klassname = f'TestConfig{class_name_from_func_name(func_name)}'
        globals()[klassname] = type(klassname,
                                    (DynamicClassBase,),
                                    {f'test_{func_name}': test_func})

    log.info(f"... completed: {time.time() - start_time:.6f}")

    unittest.main(verbosity=2)