Commit bbf93e52 authored by Lars Wirzenius's avatar Lars Wirzenius

Add: Image class

parent 5119fa71
......@@ -31,6 +31,7 @@ from .runcmd import (
progress,
error,
)
from .image import Image, UnknownTag, TagInUse, AlreadyMounted
from .unmount import unmount, NotMounted
from .spec import (
Spec,
......
# Copyright 2018 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=
# Unmount a directory, including any mount points under that
# directory. If /mnt/foo is given, and /mnt/foo/bar is also mounted,
# unmount /mnt/foo/bar first, and /mnt/foo then. Look for sub-mounts
# in /proc/mounts.
import vmdb
class Image:
def __init__(self):
self._tags = {}
def get_tags(self):
return list(self._tags.keys())
def get_dev(self, tag):
item = self._tags.get(tag)
if item is None:
raise UnknownTag(tag)
return item['dev']
def get_mount_point(self, tag):
item = self._tags.get(tag)
if item is None:
raise UnknownTag(tag)
return item['mount_point']
def add_partition(self, tag, dev):
if tag in self._tags:
raise TagInUse(tag)
self._new(tag, dev, None)
def add_mount_point(self, tag, mount_point):
item = self._tags.get(tag)
if item is None:
raise UnknownTag(tag)
if item['mount_point'] is not None:
raise AlreadyMounted(tag)
item['mount_point'] = mount_point
def _new(self, tag, dev, mount_point):
self._tags[tag] = {
'dev': dev,
'mount_point': mount_point,
}
class TagInUse(Exception):
def __init__(self, tag):
super().__init__('Tag already used: {}'.format(tag))
class UnknownTag(Exception):
def __init__(self, tag):
super().__init__('Unknown tag: {}'.format(tag))
class AlreadyMounted(Exception):
def __init__(self, tag):
super().__init__('Already mounted tag: {}'.format(tag))
# Copyright 2018 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=
import unittest
import vmdb
class ImageTests(unittest.TestCase):
def test_lists_not_tags_initally(self):
image = vmdb.Image()
self.assertEqual(image.get_tags(), [])
def test_get_dev_raises_error_for_unknown_tag(self):
image = vmdb.Image()
with self.assertRaises(vmdb.UnknownTag):
image.get_dev('does-not-exist')
def test_get_mount_point_raises_error_for_unknown_tag(self):
image = vmdb.Image()
with self.assertRaises(vmdb.UnknownTag):
image.get_mount_point('does-not-exist')
def test_raises_error_for_reused_tag(self):
image = vmdb.Image()
image.add_partition('tag', 'dev')
with self.assertRaises(vmdb.TagInUse):
image.add_partition('tag', 'dev')
def test_adds_partition(self):
image = vmdb.Image()
image.add_partition('first', '/dev/foo')
self.assertEqual(image.get_tags(), ['first'])
self.assertEqual(image.get_dev('first'), '/dev/foo')
self.assertEqual(image.get_mount_point('first'), None)
def test_adds_mount_point(self):
image = vmdb.Image()
image.add_partition('first', '/dev/foo')
image.add_mount_point('first', '/mnt/foo')
self.assertEqual(image.get_tags(), ['first'])
self.assertEqual(image.get_dev('first'), '/dev/foo')
self.assertEqual(image.get_mount_point('first'), '/mnt/foo')
def test_add_mount_point_raises_error_for_unknown_tag(self):
image = vmdb.Image()
with self.assertRaises(vmdb.UnknownTag):
image.add_mount_point('first', '/mnt/foo')
def test_add_mount_point_raises_error_for_double_mount(self):
image = vmdb.Image()
image.add_partition('first', '/dev/foo')
image.add_mount_point('first', '/mnt/foo')
with self.assertRaises(vmdb.AlreadyMounted):
image.add_mount_point('first', '/mnt/foo')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment