summaryrefslogtreecommitdiff
path: root/src/tests/test_config_tree.py
blob: ba25537ed700a6c48bc96a728320e1d978cbebe3 (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
# Copyright (C) VyOS Inc.
#
# 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 json
import time
import threading
import unittest
from unittest import TestCase

from vyos.configtree import ConfigTree
from vyos.referencetree import ReferenceTree
from vyos.derivedtree import subtree_from_list_of_partial_paths


class TestInitialSetup(TestCase):
    def setUp(self):
        with open('data/config.boot.default') as f:
            config_str = f.read()
            self.ct = ConfigTree(config_str)

        self.thread_exception = None
        self.orig_excepthook = threading.excepthook

        def custom_hook(args):
            self.thread_exception = args.exc_value

        threading.excepthook = custom_hook

    def tearDown(self):
        threading.excepthook = self.orig_excepthook

        if self.thread_exception:
            raise self.thread_exception

    def test_subtree_from_partial(self):
        reftree = ReferenceTree(cache_file='data/reftree.cache')

        # workaround since configtree.list_nodes does not take an empty path
        d = json.loads(self.ct.to_json())
        top_nodes = list(d)
        paths = [s.split() for s in top_nodes]

        reassemble = subtree_from_list_of_partial_paths(
            self.ct, paths, reference_tree=reftree
        )

        self.assertEqual(self.ct, reassemble)

    def test_cache_io(self):
        config_cache = '/tmp/config.cache'

        self.ct.write_cache(config_cache)
        ct_cached = ConfigTree(internal=config_cache)
        os.unlink(config_cache)

        self.assertEqual(self.ct, ct_cached)

    def test_cache_io_thread(self):
        config_cache = '/tmp/config.cache'

        def task():
            time.sleep(0.1)
            self.ct.write_cache(config_cache)
            ct_cached = ConfigTree(internal=config_cache)
            os.unlink(config_cache)

            self.assertEqual(self.ct, ct_cached)

        t = threading.Thread(target=task)
        t.start()
        t.join()


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