def it_is_disallowed(): assert_command( ('pgctl', 'start'), '', '''\ [pgctl] Starting: sweet [pgctl] Started: sweet ''', 0, ) first = Popen(('pgctl', 'restart'), stdout=PIPE, stderr=PIPE) # slow-shutdown takes two seconds to shut down; aim for the middle: sleep(1) second = Popen(('pgctl', 'restart'), stdout=PIPE, stderr=PIPE) first_stdout, first_stderr = first.communicate() first_stdout, first_stderr = first_stdout.decode('UTF-8'), first_stderr.decode('UTF-8') show_both(first_stdout, first_stderr) assert norm.pgctl(first_stderr) == '''\ [pgctl] Stopping: sweet [pgctl] ERROR: service 'sweet' failed to stop after {TIME} seconds, its status is ready (pid {PID}) {TIME} seconds ==> playground/sweet/log <== {TIMESTAMP} sweet {TIMESTAMP} sweet_error [pgctl] [pgctl] There might be useful information further up in the log; you can view it by running: [pgctl] less +G playground/sweet/log [pgctl] ERROR: Some services failed to stop: sweet ''' assert first_stdout == '' assert first.returncode == 1 second_stdout, second_stderr = second.communicate() second_stdout, second_stderr = second_stdout.decode('UTF-8'), second_stderr.decode('UTF-8') show_both(second_stdout, second_stderr) assert norm.pgctl(second_stderr) == '''\ [pgctl] ERROR: another pgctl command is currently managing this service: (playground/sweet/.pgctl.lock) {PS-HEADER} {PS-STATS} ${PREFIX}/bin/python ${PREFIX}/bin/pgctl restart ''' assert second_stdout == '' assert second.returncode == 1
def it_is_disallowed(): assert_command( ('pgctl-2015', 'start'), '', '''\ [pgctl] Starting: sweet [pgctl] Started: sweet ''', 0, ) first = Popen(('pgctl-2015', 'restart'), stdout=PIPE, stderr=PIPE) # slow-shutdown takes two seconds to shut down; aim for the middle: sleep(1) second = Popen(('pgctl-2015', 'restart'), stdout=PIPE, stderr=PIPE) first_stdout, first_stderr = first.communicate() first_stdout, first_stderr = first_stdout.decode('UTF-8'), first_stderr.decode('UTF-8') show_both(first_stdout, first_stderr) assert norm.pgctl(first_stderr) == '''\ [pgctl] Stopping: sweet [pgctl] ERROR: service 'sweet' failed to stop after {TIME} seconds, its status is ready (pid {PID}) {TIME} seconds ==> playground/sweet/log <== {TIMESTAMP} sweet {TIMESTAMP} sweet_error [pgctl] [pgctl] There might be useful information further up in the log; you can view it by running: [pgctl] less +G playground/sweet/log [pgctl] ERROR: Some services failed to stop: sweet ''' assert first_stdout == '' assert first.returncode == 1 second_stdout, second_stderr = second.communicate() second_stdout, second_stderr = second_stdout.decode('UTF-8'), second_stderr.decode('UTF-8') show_both(second_stdout, second_stderr) assert norm.pgctl(second_stderr) == '''\ [pgctl] ERROR: another pgctl command is currently managing this service: (playground/sweet/.pgctl.lock) {PS-HEADER} {PS-STATS} ${PREFIX}/bin/python ${PREFIX}/bin/pgctl-2015 restart ''' assert second_stdout == '' assert second.returncode == 1
def it_logs_continuously_when_run_interactively(self, in_example_dir): check_call(('pgctl', 'start')) # this pty simulates running in a terminal read, write = os.openpty() pty.normalize_newlines(read) p = Popen(('pgctl', 'log'), stdout=write, stderr=write) os.close(write) import fcntl fl = fcntl.fcntl(read, fcntl.F_GETFL) fcntl.fcntl(read, fcntl.F_SETFL, fl | os.O_NONBLOCK) assert p.poll() is None # it's still running # needs to loop for several seconds because the default event loop # in tail-f is one second. # TODO: buf is a list, use wait_for() to append to it limit = 3.0 wait = .1 buf = b'' while True: try: block = os.read(read, 1024) print('BLOCK:', block) except OSError as error: print('ERROR:', error) if error.errno == 11: # other end didn't write yet if limit > 0: import time time.sleep(wait) limit -= wait continue else: break else: raise buf += block from testfixtures import StringComparison as S buf = norm.pgctl(buf.decode('UTF-8')) print('NORMED:') print(buf) assert buf == S('''(?s)\ ==> playground/ohhi/logs/current <== {TIMESTAMP} [oe].* ==> playground/sweet/logs/current <== {TIMESTAMP} sweet {TIMESTAMP} sweet_error ==> playground/ohhi/logs/current <== .*{TIMESTAMP} .*$''') assert p.poll() is None # it's still running p.terminate() assert p.wait() == -15
def it_logs_continuously_when_run_interactively(self, in_example_dir): check_call(('pgctl', 'start')) # this pty simulates running in a terminal read, write = os.openpty() pty.normalize_newlines(read) p = Popen(('pgctl', 'log'), stdout=write, stderr=write) os.close(write) import fcntl fl = fcntl.fcntl(read, fcntl.F_GETFL) fcntl.fcntl(read, fcntl.F_SETFL, fl | os.O_NONBLOCK) assert p.poll() is None # it's still running # needs to loop for several seconds because the default event loop # in tail-f is one second. # TODO: buf is a list, use wait_for() to append to it limit = 3.0 wait = .1 buf = b'' while True: try: block = os.read(read, 1024) print('BLOCK:', block) except OSError as error: print('ERROR:', error) if error.errno == 11: # other end didn't write yet if limit > 0: import time time.sleep(wait) limit -= wait continue else: break else: raise buf += block from testfixtures import StringComparison as S buf = norm.pgctl(buf.decode('UTF-8')) print('NORMED:') print(buf) assert buf == S('''(?s)\ ==> playground/ohhi/log <== {TIMESTAMP} [oe].* ==> playground/sweet/log <== {TIMESTAMP} sweet {TIMESTAMP} sweet_error ==> playground/ohhi/log <== .*{TIMESTAMP} .*$''') assert p.poll() is None # it's still running p.terminate() assert p.wait() == -15