Welcome to mirror list, hosted at ThFree Co, Russian Federation.

auto_stop_worker_spec.rb « environments « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb162b5a01c46f5e2dbb59abb6ee2bcd8261f2e4 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Environments::AutoStopWorker do
  include CreateEnvironmentsHelpers

  subject { worker.perform(environment_id) }

  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:developer) { create(:user).tap { |u| project.add_developer(u) } }
  let_it_be(:reporter) { create(:user).tap { |u| project.add_reporter(u) } }

  before_all do
    project.repository.add_branch(developer, 'review/feature', 'master')
  end

  let!(:environment) { create_review_app(user, project, 'review/feature').environment }
  let(:environment_id) { environment.id }
  let(:worker) { described_class.new }
  let(:user) { developer }

  it 'stops the environment' do
    expect { subject }
      .to change { Environment.find_by_name('review/feature').state }
      .from('available').to('stopping')
  end

  it 'executes the stop action' do
    expect { subject }
      .to change { Ci::Build.find_by_name('stop_review_app').status }
      .from('manual').to('pending')
  end

  context 'when user does not have a permission to play the stop action' do
    let(:user) { reporter }

    it 'raises an error' do
      expect { subject }.to raise_error(Gitlab::Access::AccessDeniedError)
    end
  end

  context 'when the environment has already been stopped' do
    before do
      environment.stop!
    end

    it 'does not execute the stop action' do
      expect { subject }
        .not_to change { Ci::Build.find_by_name('stop_review_app').status }
    end
  end

  context 'when there are no deployments and associted stop actions' do
    let!(:environment) { create(:environment) }

    it 'stops the environment' do
      subject

      expect(environment.reload).to be_stopped
    end
  end

  context 'when there are no corresponding environment record' do
    let!(:environment) { double(:environment, id: non_existing_record_id) }

    it 'ignores the invalid record' do
      expect { subject }.not_to raise_error
    end
  end
end