Merge "Fix last Pylint error in Functest"
[functest.git] / functest / opnfv_tests / vnf / router / vnf_controller / checker.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 """vrouter test result check module"""
13
14 import json
15 import logging
16 import re
17
18 from jinja2 import Environment, FileSystemLoader
19
20
21 class Checker(object):
22     """vrouter test result check class"""
23
24     logger = logging.getLogger(__name__)
25
26     def __init__(self):
27         self.logger.debug("init checker")
28
29     @staticmethod
30     def load_check_rule(rule_file_dir, rule_file_name, parameter):
31         loader = FileSystemLoader(rule_file_dir,
32                                   encoding='utf8')
33         env = Environment(loader=loader)
34         check_rule_template = env.get_template(rule_file_name)
35         check_rule = check_rule_template.render(parameter)
36         check_rule_data = json.loads(check_rule)
37         return check_rule_data
38
39     @staticmethod
40     def regexp_information(response, rules):
41         status = False
42         result_data = {}
43
44         for rule in rules["rules"]:
45             result_data = {
46                 "test_name": rule["description"],
47                 "result": "NG"
48             }
49
50             match = re.search(rule["regexp"],
51                               response)
52             rule["response"] = response
53             if match is None:
54                 status = False
55                 break
56
57             if not match.group(1) == rule["result"]:
58                 status = False
59             else:
60                 result_data["result"] = "OK"
61                 status = True
62
63         return status, result_data