PyUT: Implement test for lsb_release.guess_debian_release.
authorDidier Raboud <odyx@debian.org>
Wed, 23 May 2012 08:08:42 +0000 (10:08 +0200)
committerDidier Raboud <odyx@debian.org>
Wed, 23 May 2012 12:53:47 +0000 (14:53 +0200)
This includes a modification of lsb_release.py to enable an
overridability with an environment variable LSB_ETC_DEBIAN_VERSION that
can point to another /etc/debian_version.

lsb_release.py
test/apt-cache
test/test_lsb_release.py

index e7e3cbe5828f81af970cbc2d180dc579eb264965..611737ca7f2c336a2a83f815b526137e17cc7e1f 100644 (file)
@@ -242,12 +242,13 @@ def guess_debian_release():
 
     distinfo['DESCRIPTION'] = '%(ID)s %(OS)s' % distinfo
 
-    if os.path.exists('/etc/debian_version'):
+    etc_debian_version = os.environ.get('LSB_ETC_DEBIAN_VERSION','/etc/debian_version')
+    if os.path.exists(etc_debian_version):
         try:
-            with open('/etc/debian_version') as debian_version:
+            with open(etc_debian_version) as debian_version:
                 release = debian_version.read().strip()
         except IOError, msg:
-            print >> sys.stderr, 'Unable to open /etc/debian_version:', str(msg)
+            print >> sys.stderr, 'Unable to open ' + etc_debian_version + ':', str(msg)
             release = 'unknown'
             
         if not release[0:1].isalpha():
index 9106a42718541120c6c3642fbefe606346afcc82..a31c998e23642567ad9702c9056d96184d3da2a9 100755 (executable)
@@ -37,3 +37,15 @@ if os.environ.get('TEST_APT_CACHE_RELEASE') == '512':
        print(' 512 http://MirRor_is_not_read/folder-either-debian/ exp/main arch Packages')
        print('     release o=P-or1g1n,a=sid,n=codename-not-read,l=P-l8bel,c=OtherComp')
        print('     origin MirRor-is-not-read')
+
+if os.environ.get('TEST_APT_CACHE_UNSTABLE') == '500':
+       print(' 500 http://MirRor_is_not_read/folder-either-debian/ sid/main arch Packages')
+       print('     release o=Debian,a=unstable,n=sid,l=Debian,c=main')
+       print('     origin MirRor-is-not-read')
+
+if os.environ.get('TEST_APT_CACHE_UNSTABLE_PORTS') == '500':
+       print(' 500 http://MirRor_is_not_read/folder-either-debian-ports/ sid/main arch Packages')
+       print('     release o=Debian Ports,a=unstable,n=sid,l=ftp.debian-ports.org,c=main,v=1.0')
+       print('     origin MirRor-is-not-read')
+
+print('Pinned packages:')
index ccd7ec5c362756b65745222f25d02240ad23f4b7..52e69ae1764728293b12618962747a1cf97d9d2d 100644 (file)
@@ -142,9 +142,92 @@ class TestLSBRelease(unittest.TestCase):
                os.environ.pop('TEST_APT_CACHE2')
                os.environ.pop('TEST_APT_CACHE_RELEASE')
 
-       @unittest.skip('Test not implemented.')
        def test_guess_debian_release(self):
