2020-08-27 14:10:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe CiPlatformMetric do
|
|
|
|
subject { build(:ci_platform_metric) }
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
it_behaves_like 'a BulkInsertSafe model', CiPlatformMetric do
|
|
|
|
let(:valid_items_for_bulk_insertion) { build_list(:ci_platform_metric, 10) }
|
|
|
|
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any non-constraint validations defined
|
|
|
|
end
|
|
|
|
|
2020-08-27 14:10:29 -04:00
|
|
|
describe 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of(:recorded_at) }
|
|
|
|
it { is_expected.to validate_presence_of(:count) }
|
2020-09-10 17:08:28 -04:00
|
|
|
it { is_expected.to validate_numericality_of(:count).only_integer.is_greater_than(0) }
|
|
|
|
it { is_expected.to allow_values('').for(:platform_target) }
|
|
|
|
it { is_expected.not_to allow_values(nil).for(:platform_target) }
|
2020-08-27 14:10:29 -04:00
|
|
|
it { is_expected.to validate_length_of(:platform_target).is_at_most(255) }
|
|
|
|
end
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
describe '.insert_auto_devops_platform_targets!' do
|
2020-08-27 14:10:29 -04:00
|
|
|
def platform_target_counts_by_day
|
|
|
|
report = Hash.new { |hash, key| hash[key] = {} }
|
2020-09-10 17:08:28 -04:00
|
|
|
described_class.all.each do |metric|
|
2020-08-27 14:10:29 -04:00
|
|
|
date = metric.recorded_at.to_date
|
|
|
|
report[date][metric.platform_target] = metric.count
|
|
|
|
end
|
|
|
|
report
|
|
|
|
end
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
context 'when there is already existing metrics data' do
|
2020-08-27 14:10:29 -04:00
|
|
|
let!(:metric_1) { create(:ci_platform_metric) }
|
|
|
|
let!(:metric_2) { create(:ci_platform_metric) }
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
it 'does not erase any existing data' do
|
|
|
|
described_class.insert_auto_devops_platform_targets!
|
2020-08-27 14:10:29 -04:00
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
expect(described_class.all.to_a).to contain_exactly(metric_1, metric_2)
|
2020-08-27 14:10:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
context 'when there are multiple platform target variables' do
|
2020-08-27 14:10:29 -04:00
|
|
|
let(:today) { Time.zone.local(1982, 4, 24) }
|
|
|
|
let(:tomorrow) { today + 1.day }
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
it 'inserts platform target counts for that day' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(today) do
|
2020-09-15 02:09:32 -04:00
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'ECS')
|
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'ECS')
|
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
|
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
|
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
|
2020-09-10 17:08:28 -04:00
|
|
|
described_class.insert_auto_devops_platform_targets!
|
2020-08-27 14:10:29 -04:00
|
|
|
end
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(tomorrow) do
|
2020-09-15 02:09:32 -04:00
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FARGATE')
|
2020-09-10 17:08:28 -04:00
|
|
|
described_class.insert_auto_devops_platform_targets!
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(platform_target_counts_by_day).to eq({
|
2020-09-15 02:09:32 -04:00
|
|
|
today.to_date => { 'ECS' => 2, 'FARGATE' => 3 },
|
|
|
|
tomorrow.to_date => { 'ECS' => 2, 'FARGATE' => 4 }
|
2020-09-10 17:08:28 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-15 02:09:32 -04:00
|
|
|
context 'when there are invalid ci variable values for platform_target' do
|
2020-09-10 17:08:28 -04:00
|
|
|
let(:today) { Time.zone.local(1982, 4, 24) }
|
|
|
|
|
2020-09-15 02:09:32 -04:00
|
|
|
it 'ignores those values' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(today) do
|
2020-09-15 02:09:32 -04:00
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'ECS')
|
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'FOO')
|
|
|
|
create(:ci_variable, key: described_class::CI_VARIABLE_KEY, value: 'BAR')
|
2020-09-10 17:08:28 -04:00
|
|
|
described_class.insert_auto_devops_platform_targets!
|
2020-08-27 14:10:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(platform_target_counts_by_day).to eq({
|
2020-09-15 02:09:32 -04:00
|
|
|
today.to_date => { 'ECS' => 1 }
|
2020-08-27 14:10:29 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-10 17:08:28 -04:00
|
|
|
context 'when there are no platform target variables' do
|
|
|
|
it 'does not generate any new platform metrics' do
|
2020-09-15 02:09:32 -04:00
|
|
|
create(:ci_variable, key: 'KEY_WHATEVER', value: 'ECS')
|
2020-09-10 17:08:28 -04:00
|
|
|
described_class.insert_auto_devops_platform_targets!
|
2020-08-27 14:10:29 -04:00
|
|
|
|
|
|
|
expect(platform_target_counts_by_day).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|