def fork_one_request(self, conn, client_addr, command_argv, env): """Fork myself and serve a request.""" temp_name = tempfile.mkdtemp(prefix='lp-forking-service-child-') # Now that we've set everything up, send the response to the # client we create them first, so the client can start trying to # connect to them, while we fork and have the child do the same. self._children_spawned += 1 pid = self._fork_function() if pid == 0: pid = os.getpid() trace.mutter('%d spawned' % (pid,)) self._server_socket.close() for env_var, value in env.iteritems(): osutils.set_or_unset_env(env_var, value) # See [Decision #3] self._create_child_file_descriptors(temp_name) conn.sendall('ok\n%d\n%s\n' % (pid, temp_name)) conn.close() self.become_child(command_argv, temp_name) trace.warning('become_child returned!!!') sys.exit(1) else: self._child_processes[pid] = (temp_name, conn) self.log(client_addr, 'Spawned process %s for %r: %s' % (pid, command_argv, temp_name))
def test_in_path(self): if sys.platform != 'win32': raise TestSkipped('Searching in PATH implemented only for win32') os.mkdir('foo') in_dir = os.path.join(os.getcwd(), 'foo') self._make_file(in_dir=in_dir) osutils.set_or_unset_env('PATH', in_dir) shouldbe = os.path.join(in_dir, 'curl-ca-bundle.crt') self.assertEqual(shouldbe, ca_bundle.get_ca_path(use_cache=False))
def test_simple(self): bzr_log = 'my.bzr.log' osutils.set_or_unset_env('BZR_LOG', bzr_log) default_log = os.path.join(os.environ['BZR_HOME'], '.bzr.log') self.failIfExists([default_log, bzr_log]) out = self.run_bzr_subprocess('version')[0] self.assertTrue(len(out) > 0) self.assertContainsRe(out, r"(?m)^ Bazaar log file: " + bzr_log) self.failIfExists(default_log) self.failUnlessExists(bzr_log)
def test_unicode_bzr_home(self): uni_val, str_val = probe_unicode_in_user_encoding() if uni_val is None: raise TestSkipped('Cannot find a unicode character that works in' ' encoding %s' % (bzrlib.user_encoding, )) osutils.set_or_unset_env('BZR_HOME', str_val) out = self.run_bzr("version")[0] self.assertTrue(len(out) > 0) self.assertContainsRe(out, r"(?m)^ Bazaar configuration: " + str_val)
def test_unicode_bzr_home(self): uni_val, str_val = probe_unicode_in_user_encoding() if uni_val is None: raise TestSkipped('Cannot find a unicode character that works in' ' encoding %s' % (bzrlib.user_encoding,)) osutils.set_or_unset_env('BZR_HOME', str_val) out = self.run_bzr("version")[0] self.assertTrue(len(out) > 0) self.assertContainsRe(out, r"(?m)^ Bazaar configuration: " + str_val)
def test_dev_null(self): if sys.platform == 'win32': bzr_log = 'NUL' else: bzr_log = '/dev/null' osutils.set_or_unset_env('BZR_LOG', bzr_log) default_log = os.path.join(os.environ['BZR_HOME'], '.bzr.log') self.failIfExists(default_log) out = self.run_bzr_subprocess('version')[0] self.assertTrue(len(out) > 0) self.assertContainsRe(out, r"(?m)^ Bazaar log file: " + bzr_log) self.failIfExists(default_log)
def test_unicode_bzr_home(self): uni_val, str_val = probe_unicode_in_user_encoding() if uni_val is None: raise TestSkipped('Cannot find a unicode character that works in' ' encoding %s' % \ bzrlib.osutils.get_user_encoding()) osutils.set_or_unset_env('BZR_HOME', str_val) out = self.run_bzr("xmlversion")[0] self.assertTrue(len(out) > 0) versionElem = fromstring(out) self.assertEquals(1, len(versionElem.findall('bazaar/configuration'))) self.assertContainsRe(out, r"<configuration>" + str_val)
def test_get_paramiko_vendor(self): """Test that if no 'ssh' is available we get builtin paramiko""" from bzrlib.transport import ssh # set '.' as the only location in the path, forcing no 'ssh' to exist orig_vendor = ssh._ssh_vendor_manager._cached_ssh_vendor orig_path = set_or_unset_env('PATH', '.') try: # No vendor defined yet, query for one ssh._ssh_vendor_manager.clear_cache() vendor = ssh._get_ssh_vendor() self.assertIsInstance(vendor, ssh.ParamikoVendor) finally: set_or_unset_env('PATH', orig_path) ssh._ssh_vendor_manager._cached_ssh_vendor = orig_vendor
def test_unsupported_encoding_commit_message(self): old_env = osutils.set_or_unset_env('LANG', 'C') try: # LANG env variable has no effect on Windows # but some characters anyway cannot be represented # in default user encoding char = probe_bad_non_ascii(bzrlib.user_encoding) if char is None: raise TestSkipped('Cannot find suitable non-ascii character ' 'for user_encoding (%s)' % bzrlib.user_encoding) self.make_fake_editor(message=char) working_tree = self.make_uncommitted_tree() self.assertRaises(errors.BadCommitMessageEncoding, msgeditor.edit_commit_message, '') finally: osutils.set_or_unset_env('LANG', old_env)
def setUp(self): TestCaseInTempDir.setUp(self) new_env = { 'CURL_CA_BUNDLE': None, 'PATH': None, } self._old_env = {} self.addCleanup(self._restore) for name, value in new_env.iteritems(): self._old_env[name] = osutils.set_or_unset_env(name, None)
def fork_one_request(self, conn, client_addr, command_argv, env): """Fork myself and serve a request.""" temp_name = tempfile.mkdtemp(prefix="lp-forking-service-child-") # Now that we've set everything up, send the response to the # client we create them first, so the client can start trying to # connect to them, while we fork and have the child do the same. self._children_spawned += 1 pid = self._fork_function() if pid == 0: pid = os.getpid() trace.mutter("%d spawned" % (pid,)) self._server_socket.close() for env_var, value in env.iteritems(): osutils.set_or_unset_env(env_var, value) # See [Decision #3] self._create_child_file_descriptors(temp_name) conn.sendall("ok\n%d\n%s\n" % (pid, temp_name)) conn.close() self.become_child(command_argv, temp_name) trace.warning("become_child returned!!!") sys.exit(1) else: self._child_processes[pid] = (temp_name, conn) self.log(client_addr, "Spawned process %s for %r: %s" % (pid, command_argv, temp_name))
def _reset(self): locale.getpreferredencoding = self._getpreferredencoding sys.stderr = self._stderr # restore $LANG osutils.set_or_unset_env('LANG', self._LANG)
def restore_environment(): for env_var, value in old_env.iteritems(): osutils.set_or_unset_env(env_var, value)
def cleanup_environment(): for env_var, value in env_changes.iteritems(): old_env[env_var] = osutils.set_or_unset_env(env_var, value)
def _restore(self): for name, value in self._old_env.iteritems(): osutils.set_or_unset_env(name, value)
def test_env_var(self): osutils.set_or_unset_env('CURL_CA_BUNDLE', 'foo.bar') self._make_file() self.assertEqual('foo.bar', ca_bundle.get_ca_path(use_cache=False))