def it_times_out(): assert_command( ('pgctl-2015', 'start'), '''\ ==> playground/slow-startup/stdout.log <== ==> playground/slow-startup/stderr.log <== ''', '''\ [pgctl] Starting: slow-startup [pgctl] ERROR: 'slow-startup' timed out at 2 seconds: not ready [pgctl] Stopping: slow-startup [pgctl] Stopped: slow-startup [pgctl] ERROR: Some services failed to start: slow-startup ''', 1 ) assert_svstat('playground/slow-startup', state=SvStat.UNSUPERVISED) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/slow-startup/stdout.log <== ==> playground/slow-startup/stderr.log <== pgctl-poll-ready: timeout while waiting for ready ''', '', 0, )
def it_can_still_show_config(self, tmpdir): expected_output = '''\ { "aliases": { "default": [ "(all services)" ] }, "command": "config", "pgdir": "playground", "pghome": "~/.run/pgctl", "poll": ".01", "services": [ "default" ], "timeout": "2.0" } ''' # noqa with tmpdir.as_cwd(): assert_command( ('pgctl-2015', 'config'), expected_output, '', 0, )
def it_restarts_on_unready(): def it_is_ready(): assert_svstat('playground/slow-startup', state='ready') def it_stopped(): assert_svstat('playground/slow-startup', state=SvStat.UNSUPERVISED) it_can_succeed() os.remove('playground/slow-startup/readyfile') wait_for(it_stopped, limit=5) # TODO see why it takes so long, we found python taking .5 seconds to start wait_for(it_is_ready, limit=5) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/slow-startup/stdout.log <== ==> playground/slow-startup/stderr.log <== pgctl-poll-ready: service's ready check succeeded pgctl-poll-ready: service's ready check failed -- we are restarting it for you [pgctl] Stopping: slow-startup [pgctl] Stopped: slow-startup [pgctl] Starting: slow-startup pgctl-poll-ready: service's ready check succeeded [pgctl] Started: slow-startup ''', '', 0, )
def it_can_be_run_via_python_m(self, tmpdir): from sys import executable expected_output = '''\ { "app": "app", "app/a": "app/a", "app/b": "app/b", "apps": [ "1", "2", "3" ], "environ": "environ", "environs": [ "1", "2", "3" ], "etc": "etc", "home": "home" } ''' # noqa with setup(tmpdir): assert_command( (executable, '-m', 'pgctl.config', 'my'), expected_output, '', 0, )
def it_shows_version(self): assert_command( ('pgctl-2015', '--version'), '', __version__ + '\n', 0, # too few arguments )
def it_fails_with_multiple_services(self, in_example_dir): assert_command( ('pgctl-2015', 'debug', 'abc', 'def'), '', '[pgctl] ERROR: Must debug exactly one service, not: abc, def\n', 1, )
def it_can_accept_different_environment_variables(self, in_example_dir): check_call(('sh', '-c', 'MYVAR=ohhi pgctl-2015 start')) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/environment/stdout.log <== ohhi ==> playground/environment/stderr.log <== ''', '', 0, ) check_call(('sh', '-c', 'MYVAR=bye pgctl-2015 restart')) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/environment/stdout.log <== ohhi bye ==> playground/environment/stderr.log <== ''', '', 0, )
def it_can_detect_cycles(self, in_example_dir): assert_command( ('pgctl-2015', 'start', 'b'), '', "[pgctl] ERROR: Circular aliases! Visited twice during alias expansion: 'b'\n", 1, )
def it_starts_up_fine(self): assert_command( ('pgctl-2015', 'start'), '', '''\ [pgctl] Starting: slow-startup, sweet [pgctl] Started: sweet [pgctl] Started: slow-startup ''', 0, ) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/slow-startup/stdout.log <== ==> playground/slow-startup/stderr.log <== pgctl-poll-ready: service's ready check succeeded ==> playground/sweet/stdout.log <== sweet ==> playground/sweet/stderr.log <== sweet_error ''', '', 0, )
def it_shows_error_on_stop_for_sweet(self): assert_command( ('pgctl-2015', 'start', 'sweet'), '', '''\ [pgctl] Starting: sweet [pgctl] Started: sweet ''', 0, ) assert_command( ('pgctl-2015', 'restart', 'sweet'), '''\ ==> playground/sweet/stdout.log <== sweet ==> playground/sweet/stderr.log <== sweet_error ''', S( self.LOCKERROR.format(service='sweet', time='5', cmd='sleep infinity')), 1, )
def it_shows_error_on_stop_for_slow_start(self): assert_command( ('pgctl-2015', 'start', 'slow-startup'), '', '''\ [pgctl] Starting: slow-startup [pgctl] Started: slow-startup ''', 0, ) assert_command( ('pgctl-2015', 'restart', 'slow-startup'), '''\ ==> playground/slow-startup/stdout.log <== ==> playground/slow-startup/stderr.log <== pgctl-poll-ready: service's ready check succeeded pgctl-poll-ready: service is stopping -- quitting the poll ''', S( self.LOCKERROR.format(service='slow-startup', time='5', cmd='sleep 987654')), 1, )
def it_displays_correctly_when_the_service_is_up(self, in_example_dir): check_call(('pgctl-2015', 'start', 'sleep')) assert_command( ('pgctl-2015', 'status', 'sleep'), S('sleep: ready \\(pid \\d+\\) \\d+ seconds\\n$'), '', 0, )
def it_shows_an_error(self, command, tmpdir): with tmpdir.as_cwd(): assert_command( ('pgctl-2015', command), '', "[pgctl] ERROR: could not find any directory named 'playground'\n", 1, )
def it_displays_status_for_unknown_services(self, in_example_dir): assert_command( ('pgctl-2015', 'status', 'garbage'), '', '''\ [pgctl] ERROR: No such playground service: 'garbage' ''', 1, )
def it_displays_correctly_when_the_service_is_down(self, in_example_dir): check_call(('pgctl-2015', 'start', 'sleep')) check_call(('pgctl-2015', 'stop', 'sleep')) assert_command( ('pgctl-2015', 'status', 'sleep'), 'sleep: down\n', '', 0, )
def it_is_unimplemented(self, in_example_dir): assert_command( ('pgctl-2015', 'reload'), '', '''\ [pgctl] reload: sleep [pgctl] ERROR: reloading is not yet implemented. ''', 1, )
def it_fails_given_unknown(self, in_example_dir): assert_command( ('pgctl-2015', 'stop', 'unknown'), '', '''\ [pgctl] Stopping: unknown [pgctl] ERROR: No such playground service: 'unknown' ''', 1, )
def it_can_succeed(): import mock with mock.patch.dict(os.environ, [('PGCTL_TIMEOUT', '5')]): assert_command( ('pgctl-2015', 'start'), '', '[pgctl] Starting: slow-startup\n[pgctl] Started: slow-startup\n', 0 ) assert_svstat('playground/slow-startup', state='ready')
def it_should_work_in_a_subdirectory(self, in_example_dir): os.chdir(in_example_dir.join('playground').strpath) assert_command( ('pgctl-2015', 'start', 'sleep'), '', '''\ [pgctl] Starting: sleep [pgctl] Started: sleep ''', 0, )
def it_can_start_when_default_is_not_defined_explicitly(self, in_example_dir): assert_command( ('pgctl-2015', 'start'), '', '''\ [pgctl] Starting: ohhi, sweet [pgctl] Started: ohhi [pgctl] Started: sweet ''', 0, )
def it_can_expand_properly(self, in_example_dir): assert_command( ('pgctl-2015', 'start', 'a'), '', '''\ [pgctl] Starting: ohhi, sweet [pgctl] Started: ohhi [pgctl] Started: sweet ''', 0, )
def it_displays_the_status_of_all_services(self, in_example_dir): """Expect all services to provide status when no service is specified""" check_call(('pgctl-2015', 'start', 'tail')) assert_command( ('pgctl-2015', 'status'), S('''\ sleep: down tail: ready \\(pid \\d+\\) \\d+ seconds $'''), '', 0, )
def it_can_start_when_default_is_not_defined_explicitly( self, in_example_dir): assert_command( ('pgctl-2015', 'start'), '', '''\ [pgctl] Starting: ohhi, sweet [pgctl] Started: ohhi [pgctl] Started: sweet ''', 0, )
def it_shows_help_with_no_arguments(self): assert_command( ('pgctl-2015', ), '', '''\ usage: pgctl-2015 [-h] [--version] [--pgdir PGDIR] [--pghome PGHOME] {start,stop,status,restart,reload,log,debug,config} [services [services ...]] pgctl-2015: error: too few arguments ''', 2, # too few arguments )
def it_displays_the_status_of_multiple_services(self, in_example_dir): """Expect multiple services with status and PID""" check_call(('pgctl-2015', 'start', 'sleep')) assert_command( ('pgctl-2015', 'status', 'sleep', 'tail'), S('''\ sleep: ready \\(pid \\d+\\) \\d+ seconds tail: down $'''), '', 0, )
def it_shows_help_with_no_arguments(self): assert_command( ('pgctl-2015',), '', '''\ usage: pgctl-2015 [-h] [--version] [--pgdir PGDIR] [--pghome PGHOME] {start,stop,status,restart,reload,log,debug,config} [services [services ...]] pgctl-2015: error: too few arguments ''', 2, # too few arguments )
def it_is_just_stop_then_start(self, in_example_dir): assert_command( ('pgctl-2015', 'restart', 'sleep'), '', '''\ [pgctl] Stopping: sleep [pgctl] Stopped: sleep [pgctl] Starting: sleep [pgctl] Started: sleep ''', 0, ) assert_svstat('playground/sleep', state='up')
def it_still_shows_help_without_args(self, tmpdir): with tmpdir.as_cwd(): assert_command( ('pgctl-2015'), '', '''\ usage: pgctl-2015 [-h] [--version] [--pgdir PGDIR] [--pghome PGHOME] {start,stop,status,restart,reload,log,debug,config} [services [services ...]] pgctl-2015: error: too few arguments ''', 2, )
def it_fails_by_default(self): check_call(('pgctl-2015', 'start')) assert_svstat('playground/sweet', state='up') assert_command( ('pgctl-2015', 'stop'), '''\ ==> playground/sweet/stdout.log <== sweet ==> playground/sweet/stderr.log <== sweet_error ''', S(self.LOCKERROR.format(service='sweet', time='1\\.5', cmd='sleep 2\\.25')), 1, )
def it_is_empty_before_anything_starts(self, in_example_dir): assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/ohhi/stdout.log <== ==> playground/ohhi/stderr.log <== ==> playground/sweet/stdout.log <== ==> playground/sweet/stderr.log <== ''', '', 0, )
def it_fails_by_default(self): check_call(('pgctl-2015', 'start')) assert_svstat('playground/sweet', state='up') assert_command( ('pgctl-2015', 'stop'), '''\ ==> playground/sweet/stdout.log <== sweet ==> playground/sweet/stderr.log <== sweet_error ''', S( self.LOCKERROR.format(service='sweet', time='1\\.5', cmd='sleep 2\\.25')), 1, )
def it_can_show_its_configuration( self, xdg_runtime_dir, expected_pghome, tmpdir, homedir, ): env = dict(os.environ, XDG_RUNTIME_DIR=xdg_runtime_dir) expected_output = '''\ {{ "aliases": {{ "default": [ "(all services)" ] }}, "command": "config", "pgdir": "playground", "pghome": "{pghome}", "poll": ".01", "services": [ "default" ], "timeout": "2.0" }} '''.format(pghome=expected_pghome) # noqa assert_command( ('pgctl-2015', 'config'), expected_output, '', 0, cwd=tmpdir.strpath, env=env, ) from sys import executable assert_command( (executable, '-m', 'pgctl.cli', 'config'), expected_output, '', 0, cwd=tmpdir.strpath, env=env, )
def it_shows_stdout_and_stderr(self, in_example_dir): check_call(('pgctl-2015', 'start', 'sweet')) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/ohhi/stdout.log <== ==> playground/ohhi/stderr.log <== ==> playground/sweet/stdout.log <== sweet ==> playground/sweet/stderr.log <== sweet_error ''', '', 0, ) check_call(('pgctl-2015', 'restart', 'sweet')) assert_command( ('pgctl-2015', 'log'), '''\ ==> playground/ohhi/stdout.log <== ==> playground/ohhi/stderr.log <== ==> playground/sweet/stdout.log <== sweet sweet ==> playground/sweet/stderr.log <== sweet_error sweet_error ''', '', 0, )
def it_shows_error_on_stop_for_sweet(self): assert_command( ('pgctl-2015', 'start', 'sweet'), '', '''\ [pgctl] Starting: sweet [pgctl] Started: sweet ''', 0, ) assert_command( ('pgctl-2015', 'restart', 'sweet'), '''\ ==> playground/sweet/stdout.log <== sweet ==> playground/sweet/stderr.log <== sweet_error ''', S(self.LOCKERROR.format(service='sweet', time='5', cmd='sleep infinity')), 1, )
def it_shows_error_on_stop_for_slow_start(self): assert_command( ('pgctl-2015', 'start', 'slow-startup'), '', '''\ [pgctl] Starting: slow-startup [pgctl] Started: slow-startup ''', 0, ) assert_command( ('pgctl-2015', 'restart', 'slow-startup'), '''\ ==> playground/slow-startup/stdout.log <== ==> playground/slow-startup/stderr.log <== pgctl-poll-ready: service's ready check succeeded pgctl-poll-ready: service is stopping -- quitting the poll ''', S(self.LOCKERROR.format(service='slow-startup', time='5', cmd='sleep 987654')), 1, )