summaryrefslogtreecommitdiff
path: root/spec/features/projects/environments/environment_metrics_spec.rb
blob: 0983bfa7abd63effafeda068052d63dab2160a06 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Environment > Metrics', feature_category: :projects do
  include PrometheusHelpers

  let(:user) { create(:user) }
  let(:project) { create(:project, :with_prometheus_integration, :repository) }
  let(:pipeline) { create(:ci_pipeline, project: project) }
  let(:build) { create(:ci_build, pipeline: pipeline) }
  let(:environment) { create(:environment, project: project) }
  let(:current_time) { Time.now.utc }
  let!(:staging) { create(:environment, name: 'staging', project: project) }

  before do
    project.add_developer(user)
    stub_any_prometheus_request

    sign_in(user)
    stub_feature_flags(remove_monitor_metrics: false)
  end

  around do |example|
    travel_to(current_time) { example.run }
  end

  shared_examples 'has environment selector' do
    it 'has a working environment selector', :js do
      visit_environment(environment)
      click_link 'Monitoring'

      expect(page).to have_current_path(project_metrics_dashboard_path(project, environment: environment.id))
      expect(page).to have_css('[data-testid="environments-dropdown"]')

      within('[data-testid="environments-dropdown"]') do
        # Click on the dropdown
        click_on(environment.name)

        # Select the staging environment
        click_on(staging.name)
      end

      expect(page).to have_current_path(project_metrics_dashboard_path(project, environment: staging.id))

      wait_for_requests
    end
  end

  context 'without deployments' do
    it_behaves_like 'has environment selector'
  end

  context 'with deployments and related deployable present' do
    before do
      create(:deployment, environment: environment, deployable: build)
    end

    it 'shows metrics', :js do
      visit_environment(environment)
      click_link 'Monitoring'

      expect(page).to have_css('[data-testid="prometheus-graphs"]')
    end

    it_behaves_like 'has environment selector'
  end

  def visit_environment(environment)
    visit project_environment_path(environment.project, environment)
  end
end