-               raise NotImplementedError()
+               # Copied verbatim from guess_debian_release; sucks but unavoidable.
+               distinfo = {'ID' : 'Debian'}
+               kern = os.uname()[0]
+               if kern in ('Linux', 'Hurd', 'NetBSD'):
+                       distinfo['OS'] = 'GNU/'+kern
+               elif kern == 'FreeBSD':
+                       distinfo['OS'] = 'GNU/k'+kern
+               elif kern in ('GNU/Linux', 'GNU/kFreeBSD'):
+                       distinfo['OS'] = kern
+               else:
+                       distinfo['OS'] = 'GNU'
+
+               # Test "stable releases" with numeric debian_versions
+               for rno in lr.RELEASE_CODENAME_LOOKUP:
+                       distinfo['RELEASE'] = rno + random.choice('.r') + str(random.randint(0,9))
+                       distinfo['CODENAME'] = lr.RELEASE_CODENAME_LOOKUP[rno]
+                       distinfo['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s (%(CODENAME)s)' % distinfo
+                       fn = 'test/debian_version_' + rnd_string(5,5)
+                       f = open(fn,'w')
+                       f.write(distinfo['RELEASE'])
+                       f.close()
+                       os.environ['LSB_ETC_DEBIAN_VERSION'] = fn
+                       self.assertEqual(lr.guess_debian_release(),distinfo)
+                       os.remove(fn)
+               os.environ.pop('LSB_ETC_DEBIAN_VERSION')
+
+               # Remove the CODENAME from the supposed output
+               distinfo.pop('CODENAME')
+               # Test "stable releases" with string debian_versions, go read invalid apt-cache policy
+               for rno in lr.RELEASE_CODENAME_LOOKUP:
+                       distinfo['RELEASE']  = lr.RELEASE_CODENAME_LOOKUP[rno]
+                       distinfo['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s' % distinfo
+                       fn = 'test/debian_version_' + rnd_string(5,12)
+                       f = open(fn,'w')
+                       f.write(distinfo['RELEASE'])
+                       f.close()
+                       os.environ['LSB_ETC_DEBIAN_VERSION'] = fn
+                       self.assertEqual(lr.guess_debian_release(),distinfo)
+                       os.remove(fn)
+               os.environ.pop('LSB_ETC_DEBIAN_VERSION')
+
+               # Test "unstable releases" that end in /sid, go read invalid apt-cache policy
+               distinfo['RELEASE'] = 'testing/unstable'
+               distinfo['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s' % distinfo
+               for rno in lr.RELEASE_CODENAME_LOOKUP:
+                       fn = 'test/debian_version_' + rnd_string(5,12)
+                       f = open(fn,'w')
+                       f.write(lr.RELEASE_CODENAME_LOOKUP[rno] + '/sid')
+                       f.close()
+                       os.environ['LSB_ETC_DEBIAN_VERSION'] = fn
+                       self.assertEqual(lr.guess_debian_release(),distinfo)
+                       os.remove(fn)
+               os.environ.pop('LSB_ETC_DEBIAN_VERSION')
+
+               # Test "unstable releases" that end in /sid, go read valid apt-cache policy
+               os.environ['TEST_APT_CACHE_UNSTABLE'] = '500'
+               distinfo['CODENAME'] = 'sid'
+               distinfo['RELEASE'] = 'unstable'
+               distinfo['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s (%(CODENAME)s)' % distinfo
+               for rno in lr.RELEASE_CODENAME_LOOKUP:
+                       fn = 'test/debian_version_' + rnd_string(5,12)
+                       f = open(fn,'w')
+                       f.write(lr.RELEASE_CODENAME_LOOKUP[rno] + '/sid')
+                       f.close()
+                       os.environ['LSB_ETC_DEBIAN_VERSION'] = fn
+                       self.assertEqual(lr.guess_debian_release(),distinfo)
+                       os.remove(fn)
+               os.environ.pop('LSB_ETC_DEBIAN_VERSION')
+
+               # Test "unstable releases with Debian Ports" that end in /sid, go read valid apt-cache policy
+               os.environ['TEST_APT_CACHE_UNSTABLE_PORTS'] = '500'
+               distinfo['CODENAME'] = 'sid'
+               distinfo['RELEASE'] = 'unstable'
+               distinfo['DESCRIPTION'] = '%(ID)s %(OS)s %(RELEASE)s (%(CODENAME)s)' % distinfo
+               for rno in lr.RELEASE_CODENAME_LOOKUP:
+                       fn = 'test/debian_version_' + rnd_string(5,12)
+                       f = open(fn,'w')
+                       f.write(lr.RELEASE_CODENAME_LOOKUP[rno] + '/sid')
+                       f.close()
+                       os.environ['LSB_ETC_DEBIAN_VERSION'] = fn
+                       self.assertEqual(lr.guess_debian_release(),distinfo)
+                       os.remove(fn)
+               os.environ.pop('LSB_ETC_DEBIAN_VERSION')
+               os.environ.pop('TEST_APT_CACHE_UNSTABLE_PORTS')
+               os.environ.pop('TEST_APT_CACHE_UNSTABLE')
 
        def test_get_lsb_information(self):
                # Test that an inexistant /etc/lsb-release leads to empty output