예제 #1
0
class TestCases_50(C.TestCaseWithWorkdir):

    maxDiff = None
    cpaths = C.list_res_files("show_configs", "*.txt")

    def test_10_parse_show_config_and_dump(self):
        for idx, cpath in enumerate(self.cpaths):
            outdir = TT.os.path.join(self.workdir, "{!s}".format(idx))
            (opath, cnf) = TT.parse_show_config_and_dump(cpath, outdir)

            self.assertTrue(cnf)
            self.assertNotEqual(opath, cpath)

            hdir = os.path.dirname(opath)
            for fname in (TT.METADATA_FILENAME, TT.ALL_FILENAME):
                self.assertTrue(os.path.exists(os.path.join(hdir, fname)))

    def test_20_parse_show_configs_and_dump(self):
        for opath, cnf in TT.parse_show_configs_and_dump_itr(
                self.cpaths, self.workdir):
            self.assertTrue(cnf)
            self.assertTrue(os.path.exists(opath))

            hdir = os.path.dirname(opath)
            for fname in (TT.METADATA_FILENAME, TT.ALL_FILENAME):
                self.assertTrue(os.path.exists(os.path.join(hdir, fname)))
예제 #2
0
class Base(C.TestCase):

    cpaths = C.list_res_files("show_configs", "*.txt")
    cnfs = [P.parse_show_config(p) for p in cpaths]
    hnames = [P.hostname_from_configs(c, P.has_vdom(c)) for c in cnfs]
    sargss = [dict(has_vdoms_=P.has_vdom(c)) for c in cnfs]

    # .. seealso:: tests/res/show_configs/*.txt
    ref_ips = [TT.ipaddress.ip_interface("192.168.122.10/24"),
               TT.ipaddress.ip_interface("192.168.1.10/24")]
    ref_fas = set(("0.0.0.0/32",
                   "192.168.3.3/32",
                   "192.168.3.1/32",
                   "192.168.3.5/32",
                   "192.168.122.0/24",
                   "192.168.122.1/32",
                   "192.168.1.0/24",
                   "192.168.2.3/32",
                   "192.168.2.2/32",
                   "192.168.5.202/32",
                   "192.168.2.1/32",
                   "192.168.5.201/32",
                   "192.168.5.200/32"))
예제 #3
0
class TestCases_30_parse(C.unittest.TestCase):

    maxDiff = None
    cpaths = C.list_res_files("show_configs", "*.txt")

    def test_10_parse_show_config(self):
        for cpath in self.cpaths:
            cnf = TT.parse_show_config(cpath)
            self.assertTrue(cnf)
            TT.validate(cnf)

    def test_20_hostname_from_configs(self):
        cnf = TT.parse_show_config(self.cpaths[0])
        hostname = TT.hostname_from_configs(cnf)
        self.assertEqual(hostname, "fortigate-01")

        cnf = TT.parse_show_config(self.cpaths[1])
        hostname = TT.hostname_from_configs(cnf, has_vdoms_=True)
        self.assertEqual(hostname, "fortigate-02")

    def test_30_list_vdom_names(self):
        cnf = TT.parse_show_config(self.cpaths[0])
        self.assertEqual(TT.list_vdom_names(cnf), ["root"])

        cnf = TT.parse_show_config(self.cpaths[1])
        self.assertEqual(TT.list_vdom_names(cnf), ["ro", "root"])

    def test_50_list_cnames_for_regexp(self):
        reg = TT.re.compile(r"firewall service.*")
        for cpath in self.cpaths:
            ref = _list_cnames_from_file(cpath)
            cnf = TT.parse_show_config(cpath)
            opts = dict(has_vdoms_=TT.has_vdom(cnf))
            res = TT.list_cnames_for_regexp(cnf, reg, **opts)

            self.assertEqual(res, ref)
예제 #4
0
class TestCasesWithConfigs(C.TestCase):

    cpaths = C.list_res_files("parsed", "*/all.json")
    cnfs = [P.load(p) for p in cpaths]
