summaryrefslogtreecommitdiff
path: root/spec/migrations/create_environment_for_self_monitoring_project_spec.rb
blob: 1ba464f161069478b92ad544bafb7113160dd563 (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
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200214214934_create_environment_for_self_monitoring_project')

RSpec.describe CreateEnvironmentForSelfMonitoringProject do
  let(:application_settings_table) { table(:application_settings) }

  let(:environments) { table(:environments) }

  let(:instance_administrators_group) do
    table(:namespaces).create!(
      id: 1,
      name: 'GitLab Instance Administrators',
      path: 'gitlab-instance-administrators-random',
      type: 'Group'
    )
  end

  let(:self_monitoring_project) do
    table(:projects).create!(
      id: 2,
      name: 'Self Monitoring',
      path: 'self_monitoring',
      namespace_id: instance_administrators_group.id
    )
  end

  context 'when the self monitoring project ID is not set' do
    it 'does not make changes' do
      expect(environments.find_by(project_id: self_monitoring_project.id)).to be_nil

      migrate!

      expect(environments.find_by(project_id: self_monitoring_project.id)).to be_nil
    end
  end

  context 'when the self monitoring project ID is set' do
    before do
      application_settings_table.create!(instance_administration_project_id: self_monitoring_project.id)
    end

    context 'when the environment already exists' do
      let!(:environment) do
        environments.create!(project_id: self_monitoring_project.id, name: 'production', slug: 'production')
      end

      it 'does not make changes' do
        expect(environments.find_by(project_id: self_monitoring_project.id)).to eq(environment)

        migrate!

        expect(environments.find_by(project_id: self_monitoring_project.id)).to eq(environment)
      end
    end

    context 'when the environment does not exist' do
      it 'creates the environment' do
        expect(environments.find_by(project_id: self_monitoring_project.id)).to be_nil

        migrate!

        expect(environments.find_by(project_id: self_monitoring_project.id)).to be
      end
    end
  end
end