diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-08-18 20:42:12 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-08-18 20:42:12 +0300 |
commit | bbf93e52c16646dd488c33261c907d3183691d34 (patch) | |
tree | 2d7b994cda164dfa2c2ddf433274397bda1e4a25 | |
parent | 5119fa71f37223bd3a5c617b97688d711da89e63 (diff) | |
download | vmdb2-bbf93e52c16646dd488c33261c907d3183691d34.tar.gz |
Add: Image class
-rw-r--r-- | vmdb/__init__.py | 1 | ||||
-rw-r--r-- | vmdb/image.py | 83 | ||||
-rw-r--r-- | vmdb/image_tests.py | 72 |
3 files changed, 156 insertions, 0 deletions
diff --git a/vmdb/__init__.py b/vmdb/__init__.py index 7a2c584..0163b53 100644 --- a/vmdb/__init__.py +++ b/vmdb/__init__.py @@ -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, diff --git a/vmdb/image.py b/vmdb/image.py new file mode 100644 index 0000000..67c39de --- /dev/null +++ b/vmdb/image.py @@ -0,0 +1,83 @@ +# 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)) diff --git a/vmdb/image_tests.py b/vmdb/image_tests.py new file mode 100644 index 0000000..e2e72db --- /dev/null +++ b/vmdb/image_tests.py @@ -0,0 +1,72 @@ +# 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') |