summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/ltemplate.py
blob: e897a81035799cd7d29665038862125da63e9f93 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# SPDX-License-Identifier: ISC

#
# Part of NetDEF Topology Tests
#
# Copyright (c) 2017 by
# Network Device Education Foundation, Inc. ("NetDEF")
#

"""
ltemplate.py: LabN template for FRR tests.
"""

import os
import sys
import platform

import pytest

# pylint: disable=C0413
# Import topogen and topotest helpers
from lib import topotest
from lib.topogen import Topogen, TopoRouter, get_topogen
from lib.topolog import logger
from lib.lutil import *

# Required to instantiate the topology builder class.

customize = None


class LTemplate:
    test = None
    testdir = None
    scriptdir = None
    logdir = None
    prestarthooksuccess = True
    poststarthooksuccess = True
    iproute2Ver = None

    def __init__(self, test, testdir):
        pathname = os.path.join(testdir, "customize.py")
        global customize
        if sys.version_info >= (3, 5):
            import importlib.util

            spec = importlib.util.spec_from_file_location("customize", pathname)
            customize = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(customize)
        else:
            import imp

            customize = imp.load_source("customize", pathname)
        self.test = test
        self.testdir = testdir
        self.scriptdir = testdir
        self.logdir = ""
        logger.info("LTemplate: " + test)

    def setup_module(self, mod):
        "Sets up the pytest environment"
        # This function initiates the topology build with Topogen...
        tgen = Topogen(customize.build_topo, mod.__name__)
        # ... and here it calls Mininet initialization functions.
        tgen.start_topology()

        self.logdir = tgen.logdir

        logger.info("Topology started")
        try:
            self.prestarthooksuccess = customize.ltemplatePreRouterStartHook()
        except AttributeError:
            # not defined
            logger.debug("ltemplatePreRouterStartHook() not defined")
        if self.prestarthooksuccess != True:
            logger.info("ltemplatePreRouterStartHook() failed, skipping test")
            return

        # This is a sample of configuration loading.
        router_list = tgen.routers()

        # For all registered routers, load the zebra configuration file
        for rname, router in router_list.items():
            logger.info("Setting up %s" % rname)
            for rd_val in TopoRouter.RD:
                config = os.path.join(
                    self.testdir, "{}/{}.conf".format(rname, TopoRouter.RD[rd_val])
                )
                prog = os.path.join(tgen.net[rname].daemondir, TopoRouter.RD[rd_val])
                if os.path.exists(config):
                    if os.path.exists(prog):
                        router.load_config(rd_val, config)
                    else:
                        logger.warning(
                            "{} not found, but have {}.conf file".format(
                                prog, TopoRouter.RD[rd_val]
                            )
                        )

        # After loading the configurations, this function loads configured daemons.
        logger.info("Starting routers")
        tgen.start_router()
        try:
            self.poststarthooksuccess = customize.ltemplatePostRouterStartHook()
        except AttributeError:
            # not defined
            logger.debug("ltemplatePostRouterStartHook() not defined")
        luStart(baseScriptDir=self.scriptdir, baseLogDir=self.logdir, net=tgen.net)


# initialized by ltemplate_start
_lt = None


def setup_module(mod):
    global _lt
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    test = mod.__name__[: mod.__name__.rfind(".")]
    testdir = os.path.join(root, test)

    # don't do this for now as reload didn't work as expected
    # fixup sys.path, want test dir there only once
    # try:
    #    sys.path.remove(testdir)
    # except ValueError:
    #    logger.debug(testdir+" not found in original sys.path")
    # add testdir
    # sys.path.append(testdir)

    # init class
    _lt = LTemplate(test, testdir)
    _lt.setup_module(mod)

    # drop testdir
    # sys.path.remove(testdir)


def teardown_module(mod):
    global _lt
    "Teardown the pytest environment"
    tgen = get_topogen()

    if _lt != None and _lt.scriptdir != None and _lt.prestarthooksuccess == True:
        luShowResults(logger.info)
        print(luFinish())

    # This function tears down the whole topology.
    tgen.stop_topology()
    _lt = None


