Merge ~newell-jensen/maas:2.4-lp1799161 into maas:2.4

Proposed by Newell Jensen
Status: Merged
Approved by: Newell Jensen
Approved revision: b96184de8d4558efe91beef2dd31524a0b4754f3
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~newell-jensen/maas:2.4-lp1799161
Merge into: maas:2.4
Diff against target: 135 lines (+47/-9)
4 files modified
src/maasserver/api/tests/test_blockdevice.py (+1/-1)
src/maasserver/forms/__init__.py (+23/-6)
src/maasserver/forms/tests/test_blockdevice.py (+12/-1)
src/maasserver/forms/tests/test_partition.py (+11/-1)
Reviewer Review Type Date Requested Status
Newell Jensen (community) Approve
Review via email: mp+360103@code.launchpad.net

Commit message

Backport of d7581c456287e5dc5149a77d96b7943a926a1aa6

LP: #1799161 -- Do not allow partition formatting if current filesystem is a non-formatted type.

To post a comment you must log in.
Revision history for this message
Newell Jensen (newell-jensen) wrote :

Self-approved backport.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/maasserver/api/tests/test_blockdevice.py b/src/maasserver/api/tests/test_blockdevice.py
index 9312d9a..1c40285 100644
--- a/src/maasserver/api/tests/test_blockdevice.py
+++ b/src/maasserver/api/tests/test_blockdevice.py
@@ -1,4 +1,4 @@
1# Copyright 2015-2016 Canonical Ltd. This software is licensed under the1# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4"""Tests for blockdevice API."""4"""Tests for blockdevice API."""
diff --git a/src/maasserver/forms/__init__.py b/src/maasserver/forms/__init__.py
index d59b282..4e97792 100644
--- a/src/maasserver/forms/__init__.py
+++ b/src/maasserver/forms/__init__.py
@@ -111,6 +111,7 @@ from maasserver.enum import (
111 BOOT_RESOURCE_TYPE,111 BOOT_RESOURCE_TYPE,
112 CACHE_MODE_TYPE_CHOICES,112 CACHE_MODE_TYPE_CHOICES,
113 FILESYSTEM_FORMAT_TYPE_CHOICES,113 FILESYSTEM_FORMAT_TYPE_CHOICES,
114 FILESYSTEM_FORMAT_TYPE_CHOICES_DICT,
114 FILESYSTEM_GROUP_RAID_TYPE_CHOICES,115 FILESYSTEM_GROUP_RAID_TYPE_CHOICES,
115 FILESYSTEM_TYPE,116 FILESYSTEM_TYPE,
116 INTERFACE_TYPE,117 INTERFACE_TYPE,
@@ -2577,10 +2578,18 @@ class FormatBlockDeviceForm(Form):
25772578
2578 This implementation of `save` does not support the `commit` argument.2579 This implementation of `save` does not support the `commit` argument.
2579 """2580 """
2580 # Remove the previous format if one already exists.2581 filesystem = Filesystem.objects.filter(
2581 Filesystem.objects.filter(
2582 block_device=self.block_device,2582 block_device=self.block_device,
2583 acquired=self.node.is_in_allocated_state()).delete()2583 acquired=self.node.is_in_allocated_state()).first()
2584 if (filesystem is not None and
2585 filesystem.fstype not in FILESYSTEM_FORMAT_TYPE_CHOICES_DICT):
2586 raise ValidationError(
2587 "Cannot format a block device that has a filesystem "
2588 "type of %s." % filesystem.fstype)
2589
2590 # Remove the previous format if one already exists.
2591 if filesystem is not None:
2592 filesystem.delete()
25842593
2585 # Create the new filesystem2594 # Create the new filesystem
2586 Filesystem.objects.create(2595 Filesystem.objects.create(
@@ -2640,10 +2649,18 @@ class FormatPartitionForm(Form):
26402649
2641 This implementation of `save` does not support the `commit` argument.2650 This implementation of `save` does not support the `commit` argument.
2642 """2651 """
2643 # Remove the previous format if one already exists.2652 filesystem = Filesystem.objects.filter(
2644 Filesystem.objects.filter(
2645 partition=self.partition,2653 partition=self.partition,
2646 acquired=self.node.is_in_allocated_state()).delete()2654 acquired=self.node.is_in_allocated_state()).first()
2655 if (filesystem is not None and
2656 filesystem.fstype not in FILESYSTEM_FORMAT_TYPE_CHOICES_DICT):
2657 raise ValidationError(
2658 "Cannot format a partition that has a filesystem "
2659 "type of %s." % filesystem.fstype)
2660
2661 # Remove the previous format if one already exists.
2662 if filesystem is not None:
2663 filesystem.delete()
26472664
2648 # Create the new filesystem2665 # Create the new filesystem
2649 Filesystem.objects.create(2666 Filesystem.objects.create(
diff --git a/src/maasserver/forms/tests/test_blockdevice.py b/src/maasserver/forms/tests/test_blockdevice.py
index 39e6596..cef2280 100644
--- a/src/maasserver/forms/tests/test_blockdevice.py
+++ b/src/maasserver/forms/tests/test_blockdevice.py
@@ -1,4 +1,4 @@
1# Copyright 2015-2016 Canonical Ltd. This software is licensed under the1# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4"""Tests for all forms that are used with `BlockDevice`."""4"""Tests for all forms that are used with `BlockDevice`."""
@@ -8,6 +8,7 @@ __all__ = []
8import random8import random
9import uuid9import uuid
1010
11from django.core.exceptions import ValidationError
11from maasserver.enum import FILESYSTEM_TYPE12from maasserver.enum import FILESYSTEM_TYPE
12from maasserver.forms import (13from maasserver.forms import (
13 CreatePhysicalBlockDeviceForm,14 CreatePhysicalBlockDeviceForm,
@@ -109,6 +110,16 @@ class TestFormatBlockDeviceForm(MAASServerTestCase):
109 "Should be invalid because of an invalid uuid.")110 "Should be invalid because of an invalid uuid.")
110 self.assertEqual({'uuid': ["Enter a valid value."]}, form._errors)111 self.assertEqual({'uuid': ["Enter a valid value."]}, form._errors)
111112
113 def test_is_not_valid_if_non_user_format_fstype(self):
114 block_device = factory.make_PhysicalBlockDevice()
115 factory.make_Filesystem(
116 fstype='bcache-backing', block_device=block_device)
117 data = {
118 'fstype': FILESYSTEM_TYPE.EXT4,
119 }
120 form = FormatBlockDeviceForm(block_device, data=data)
121 self.assertRaises(ValidationError, form.save)
122
112 def test_creates_filesystem(self):123 def test_creates_filesystem(self):
113 fsuuid = "%s" % uuid.uuid4()124 fsuuid = "%s" % uuid.uuid4()
114 fstype = factory.pick_filesystem_type()125 fstype = factory.pick_filesystem_type()
diff --git a/src/maasserver/forms/tests/test_partition.py b/src/maasserver/forms/tests/test_partition.py
index b3a5d68..c3de82e 100644
--- a/src/maasserver/forms/tests/test_partition.py
+++ b/src/maasserver/forms/tests/test_partition.py
@@ -1,4 +1,4 @@
1# Copyright 2015-2016 Canonical Ltd. This software is licensed under the1# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4"""Tests for all forms that are used with `Partition`."""4"""Tests for all forms that are used with `Partition`."""
@@ -7,6 +7,7 @@ __all__ = []
77
8import uuid8import uuid
99
10from django.core.exceptions import ValidationError
10from maasserver.enum import FILESYSTEM_TYPE11from maasserver.enum import FILESYSTEM_TYPE
11from maasserver.forms import (12from maasserver.forms import (
12 AddPartitionForm,13 AddPartitionForm,
@@ -159,6 +160,15 @@ class TestFormatPartitionForm(MAASServerTestCase):
159 ],160 ],
160 }, form._errors)161 }, form._errors)
161162
163 def test_is_not_valid_if_non_user_format_fstype(self):
164 partition = factory.make_Partition()
165 factory.make_Filesystem(fstype='bcache-backing', partition=partition)
166 data = {
167 'fstype': FILESYSTEM_TYPE.EXT4,
168 }
169 form = FormatPartitionForm(partition, data=data)
170 self.assertRaises(ValidationError, form.save)
171
162 def test_creates_filesystem(self):172 def test_creates_filesystem(self):
163 fsuuid = "%s" % uuid.uuid4()173 fsuuid = "%s" % uuid.uuid4()
164 fstype = factory.pick_filesystem_type()174 fstype = factory.pick_filesystem_type()

Subscribers

People subscribed via source and target branches