chiark / gitweb /
sysv-generator: Skip init scripts for existing native services
[elogind.git] / test / sysv-generator-test.py
index 492e99d..09f5c01 100644 (file)
@@ -133,11 +133,11 @@ class SysvGeneratorTest(unittest.TestCase):
 
         # should be enabled
         for runlevel in all_runlevels:
-            link = os.path.join(self.out_dir, 'runlevel%i.target.wants' % runlevel, 'foo.service')
+            link = os.path.join(self.out_dir, 'runlevel%i.target.wants' % runlevel, unit)
             if runlevel in runlevels:
                 target = os.readlink(link)
                 self.assertTrue(os.path.exists(target))
-                self.assertEqual(os.path.basename(target), 'foo.service')
+                self.assertEqual(os.path.basename(target), unit)
             else:
                 self.assertFalse(os.path.exists(link),
                                  '%s unexpectedly exists' % link)
@@ -271,6 +271,25 @@ class SysvGeneratorTest(unittest.TestCase):
             self.assertEqual(os.readlink(os.path.join(self.out_dir, f)),
                              'foo.service')
 
+    def test_same_provides_in_multiple_scripts(self):
+        '''multiple init.d scripts provide the same name'''
+
+        self.add_sysv('foo', {'Provides': 'foo common'}, enable=True, prio=1)
+        self.add_sysv('bar', {'Provides': 'bar common'}, enable=True, prio=2)
+        err, results = self.run_generator()
+        self.assertEqual(sorted(results), ['bar.service', 'foo.service'])
+        # should create symlink for the alternative name for either unit
+        self.assertIn(os.readlink(os.path.join(self.out_dir, 'common.service')),
+                      ['foo.service', 'bar.service'])
+
+    def test_provide_other_script(self):
+        '''init.d scripts provides the name of another init.d script'''
+
+        self.add_sysv('foo', {'Provides': 'foo bar'}, enable=True)
+        self.add_sysv('bar', {'Provides': 'bar'}, enable=True)
+        err, results = self.run_generator()
+        self.assertEqual(sorted(results), ['bar.service', 'foo.service'])
+
     def test_nonexecutable_script(self):
         '''ignores non-executable init.d script'''
 
@@ -278,6 +297,88 @@ class SysvGeneratorTest(unittest.TestCase):
         err, results = self.run_generator()
         self.assertEqual(results, {})
 
+    def test_sh_suffix(self):
+        '''init.d script with .sh suffix'''
+
+        self.add_sysv('foo.sh', {}, enable=True)
+        err, results = self.run_generator()
+        s = results['foo.service']
+
+        self.assertEqual(s.sections(), ['Unit', 'Service'])
+        # should not have a .sh
+        self.assertEqual(s.get('Unit', 'Description'), 'LSB: test foo service')
+
+        # calls correct script with .sh
+        init_script = os.path.join(self.init_d_dir, 'foo.sh')
+        self.assertEqual(s.get('Service', 'ExecStart'),
+                         '%s start' % init_script)
+        self.assertEqual(s.get('Service', 'ExecStop'),
+                         '%s stop' % init_script)
+
+        self.assert_enabled('foo.service', [2, 3, 4, 5])
+
+    def test_sh_suffix_with_provides(self):
+        '''init.d script with .sh suffix and Provides:'''
+
+        self.add_sysv('foo.sh', {'Provides': 'foo bar'})
+        err, results = self.run_generator()
+        # ensure we don't try to create a symlink to itself
+        self.assertNotIn(err, 'itself')
+        self.assertEqual(list(results), ['foo.service'])
+        self.assertEqual(results['foo.service'].get('Unit', 'Description'),
+                         'LSB: test foo service')
+
+        # should create symlink for the alternative name
+        self.assertEqual(os.readlink(os.path.join(self.out_dir, 'bar.service')),
+                         'foo.service')
+
+    def test_hidden_files(self):
+        '''init.d script with hidden file suffix'''
+
+        script = self.add_sysv('foo', {}, enable=True)
+        # backup files (not enabled in rcN.d/)
+        shutil.copy(script, script + '.dpkg-new')
+        shutil.copy(script, script + '.dpkg-dist')
+        shutil.copy(script, script + '.swp')
+        shutil.copy(script, script + '.rpmsave')
+
+        err, results = self.run_generator()
+        self.assertEqual(list(results), ['foo.service'])
+
+        self.assert_enabled('foo.service', [2, 3, 4, 5])
+
+    def test_backup_file(self):
+        '''init.d script with backup file'''
+
+        script = self.add_sysv('foo', {}, enable=True)
+        # backup files (not enabled in rcN.d/)
+        shutil.copy(script, script + '.bak')
+        shutil.copy(script, script + '.old')
+
+        err, results = self.run_generator()
+        print(err)
+        self.assertEqual(sorted(results),
+                         ['foo.bak.service', 'foo.old.service', 'foo.service'])
+
+        # ensure we don't try to create a symlink to itself
+        self.assertNotIn(err, 'itself')
+
+        self.assert_enabled('foo.service', [2, 3, 4, 5])
+        self.assert_enabled('foo.bak.service', [])
+        self.assert_enabled('foo.old.service', [])
+
+    def test_existing_native_unit(self):
+        '''existing native unit'''
+
+        with open(os.path.join(self.unit_dir, 'foo.service'), 'w') as f:
+            f.write('[Unit]\n')
+
+        self.add_sysv('foo.sh', {'Provides': 'foo bar'}, enable=True)
+        err, results = self.run_generator()
+        self.assertEqual(list(results), [])
+        # no enablement or alias links, as native unit is disabled
+        self.assertEqual(os.listdir(self.out_dir), [])
+
 
 if __name__ == '__main__':
     unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))