xref: /unit/test/test_usr1.py (revision 2073:bc6ad31ce286)
11248Szelenkov@nginx.comimport os
21850Smax.romanov@nginx.comimport signal
31477Szelenkov@nginx.com
41248Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
51850Smax.romanov@nginx.comfrom unit.log import Log
61735Szelenkov@nginx.comfrom unit.utils import waitforfiles
71248Szelenkov@nginx.com
81248Szelenkov@nginx.com
91248Szelenkov@nginx.comclass TestUSR1(TestApplicationPython):
101467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
111248Szelenkov@nginx.com
121850Smax.romanov@nginx.com    def test_usr1_access_log(self, temp_dir, unit_pid):
131248Szelenkov@nginx.com        self.load('empty')
141248Szelenkov@nginx.com
151397Szelenkov@nginx.com        log = 'access.log'
161397Szelenkov@nginx.com        log_new = 'new.log'
171654Szelenkov@nginx.com        log_path = temp_dir + '/' + log
181248Szelenkov@nginx.com
191596Szelenkov@nginx.com        assert 'success' in self.conf(
201596Szelenkov@nginx.com            '"' + log_path + '"', 'access_log'
211596Szelenkov@nginx.com        ), 'access log configure'
221248Szelenkov@nginx.com
231596Szelenkov@nginx.com        assert waitforfiles(log_path), 'open'
241596Szelenkov@nginx.com
251654Szelenkov@nginx.com        os.rename(log_path, temp_dir + '/' + log_new)
261248Szelenkov@nginx.com
271596Szelenkov@nginx.com        assert self.get()['status'] == 200
281248Szelenkov@nginx.com
291596Szelenkov@nginx.com        assert (
301596Szelenkov@nginx.com            self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new)
311596Szelenkov@nginx.com            is not None
321596Szelenkov@nginx.com        ), 'rename new'
331596Szelenkov@nginx.com        assert not os.path.isfile(log_path), 'rename old'
341248Szelenkov@nginx.com
351850Smax.romanov@nginx.com        os.kill(unit_pid, signal.SIGUSR1)
361248Szelenkov@nginx.com
371596Szelenkov@nginx.com        assert waitforfiles(log_path), 'reopen'
381248Szelenkov@nginx.com
391596Szelenkov@nginx.com        assert self.get(url='/usr1')['status'] == 200
401397Szelenkov@nginx.com
411596Szelenkov@nginx.com        assert (
421596Szelenkov@nginx.com            self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log)
431596Szelenkov@nginx.com            is not None
441596Szelenkov@nginx.com        ), 'reopen 2'
451596Szelenkov@nginx.com        assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2'
461248Szelenkov@nginx.com
471850Smax.romanov@nginx.com    def test_usr1_unit_log(self, temp_dir, unit_pid):
481248Szelenkov@nginx.com        self.load('log_body')
491248Szelenkov@nginx.com
501397Szelenkov@nginx.com        log_new = 'new.log'
511654Szelenkov@nginx.com        log_path = temp_dir + '/unit.log'
521654Szelenkov@nginx.com        log_path_new = temp_dir + '/' + log_new
531248Szelenkov@nginx.com
541248Szelenkov@nginx.com        os.rename(log_path, log_path_new)
551248Szelenkov@nginx.com
561850Smax.romanov@nginx.com        Log.swap(log_new)
571248Szelenkov@nginx.com
581850Smax.romanov@nginx.com        try:
591850Smax.romanov@nginx.com            body = 'body_for_a_log_new\n'
601850Smax.romanov@nginx.com            assert self.post(body=body)['status'] == 200
611248Szelenkov@nginx.com
62*2073Szelenkov@nginx.com            assert self.wait_for_record(body, log_new) is not None, 'rename new'
631850Smax.romanov@nginx.com            assert not os.path.isfile(log_path), 'rename old'
641248Szelenkov@nginx.com
651850Smax.romanov@nginx.com            os.kill(unit_pid, signal.SIGUSR1)
661248Szelenkov@nginx.com
671850Smax.romanov@nginx.com            assert waitforfiles(log_path), 'reopen'
681248Szelenkov@nginx.com
691850Smax.romanov@nginx.com            body = 'body_for_a_log_unit\n'
701850Smax.romanov@nginx.com            assert self.post(body=body)['status'] == 200
711397Szelenkov@nginx.com
721850Smax.romanov@nginx.com            assert self.wait_for_record(body) is not None, 'rename new'
731850Smax.romanov@nginx.com            assert self.search_in_log(body, log_new) is None, 'rename new 2'
741850Smax.romanov@nginx.com
751850Smax.romanov@nginx.com        finally:
761850Smax.romanov@nginx.com            # merge two log files into unit.log to check alerts
771248Szelenkov@nginx.com
781850Smax.romanov@nginx.com            with open(log_path, 'r', errors='ignore') as unit_log:
791850Smax.romanov@nginx.com                log = unit_log.read()
801248Szelenkov@nginx.com
811850Smax.romanov@nginx.com            with open(log_path, 'w') as unit_log, open(
821850Smax.romanov@nginx.com                log_path_new, 'r', errors='ignore'
831850Smax.romanov@nginx.com            ) as unit_log_new:
841850Smax.romanov@nginx.com                unit_log.write(unit_log_new.read())
851850Smax.romanov@nginx.com                unit_log.write(log)
861850Smax.romanov@nginx.com
871850Smax.romanov@nginx.com            Log.swap(log_new)
88