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

Proposed by Newell Jensen
Status: Merged
Approved by: Newell Jensen
Approved revision: afa1c959e97bd3a68deba748126f2b4c9574bb1c
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~newell-jensen/maas:2.3-lp1799161
Merge into: maas:2.3
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+360143@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
1diff --git a/src/maasserver/api/tests/test_blockdevice.py b/src/maasserver/api/tests/test_blockdevice.py
2index 9312d9a..1c40285 100644
3--- a/src/maasserver/api/tests/test_blockdevice.py
4+++ b/src/maasserver/api/tests/test_blockdevice.py
5@@ -1,4 +1,4 @@
6-# Copyright 2015-2016 Canonical Ltd. This software is licensed under the
7+# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
8 # GNU Affero General Public License version 3 (see the file LICENSE).
9
10 """Tests for blockdevice API."""
11diff --git a/src/maasserver/forms/__init__.py b/src/maasserver/forms/__init__.py
12index 322aa6a..b282c56 100644
13--- a/src/maasserver/forms/__init__.py
14+++ b/src/maasserver/forms/__init__.py
15@@ -108,6 +108,7 @@ from maasserver.enum import (
16 BOOT_RESOURCE_TYPE,
17 CACHE_MODE_TYPE_CHOICES,
18 FILESYSTEM_FORMAT_TYPE_CHOICES,
19+ FILESYSTEM_FORMAT_TYPE_CHOICES_DICT,
20 FILESYSTEM_GROUP_RAID_TYPE_CHOICES,
21 FILESYSTEM_TYPE,
22 INTERFACE_TYPE,
23@@ -2531,10 +2532,18 @@ class FormatBlockDeviceForm(Form):
24
25 This implementation of `save` does not support the `commit` argument.
26 """
27- # Remove the previous format if one already exists.
28- Filesystem.objects.filter(
29+ filesystem = Filesystem.objects.filter(
30 block_device=self.block_device,
31- acquired=self.node.is_in_allocated_state()).delete()
32+ acquired=self.node.is_in_allocated_state()).first()
33+ if (filesystem is not None and
34+ filesystem.fstype not in FILESYSTEM_FORMAT_TYPE_CHOICES_DICT):
35+ raise ValidationError(
36+ "Cannot format a block device that has a filesystem "
37+ "type of %s." % filesystem.fstype)
38+
39+ # Remove the previous format if one already exists.
40+ if filesystem is not None:
41+ filesystem.delete()
42
43 # Create the new filesystem
44 Filesystem.objects.create(
45@@ -2594,10 +2603,18 @@ class FormatPartitionForm(Form):
46
47 This implementation of `save` does not support the `commit` argument.
48 """
49- # Remove the previous format if one already exists.
50- Filesystem.objects.filter(
51+ filesystem = Filesystem.objects.filter(
52 partition=self.partition,
53- acquired=self.node.is_in_allocated_state()).delete()
54+ acquired=self.node.is_in_allocated_state()).first()
55+ if (filesystem is not None and
56+ filesystem.fstype not in FILESYSTEM_FORMAT_TYPE_CHOICES_DICT):
57+ raise ValidationError(
58+ "Cannot format a partition that has a filesystem "
59+ "type of %s." % filesystem.fstype)
60+
61+ # Remove the previous format if one already exists.
62+ if filesystem is not None:
63+ filesystem.delete()
64
65 # Create the new filesystem
66 Filesystem.objects.create(
67diff --git a/src/maasserver/forms/tests/test_blockdevice.py b/src/maasserver/forms/tests/test_blockdevice.py
68index 39e6596..cef2280 100644
69--- a/src/maasserver/forms/tests/test_blockdevice.py
70+++ b/src/maasserver/forms/tests/test_blockdevice.py
71@@ -1,4 +1,4 @@
72-# Copyright 2015-2016 Canonical Ltd. This software is licensed under the
73+# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
74 # GNU Affero General Public License version 3 (see the file LICENSE).
75
76 """Tests for all forms that are used with `BlockDevice`."""
77@@ -8,6 +8,7 @@ __all__ = []
78 import random
79 import uuid
80
81+from django.core.exceptions import ValidationError
82 from maasserver.enum import FILESYSTEM_TYPE
83 from maasserver.forms import (
84 CreatePhysicalBlockDeviceForm,
85@@ -109,6 +110,16 @@ class TestFormatBlockDeviceForm(MAASServerTestCase):
86 "Should be invalid because of an invalid uuid.")
87 self.assertEqual({'uuid': ["Enter a valid value."]}, form._errors)
88
89+ def test_is_not_valid_if_non_user_format_fstype(self):
90+ block_device = factory.make_PhysicalBlockDevice()
91+ factory.make_Filesystem(
92+ fstype='bcache-backing', block_device=block_device)
93+ data = {
94+ 'fstype': FILESYSTEM_TYPE.EXT4,
95+ }
96+ form = FormatBlockDeviceForm(block_device, data=data)
97+ self.assertRaises(ValidationError, form.save)
98+
99 def test_creates_filesystem(self):
100 fsuuid = "%s" % uuid.uuid4()
101 fstype = factory.pick_filesystem_type()
102diff --git a/src/maasserver/forms/tests/test_partition.py b/src/maasserver/forms/tests/test_partition.py
103index b3a5d68..c3de82e 100644
104--- a/src/maasserver/forms/tests/test_partition.py
105+++ b/src/maasserver/forms/tests/test_partition.py
106@@ -1,4 +1,4 @@
107-# Copyright 2015-2016 Canonical Ltd. This software is licensed under the
108+# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
109 # GNU Affero General Public License version 3 (see the file LICENSE).
110
111 """Tests for all forms that are used with `Partition`."""
112@@ -7,6 +7,7 @@ __all__ = []
113
114 import uuid
115
116+from django.core.exceptions import ValidationError
117 from maasserver.enum import FILESYSTEM_TYPE
118 from maasserver.forms import (
119 AddPartitionForm,
120@@ -159,6 +160,15 @@ class TestFormatPartitionForm(MAASServerTestCase):
121 ],
122 }, form._errors)
123
124+ def test_is_not_valid_if_non_user_format_fstype(self):
125+ partition = factory.make_Partition()
126+ factory.make_Filesystem(fstype='bcache-backing', partition=partition)
127+ data = {
128+ 'fstype': FILESYSTEM_TYPE.EXT4,
129+ }
130+ form = FormatPartitionForm(partition, data=data)
131+ self.assertRaises(ValidationError, form.save)
132+
133 def test_creates_filesystem(self):
134 fsuuid = "%s" % uuid.uuid4()
135 fstype = factory.pick_filesystem_type()

Subscribers

People subscribed via source and target branches