def ltemplateTest(
    script, SkipIfFailed=True, CallOnFail=None, CheckFuncStr=None, KeepGoing=False
):
    global _lt
    if _lt == None or _lt.prestarthooksuccess != True:
        return

    tgen = get_topogen()
    if not os.path.isfile(script):
        if not os.path.isfile(os.path.join(_lt.scriptdir, script)):
            logger.error("Could not find script file: " + script)
            assert "Could not find script file: " + script
    logger.info("Starting template test: " + script)
    numEntry = luNumFail()

    if SkipIfFailed and tgen.routers_have_failure():
        pytest.skip(tgen.errors)
    if numEntry > 0:
        if not KeepGoing:
            pytest.skip("Have %d errors" % numEntry)

    if CheckFuncStr != None:
        check = eval(CheckFuncStr)
        if check != True:
            pytest.skip("Check function '" + CheckFuncStr + "' returned: " + check)

    if CallOnFail != None:
        CallOnFail = eval(CallOnFail)
    luInclude(script, CallOnFail)
    numFail = luNumFail() - numEntry
    if numFail > 0:
        luShowFail()
        fatal_error = "%d tests failed" % numFail
        if not KeepGoing:
            assert (
                "scripts/cleanup_all.py failed" == "See summary output above"
            ), fatal_error


# Memory leak test template
def test_memory_leak():
    "Run the memory leak test and report results."
    tgen = get_topogen()
    if not tgen.is_memleak_enabled():
        pytest.skip("Memory leak test/report is disabled")

    tgen.report_memory_leaks()


class ltemplateRtrCmd:
    def __init__(self):
        self.resetCounts()

    def doCmd(self, tgen, rtr, cmd, checkstr=None):
        logger.info("doCmd: {} {}".format(rtr, cmd))
        output = tgen.net[rtr].cmd(cmd).strip()
        if len(output):
            self.output += 1
            if checkstr != None:
                ret = re.search(checkstr, output)
                if ret == None:
                    self.nomatch += 1
                else:
                    self.match += 1
                return ret
            logger.info("output: " + output)
        else:
            logger.info("No output")
            self.none += 1
        return None

    def resetCounts(self):
        self.match = 0
        self.nomatch = 0
        self.output = 0
        self.none = 0

    def getMatch(self):
        return self.match

    def getNoMatch(self):
        return self.nomatch

    def getOutput(self):
        return self.output

    def getNone(self):
        return self.none


def ltemplateVersionCheck(
    vstr, rname="r1", compstr="<", cli=False, kernel="4.9", iproute2=None, mpls=True
):
    tgen = get_topogen()
    router = tgen.gears[rname]

    if cli:
        logger.info("calling mininet CLI")
        tgen.mininet_cli()
        logger.info("exited mininet CLI")

    if _lt == None:
        ret = "Template not initialized"
        return ret

    if _lt.prestarthooksuccess != True:
        ret = "ltemplatePreRouterStartHook failed"
        return ret

    if _lt.poststarthooksuccess != True:
        ret = "ltemplatePostRouterStartHook failed"
        return ret

    if mpls == True and tgen.hasmpls != True:
        ret = "MPLS not initialized"
        return ret

    if kernel != None:
        krel = platform.release()
        if topotest.version_cmp(krel, kernel) < 0:
            ret = "Skipping tests, old kernel ({} < {})".format(krel, kernel)
            return ret

    if iproute2 != None:
        if _lt.iproute2Ver == None:
            # collect/log info on iproute2
            cc = ltemplateRtrCmd()
            found = cc.doCmd(
                tgen, rname, "apt-cache policy iproute2", r"Installed: ([\d\.]*)"
            )
            if found != None:
                iproute2Ver = found.group(1)
            else:
                iproute2Ver = "0-unknown"
            logger.info("Have iproute2 version=" + iproute2Ver)

        if topotest.version_cmp(iproute2Ver, iproute2) < 0:
            ret = "Skipping tests, old iproute2 ({} < {})".format(iproute2Ver, iproute2)
            return ret

    ret = True
    try:
        if router.has_version(compstr, vstr):
            ret = "Skipping tests, old FRR version {} {}".format(compstr, vstr)
            return ret
    except:
        ret = True

    return ret


# for testing
if __name__ == "__main__":
    args = ["-s"] + sys.argv[1:]
    sys.exit(pytest.main(args))