def test_command_init(self): """ Tests the cli._Command.__init__ method. """ try: cli._Command(uuid.uuid4().hex, self.app_ctx, False) except SystemExit, e: eq_(e.code, 3)
def test_too_few_arguments(self): """ Tests the expected exception and the return code when there are too few arguments passed in to 'zdaemon' command. """ # Too few arguments to the 'zdaemon' command. with Replacer() as r: stdout = uuid.uuid4().hex stderr = uuid.uuid4().hex def _communicate(self): return [stdout, stderr] r.replace('subprocess.Popen.communicate', _communicate) try: command = cli._Command(self.test_dir, self.app_ctx, False) command._execute_zdaemon_command(['zdaemon']) except Exception, e: msg = e.args[0] expected_start = 'Failed to execute command [u\'zdaemon\']. return code=[' expected_end = '], stdout=[{0}], stderr=[{1}]'.format(stdout, stderr) assert_true(msg.startswith(expected_start)) assert_true(msg.endswith(expected_end)) return_code = msg[len(expected_start):-len(expected_end)] # We caught an error so the return_code must be a positive integer. return_code = int(return_code) assert_true(return_code > 0) else:
def test_too_few_arguments(self): """ Tests the expected exception and the return code when there are too few arguments passed in to 'zdaemon' command. """ # Too few arguments to the 'zdaemon' command. with Replacer() as r: stdout = uuid.uuid4().hex stderr = uuid.uuid4().hex def _communicate(self): return [stdout, stderr] r.replace('subprocess.Popen.communicate', _communicate) try: command = cli._Command(self.test_dir, self.app_ctx, False) command._execute_zdaemon_command(['zdaemon']) except Exception, e: msg = e.args[0] expected_start = 'Failed to execute command [u\'zdaemon\']. return code=[' expected_end = '], stdout=[{0}], stderr=[{1}]'.format( stdout, stderr) assert_true(msg.startswith(expected_start)) assert_true(msg.endswith(expected_end)) return_code = msg[len(expected_start):-len(expected_end)] # We caught an error so the return_code must be a positive integer. return_code = int(return_code) assert_true(return_code > 0) else:
def test_pid_returning(self): """ Tests whether the correct PID is being returned by the '_execute_zdaemon_command' method. """ with Replacer() as r: expected_pid = 4893 stdout = 'program running; pid={0}'.format(expected_pid) stderr = uuid.uuid4().hex def _communicate(self): return [stdout, stderr] def _Popen(self, *ignored_args, **ignored_kwargs): class _DummyPopen(object): def __init__(self, *ignored_args, **ignored_kwargs): self.returncode = 0 def communicate(self): return stdout, stderr def wait(self): pass return _DummyPopen() r.replace('subprocess.Popen', _Popen) command = cli._Command(self.test_dir, self.app_ctx, False) given_pid = int(command._execute_zdaemon_command(['zdaemon'])) # PIDs must be the same. eq_(given_pid, expected_pid)
def test_wait_none(self): """ Tests whether an Exception is being raised when the return value of the .wait call is None. """ # The return code of the 'wait' call on a Popen object returned None. # Doesn't even matter that there were too few arguments in the call # to 'zdaemon' command as we hadn't even got as far as to actually call # it. with Replacer() as r: def _wait(self): self.returncode = None r.replace('subprocess.Popen.wait', _wait) try: command = cli._Command(self.test_dir, self.app_ctx, False) command._execute_zdaemon_command(['zdaemon']) except Exception, e: eq_( e.args[0], 'Could not execute command [u\'zdaemon\'] (p.returncode is None)' ) else:
def test_run_not_implemented_error(self): """ Tests whether the default implementation of the .run method raises a NotImplementedError. """ # The 'run' method must be implemented by subclasses. command = cli._Command(self.test_dir, self.app_ctx, False) assert_raises(NotImplementedError, command.run)
def test_config_mod_missing(self): """ A SystemExit should be raised when the config directory doesn't contain a config marker file. """ command = cli._Command(self.test_dir, self.app_ctx, False) command.config_dir = tempfile.mkdtemp(prefix=self.temp_dir_prefix) try: command._get_config_mod() except SystemExit, e: return_code = e.args[0] eq_(int(return_code), 3)
def test_command_stop(self): """ Tests whether executing a 'stop' command deletes a temporary zdaemon's config file. """ expected_pid = uuid.uuid4().int with patch.object(cli._Command, '_execute_zdaemon_command') as mock_method: # The 'stop' command. Not only does it communicate with # the subprocesses but also deleted the zdaemon's config file # created in the self.setUp method. command = cli._Command(self.test_dir, self.app_ctx, False) command._zdaemon_command('stop', 'zdaemon.conf') exists = os.path.exists(os.path.join(self.test_dir, 'zdaemon.conf')) eq_(exists, False)
def test_enrichment(self): """ Tests whether enrichment of the config module works fine. """ command = cli._Command(self.test_dir, self.app_ctx, False) config_mod = command._get_config_mod() elems = [elem for elem in dir(config_mod) if not elem.startswith('__')] eq_(len(elems), 27) names = ('server_type', 'host', 'port', 'log', 'crypto_dir', 'keyfile', 'certfile', 'ca_certs', 'not_authorized', 'forbidden', 'no_url_match', 'internal_server_error', 'validation_precedence', 'client_cert_401_www_auth', 'syslog_facility', 'syslog_address', 'log_level', 'log_file_config', 'server_tag', 'instance_name', 'quote_path_info', 'quote_query_string', 'from_backend_ignore', 'add_invocation_id', 'sign_invocation_id', 'default_url_config', 'add_default_if_not_found') for name in names: assert_true(name in elems, (name,))
def test_command_not_stop(self): """ Tests whether executing a command other that 'stop' returns the process' PID. """ expected_pid = uuid.uuid4().int with patch.object(cli._Command, '_execute_zdaemon_command') as mock_method: # Any command other than 'stop'. Should simply return the pid # of the subprocess. command_name = uuid.uuid4().hex mock_method.return_value = expected_pid command = cli._Command(self.test_dir, self.app_ctx, False) given_pid = command._zdaemon_command(command_name, 'foo.conf') eq_(given_pid, expected_pid) eq_(mock_method.called, True) mock_method.assert_called_with( [u'zdaemon', u'-C', os.path.join(self.test_dir, 'foo.conf'), command_name])
def test_wait_none(self): """ Tests whether an Exception is being raised when the return value of the .wait call is None. """ # The return code of the 'wait' call on a Popen object returned None. # Doesn't even matter that there were too few arguments in the call # to 'zdaemon' command as we hadn't even got as far as to actually call # it. with Replacer() as r: def _wait(self): self.returncode = None r.replace('subprocess.Popen.wait', _wait) try: command = cli._Command(self.test_dir, self.app_ctx, False) command._execute_zdaemon_command(['zdaemon']) except Exception, e: eq_(e.args[0], 'Could not execute command [u\'zdaemon\'] (p.returncode is None)') else:
def test_enrichment(self): """ Tests whether enrichment of the config module works fine. """ command = cli._Command(self.test_dir, self.app_ctx, False) config_mod = command._get_config_mod() elems = [elem for elem in dir(config_mod) if not elem.startswith('__')] eq_(len(elems), 27) names = ('server_type', 'host', 'port', 'log', 'crypto_dir', 'keyfile', 'certfile', 'ca_certs', 'not_authorized', 'forbidden', 'no_url_match', 'internal_server_error', 'validation_precedence', 'client_cert_401_www_auth', 'syslog_facility', 'syslog_address', 'log_level', 'log_file_config', 'server_tag', 'instance_name', 'quote_path_info', 'quote_query_string', 'from_backend_ignore', 'add_invocation_id', 'sign_invocation_id', 'default_url_config', 'add_default_if_not_found') for name in names: assert_true(name in elems, (name, ))
def test_command_not_stop(self): """ Tests whether executing a command other that 'stop' returns the process' PID. """ expected_pid = uuid.uuid4().int with patch.object(cli._Command, '_execute_zdaemon_command') as mock_method: # Any command other than 'stop'. Should simply return the pid # of the subprocess. command_name = uuid.uuid4().hex mock_method.return_value = expected_pid command = cli._Command(self.test_dir, self.app_ctx, False) given_pid = command._zdaemon_command(command_name, 'foo.conf') eq_(given_pid, expected_pid) eq_(mock_method.called, True) mock_method.assert_called_with([ u'zdaemon', u'-C', os.path.join(self.test_dir, 'foo.conf'), command_name ])