From 4fcc40343703ab7c561a2a0bff63c1608f2c089b Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Wed, 26 Jul 2023 17:08:52 -0400 Subject: [PATCH 1/4] Add spec for MiqPolicy.import --- spec/models/miq_policy/import_export_spec.rb | 94 ++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/spec/models/miq_policy/import_export_spec.rb b/spec/models/miq_policy/import_export_spec.rb index 492e25d401d..33b368a68f4 100644 --- a/spec/models/miq_policy/import_export_spec.rb +++ b/spec/models/miq_policy/import_export_spec.rb @@ -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 = { From 9c04c6e6855a95d39d23e861848057930383b0ce Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Wed, 26 Jul 2023 17:09:09 -0400 Subject: [PATCH 2/4] Remove ivar in favor of local var --- spec/models/mixins/yaml_import_export_mixin_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/models/mixins/yaml_import_export_mixin_spec.rb b/spec/models/mixins/yaml_import_export_mixin_spec.rb index 4e3d91505ef..a3a454ffecd 100644 --- a/spec/models/mixins/yaml_import_export_mixin_spec.rb +++ b/spec/models/mixins/yaml_import_export_mixin_spec.rb @@ -45,15 +45,15 @@ subject { MiqReport } it "valid YAML file" do - @fd = StringIO.new("---\n- MiqReport:\n") + fd = StringIO.new("---\n- MiqReport:\n") # if it gets to import_from_array, then it did not choke on yml expect(subject).to receive(:import_from_array) - subject.import(@fd) + subject.import(fd) end it "invalid YAML file" do - @fd = StringIO.new("---\na:\nb") - expect { subject.import(@fd) }.to raise_error("Invalid YAML file") + fd = StringIO.new("---\na:\nb") + expect { subject.import(fd) }.to raise_error("Invalid YAML file") end end From 6fda87043f9a26a57d0e90bcbc91504d4c3adabf Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Wed, 26 Jul 2023 17:46:03 -0400 Subject: [PATCH 3/4] Add spec for MiqWidget.import --- spec/models/miq_widget/import_export_spec.rb | 170 +++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/spec/models/miq_widget/import_export_spec.rb b/spec/models/miq_widget/import_export_spec.rb index 509c21d5ea0..54e890414f5 100644 --- a/spec/models/miq_widget/import_export_spec.rb +++ b/spec/models/miq_widget/import_export_spec.rb @@ -47,6 +47,176 @@ end end + describe ".import" do + before { EvmSpecHelper.local_guid_miq_server_zone } + + it "imports" do + fd = StringIO.new(<<~YAML) + - MiqWidget: + guid: 7e1fc241-5219-4ee2-8a8f-c4c2fdd587de + description: chart_guest_os_information_any_os + title: Guest OS Information + content_type: chart + options: + :timezone_matters: false + visibility: + :roles: + - _ALL_ + user_id: + resource_id: 21 + resource_type: MiqReport + enabled: true + read_only: true + MiqReportContent: + - MiqReport: + title: Guest OS Information - any OS + rpt_group: Configuration Management - Virtual Machines + rpt_type: Default + priority: 59 + db: Vm + cols: + - name + - vendor_display + - os_image_name + include: + operating_system: + columns: + - product_name + - service_pack + - version + - build_number + - product_key + - productid + col_order: + - operating_system.product_name + - operating_system.service_pack + - name + - vendor_display + - os_image_name + - operating_system.version + - operating_system.build_number + - operating_system.product_key + - operating_system.productid + headers: + - Guest OS + - OS Service Pack + - VM Name + - Vendor + - OS Name + - OS Version + - OS Build Number + - OS Product Key + - OS Productid + conditions: + order: Ascending + sortby: + - operating_system.product_name + - operating_system.service_pack + group: c + graph: + :type: Pie + :count: 10 + :other: true + dims: 1 + filename: 100_Configuration Management - Virtual Machines/059_Guest OS Information + (any OS).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: Guest OS Information - any OS + userid: '' + group_description: '' + MiqSchedule: + name: chart_guest_os_information_any_os + description: chart_guest_os_information_any_os + sched_action: + :method: generate_widget + filter: !ruby/object:MiqExpression + exp: + "=": + field: MiqWidget-id + value: 1 + context_type: + col_details: + ruby: + resource_type: MiqWidget + run_at: + :interval: + :value: '1' + :unit: daily + :tz: UTC + :start_time: !ruby/object:ActiveSupport::TimeWithZone + utc: &1 2023-07-11 00:00:00.000000000 Z + zone: !ruby/object:ActiveSupport::TimeZone + name: Etc/UTC + time: *1 + enabled: true + userid: system + prod_default: system + adhoc: + file_depot_id: + resource_id: + YAML + + MiqWidget.import(fd, :save => true) + + expect(MiqWidget.count).to eq(1) + + widget = MiqWidget.first + expect(widget).to have_attributes( + :guid => "7e1fc241-5219-4ee2-8a8f-c4c2fdd587de", + :description => "chart_guest_os_information_any_os", + :title => "Guest OS Information", + :content_type => "chart", + :options => {:timezone_matters => false}, + :visibility => {:roles => ["_ALL_"]}, + :user_id => nil, + :resource_type => "MiqReport", + :enabled => true, + :read_only => true + ) + + schedule = widget.miq_schedule + expect(schedule).to have_attributes( + :name => "chart_guest_os_information_any_os", + :description => "chart_guest_os_information_any_os", + :sched_action => {:method => "generate_widget"}, + :resource_type => "MiqWidget", + :enabled => true, + :userid => "system", + :prod_default => "system", + :adhoc => nil, + :file_depot_id => nil, + :resource_id => nil + ) + + filter = schedule.filter + expect(filter).to be_a(MiqExpression) + expect(filter.exp).to eq({"=" => {"field" => "MiqWidget-id", "value" => widget.id}}) + + run_at = schedule.run_at + expect(run_at[:start_time]).to be_a(ActiveSupport::TimeWithZone) + expect(run_at[:start_time]).to eq(Time.zone.parse('2023-07-11 00:00:00.000000000 Z')) + end + end + describe "#import_from_hash" do context "when the widget given is nil" do let(:widget) { nil } From 8eecf987f2e834668b0a8937be952382624fc655 Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Wed, 26 Jul 2023 17:59:00 -0400 Subject: [PATCH 4/4] Add spec for MiqReport.import --- spec/models/miq_report/import_export_spec.rb | 146 +++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/spec/models/miq_report/import_export_spec.rb b/spec/models/miq_report/import_export_spec.rb index 6241ffc9e27..242468e16f7 100644 --- a/spec/models/miq_report/import_export_spec.rb +++ b/spec/models/miq_report/import_export_spec.rb @@ -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)