def test_assert_remove_add_all(self): """Test if removing/adding all the entries works""" for entry in self.fstab.entries: assert_is(self.fstab.remove_entry(entry), True) for device in ('sda', 'sdb', 'sdc', ): self.fstab.add_entry( Fstab.Entry('/dev/%s' % device, '/mnt/%s' % device, 'ext3', None)) self.fstab.add_entry(Fstab.Entry( 'UUID=3af44368-c50b-4768-8e58-aff003cef8be', '/', 'ext4', 'errors=remount-ro', 0, 1)) assert_equal(sorted(GENERATED_FSTAB_FILE.splitlines()), sorted(str(entry) for entry in self.fstab.entries))
def test_add_entry(self): """Test if add_entry works for a new entry""" for device in ('sdf', 'sdg', 'sdh'): entry = Fstab.Entry('/dev/%s' % device, '/mnt/%s' % device, 'ext3', None) assert_is_not(self.fstab.add_entry(entry), None) assert_is_not(self.fstab.get_entry_by_attr( 'device', '/dev/%s' % device), None) assert_is(self.fstab.add_entry(entry), False, "Check if adding an existing entry returns false")
def install_hugepages(): """ Configure hugepages """ hugepage_config = config('hugepages') if hugepage_config: mnt_point = '/run/hugepages/kvm' hugepage_support( 'nova', mnt_point=mnt_point, group='root', nr_hugepages=get_hugepage_number(), mount=False, set_shmmax=True, ) # Remove hugepages entry if present due to Bug #1518771 Fstab.remove_by_mountpoint(mnt_point) if subprocess.call(['mountpoint', mnt_point]): service_restart('qemu-kvm') rsync(charm_dir() + '/files/qemu-hugefsdir', '/etc/init.d/qemu-hugefsdir') subprocess.check_call('/etc/init.d/qemu-hugefsdir') subprocess.check_call(['update-rc.d', 'qemu-hugefsdir', 'defaults'])
def install_hugepages(): """ Configure hugepages """ hugepage_config = config('hugepages') if hugepage_config: mnt_point = '/run/hugepages/kvm' hugepage_support( 'nova', mnt_point=mnt_point, group='root', nr_hugepages=get_hugepage_number(), mount=False, set_shmmax=True, ) # Remove hugepages entry if present due to Bug #1518771 Fstab.remove_by_mountpoint(mnt_point) if subprocess.call(['mountpoint', mnt_point]): service_restart('qemu-kvm') rsync( charm_dir() + '/files/qemu-hugefsdir', '/etc/init.d/qemu-hugefsdir' ) subprocess.check_call('/etc/init.d/qemu-hugefsdir') subprocess.check_call(['update-rc.d', 'qemu-hugefsdir', 'defaults'])
def swapoff(fstab='/etc/fstab'): '''Turn off swapping in the container, permanently.''' # Turn off swap in the current session if helpers.is_lxc(): hookenv.log("In an LXC. Not touching swap.") return else: try: subprocess.check_call(['swapoff', '-a']) except Exception as e: hookenv.log( "Got an error trying to turn off swapping. {}. " "We may be in an LXC. Exiting gracefully" "".format(e), WARNING) return # Disable swap permanently with closing(Fstab(fstab)) as fstab: while True: swap_entry = fstab.get_entry_by_attr('filesystem', 'swap') if swap_entry is None: break fstab.remove_entry(swap_entry)
def setUp(self): self.tempfile = tempfile.NamedTemporaryFile('w+', delete=False) self.tempfile.write(DEFAULT_FSTAB_FILE) self.tempfile.close() self.fstab = Fstab(path=self.tempfile.name)
class FstabTest(unittest.TestCase): def setUp(self): self.tempfile = tempfile.NamedTemporaryFile('w+', delete=False) self.tempfile.write(DEFAULT_FSTAB_FILE) self.tempfile.close() self.fstab = Fstab(path=self.tempfile.name) def tearDown(self): os.unlink(self.tempfile.name) def test_entries(self): """Test if entries are correctly read from fstab file""" self.assertEqual(sorted(GENERATED_FSTAB_FILE.splitlines()), sorted(str(entry) for entry in self.fstab.entries)) def test_get_entry_by_device_attr(self): """Test if the get_entry_by_attr method works for device attr""" for device in ( 'sda', 'sdb', 'sdc', ): self.assertIsNot( self.fstab.get_entry_by_attr('device', '/dev/%s' % device), None) def test_get_entry_by_mountpoint_attr(self): """Test if the get_entry_by_attr method works for mountpoint attr""" for mnt in ( 'sda', 'sdb', 'sdc', ): self.assertIsNot( self.fstab.get_entry_by_attr('mountpoint', '/mnt/%s' % mnt), None) def test_add_entry(self): """Test if add_entry works for a new entry""" for device in ('sdf', 'sdg', 'sdh'): entry = Fstab.Entry('/dev/%s' % device, '/mnt/%s' % device, 'ext3', None) self.assertIsNot(self.fstab.add_entry(entry), None) self.assertIsNot( self.fstab.get_entry_by_attr('device', '/dev/%s' % device), None) self.assertIs(self.fstab.add_entry(entry), False, "Check if adding an existing entry returns false") def test_remove_entry(self): """Test if remove entry works for already existing entries""" for entry in self.fstab.entries: self.assertIs(self.fstab.remove_entry(entry), True) self.assertEqual(len([entry for entry in self.fstab.entries]), 0) self.assertEqual(self.fstab.add_entry(entry), entry) self.assertEqual(len([entry for entry in self.fstab.entries]), 1) def test_assert_remove_add_all(self): """Test if removing/adding all the entries works""" for entry in self.fstab.entries: self.assertIs(self.fstab.remove_entry(entry), True) for device in ( 'sda', 'sdb', 'sdc', ): self.fstab.add_entry( Fstab.Entry('/dev/%s' % device, '/mnt/%s' % device, 'ext3', None)) self.fstab.add_entry( Fstab.Entry('UUID=3af44368-c50b-4768-8e58-aff003cef8be', '/', 'ext4', 'errors=remount-ro', 0, 1)) self.assertEqual(sorted(GENERATED_FSTAB_FILE.splitlines()), sorted(str(entry) for entry in self.fstab.entries))