summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/prometheus/query_variables_spec.rb
blob: 1dbdb892a5d1d898078c48078e409d995b8ba5e2 (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
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Prometheus::QueryVariables do
  describe '.call' do
    let_it_be_with_refind(:environment) { create(:environment) }
    let(:project) { environment.project }
    let(:slug) { environment.slug }
    let(:params) { {} }

    subject { described_class.call(environment, **params) }

    it { is_expected.to include(ci_environment_slug: slug) }
    it { is_expected.to include(ci_project_name: project.name) }
    it { is_expected.to include(ci_project_namespace: project.namespace.name) }
    it { is_expected.to include(ci_project_path: project.full_path) }
    it { is_expected.to include(ci_environment_name: environment.name) }

    it do
      is_expected.to include(environment_filter:
                             %Q[container_name!="POD",environment="#{slug}"])
    end

    context 'without deployment platform' do
      it { is_expected.to include(kube_namespace: '') }
    end

    context 'with deployment platform' do
      context 'with project cluster' do
        let(:kube_namespace) { environment.deployment_namespace }

        before do
          create(:cluster, :project, :provided_by_user, projects: [project])
        end

        it { is_expected.to include(kube_namespace: kube_namespace) }
      end

      context 'with group cluster' do
        let(:cluster) { create(:cluster, :group, :provided_by_user, groups: [group]) }
        let(:group) { create(:group) }
        let(:project2) { create(:project) }
        let(:kube_namespace) { k8s_ns.namespace }

        let!(:k8s_ns) { create(:cluster_kubernetes_namespace, cluster: cluster, project: project, environment: environment) }
        let!(:k8s_ns2) { create(:cluster_kubernetes_namespace, cluster: cluster, project: project2, environment: environment) }

        before do
          group.projects << project
          group.projects << project2
        end

        it { is_expected.to include(kube_namespace: kube_namespace) }
      end
    end

    context '__range' do
      context 'when start_time and end_time are present' do
        let(:params) do
          {
            start_time: Time.rfc3339('2020-05-29T07:23:05.008Z'),
            end_time: Time.rfc3339('2020-05-29T15:23:05.008Z')
          }
        end

        it { is_expected.to include(__range: "#{8.hours.to_i}s") }
      end

      context 'when start_time and end_time are not present' do
        it { is_expected.to include(__range: nil) }
      end

      context 'when end_time is not present' do
        let(:params) do
          {
            start_time: Time.rfc3339('2020-05-29T07:23:05.008Z')
          }
        end

        it { is_expected.to include(__range: nil) }
      end

      context 'when start_time is not present' do
        let(:params) do
          {
            end_time: Time.rfc3339('2020-05-29T07:23:05.008Z')
          }
        end

        it { is_expected.to include(__range: nil) }
      end
    end
  end
end