gitlab-org--gitlab-foss/spec/models/environment_status_spec.rb
Shinya Maeda 4bd00e5378 Squashed commit of the following:
commit 04b06a2293fa12660a9c213a9db27fe90b83248b
Merge: d580841d4ed a445aa0a92
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Mon Dec 3 10:51:55 2018 +0900

    Merge branch 'master-ce' into fix-mr-widget-unrelated-deployment-status

commit d580841d4ed944f01e6417fa77842826843b6a04
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Fri Nov 30 18:11:04 2018 +0900

    Add environment to all_models.yml

commit 689fbe2699a3adf10804312e680fa336e4560eaf
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Fri Nov 30 17:00:35 2018 +0900

    Proper way to get uniq relationship

commit c0733c6ecd535a6a1b6243080a2226836890f479
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Fri Nov 30 16:20:40 2018 +0900

    Revert build change

commit 19dc55a8fe6e0fa575858d51144516b7fb0120de
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Fri Nov 30 16:19:18 2018 +0900

    Add uniq option

commit 0a6995f311c09b453fd0aecff2f6052de38efc27
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Fri Nov 30 16:14:30 2018 +0900

    Drop persisted_environment

commit 3f68fc783b0ee0d66e03de6d979616c4c4892118
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Wed Nov 28 13:59:04 2018 +0900

    Return empty array if pipeline is nil

commit 73801c5c3d06339e38dce7461a71285bcdbb8f45
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Tue Nov 27 16:34:47 2018 +0900

    Add changelog

commit d461699abf2835cc51949a5138e829628209dd6d
Author: Shinya Maeda <shinya@gitlab.com>
Date:   Tue Nov 27 16:32:20 2018 +0900

    Squashed commit of the following:

    commit 77ab5259605e217a39b04d2cea6204277e42d2b5
    Merge: 7ac1ed50612 2ee8c40fc1
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Tue Nov 27 16:31:07 2018 +0900

        Merge branch 'master-ce' into fix-mr-widget-unrelated-deployment-status

    commit 7ac1ed50612620df6408220b0a7cfcb626a04c48
    Merge: 49ba5c5aeff b55aeca25e
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Mon Nov 26 20:01:43 2018 +0900

        Merge branch 'master-ce' into fix-mr-widget-unrelated-deployment-status

    commit 49ba5c5aeff3efee7b7498d443372021c3f4f8b5
    Merge: aa3fd0ff9e8 fbbe5ccd1b
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Mon Nov 26 15:27:36 2018 +0900

        Merge branch 'master-ce' into fix-mr-widget-unrelated-deployment-status

    commit aa3fd0ff9e8a418a233ebaa60b38c081cab50099
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Tue Nov 20 18:28:53 2018 +0900

        Fix static analysis

    commit 7afe5f37003869a73dbb297229f8533f78b82684
    Merge: e65b9580ff4 8a581d531b
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Tue Nov 20 18:27:33 2018 +0900

        Merge branch 'master-ce' into fix-mr-widget-unrelated-deployment-status

    commit e65b9580ff422359113e1a4e37c212f7b13aba4d
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Mon Nov 19 17:59:48 2018 +0900

        Ignore deployments from project import/export

    commit 9eb4ddab8415c1ef61a3c646bdc4602bcf4ebe24
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Mon Nov 19 16:26:00 2018 +0900

        Add memoization

    commit 57f0bea3aaaa07b75d18e52068c532277350cda0
    Author: Shinya Maeda <shinya@gitlab.com>
    Date:   Mon Nov 19 16:21:39 2018 +0900

        Fix unrelated deployment status in MR widget
2018-12-03 15:47:09 +09:00

194 lines
6.3 KiB
Ruby

