/
add_partition_test.rb
317 lines (250 loc) · 10.5 KB
/
add_partition_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env rspec
# encoding: utf-8
# Copyright (c) [2017] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# 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, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require_relative "../test_helper"
require "y2partitioner/device_graphs"
require "y2partitioner/actions/add_partition"
require "y2partitioner/dialogs"
describe Y2Partitioner::Actions::AddPartition do
# Defined as method instead of let clause because using let it points to the
# current devicegraph at the moment to call #current_graph for first time, but
# after a transaction, the current devicegraph could change. The tests need to
# always access to the current devicegraph, even after a transaction.
def current_graph
Y2Partitioner::DeviceGraphs.instance.current
end
before do
devicegraph_stub(scenario)
allow(Yast::Wizard).to receive(:OpenNextBackDialog)
allow(Yast::Wizard).to receive(:CloseDialog)
allow(subject).to receive(:skip_steps)
end
subject(:action) { described_class.new(disk) }
let(:disk) { Y2Storage::Disk.find_by_name(current_graph, disk_name) }
describe "#run" do
let(:scenario) { "lvm-two-vgs.yml" }
let(:disk_name) { "/dev/sda" }
# Regression test
it "uses the device belonging to the current devicegraph" do
# Only to finish
allow(subject).to receive(:run?).and_return(false)
initial_graph = current_graph
expect(Y2Partitioner::Actions::Controllers::Partition).to receive(:new) do |disk_name|
# Modifies used device
disk = Y2Storage::BlkDevice.find_by_name(current_graph, disk_name)
disk.remove_descendants
# Initial device is not modified
initial_disk = Y2Storage::BlkDevice.find_by_name(initial_graph, disk_name)
expect(initial_disk.descendants).to_not be_empty
end
subject.run
end
context "if it is not possible to create a new partition on the disk" do
let(:scenario) { "lvm-two-vgs.yml" }
let(:disk_name) { "/dev/sda" }
it "shows an error popup" do
expect(Yast::Popup).to receive(:Error)
action.run
end
it "quits returning :back" do
expect(action.run).to eq(:back)
end
it "does not create any partition" do
partitions_before = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
action.run
partitions_after = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
expect(partitions_after).to eq(partitions_before)
end
end
context "if called for a StrayBlkDevice (Xen virtual partition)" do
let(:scenario) { "xen-partitions.xml" }
let(:disk) { Y2Storage::BlkDevice.find_by_name(current_graph, "/dev/xvda1") }
it "shows an error popup" do
expect(Yast::Popup).to receive(:Error)
action.run
end
it "quits returning :back" do
expect(action.run).to eq(:back)
end
it "does not create any partition" do
partitions_before = current_graph.partitions
action.run
partitions_after = current_graph.partitions
expect(partitions_after).to eq(partitions_before)
end
end
context "if the disk is in use" do
let(:scenario) { "empty_hard_disk_50GiB.yml" }
let(:disk_name) { "/dev/sda" }
before do
md = Y2Storage::Md.create(current_graph, "/dev/md0")
md.add_device(disk)
end
it "shows an error popup" do
expect(Yast::Popup).to receive(:Error)
action.run
end
it "quits returning :back" do
expect(action.run).to eq(:back)
end
it "does not create any partition" do
partitions_before = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
action.run
partitions_after = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
expect(partitions_after).to eq(partitions_before)
end
end
context "if the disk is formatted" do
let(:scenario) { "empty_hard_disk_50GiB.yml" }
let(:disk_name) { "/dev/sda" }
before do
disk.create_filesystem(Y2Storage::Filesystems::Type::EXT4)
end
it "shows an confirm popup" do
expect(Yast::Popup).to receive(:YesNo)
action.run
end
context "when the confirm popup is not accepted" do
before do
allow(Yast::Popup).to receive(:YesNo).and_return(false)
end
it "quits returning :back" do
expect(action.run).to eq(:back)
end
it "does not create any partition" do
partitions_before = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
action.run
partitions_after = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
expect(partitions_after).to eq(partitions_before)
end
end
context "when the confirm popup is accepted" do
before do
allow(Yast::Popup).to receive(:YesNo).and_return(true)
allow(Y2Partitioner::Actions::Controllers::Partition).to receive(:new).with(disk_name)
.and_return(controller)
# Only to finish
allow(Y2Partitioner::Dialogs::PartitionType).to receive(:run).and_return(:abort)
allow(controller).to receive(:available_partition_types).and_return(available_types)
end
let(:controller) { Y2Partitioner::Actions::Controllers::Partition.new(disk_name) }
let(:available_types) { Y2Storage::PartitionType.all }
it "removes the filesystem" do
expect(controller).to receive(:delete_filesystem)
action.run
end
end
end
context "if the user goes forward through all the dialogs" do
let(:scenario) { "empty_hard_disk_50GiB.yml" }
let(:disk_name) { "/dev/sda" }
before do
allow(Y2Partitioner::Actions::Controllers::Partition).to receive(:new).with(disk_name)
.and_return(controller)
allow(controller).to receive(:region).and_return(region)
allow(controller).to receive(:type).and_return(type)
allow(Y2Partitioner::Dialogs::PartitionType).to receive(:run).and_return(:next)
allow(Y2Partitioner::Dialogs::PartitionSize).to receive(:run).and_return(:next)
allow(Y2Partitioner::Dialogs::PartitionRole).to receive(:run).and_return(:next)
allow(Y2Partitioner::Dialogs::FormatAndMount).to receive(:run).and_return(:next)
end
let(:region) { disk.ensure_partition_table.unused_partition_slots.first.region }
let(:type) { Y2Storage::PartitionType::PRIMARY }
let(:controller) { Y2Partitioner::Actions::Controllers::Partition.new(disk_name) }
it "returns :finish" do
expect(action.run).to eq(:finish)
end
it "creates a partition" do
partitions = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
expect(partitions).to be_empty
action.run
partitions = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
expect(partitions).to_not be_empty
end
end
context "if the user aborts the process at some point" do
let(:scenario) { "empty_hard_disk_50GiB.yml" }
let(:disk_name) { "/dev/sda" }
before do
allow(Y2Partitioner::Dialogs::PartitionType).to receive(:run).and_return(:next)
allow(Y2Partitioner::Dialogs::PartitionSize).to receive(:run).and_return(:abort)
end
it "returns :abort" do
expect(action.run).to eq :abort
end
it "does not create any partition" do
partitions_before = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
action.run
partitions_after = Y2Storage::Disk.find_by_name(current_graph, disk_name).partitions
expect(partitions_after).to eq(partitions_before)
end
end
context "if some dialog is skipped and then the user goes back" do
let(:scenario) { "empty_hard_disk_50GiB.yml" }
let(:disk_name) { "/dev/sda" }
let(:available_types) { [Y2Storage::PartitionType.new("primary")] }
let(:controller) { Y2Partitioner::Actions::Controllers::Partition.new(disk_name) }
before do
allow(controller).to receive(:available_partition_types).and_return(available_types)
allow(subject).to receive(:type).and_return(:next)
allow(Y2Partitioner::Dialogs::PartitionSize).to receive(:run).and_return(:back)
allow(Y2Partitioner::Actions::Controllers::Partition).to receive(:new).with(disk_name)
.and_return(controller)
allow(subject).to receive(:skip_steps).and_call_original
end
it "does not run again skipped dialogs" do
expect(subject).to receive(:type).once.and_return(:next)
action.run
end
it "returns :back" do
expect(action.run).to eq :back
end
end
end
describe "#type" do
let(:scenario) { "empty_hard_disk_50GiB.yml" }
let(:disk_name) { "/dev/sda" }
let(:controller) { Y2Partitioner::Actions::Controllers::Partition.new(disk_name) }
let(:available_types) { Y2Storage::PartitionType.all }
before do
allow(controller).to receive(:available_partition_types).and_return(available_types)
subject.instance_variable_set(:@part_controller, controller)
end
context "when there is no partition types available" do
let(:available_types) { [] }
it "raises an error" do
expect { subject.type }.to raise_error("No partition type possible")
end
end
context "when there is only one partition type available" do
let(:available_types) { [Y2Storage::PartitionType.new("extended")] }
it "uses it as the type for the new partition" do
expect(controller).to receive(:type=).with(available_types.first)
subject.type
end
end
context "when there is more than one partition type available" do
it "runs the dialogs for choising the partition type" do
expect(Y2Partitioner::Dialogs::PartitionType).to receive(:run)
subject.type
end
end
end
end