summaryrefslogtreecommitdiff
path: root/spec/services/metrics/dashboard/panel_preview_service_spec.rb
blob: d58dee3e7a329da8629be588abba117eb826a98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Metrics::Dashboard::PanelPreviewService do
  let_it_be(:project) { create(:project) }
  let_it_be(:environment) { create(:environment, project: project) }
  let_it_be(:panel_yml) do
    <<~YML
    ---
    title: test panel
    YML
  end
  let_it_be(:dashboard) do
    {
      panel_groups: [
        {
          panels: [{ 'title' => 'test panel' }]
        }
      ]
    }
  end

  describe '#execute' do
    subject(:service_response) { described_class.new(project, panel_yml, environment).execute }

    context "valid panel's yaml" do
      before do
        allow_next_instance_of(::Gitlab::Metrics::Dashboard::Processor) do |processor|
          allow(processor).to receive(:process).and_return(dashboard)
        end
      end

      it 'returns success service response' do
        expect(service_response.success?).to be_truthy
      end

      it 'returns processed panel' do
        expect(service_response.payload).to eq('title' => 'test panel')
      end

      it 'uses dashboard processor' do
        sequence = [
          ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
          ::Gitlab::Metrics::Dashboard::Stages::MetricEndpointInserter,
          ::Gitlab::Metrics::Dashboard::Stages::PanelIdsInserter,
          ::Gitlab::Metrics::Dashboard::Stages::AlertsInserter,
          ::Gitlab::Metrics::Dashboard::Stages::UrlValidator
        ]
        processor_params = [project, dashboard, sequence, environment: environment]

        expect_next_instance_of(::Gitlab::Metrics::Dashboard::Processor, *processor_params) do |processor|
          expect(processor).to receive(:process).and_return(dashboard)
        end

        service_response
      end
    end

    context "invalid  panel's yaml" do
      [
        Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError,
        Gitlab::Config::Loader::Yaml::NotHashError,
        Gitlab::Config::Loader::Yaml::DataTooLargeError,
        Gitlab::Config::Loader::FormatError
      ].each do |error_class|
        before do
          allow_next_instance_of(::Gitlab::Metrics::Dashboard::Processor) do |processor|
            allow(processor).to receive(:process).and_raise(error_class.new('error'))
          end
        end

        it 'returns error service response' do
          expect(service_response.error?).to be_truthy
        end

        it 'returns error message' do
          expect(service_response.message).to eq('error')
        end
      end
    end
  end
end