require 'spec_helper'
describe EnvironmentStatus do
include ProjectForksHelper
let(:deployment) { create(:deployment, :succeed, :review_app) }
let(:environment) { deployment.environment }
let(:project) { deployment.project }
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
let(:sha) { deployment.sha }
subject(:environment_status) { described_class.new(environment, merge_request, sha) }
it { is_expected.to delegate_method(:id).to(:environment) }
it { is_expected.to delegate_method(:name).to(:environment) }
it { is_expected.to delegate_method(:project).to(:environment) }
it { is_expected.to delegate_method(:deployed_at).to(:deployment) }
it { is_expected.to delegate_method(:status).to(:deployment) }
describe '#project' do
subject { environment_status.project }
it { is_expected.to eq(project) }
end
describe '#merge_request' do
subject { environment_status.merge_request }
it { is_expected.to eq(merge_request) }
end
describe '#deployment' do
subject { environment_status.deployment }
it { is_expected.to eq(deployment) }
end
# $ git diff --stat pages-deploy-target...pages-deploy
# .gitlab/route-map.yml | 5 +++++
# files/html/500.html | 13 -------------
# files/html/page.html | 3 +++
# files/js/application.js | 3 +++
# files/markdown/ruby-style-guide.md | 4 ++++
# pages-deploy.txt | 1 +
#
# $ cat .gitlab/route-map.yml
# - source: /files\/markdown\/(.+)\.md$/
# public: '\1.html'
#
# - source: /files\/(.+)/
# public: '\1'
describe '#changes' do
it 'contains only added and modified public pages' do
expect(environment_status.changes).to contain_exactly(
{
path: 'ruby-style-guide.html',
external_url: "#{environment.external_url}/ruby-style-guide.html"
}, {
path: 'html/page.html',
external_url: "#{environment.external_url}/html/page.html"
}
)
end
end
describe '.for_merge_request' do
let(:admin) { create(:admin) }
let(:pipeline) { create(:ci_pipeline, sha: sha) }
it 'is based on merge_request.diff_head_sha' do
expect(merge_request).to receive(:diff_head_sha)
expect(merge_request).not_to receive(:merge_commit_sha)
described_class.for_merge_request(merge_request, admin)
end
end
describe '.after_merge_request' do
let(:admin) { create(:admin) }
let(:pipeline) { create(:ci_pipeline, sha: sha) }
before do
merge_request.mark_as_merged!
end
it 'is based on merge_request.merge_commit_sha' do
expect(merge_request).to receive(:merge_commit_sha)
expect(merge_request).not_to receive(:diff_head_sha)
described_class.after_merge_request(merge_request, admin)
end
end
describe '.build_environments_status' do
subject { described_class.send(:build_environments_status, merge_request, user, pipeline) }
let!(:build) { create(:ci_build, :deploy_to_production, pipeline: pipeline) }
let(:environment) { build.deployment.environment }
let(:user) { project.owner }
context 'when environment is created on a forked project' do
let(:project) { create(:project, :repository) }
let(:forked) { fork_project(project, user, repository: true) }
let(:sha) { forked.commit.sha }
let(:pipeline) { create(:ci_pipeline, sha: sha, project: forked) }
let(:merge_request) do
create(:merge_request,
source_project: forked,
target_project: project,
target_branch: 'master',
head_pipeline: pipeline)
end
it 'returns environment status' do
expect(subject.count).to eq(1)
expect(subject[0].environment).to eq(environment)
expect(subject[0].merge_request).to eq(merge_request)
expect(subject[0].sha).to eq(sha)
end
end
context 'when environment is created on a target project' do
let(:project) { create(:project, :repository) }
let(:sha) { project.commit.sha }
let(:pipeline) { create(:ci_pipeline, sha: sha, project: project) }
let(:merge_request) do
create(:merge_request,
source_project: project,
source_branch: 'feature',
target_project: project,
target_branch: 'master',
head_pipeline: pipeline)
end
it 'returns environment status' do
expect(subject.count).to eq(1)
expect(subject[0].environment).to eq(environment)
expect(subject[0].merge_request).to eq(merge_request)
expect(subject[0].sha).to eq(sha)
end
context 'when the build stops an environment' do
let!(:build) { create(:ci_build, :stop_review_app, pipeline: pipeline) }
it 'does not return environment status' do
expect(subject.count).to eq(0)
end
end
context 'when user does not have a permission to see the environment' do
let(:user) { create(:user) }
it 'does not return environment status' do
expect(subject.count).to eq(0)
end
end
context 'when multiple deployments with the same SHA in different environments' do
let(:pipeline2) { create(:ci_pipeline, sha: sha, project: project) }
let!(:build2) { create(:ci_build, :start_review_app, pipeline: pipeline2) }
it 'returns deployments related to the head pipeline' do
expect(subject.count).to eq(1)
expect(subject[0].environment).to eq(environment)
expect(subject[0].merge_request).to eq(merge_request)
expect(subject[0].sha).to eq(sha)
end
end
context 'when multiple deployments in the same pipeline for the same environments' do
let!(:build2) { create(:ci_build, :deploy_to_production, pipeline: pipeline) }
it 'returns unique entries' do
expect(subject.count).to eq(1)
expect(subject[0].environment).to eq(environment)
expect(subject[0].merge_request).to eq(merge_request)
expect(subject[0].sha).to eq(sha)
end
end
context 'when environment is stopped' do
before do
environment.stop!
end
it 'does not return environment status' do
expect(subject.count).to eq(0)
end
end
end
end
end