PyUT: Implement test for lsb_release.get_lsb_information.
authorDidier Raboud <odyx@debian.org>
Tue, 22 May 2012 13:19:54 +0000 (15:19 +0200)
committerDidier Raboud <odyx@debian.org>
Tue, 22 May 2012 13:19:56 +0000 (15:19 +0200)
This includes a modification of lsb_release.py to enable an
overridability with an environment variable LSB_ETC_LSB_RELEASE that can
point to another /etc/lsb-release

lsb_release.py
test/lsb-release [new file with mode: 0644]
test/test_lsb_release.py

index ed0667b9ae0dae5a6927ba566e6f276dfbe26e91..e7e3cbe5828f81af970cbc2d180dc579eb264965 100644 (file)
@@ -300,9 +300,10 @@ def guess_debian_release():
 # Whatever is guessed above can be overridden in /etc/lsb-release
 def get_lsb_information():
     distinfo = {}
-    if os.path.exists('/etc/lsb-release'):
+    etc_lsb_release = os.environ.get('LSB_ETC_LSB_RELEASE','/etc/lsb-release')
+    if os.path.exists(etc_lsb_release):
         try:
-            with open('/etc/lsb-release') as lsb_release_file:
+            with open(etc_lsb_release) as lsb_release_file:
                 for line in lsb_release_file:
                     line = line.strip()
                     if not line:
@@ -318,7 +319,7 @@ def get_lsb_information():
                         if arg: # Ignore empty arguments
                             distinfo[var] = arg.strip()
         except IOError, msg:
-            print >> sys.stderr, 'Unable to open /etc/lsb-release:', str(msg)
+            print >> sys.stderr, 'Unable to open ' + etc_lsb_release , str(msg)
             
     return distinfo
 
diff --git a/test/lsb-release b/test/lsb-release
new file mode 100644 (file)
index 0000000..06d7473
--- /dev/null
@@ -0,0 +1,5 @@
+DISTRIB_ID=(Distributor ID)
+DISTRIB_DESCRIPTION=(A human-readable description of the release)
+DISTRIB_RELEASE=(The release number)
+DISTRIB_CODENAME=(The codename for the release)
+OTHER_VARIABLE=Not supposed to exist
index ef085401e7b36d850d122f069952c06568d98b54..eecd4e80baeef583c14c75b7e84390acabb25c08 100644 (file)
@@ -132,9 +132,20 @@ class TestLSBRelease(unittest.TestCase):
        @unittest.skip('Test not implemented.')
        def test_guess_debian_release(self):
                raise NotImplementedError()
-       @unittest.skip('Test not implemented.')
+
        def test_get_lsb_information(self):
-               raise NotImplementedError()
+               # Test that an inexistant /etc/lsb-release leads to empty output
+               supposed_output = {}
+               os.environ['LSB_ETC_LSB_RELEASE'] = 'test/inexistant_file_' + rnd_string(2,5)
+               self.assertEqual(lr.get_lsb_information(),supposed_output)
+               # Test that a fake /etc/lsb-release leads to output with only the content we want
+               supposed_output = {'RELEASE': '(The release number)',
+                                  'CODENAME': '(The codename for the release)',
+                                  'ID': '(Distributor ID)',
+                                  'DESCRIPTION': '(A human-readable description of the release)'}
+               os.environ['LSB_ETC_LSB_RELEASE'] = 'test/lsb-release'
+               self.assertEqual(lr.get_lsb_information(),supposed_output)
+
        @unittest.skip('Test not implemented.')
        def test_get_distro_information(self):
                raise NotImplementedError()