Skip to content

Commit

Permalink
Improve action, dialog and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 4, 2017
1 parent 9393d42 commit 9851fa4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 113 deletions.
13 changes: 5 additions & 8 deletions src/lib/y2partitioner/actions/resize_partition.rb
Expand Up @@ -67,7 +67,7 @@ def run
# @return [Symbol] :finish if the dialog returns :next; dialog result otherwise.
def resize
result = Dialogs::PartitionResize.run(partition, resize_info)
fix_end_alignment(resize_info)
fix_end_alignment

result == :next ? :finish : result
end
Expand All @@ -94,16 +94,13 @@ def validate

# After the partition's size was changed during resizing, make sure the
# new size meets all alignment requirements, but is still between
# min_size and max_size. This may change the partition's size (and
# region).
# min_size and max_size.
#
# @param resize_info [ResizeInfo]
def fix_end_alignment(resize_info)
return if partition.nil?
# @note This may change the partition's size (and region).
def fix_end_alignment
return if partition.nil? || partition.end_aligned?

ptable = partition.partition_table
return if partition.end_aligned?

region = ptable.align(partition.region, Y2Storage::AlignPolicy::ALIGN_END)
min_blocks = (resize_info.min_size.to_i / region.block_size.to_i)
max_blocks = (resize_info.max_size.to_i / region.block_size.to_i)
Expand Down
4 changes: 4 additions & 0 deletions src/lib/y2partitioner/dialogs/partition_resize.rb
Expand Up @@ -117,12 +117,16 @@ def size_info
# Widget for current size
def current_size_info
size = partition.size.to_human_string
# TRANSLATORS: label for current size of the partition, where %{size} is
# replaced by a size (e.g., 5.5 GiB)
Left(Label(format("Current size: %{size}", size: size)))
end

# Widget for used size
def used_size_info
size = used_size.to_human_string
# TRANSLATORS: label for currently used size of the partition, where %{size} is
# replaced by a size (e.g., 5.5 GiB)
Left(Label(format("Currently used: %{size}", size: size)))
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2storage/partition_tables/base.rb
Expand Up @@ -123,7 +123,7 @@ class Base < Device
# @param align_policy [AlignPolicy] policy to consider while aligning
# @param align_type [AlignType]
#
# @return [Region]
# @return [Region] always returns a new object
storage_forward :align, as: "Region"

# @!method alignment
Expand Down
201 changes: 98 additions & 103 deletions test/y2partitioner/actions/resize_partition_test.rb
Expand Up @@ -31,156 +31,151 @@
before do
allow(Yast::Wizard).to receive(:OpenNextBackDialog)
allow(Yast::Wizard).to receive(:CloseDialog)

devicegraph_stub(scenario)
allow(partition).to receive(:detect_resize_info).and_return(resize_info)
end

subject(:action) { described_class.new(partition) }

context "With a mixed partition setup" do
before do
devicegraph_stub(scenario)

allow(partition).to receive(:detect_resize_info).and_return(resize_info)
end

let(:scenario) { "mixed_disks.yml" }
let(:scenario) { "mixed_disks.yml" }

let(:current_graph) { Y2Partitioner::DeviceGraphs.instance.current }
let(:current_graph) { Y2Partitioner::DeviceGraphs.instance.current }

let(:partition) { Y2Storage::Partition.find_by_name(current_graph, "/dev/sda1") }
let(:partition) { Y2Storage::Partition.find_by_name(current_graph, "/dev/sda1") }

let(:resize_info) do
instance_double(Y2Storage::ResizeInfo,
resize_ok?: can_resize,
min_size: min_size,
max_size: max_size)
end
let(:resize_info) do
instance_double(Y2Storage::ResizeInfo,
resize_ok?: can_resize,
min_size: min_size,
max_size: max_size)
end

let(:can_resize) { nil }
let(:can_resize) { nil }

let(:min_size) { 100.KiB }
let(:min_size) { 100.KiB }

let(:max_size) { 1.GiB }
let(:max_size) { 1.GiB }

describe "#run" do
context "when the partition cannot be resized" do
let(:can_resize) { false }
describe "#run" do
context "when the partition cannot be resized" do
let(:can_resize) { false }

it "shows an error popup" do
expect(Yast::Popup).to receive(:Error)
action.run
end
it "shows an error popup" do
expect(Yast::Popup).to receive(:Error)
action.run
end

it "returns :back" do
expect(action.run).to eq(:back)
end
it "returns :back" do
expect(action.run).to eq(:back)
end
end

context "when the partition can be resized" do
let(:can_resize) { true }
context "when the partition can be resized" do
let(:can_resize) { true }

context "and the user goes forward in the dialog" do
before do
allow(Y2Partitioner::Dialogs::PartitionResize).to receive(:run).and_return(:next)
partition.size = new_size
end
context "and the user goes forward in the dialog" do
before do
allow(Y2Partitioner::Dialogs::PartitionResize).to receive(:run).and_return(:next)
partition.size = new_size
end

