Skip to content

Commit

Permalink
Merge pull request #22637 from Fryguy/import_specs
Browse files Browse the repository at this point in the history
Add import specs

(cherry picked from commit c2617cc)
  • Loading branch information
kbrock authored and Fryguy committed Jul 26, 2023
1 parent 19d456f commit 35b390c
Show file tree
Hide file tree
Showing 4 changed files with 414 additions and 4 deletions.
94 changes: 94 additions & 0 deletions spec/models/miq_policy/import_export_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,98 @@
RSpec.describe MiqPolicy::ImportExport do
describe ".import" do
it "imports" do
fd = StringIO.new(<<~YAML)
- MiqPolicy:
name: analyse incoming container images
description: Analyse incoming container images
expression:
towhat: ContainerImage
guid: e7a270bc-109b-11e6-86ba-02424d459b45
created_by: admin
updated_by: admin
notes:
active: true
mode: control
read_only: true
MiqPolicyContent:
- qualifier: success
success_sequence: 1
failure_sequence: 1
failure_synchronous: true
MiqEventDefinition:
name: containerimage_created
description: Container Image Discovered
guid: ab5c4cf5-4e71-43f8-964c-6446d9db10ec
event_type: Default
definition:
default:
enabled:
MiqAction:
name: container_image_analyze
description: Initiate SmartState Analysis for Container Image
guid: 0ac47229-b8dd-49f7-9051-45a4ada5b709
action_type: default
options: {}
Condition:
- name: Do not scan image-inspector's image
description: Don't scan image-inspector's image
expression: !ruby/object:MiqExpression
exp:
not:
ENDS WITH:
field: ContainerImage-name
value: "/image-inspector"
context_type:
towhat: ContainerImage
file_mtime:
guid: e744245a-3d08-11e6-8d39-02422087d789
filename:
applies_to_exp:
miq_policy_id:
notes:
read_only: true
YAML

MiqPolicy.import(fd, :save => true)

expect(MiqPolicy.count).to eq(1)

policy = MiqPolicy.first
expect(policy).to have_attributes(
:name => "analyse incoming container images",
:description => "Analyse incoming container images",
:expression => nil,
:towhat => "ContainerImage",
:guid => "e7a270bc-109b-11e6-86ba-02424d459b45",
:created_by => "admin",
:updated_by => "admin",
:notes => nil,
:active => true,
:mode => "control",
:read_only => true
)

expect(policy.conditions.size).to eq(1)
condition = policy.conditions.first
expect(condition).to have_attributes(
:name => "Do not scan image-inspector's image",
:description => "Don't scan image-inspector's image",
:towhat => "ContainerImage",
:file_mtime => nil,
:guid => "e744245a-3d08-11e6-8d39-02422087d789",
:filename => nil,
:applies_to_exp => nil,
:miq_policy_id => nil,
:notes => nil,
:read_only => true
)

expression = condition.expression
expect(expression).to be_a(MiqExpression)
expect(expression.exp).to eq({"not" => {"ENDS WITH" => {"field" => "ContainerImage-name", "value" => "/image-inspector"}}})
end
end

context '.import_from_hash' do
it "loads attributes" do
p_hash = {
Expand Down
146 changes: 146 additions & 0 deletions spec/models/miq_report/import_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,152 @@
:miq_group_id => @some_group.id)
end

describe ".import" do
it "imports" do
fd = StringIO.new(<<~YAML)
---
- MiqReport:
title: VMs with Volume Free Space > 50% by Department
rpt_group: Configuration Management - Virtual Machines
rpt_type: Custom
priority: 5
db: Vm
cols:
- name
- v_owning_cluster
include:
hardware:
include:
volumes:
columns:
- name
- free_space_percent
- free_space
- size
- used_space_percent
- used_space
- filesystem
managed:
columns:
- department
storage:
columns:
- name
col_order:
- managed.department
- name
- v_owning_cluster
- storage.name
- hardware.volumes.name
- hardware.volumes.free_space_percent
- hardware.volumes.free_space
- hardware.volumes.size
- hardware.volumes.used_space_percent
- hardware.volumes.used_space
- hardware.volumes.filesystem
headers:
- Department Classification
- Name
- Parent Cluster
- Storage Name
- Volume Name
- Volume Free Space Percent
- Volume Free Space
- Volume Size
- Volume Used Space Percent
- Volume Used Space
- Volume Filesystem
conditions: !ruby/object:MiqExpression
exp:
FIND:
search:
IS NOT EMPTY:
field: Vm.hardware.volumes-name
checkany:
">":
field: Vm.hardware.volumes-free_space_percent
value: 50
order: Descending
sortby:
- managed.department
- hardware.volumes.free_space_percent
group: c
graph:
:type: Pie
:count: '8'
:other: false
dims: 1
filename: 100_Configuration Management - Virtual Machines/005_VMs with Free Space
_ 50% by Department.yaml
file_mtime: !ruby/object:ActiveSupport::TimeWithZone
utc: 2023-06-06 20:07:17.000000000 Z
zone: !ruby/object:ActiveSupport::TimeZone
name: Etc/UTC
time: 2023-06-06 20:07:17.000000000 Z
categories:
timeline:
template_type: report
where_clause:
db_options: {}
generate_cols:
generate_rows:
col_formats:
tz:
time_profile_id:
display_filter:
col_options:
rpt_options:
miq_group_id:
user_id:
menu_name: VMs with Free Space > 50% by Department
userid: ''
group_description: ''
YAML

MiqReport.import(fd, :save => true, :user => @some_user)

expect(MiqReport.count).to eq(2)

report = MiqReport.find_by(:title => "VMs with Volume Free Space > 50% by Department")
expect(report).to have_attributes(
:title => "VMs with Volume Free Space > 50% by Department",
:rpt_group => "Configuration Management - Virtual Machines",
:rpt_type => "Custom",
:priority => 5,
:db => "Vm",
:cols => %w[name v_owning_cluster],
:order => "Descending",
:sortby => %w[managed.department hardware.volumes.free_space_percent],
:group => "c",
:dims => 1,
:categories => nil,
:timeline => nil,
:template_type => "report",
:where_clause => nil,
:db_options => {},
:generate_cols => nil,
:generate_rows => nil,
:col_formats => nil,
:tz => nil,
:time_profile_id => nil,
:display_filter => nil,
:col_options => nil,
:rpt_options => nil,
:miq_group_id => nil,
:user_id => @some_user.id,
:menu_name => "VMs with Free Space > 50% by Department"
)

conditions = report.conditions
expect(conditions).to be_a(MiqExpression)
expect(conditions.exp).to eq({"FIND" => {"checkany" => {">" => {"field" => "Vm.hardware.volumes-free_space_percent", "value" => 50}}, "search" => {"IS NOT EMPTY" => {"field" => "Vm.hardware.volumes-name"}}}})

file_mtime = report.file_mtime
expect(file_mtime).to be_a(ActiveSupport::TimeWithZone)
expect(file_mtime).to eq(Time.zone.parse('2023-06-06 20:07:17.000000000 Z'))
end
end

context ".import_from_hash" do
before do
@user_admin = FactoryBot.create(:user_admin)
Expand Down
Loading

0 comments on commit 35b390c

Please sign in to comment.