From 23f41a3eb9a185f7ad0f7face2ad03590c89ec72 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 12 Sep 2007 12:02:56 +0000
Subject: [PATCH] Change the way how locales are overridden: - Set only LC_ALL,
 it overrides any other variable - Restore environment after unittest

Reviewed-by: iustinp
---
 lib/utils.py                     |  6 +++---
 testing/ganeti.utils_unittest.py | 21 ++++++++++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/lib/utils.py b/lib/utils.py
index 28f509a10..b43570f6b 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -209,13 +209,13 @@ def RunCmd(cmd):
   else:
     strcmd = cmd
     shell = True
-  new_env = dict([(key, val) for (key, val) in os.environ.items()
-                  if not (key == "LANG" or key.startswith("LC_"))])
+  env = os.environ.copy()
+  env["LC_ALL"] = "C"
   child = subprocess.Popen(cmd, shell=shell,
                            stderr=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stdin=subprocess.PIPE,
-                           close_fds=True, env=new_env)
+                           close_fds=True, env=env)
 
   child.stdin.close()
   out = child.stdout.read()
diff --git a/testing/ganeti.utils_unittest.py b/testing/ganeti.utils_unittest.py
index 65a4eab9c..285c7fff5 100755
--- a/testing/ganeti.utils_unittest.py
+++ b/testing/ganeti.utils_unittest.py
@@ -151,13 +151,20 @@ class TestRunCmd(unittest.TestCase):
 
   def testLang(self):
     """Test locale environment"""
-    os.environ["LANG"] = "en_US.UTF-8"
-    os.environ["LC_ALL"] = "en_US.UTF-8"
-    result = RunCmd(["env"])
-    for line in result.output.splitlines():
-      key, val = line.split("=", 1)
-      if key.startswith("LC_") or key == "LANG":
-        self.fail("Unexpected language variable '%s' = '%s'" % (key, val))
+    old_env = os.environ.copy()
+    try:
+      os.environ["LANG"] = "en_US.UTF-8"
+      os.environ["LC_ALL"] = "en_US.UTF-8"
+      result = RunCmd(["locale"])
+      for line in result.output.splitlines():
+        key, value = line.split("=", 1)
+        # Ignore these variables, they're overridden by LC_ALL
+        if key == "LANG" or key == "LANGUAGE":
+          continue
+        self.failIf(value and value != "C" and value != '"C"',
+            "Variable %s is set to the invalid value '%s'" % (key, value))
+    finally:
+      os.environ = old_env
 
 
 class TestRemoveFile(unittest.TestCase):
-- 
GitLab