let(:new_size) { 1.GiB }
let(:new_size) { 1.GiB }

it "returns :finish" do
expect(action.run).to eq(:finish)
end
it "returns :finish" do
expect(action.run).to eq(:finish)
end

context "when the partition table does not require end-alignment" do
let(:scenario) { "mixed_disks" }
context "when the partition table does not require end-alignment" do
let(:scenario) { "mixed_disks" }

context "and the partition is end-aligned" do
let(:new_size) { 10.MiB }
context "and the partition is end-aligned" do
let(:new_size) { 10.MiB }

it "does not change the partition size" do
size_before = partition.size
it "does not change the partition size" do
size_before = partition.size

expect(partition.end_aligned?).to eq(true)
action.run
expect(partition.size).to eq(size_before)
end
expect(partition.end_aligned?).to eq(true)
action.run
expect(partition.size).to eq(size_before)
end
end

context "and the partition is not end-aligned" do
let(:new_size) { 10.5.MiB }
context "and the partition is not end-aligned" do
let(:new_size) { 10.5.MiB }

it "aligns the partition" do
expect(partition.end_aligned?).to eq(false)
action.run
expect(partition.end_aligned?).to eq(true)
end
it "aligns the partition" do
expect(partition.end_aligned?).to eq(false)
action.run
expect(partition.end_aligned?).to eq(true)
end
end
end

context "when the partition table requires end-alignment" do
let(:scenario) { "dasd_50GiB.yml" }
context "when the partition table requires end-alignment" do
let(:scenario) { "dasd_50GiB.yml" }

context "and the partition is end-aligned" do
let(:new_size) { 102432.KiB }
context "and the partition is end-aligned" do
let(:new_size) { 102432.KiB }

it "does not change the partition size" do
size_before = partition.size
it "does not change the partition size" do
size_before = partition.size

expect(partition.end_aligned?).to eq(true)
action.run
expect(partition.size).to eq(size_before)
end
expect(partition.end_aligned?).to eq(true)
action.run
expect(partition.size).to eq(size_before)
end
end

context "and the partition is not end-aligned" do
let(:new_size) { 12344.KiB }
context "and the partition is not end-aligned" do
let(:new_size) { 12344.KiB }

it "aligns the partition" do
expect(partition.end_aligned?).to eq(false)
action.run
expect(partition.end_aligned?).to eq(true)
end
it "aligns the partition" do
expect(partition.end_aligned?).to eq(false)
action.run
expect(partition.end_aligned?).to eq(true)
end
end
end
end

context "and the user aborts the process" do
before do
allow(Y2Partitioner::Dialogs::PartitionResize).to receive(:run).and_return(:abort)
end
context "and the user aborts the process" do
before do
allow(Y2Partitioner::Dialogs::PartitionResize).to receive(:run).and_return(:abort)
end

it "returns :abort" do
expect(action.run).to eq(:abort)
end
it "returns :abort" do
expect(action.run).to eq(:abort)
end
end
end
end

describe "#fix_region_end" do
let(:region) { Y2Storage::Region.create(50, 100, Y2Storage::DiskSize.new(100)) }
describe "#fix_region_end" do
let(:region) { Y2Storage::Region.create(50, 100, Y2Storage::DiskSize.new(100)) }

it "leaves a region untouched if in range" do
new_region = subject.send(:fix_region_end, region, 90, 110, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 100
end
it "leaves a region untouched if in range" do
new_region = subject.send(:fix_region_end, region, 90, 110, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 100
end

it "enlarges the region if below min" do
new_region = subject.send(:fix_region_end, region, 122, 150, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 130
end
it "enlarges the region if below min" do
new_region = subject.send(:fix_region_end, region, 122, 150, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 130
end

it "shrinks the region if above max" do
new_region = subject.send(:fix_region_end, region, 50, 69, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 60
end
it "shrinks the region if above max" do
new_region = subject.send(:fix_region_end, region, 50, 69, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 60
end

it "does not explode if contradictory restrictions" do
new_region = subject.send(:fix_region_end, region, 85, 85, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 80
end
it "does not explode if contradictory restrictions" do
new_region = subject.send(:fix_region_end, region, 85, 85, 10)
expect(new_region.start).to eq 50
expect(new_region.length).to eq 80
end
end
end
2 changes: 1 addition & 1 deletion test/y2partitioner/dialogs/partition_resize_test.rb
Expand Up @@ -63,7 +63,7 @@ def find_label(contents, text)
end
end

it "contents a widget for selecting the new size" do
it "contains a widget for selecting the new size" do
widget = subject.contents.nested_find do |w|
w.is_a?(Y2Partitioner::Dialogs::PartitionResize::SizeSelector)
end
Expand Down

0 comments on commit 9851fa4

Please sign in to comment.