2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Banzai::Filter::InlineGrafanaMetricsFilter do
|
2019-12-26 22:10:19 +05:30
|
|
|
include FilterSpecHelper
|
|
|
|
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:grafana_integration) { create(:grafana_integration, project: project) }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:input) { %(<a href="#{trigger_url}">example</a>) }
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:doc) { filter(input) }
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:embed_url) { doc.at_css('.js-render-metrics')['data-dashboard-url'] }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
let(:dashboard_path) do
|
|
|
|
'/d/XDaNK6amz/gitlab-omnibus-redis' \
|
2020-04-08 14:13:33 +05:30
|
|
|
'?from=1570397739557&panelId=14' \
|
|
|
|
'&to=1570484139557&var-instance=All'
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
let(:trigger_url) { grafana_integration.grafana_url + dashboard_path }
|
|
|
|
let(:dashboard_url) do
|
|
|
|
urls.project_grafana_api_metrics_dashboard_url(
|
2019-12-26 22:10:19 +05:30
|
|
|
project,
|
2020-04-08 14:13:33 +05:30
|
|
|
grafana_url: trigger_url,
|
2019-12-26 22:10:19 +05:30
|
|
|
embedded: true,
|
|
|
|
start: "2019-10-06T21:35:39Z",
|
|
|
|
end: "2019-10-07T21:35:39Z"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it_behaves_like 'a metrics embed filter'
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
around do |example|
|
|
|
|
Timecop.freeze(Time.utc(2019, 3, 17, 13, 10)) { example.run }
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when grafana is not configured' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:grafana_integration).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves the markdown unchanged' do
|
|
|
|
expect(unescape(doc.to_s)).to eq(input)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'when "panelId" parameter is missing' do
|
|
|
|
let(:dashboard_path) { '/d/XDaNK6amz/gitlab-omnibus-redis?from=1570397739557&to=1570484139557' }
|
|
|
|
|
|
|
|
it_behaves_like 'a metrics embed filter'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when time window parameters are missing' do
|
|
|
|
let(:dashboard_path) { '/d/XDaNK6amz/gitlab-omnibus-redis?panelId=16' }
|
|
|
|
|
|
|
|
it 'sets the window to the last 8 hrs' do
|
|
|
|
expect(embed_url).to include(
|
|
|
|
'from%3D1552799400000', 'to%3D1552828200000',
|
|
|
|
'start=2019-03-17T05%3A10%3A00Z', 'end=2019-03-17T13%3A10%3A00Z'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when "to" parameter is missing' do
|
|
|
|
let(:dashboard_path) { '/d/XDaNK6amz/gitlab-omnibus-redis?panelId=16&from=1570397739557' }
|
|
|
|
|
|
|
|
it 'sets "to" to 8 hrs after "from"' do
|
|
|
|
expect(embed_url).to include(
|
|
|
|
'from%3D1570397739557', 'to%3D1570426539000',
|
|
|
|
'start=2019-10-06T21%3A35%3A39Z', 'end=2019-10-07T05%3A35%3A39Z'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when "from" parameter is missing' do
|
|
|
|
let(:dashboard_path) { '/d/XDaNK6amz/gitlab-omnibus-redis?panelId=16&to=1570484139557' }
|
|
|
|
|
|
|
|
it 'sets "from" to 8 hrs before "to"' do
|
|
|
|
expect(embed_url).to include(
|
|
|
|
'from%3D1570455339000', 'to%3D1570484139557',
|
|
|
|
'start=2019-10-07T13%3A35%3A39Z', 'end=2019-10-07T21%3A35%3A39Z'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no parameters are provided' do
|
2019-12-26 22:10:19 +05:30
|
|
|
let(:dashboard_path) { '/d/XDaNK6amz/gitlab-omnibus-redis' }
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'inserts a placeholder' do
|
|
|
|
expect(embed_url).to be_present
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Nokogiri escapes the URLs, but we don't care about that
|
|
|
|
# distinction for the purposes of this filter
|
|
|
|
def unescape(html)
|
|
|
|
CGI.unescapeHTML(html)
|
|
|
|
end
|
|
|
|
end
|