Skip to content

Commit 6da3128

Browse files
shiramaxmasayag
authored andcommitted
Fixes #27684 - Add validation for PVC (#117)
1 parent 824431d commit 6da3128

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

app/models/foreman_kubevirt/kubevirt.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ def storage_classes_for_select
106106
storage_classes.map { |sc| OpenStruct.new(id: sc.name, description: "#{sc.name} (#{sc.provisioner})") }
107107
end
108108

109-
def new_volume(attr = {})
110-
return unless new_volume_errors.empty?
111109

112-
vol = Fog::Kubevirt::Compute::Volume.new(attr)
113-
vol.boot_order = 1 if attr[:bootable] == "on" || attr[:bootable] == "true"
110+
def new_volume(attrs = {})
111+
return unless new_volume_errors.empty?
112+
capacity = attrs.delete(:capacity)
113+
args = {capacity: capacity}.merge(attrs)
114+
vol = Fog::Kubevirt::Compute::Volume.new(args)
115+
vol.boot_order = 1 if args[:bootable] == "on" || args[:bootable] == "true"
114116
vol
115117
end
116118

@@ -376,7 +378,11 @@ def add_volume_for_image_provision(options)
376378
end
377379

378380
def validate_volume_capacity(volumes_attributes)
379-
volumes_attributes.each { |_, v| raise ::Foreman::Exception.new N_('Capacity was not found') if v[:capacity].empty? }
381+
volumes_attributes.each do |_, vol|
382+
if vol[:capacity].to_s.empty? || /\A\d+G?\Z/.match(vol[:capacity].to_s).nil?
383+
raise Foreman::Exception.new(N_("Volume size #{vol[:capacity]} is not valid"))
384+
end
385+
end
380386
end
381387

382388
def validate_only_single_bootable_volume(volumes_attributes)

test/unit/foreman_kubevirt_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,22 @@ def new_kubevirt_vcr
126126
exception = assert_raise(Foreman::Exception) do
127127
compute_resource.create_vm(vm_args)
128128
end
129-
assert_match(/Capacity was not found/, exception.message)
129+
assert_match(/Volume size is not valid/, exception.message)
130130
end
131131

132+
133+
test "should fail when creating a VM with not valid capacity" do
134+
vm_args = NETWORK_BASED_VM_ARGS.deep_dup
135+
vm_args["volumes_attributes"]["0"]["capacity"] = "TG"
136+
Fog.mock!
137+
compute_resource = new_kubevirt_vcr
138+
exception = assert_raise(Foreman::Exception) do
139+
compute_resource.create_vm(vm_args)
140+
end
141+
assert_match(/Volume size TG is not valid/, exception.message)
142+
end
143+
144+
132145
test "should fail when creating a VM with two bootable PVCs" do
133146
vm_args = NETWORK_BASED_VM_ARGS.deep_dup
134147
vm_args["volumes_attributes"]["0"]["bootable"] = "true"

0 commit comments

Comments
 (0)