From 57e875cfe1baf6151492040a7141cb9008a6d5d5 Mon Sep 17 00:00:00 2001 From: Mooli Tayer Date: Wed, 15 Nov 2017 18:28:11 +0200 Subject: [PATCH] Seed Prometheus Alert Profiles --- app/models/miq_alert_set.rb | 10 +++++-- db/fixtures/miq_alert_sets.yml | 45 +++++++++++++++++++++++++++++++ spec/models/miq_alert_set_spec.rb | 45 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 db/fixtures/miq_alert_sets.yml create mode 100644 spec/models/miq_alert_set_spec.rb diff --git a/app/models/miq_alert_set.rb b/app/models/miq_alert_set.rb index 3ef55d974a5..39aaec3dc3d 100644 --- a/app/models/miq_alert_set.rb +++ b/app/models/miq_alert_set.rb @@ -35,11 +35,17 @@ def self.import_from_hash(alert_profile, options = {}) ContentImporter.import_from_hash(MiqAlertSet, MiqAlert, alert_profile, options) end - def self.import_from_yaml(fd) + def self.import_from_yaml(fd, options = {}) input = YAML.load(fd) input.collect do |e| - _a, stat = import_from_hash(e["MiqAlertSet"]) + _a, stat = import_from_hash(e["MiqAlertSet"], options) stat end end + + def self.seed + fixture_file = File.join(FIXTURE_DIR, "miq_alert_sets.yml") + return unless File.exist?(fixture_file) + File.open(fixture_file) { |fd| MiqAlertSet.import_from_yaml(fd, :save => true) } + end end diff --git a/db/fixtures/miq_alert_sets.yml b/db/fixtures/miq_alert_sets.yml new file mode 100644 index 00000000000..0ca75c618a7 --- /dev/null +++ b/db/fixtures/miq_alert_sets.yml @@ -0,0 +1,45 @@ +--- +- MiqAlertSet: + name: prometheus provider profile + description: Prometheus Provider Profile + set_type: MiqAlertSet + guid: a16fcf51-e2ae-492d-af37-19de881476ad + mode: ExtManagementSystem + MiqAlert: + - description: External Prometheus - Providers + options: + :notifications: + :delay_next_evaluation: 0 + :evm_event: {} + guid: ea3acd49-9516-4fde-b828-bf68d254c0cf + db: ExtManagementSystem + responds_to_events: datawarehouse_alert + miq_expression: + enabled: true + read_only: + hash_expression: + :eval_method: dwh_generic + :mode: internal + :options: {} +- MiqAlertSet: + name: prometheus node profile + description: Prometheus node Profile + set_type: MiqAlertSet + guid: ff0fb114-be03-4685-bebb-b6ae8f13d7ad + mode: ContainerNode + MiqAlert: + - description: External Prometheus - Nodes + options: + :notifications: + :delay_next_evaluation: 0 + :evm_event: {} + guid: efe9d4f0-9c6f-4c67-80b1-05cd83223349 + db: ContainerNode + responds_to_events: datawarehouse_alert + miq_expression: + enabled: true + read_only: + hash_expression: + :eval_method: dwh_generic + :mode: internal + :options: {} diff --git a/spec/models/miq_alert_set_spec.rb b/spec/models/miq_alert_set_spec.rb new file mode 100644 index 00000000000..a44e10246c1 --- /dev/null +++ b/spec/models/miq_alert_set_spec.rb @@ -0,0 +1,45 @@ +describe MiqAlertSet do + context ".seed" do + it "should create the prometheus profiles" do + expect(MiqAlertSet.count).to eq(0) + described_class.seed + expect(MiqAlertSet.count).to eq(2) + provider_profile = MiqAlertSet.find_by(:guid => "a16fcf51-e2ae-492d-af37-19de881476ad") + expect(provider_profile).to have_attributes(:mode => "ExtManagementSystem") + expect(provider_profile.miq_alerts.count).to eq(1) + expect(provider_profile.miq_alerts.first).to have_attributes( + :guid => "ea3acd49-9516-4fde-b828-bf68d254c0cf", + :db => "ExtManagementSystem", + :responds_to_events => "datawarehouse_alert", + ) + node_profile = MiqAlertSet.find_by(:guid => "ff0fb114-be03-4685-bebb-b6ae8f13d7ad") + expect(node_profile).to have_attributes(:mode => "ContainerNode") + expect(node_profile.miq_alerts.count).to eq(1) + expect(node_profile.miq_alerts.first).to have_attributes( + :guid => "efe9d4f0-9c6f-4c67-80b1-05cd83223349", + :db => "ContainerNode", + :responds_to_events => "datawarehouse_alert", + ) + end + + it "should not explode if the seed file is missing" do + fixture_file = ApplicationRecord::FIXTURE_DIR.join("miq_alert_sets.yml") + expect { without_file(fixture_file) { described_class.seed } }.to_not raise_exception + end + end + + def without_file(fname) + # Note: can the file you are moving cause sporadic failures in other threads? + raise _("no block given") unless block_given? + raise _("fname is blank") if fname.blank? + tempf = Tempfile.new("temporary_backup") + begin + FileUtils.mv(fname, tempf.path) + yield + ensure + FileUtils.mv(tempf.path, fname) if File.exist?(tempf.path) + tempf.close + tempf.unlink + end + end +end