Project

General

Profile

« Previous | Next » 

Revision 56de025f

Added by Tomáš Strachota almost 6 years ago

Fixes #21580 - normalize compute attributes (#4980)

Normalizes compute attributes in API show actions. For backwards
compatibility reasons this patch adds a new rabl node instead of
replacing 'vm_attrs'.

View differences:

test/models/compute_resources/openstack_test.rb
end
end
describe '#normalize_vm_attrs' do
let(:cr) do
mock_cr(FactoryBot.build(:openstack_cr),
:security_groups => [
stub(:id => 'grp1', :name => 'group 1'),
stub(:id => 'grp2', :name => 'group 2')
],
:tenants => [
stub(:id => 'tn1', :name => 'tenant 1'),
stub(:id => 'tn2', :name => 'tenant 2')
],
:flavors => [
stub(:id => 'flvr1', :name => 'flavour 1'),
stub(:id => 'flvr2', :name => 'flavour 2')
],
:internal_networks => [
stub(:id => 'nic1', :name => 'default'),
stub(:id => 'nic2', :name => 'bridge')
]
)
end
test 'maps flavor_ref to flavor_id' do
assert_attrs_mapped(cr, 'flavor_ref', 'flavor_id')
end
test 'finds flavor_name' do
vm_attrs = {
'flavor_ref' => 'flvr1'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal('flavour 1', normalized['flavor_name'])
end
test 'sets blank availability_zone to nil' do
assert_blank_attr_nilified(cr, 'availability_zone')
end
test 'sets blank tenant_id to nil' do
assert_blank_attr_nilified(cr, 'tenant_id')
end
test 'finds tenant_name' do
vm_attrs = {
'tenant_id' => 'tn1'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal('tenant 1', normalized['tenant_name'])
end
test 'maps security_groups to security_group_name' do
assert_attrs_mapped(cr, 'security_groups', 'security_group_name')
end
test 'sets blank security_group_name to nil' do
assert_blank_mapped_attr_nilified(cr, 'security_groups', 'security_group_name')
end
test 'finds security_group_id' do
vm_attrs = {
'security_groups' => 'group 2'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal('grp2', normalized['security_group_id'])
end
test 'maps network to floating_ip_network' do
assert_attrs_mapped(cr, 'network', 'floating_ip_network')
end
test 'nilifies floating_ip_network when network is blank' do
assert_blank_mapped_attr_nilified(cr, 'network', 'floating_ip_network')
end
test 'casts boot_from_volume to boolean' do
vm_attrs = {
'boot_from_volume' => 'true'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal(true, normalized['boot_from_volume'])
end
test 'translates boot_volume_size to bytes' do
vm_attrs = {
'size_gb' => '2'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal(2.gigabyte.to_s, normalized['boot_volume_size'])
end
test 'maps zero (default) boot_volume_size nil' do
vm_attrs = {
'size_gb' => '0'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_nil(normalized['boot_volume_size'])
end
test 'normalizes nics_attributes' do
vm_attrs = {
'nics' => ['', 'nic1', 'nic2']
}
expected_attrs = {
'0' => {
'id' => 'nic1',
'name' => 'default'
},
'1' => {
'id' => 'nic2',
'name' => 'bridge'
}
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal(expected_attrs, normalized['interfaces_attributes'])
end
test 'nilifies blank scheduler_hint_filter' do
assert_blank_attr_nilified(cr, 'scheduler_hint_filter')
end
test 'image_ref is mapped to image_id' do
assert_attrs_mapped(cr, 'image_ref', 'image_id')
end
describe 'images' do
let(:cr) { FactoryBot.create(:gce_cr, :with_images) }
test 'adds image name' do
vm_attrs = {
'image_id' => cr.images.last.uuid
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert_equal(cr.images.last.name, normalized['image_name'])
end
test 'leaves image name empty when image_id is nil' do
vm_attrs = {
'image_id' => nil
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert(normalized.has_key?('image_name'))
assert_nil(normalized['image_name'])
end
test "leaves image name empty when image wasn't found" do
vm_attrs = {
'image_id' => 'unknown'
}
normalized = cr.normalize_vm_attrs(vm_attrs)
assert(normalized.has_key?('image_name'))
assert_nil(normalized['image_name'])
end
end
test 'correctly fills empty attributes' do
normalized = cr.normalize_vm_attrs({})
expected_attrs = {
'availability_zone' => nil,
'tenant_id' => nil,
'tenant_name' => nil,
'boot_from_volume' => nil,
'scheduler_hint_filter' => nil,
'flavor_id' => nil,
'flavor_name' => nil,
'security_group_name' => nil,
'security_group_id' => nil,
'floating_ip_network' => nil,
'boot_volume_size' => nil,
'interfaces_attributes' => {},
'image_id' => nil,
'image_name' => nil
}
assert_equal(expected_attrs.keys.sort, normalized.keys.sort)
assert_equal(expected_attrs, normalized)
end
test 'attribute names' do
check_vm_attribute_names(cr)
end
end
private
def mocked_key_pair

Also available in: Unified diff