def test__sets_LANG_and_LC_ALL(self): self.assertThat( select_c_utf8_bytes_locale({}), Equals({ b"LANG": b"C.UTF-8", b"LC_ALL": b"C.UTF-8", }), )
def test__passes_other_variables_through(self): basis = { factory.make_name("name").encode("ascii"): (factory.make_name("value").encode("ascii")) for _ in range(5) } expected = basis.copy() expected[b"LANG"] = expected[b"LC_ALL"] = b"C.UTF-8" observed = select_c_utf8_bytes_locale(basis) self.assertThat(observed, Equals(expected))
def test__defaults_to_process_environment(self): name = factory.make_name("name") value = factory.make_name("value") with EnvironmentVariable(name, value): self.assertThat( select_c_utf8_bytes_locale(), ContainsDict({ name.encode("ascii"): Equals(value.encode("ascii")), }), )
def test__removes_other_LC_variables(self): self.assertThat( select_c_utf8_bytes_locale({ name.encode("ascii"): factory.make_name(name).encode("ascii") for name in LC_VAR_NAMES }), Equals({ b"LANG": b"C.UTF-8", b"LC_ALL": b"C.UTF-8", }), )
def test__overwrites_LANG(self): self.assertThat( select_c_utf8_bytes_locale({ b"LANG": factory.make_name("LANG").encode("ascii"), }), Equals({ b"LANG": b"C.UTF-8", b"LC_ALL": b"C.UTF-8", }), )
def startProcess(self): env = select_c_utf8_bytes_locale() log.msg("%s started." % self.getDescription()) args = self.getProcessParameters() assert all( isinstance(arg, bytes) for arg in args), ("Process arguments must all be bytes, got: %s" % repr(args)) self._protocol = self.createProcessProtocol() self._process = reactor.spawnProcess(self._protocol, args[0], args, env=env) return self._protocol.done.addBoth(self._processEnded)
def _execSupervisorServiceAction(self, service_name, action): """Perform the action with the run-supervisorctl command. :return: tuple (exit code, std-output, std-error) """ env = select_c_utf8_bytes_locale() cmd = os.path.join(snappy.get_snap_path(), "bin", "run-supervisorctl") cmd = (cmd, action, service_name) def decode(result): out, err, code = result return code, out.decode("utf-8"), err.decode("utf-8") d = getProcessOutputAndValue(cmd[0], cmd[1:], env=env) return d.addCallback(decode)
def test___execSystemDServiceAction_calls_systemctl(self): service_monitor = self.make_service_monitor() service_name = factory.make_name("service") action = factory.make_name("action") mock_getProcessOutputAndValue = self.patch(service_monitor_module, "getProcessOutputAndValue") mock_getProcessOutputAndValue.return_value = succeed((b"", b"", 0)) yield service_monitor._execSystemDServiceAction(service_name, action) cmd = ["sudo", "--non-interactive", "systemctl", action, service_name] self.assertThat( mock_getProcessOutputAndValue, MockCalledOnceWith( # The environment contains LC_ALL and LANG too. cmd[0], cmd[1:], env=select_c_utf8_bytes_locale()))
def _execSystemDServiceAction(self, service_name, action, extra_opts=None): """Perform the action with the systemctl command. :return: tuple (exit code, std-output, std-error) """ env = select_c_utf8_bytes_locale() cmd = ["sudo", "--non-interactive", "systemctl", action] if extra_opts is not None: cmd.extend(extra_opts) cmd.append(service_name) def decode(result): out, err, code = result return code, out.decode("utf-8"), err.decode("utf-8") d = getProcessOutputAndValue(cmd[0], cmd[1:], env=env) return d.addCallback(decode)
def test___execSupervisorServiceAction_calls_supervisorctl(self): snap_path = factory.make_name("path") self.patch(snappy, "get_snap_path").return_value = snap_path service_monitor = self.make_service_monitor() service_name = factory.make_name("service") action = factory.make_name("action") mock_getProcessOutputAndValue = self.patch(service_monitor_module, "getProcessOutputAndValue") mock_getProcessOutputAndValue.return_value = succeed((b"", b"", 0)) yield service_monitor._execSupervisorServiceAction( service_name, action) cmd = os.path.join(snap_path, 'bin', 'run-supervisorctl') cmd = cmd, action, service_name self.assertThat( mock_getProcessOutputAndValue, MockCalledOnceWith( # The environment contains LC_ALL and LANG too. cmd[0], cmd[1:], env=select_c_utf8_bytes_locale()))