summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage_data_counters/ci_template_unique_counter_spec.rb
blob: 6a37bfd106d97725c5cde204a2b399d4b8c96201 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::UsageDataCounters::CiTemplateUniqueCounter do
  describe '.track_unique_project_event' do
    using RSpec::Parameterized::TableSyntax
    include SnowplowHelpers

    let(:project) { build(:project) }
    let(:user) { build(:user) }

    shared_examples 'tracks template' do
      let(:subject) { described_class.track_unique_project_event(project: project, template: template_path, config_source: config_source, user: user) }

      it "has an event defined for template" do
        expect do
          subject
        end.not_to raise_error
      end

      it "tracks template" do
        expanded_template_name = described_class.expand_template_name(template_path)
        expected_template_event_name = described_class.ci_template_event_name(expanded_template_name, config_source)
        expect(Gitlab::UsageDataCounters::HLLRedisCounter).to(receive(:track_event)).with(expected_template_event_name, values: project.id)

        subject
      end

      context 'Snowplow' do
        it 'event is not tracked if FF is disabled' do
          stub_feature_flags(route_hll_to_snowplow: false)

          subject

          expect_no_snowplow_event
        end

        it 'tracks event' do
          subject

          expect_snowplow_event(
            category: described_class.to_s,
            action: 'ci_templates_unique',
            namespace: project.namespace,
            user: user,
            project: project
          )
        end
      end
    end

    context 'with explicit includes', :snowplow do
      let(:config_source) { :repository_source }

      (described_class.ci_templates - ['Verify/Browser-Performance.latest.gitlab-ci.yml', 'Verify/Browser-Performance.gitlab-ci.yml']).each do |template|
        context "for #{template}" do
          let(:template_path) { template }

          include_examples 'tracks template'
        end
      end
    end

    context 'with implicit includes', :snowplow do
      let(:config_source) { :auto_devops_source }

      [
        ['', ['Auto-DevOps.gitlab-ci.yml']],
        ['Jobs', described_class.ci_templates('lib/gitlab/ci/templates/Jobs')],
        ['Security', described_class.ci_templates('lib/gitlab/ci/templates/Security')]
      ].each do |directory, templates|
        templates.each do |template|
          context "for #{template}" do
            let(:template_path) { File.join(directory, template) }

            include_examples 'tracks template'
          end
        end
      end
    end

    it 'expands short template names' do
      expect do
        described_class.track_unique_project_event(project: project, template: 'Dependency-Scanning.gitlab-ci.yml', config_source: :repository_source, user: user)
      end.not_to raise_error
    end
  end
end