xref: /unit/test/unit/applications/proto.py (revision 1596)
1*1596Szelenkov@nginx.comimport os
21019Szelenkov@nginx.comimport re
31019Szelenkov@nginx.comimport time
41477Szelenkov@nginx.com
51019Szelenkov@nginx.comfrom unit.control import TestControl
6*1596Szelenkov@nginx.comfrom conftest import option
71019Szelenkov@nginx.com
81019Szelenkov@nginx.com
91019Szelenkov@nginx.comclass TestApplicationProto(TestControl):
101019Szelenkov@nginx.com    def sec_epoch(self):
111019Szelenkov@nginx.com        return time.mktime(time.gmtime())
121019Szelenkov@nginx.com
131019Szelenkov@nginx.com    def date_to_sec_epoch(self, date, template='%a, %d %b %Y %H:%M:%S %Z'):
141019Szelenkov@nginx.com        return time.mktime(time.strptime(date, template))
151019Szelenkov@nginx.com
161027Szelenkov@nginx.com    def search_in_log(self, pattern, name='unit.log'):
17*1596Szelenkov@nginx.com        with open(self.temp_dir + '/' + name, 'r', errors='ignore') as f:
181019Szelenkov@nginx.com            return re.search(pattern, f.read())
191028Szelenkov@nginx.com
201028Szelenkov@nginx.com    def wait_for_record(self, pattern, name='unit.log'):
211028Szelenkov@nginx.com        for i in range(50):
221028Szelenkov@nginx.com            found = self.search_in_log(pattern, name)
231028Szelenkov@nginx.com
241028Szelenkov@nginx.com            if found is not None:
251028Szelenkov@nginx.com                break
261028Szelenkov@nginx.com
271028Szelenkov@nginx.com            time.sleep(0.1)
281028Szelenkov@nginx.com
291028Szelenkov@nginx.com        return found
301050Szelenkov@nginx.com
31*1596Szelenkov@nginx.com    def get_appication_type(self):
32*1596Szelenkov@nginx.com        current_test = (
33*1596Szelenkov@nginx.com            os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]
34*1596Szelenkov@nginx.com        )
35*1596Szelenkov@nginx.com
36*1596Szelenkov@nginx.com        if current_test in option.generated_tests:
37*1596Szelenkov@nginx.com            return option.generated_tests[current_test]
38*1596Szelenkov@nginx.com
39*1596Szelenkov@nginx.com        return None
40*1596Szelenkov@nginx.com
411303St.nateldemoura@f5.com    def _load_conf(self, conf, **kwargs):
421303St.nateldemoura@f5.com        if 'applications' in conf:
431303St.nateldemoura@f5.com            for app in conf['applications'].keys():
441303St.nateldemoura@f5.com                app_conf = conf['applications'][app]
451303St.nateldemoura@f5.com                if 'user' in kwargs:
461303St.nateldemoura@f5.com                    app_conf['user'] = kwargs['user']
471303St.nateldemoura@f5.com
481303St.nateldemoura@f5.com                if 'group' in kwargs:
491303St.nateldemoura@f5.com                    app_conf['group'] = kwargs['group']
501303St.nateldemoura@f5.com
511303St.nateldemoura@f5.com                if 'isolation' in kwargs:
521303St.nateldemoura@f5.com                    app_conf['isolation'] = kwargs['isolation']
531303St.nateldemoura@f5.com
54*1596Szelenkov@nginx.com        assert 'success' in self.conf(conf), 'load application configuration'
55