예제 #5
0
#
# Copyright (C) 2020 Satoru SATOH <*****@*****.**>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring,invalid-name
from __future__ import absolute_import

import functools

import fortios_xutils.finder as TT
import tests.common as C


# tests/res/networks/graph.yml
NET_CONF_PATH = C.list_res_files("networks", pattern="*.yml")[0]


class TestCases_10(C.unittest.TestCase):

    def test_10_validate(self):
        self.assertRaises(ValueError, TT.validate, {})
        self.assertRaises(TypeError, TT.validate, [1])
        self.assertRaises(ValueError, TT.validate, dict(a=1))
        self.assertRaises(ValueError, TT.validate, dict(nodes=1, links=[]))
        self.assertRaises(ValueError, TT.validate, dict(nodes=[], links=1))

    def test_20_load(self):
        self.assertTrue(TT.load(NET_CONF_PATH))

    def test_30_find_paths(self):
        fnc = functools.partial(TT.find_paths, NET_CONF_PATH)
예제 #6
0
 def test_52_try_ac_load__ok(self):
     ok_files = C.list_res_files("*_ok.json")
     for f in ok_files:
         self.assertTrue(TT.try_ac_load(f) is not None)
예제 #7
0
class TestCase(C.TestCaseWithWorkdir):

    mod = TT

    sources = C.list_res_files("show_configs", "*.txt")
    cpaths = C.list_res_files("parsed", os.path.join('*', P.ALL_FILENAME))
    fpaths = C.list_res_files("firewall",
                              os.path.join('*', F.FWP_TABLE_FILENAME))
    npaths = C.list_res_files("networks", "graph.yml")

    def _fun(self, fun):
        """Get a callable from the module `self.mod` lazily.
        """
        return getattr(self.mod, fun)

    def test_10_parse_and_save_show_configs__single_input(self):
        # Find test target function lazily.
        tfn = self._fun("parse_and_save_show_configs")

        outdir = os.path.join(self.workdir, "out")
        for src in self.sources:
            res = tfn([src], outdir)

            self.assertTrue(res)

            self.assertTrue(os.path.exists(res[0][0]))  # output filepath
            self.assertTrue(res[0][1])  # parsed result (mapping object)

            for fname in (P.METADATA_FILENAME, P.ALL_FILENAME):
                files = glob.glob(os.path.join(outdir, '*', fname))
                self.assertTrue(files)

    def test_12_parse_and_save_show_configs__multi_inputs(self):
        tfn = self._fun("parse_and_save_show_configs")

        outdir = os.path.join(self.workdir, "out")
        res = tfn(self.sources, outdir)
        self.assertTrue(res)

        for opath, cnf in res:
            self.assertTrue(os.path.exists(opath))
            self.assertTrue(cnf)

        for fname in (P.METADATA_FILENAME, P.ALL_FILENAME):
            files = glob.glob(os.path.join(outdir, '*', fname))
            self.assertTrue(files)

    def test_20_query_json_files__single_input(self):
        tfn = self._fun("query_json_files")

        query = "configs[?config=='system interface'].edits[].ip"
        for cpath in self.cpaths:
            res = tfn([cpath], query)
            self.assertTrue(res)

    def test_22_query_json_files__multi_inputs(self):
        tfn = self._fun("query_json_files")

        query = "configs[?config=='system interface'].edits[].ip"
        res = tfn(self.cpaths, query)
        self.assertTrue(res)

    def test_30_collect_networks__single_input(self):
        tfn = self._fun("collect_networks")

        for cpath in self.cpaths:
            res = tfn([cpath])
            self.assertTrue(res)

    def test_32_collect_networks__multi_inputs(self):
        tfn = self._fun("collect_networks")

        res = tfn(self.cpaths)
        self.assertTrue(res)

    def test_40_collect_and_save_networks__single_input(self):
        tfn = self._fun("collect_and_save_networks")

        for idx, cpath in enumerate(self.cpaths):
            outdir = os.path.join(self.workdir, "out-{!s}".format(idx))
            res = tfn([cpath], outdir=outdir)
            self.assertTrue(res)

            files = glob.glob(os.path.join(outdir, '*', N.NET_FILENAME))
            self.assertTrue(files)
            self.assertEqual(len(files), 1, files)

    def test_42_collect_and_save_networks__multi_inputs(self):
        tfn = self._fun("collect_and_save_networks")

        outdir = os.path.join(self.workdir, "out")
        res = tfn(self.cpaths, outdir=outdir)
        self.assertTrue(res)

        files = glob.glob(os.path.join(outdir, '*', N.NET_FILENAME))
        self.assertTrue(files)
        self.assertEqual(len(files), 2)

    def test_50_compose_networks__single_input(self):
        tfn = self._fun("compose_networks")

        for cpath in self.cpaths:
            res = tfn([cpath])
            self.assertTrue(res)

    def test_52_compose_networks__multi_inputs(self):
        tfn = self._fun("compose_networks")

        res = tfn(self.cpaths)
        self.assertTrue(res)

    def test_60_compose_and_save_networks__single_input(self):
        tfn = self._fun("compose_and_save_networks")

        opath = os.path.join(self.workdir, "out", N.NET_ALL_FILENAME)
        for cpath in self.cpaths:
            res = tfn([cpath], opath)
            self.assertTrue(res)
            self.assertTrue(os.path.exists(opath))

    def test_62_compose_and_save_networks__multi_inputs(self):
        tfn = self._fun("compose_and_save_networks")

        opath = os.path.join(self.workdir, "out", N.NET_ALL_FILENAME)
        res = tfn(self.cpaths, opath)
        self.assertTrue(res)
        self.assertTrue(os.path.exists(opath))

    def test_70_make_save_search_firewall_policy_tables__single_input(self):
        tfn1 = self._fun("make_and_save_firewall_policy_tables")
        tfn2 = self._fun("load_firewall_policy_table")
        tfn3 = self._fun("search_firewall_policy_table_by_addr")

        for idx, cpath in enumerate(self.cpaths):
            outdir = os.path.join(self.workdir, "out-{!s}".format(idx))
            res = tfn1([cpath], outdir=outdir)
            self.assertTrue(res)

            files = glob.glob(os.path.join(outdir, '*', F.FWP_TABLE_FILENAME))
            self.assertTrue(files)
            self.assertEqual(len(files), 1)
            fpath = files[0]

            rdf = tfn2(fpath)

            # not found
            ipa = "127.0.0.1"
            res = tfn3(ipa, rdf)
            self.assertFalse(res)

            # found
            ipa = "192.168.122.3"
            res = tfn3(ipa, rdf)
            self.assertTrue(res)

    def test_72_firewall_policy_save_and_search__multi_inputs(self):
        tfn1 = self._fun("make_and_save_firewall_policy_tables")
        tfn2 = self._fun("load_firewall_policy_table")
        tfn3 = self._fun("search_firewall_policy_table_by_addr")

        outdir = os.path.join(self.workdir, "out")
        res = tfn1(self.cpaths, outdir=outdir)
        self.assertTrue(res)

        files = sorted(
            glob.glob(os.path.join(outdir, '*', F.FWP_TABLE_FILENAME)))
        self.assertTrue(files)
        self.assertEqual(len(files), 2)

        # not found
        ipa = "127.0.0.1"
        for fpath in files:
            rdf = tfn2(fpath)
            res = tfn3(ipa, rdf)
            self.assertFalse(res)

        # found
        ipa = "192.168.122.3"
        for fpath in files:
            rdf = tfn2(fpath)
            res = tfn3(ipa, rdf)
            self.assertTrue(res)

    def test_80_find_network_paths__not_found(self):
        tfn = self._fun("find_network_paths")

        (src, dst) = ("127.0.0.1", "192.168.122.2")
        for npath in self.npaths:
            res = tfn(npath, src, dst)
            self.assertFalse(res)

    def test_82_find_network_paths__found(self):
        tfn = self._fun("find_network_paths")

        (src, dst) = ("192.168.122.2", "192.168.5.10")
        for npath in self.npaths:
            res = tfn(npath, src, dst)
            self.assertTrue(res)