1. Moises Henriquez
  2. vinstall

Commits

M0E-lnx  committed d861d6a

Add test for grub2

  • Participants
  • Parent commits f007769

Comments (0)

Files changed (1)

File vinstall/backend/bootloader.py

View file
     """Grub2 bootloader
 
     """
-    def __init__(self, target):
-        self.config_path = "/etc/default/grub"
+    def __init__(self, target, config_path = "/etc/default/grub"):
+        self.config_path = config_path
         self.type = GRUB2
         self.timeout = 3
         self.target = target
     def backup_config(self):
         pass
 
-    def write_config(self, config_path="/etc/default/grub"):
+    def write_config(self, output_file="/boot/grub/grub.cfg"):
         """The grub2 config file is automatically generated, so we use
         this method to save the values to /etc/default/grub which is
         parsed while the config file is generated
                  "# END OF /etc/default/grub",""]
 
         # save the new file
-        with open(config_path, "w") as f:
+        with open(self.config_path, "w") as f:
             f.writelines("\n".join(ndata))
 
         # after these are set, we just need to run grub-mkconfig
-        subprocess.check_call(["grub-mkconfig", "-o", "/boot/grub/grub.cfg"])
+        subprocess.check_call(["grub-mkconfig", "-o", output_file ])
 
     def install(self):
         """Install grub to the target
         subprocess.check_call(["grub-mkdevicemap", "--no-floppy"])
         subprocess.check_call(["grub-install", "--no-floppy", self.target])
 
-    def test_install(self, config_file, bootdev):
+    def test_install(self, bootdev):
         subprocess.check_call(["grub-mkdevicemap", "--no-floppy"])
         subprocess.check_call(["grub-install", "--no-floppy", bootdev])
 
     def test_lilo(self):
         import os
         if not os.path.exists('/bin/lilo'):
-            print "LiLO not found.  Skipping test"
-            return True
+            #return self.fail("Cannot test without Lilo")
+            return
         lilo = Lilo(self.path)
         for ops in OperatingSystem.all(include_running_os=True):
             lilo.add_os(ops)
         self.assertEqual(mbr.bootloader(), LILO)
 
 
-class Grub2TestCase(FakeDeviceTestCase):
-
+class Grub2TestCase(unittest.TestCase):
+    def setUp(self):
+        import subprocess as sp
+        self.lodev = "/dev/loop1"
+        self.img = "/tmp/vram.img"
+        self.defaults = '/tmp/grub2-defaults'
+        self.output_file = '/tmp/grub2-test'
+        sp.check_call(['dd','if=/dev/zero','of=%s'%self.img, 'bs=512', 'count=5120'])
+        sp.check_call(['losetup', self.lodev, self.img])
+        # create partition table (label)
+        sp.check_call(['parted', self.lodev, '-s', 'mklabel msdos'])
+        sp.check_call(['parted', self.lodev, '-s', 'mkpart primary ext2 1M 2M'])
+        
+    def tearDown(self):
+        import subprocess as sp
+        import os
+        sp.check_call(['losetup', '-d', self.lodev])
+        os.remove(self.img)
+        os.remove(self.defaults)
+        os.remove(self.output_file)
+        #os.remove('/tmp/grub2-grub-default')
+        
     def test_grub2(self):
-        return True
-        grub2 = Grub2(self.path)
-        grub2.write_config(config_path="/tmp/grub2-test")
-        grub2.test_install("/tmp/grub2-test", self.path)
-        mbr = MasterBootRecord(self.path)
+        grub2 = Grub2(self.lodev, config_path=self.defaults)
+        grub2.write_config(output_file= self.output_file)
+        grub2.test_install(bootdev=self.lodev)
+        mbr = MasterBootRecord(self.lodev)
         self.assertEqual(mbr.bootloader(), GRUB2)
 
 
+#class Grub2TestCase(FakeDeviceTestCase):
+#
+#    def test_grub2(self):
+#        return True
+#        grub2 = Grub2(self.path)
+#        grub2.write_config(config_path="/tmp/grub2-test")
+#        grub2.test_install("/tmp/grub2-test", self.path)
+#        mbr = MasterBootRecord(self.path)
+#        self.assertEqual(mbr.bootloader(), GRUB2)
+
+
 if __name__ == "__main__":
     unittest.main()