diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-29 15:06:43 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-29 15:06:43 +0000 |
commit | eac0da9a47f0c7b8b970833d7d5b96cfee057bf7 (patch) | |
tree | 7b7865053435c6dcb4e2d945fcf45e309331c91b /spec | |
parent | b860c6ba2607541e3b5bdf0fc2daaa9ed41a8726 (diff) | |
download | gitlab-ce-eac0da9a47f0c7b8b970833d7d5b96cfee057bf7.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/merge_requests_controller_spec.rb | 22 | ||||
-rw-r--r-- | spec/fixtures/gitlab/import_export/corrupted_project_export.tar.gz | bin | 0 -> 4352 bytes | |||
-rw-r--r-- | spec/fixtures/gitlab/import_export/lightweight_project_export.tar.gz | bin | 0 -> 3837 bytes | |||
-rw-r--r-- | spec/fixtures/project_export.tar.gz | bin | 343091 -> 341315 bytes | |||
-rw-r--r-- | spec/helpers/diff_helper_spec.rb | 45 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/all_models.yml | 2 | ||||
-rw-r--r-- | spec/models/environment_status_spec.rb | 78 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 52 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 35 | ||||
-rw-r--r-- | spec/tasks/gitlab/import_export/import_rake_spec.rb | 106 |
10 files changed, 339 insertions, 1 deletions
diff --git a/spec/controllers/projects/merge_requests_controller_spec.rb b/spec/controllers/projects/merge_requests_controller_spec.rb index bcdff060350..4519cd014a1 100644 --- a/spec/controllers/projects/merge_requests_controller_spec.rb +++ b/spec/controllers/projects/merge_requests_controller_spec.rb @@ -1280,6 +1280,28 @@ describe Projects::MergeRequestsController do end end + it 'uses the explicitly linked deployments' do + expect(EnvironmentStatus) + .to receive(:for_deployed_merge_request) + .with(merge_request, user) + .and_call_original + + get_ci_environments_status(environment_target: 'merge_commit') + end + + context 'when the deployment_merge_requests_widget feature flag is disabled' do + it 'uses the deployments retrieved using CI builds' do + stub_feature_flags(deployment_merge_requests_widget: false) + + expect(EnvironmentStatus) + .to receive(:after_merge_request) + .with(merge_request, user) + .and_call_original + + get_ci_environments_status(environment_target: 'merge_commit') + end + end + def get_ci_environments_status(extra_params = {}) params = { namespace_id: merge_request.project.namespace.to_param, diff --git a/spec/fixtures/gitlab/import_export/corrupted_project_export.tar.gz b/spec/fixtures/gitlab/import_export/corrupted_project_export.tar.gz Binary files differnew file mode 100644 index 00000000000..cac16cf9cd8 --- /dev/null +++ b/spec/fixtures/gitlab/import_export/corrupted_project_export.tar.gz diff --git a/spec/fixtures/gitlab/import_export/lightweight_project_export.tar.gz b/spec/fixtures/gitlab/import_export/lightweight_project_export.tar.gz Binary files differnew file mode 100644 index 00000000000..c01402954dd --- /dev/null +++ b/spec/fixtures/gitlab/import_export/lightweight_project_export.tar.gz diff --git a/spec/fixtures/project_export.tar.gz b/spec/fixtures/project_export.tar.gz Binary files differindex 72ab2d71f35..5ba3bfd4f48 100644 --- a/spec/fixtures/project_export.tar.gz +++ b/spec/fixtures/project_export.tar.gz diff --git a/spec/helpers/diff_helper_spec.rb b/spec/helpers/diff_helper_spec.rb index 47c076e3322..def3078c652 100644 --- a/spec/helpers/diff_helper_spec.rb +++ b/spec/helpers/diff_helper_spec.rb @@ -258,6 +258,51 @@ describe DiffHelper do end end + context '#render_overflow_warning?' do + let(:diffs_collection) { instance_double(Gitlab::Diff::FileCollection::MergeRequestDiff, diff_files: diff_files) } + let(:diff_files) { Gitlab::Git::DiffCollection.new(files) } + let(:safe_file) { { too_large: false, diff: '' } } + let(:large_file) { { too_large: true, diff: '' } } + let(:files) { [safe_file, safe_file] } + + before do + allow(diff_files).to receive(:overflow?).and_return(false) + end + + context 'when neither collection nor individual file hit the limit' do + it 'returns false and does not log any overflow events' do + expect(Gitlab::Metrics).not_to receive(:add_event).with(:diffs_overflow_collection_limits) + expect(Gitlab::Metrics).not_to receive(:add_event).with(:diffs_overflow_single_file_limits) + + expect(render_overflow_warning?(diffs_collection)).to be false + end + end + + context 'when the file collection has an overflow' do + before do + allow(diff_files).to receive(:overflow?).and_return(true) + end + + it 'returns false and only logs collection overflow event' do + expect(Gitlab::Metrics).to receive(:add_event).with(:diffs_overflow_collection_limits).exactly(:once) + expect(Gitlab::Metrics).not_to receive(:add_event).with(:diffs_overflow_single_file_limits) + + expect(render_overflow_warning?(diffs_collection)).to be true + end + end + + context 'when two individual files are too big' do + let(:files) { [safe_file, large_file, large_file] } + + it 'returns false and only logs single file overflow events' do + expect(Gitlab::Metrics).to receive(:add_event).with(:diffs_overflow_single_file_limits).exactly(:once) + expect(Gitlab::Metrics).not_to receive(:add_event).with(:diffs_overflow_collection_limits) + + expect(render_overflow_warning?(diffs_collection)).to be false + end + end + end + context '#diff_file_path_text' do it 'returns full path by default' do expect(diff_file_path_text(diff_file)).to eq(diff_file.new_path) diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index 64d1a98ae71..7e39e9989e5 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -139,6 +139,8 @@ merge_requests: - blocking_merge_requests - blocked_merge_requests - description_versions +- deployment_merge_requests +- deployments external_pull_requests: - project merge_request_diff: diff --git a/spec/models/environment_status_spec.rb b/spec/models/environment_status_spec.rb index eea81d7c128..0f2c6928820 100644 --- a/spec/models/environment_status_spec.rb +++ b/spec/models/environment_status_spec.rb @@ -92,6 +92,84 @@ describe EnvironmentStatus do end end + describe '.for_deployed_merge_request' do + context 'when a merge request has no explicitly linked deployments' do + it 'returns the statuses based on the CI pipelines' do + mr = create(:merge_request, :merged) + + expect(described_class) + .to receive(:after_merge_request) + .with(mr, mr.author) + .and_return([]) + + statuses = described_class.for_deployed_merge_request(mr, mr.author) + + expect(statuses).to eq([]) + end + end + + context 'when a merge request has explicitly linked deployments' do + let(:merge_request) { create(:merge_request, :merged) } + + let(:environment) do + create(:environment, project: merge_request.target_project) + end + + it 'returns the statuses based on the linked deployments' do + deploy = create( + :deployment, + :success, + project: merge_request.target_project, + environment: environment, + deployable: nil + ) + + deploy.link_merge_requests(merge_request.target_project.merge_requests) + + statuses = described_class + .for_deployed_merge_request(merge_request, merge_request.author) + + expect(statuses.length).to eq(1) + expect(statuses[0].environment).to eq(environment) + expect(statuses[0].merge_request).to eq(merge_request) + end + + it 'excludes environments the user can not see' do + deploy = create( + :deployment, + :success, + project: merge_request.target_project, + environment: environment, + deployable: nil + ) + + deploy.link_merge_requests(merge_request.target_project.merge_requests) + + statuses = described_class + .for_deployed_merge_request(merge_request, create(:user)) + + expect(statuses).to be_empty + end + + it 'excludes deployments that have the status "created"' do + deploy = create( + :deployment, + :created, + project: merge_request.target_project, + environment: environment, + deployable: nil + ) + + deploy.link_merge_requests(merge_request.target_project.merge_requests) + + statuses = described_class + .for_deployed_merge_request(merge_request, merge_request.author) + + expect(statuses).to be_empty + end + end + end + describe '.build_environments_status' do subject { described_class.send(:build_environments_status, merge_request, user, pipeline) } diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index f3d270cdf7e..bec817f2416 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -3391,4 +3391,56 @@ describe MergeRequest do ]) end end + + describe '#recent_visible_deployments' do + let(:merge_request) { create(:merge_request) } + + let(:environment) do + create(:environment, project: merge_request.target_project) + end + + it 'returns visible deployments' do + created = create( + :deployment, + :created, + project: merge_request.target_project, + environment: environment + ) + + success = create( + :deployment, + :success, + project: merge_request.target_project, + environment: environment + ) + + failed = create( + :deployment, + :failed, + project: merge_request.target_project, + environment: environment + ) + + merge_request.deployment_merge_requests.create!(deployment: created) + merge_request.deployment_merge_requests.create!(deployment: success) + merge_request.deployment_merge_requests.create!(deployment: failed) + + expect(merge_request.recent_visible_deployments).to eq([failed, success]) + end + + it 'only returns a limited number of deployments' do + 20.times do + deploy = create( + :deployment, + :success, + project: merge_request.target_project, + environment: environment + ) + + merge_request.deployment_merge_requests.create!(deployment: deploy) + end + + expect(merge_request.recent_visible_deployments.count).to eq(10) + end + end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index bad05990965..c0245dfdf1a 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -2399,7 +2399,40 @@ describe Repository do end describe '#ancestor? with Gitaly enabled' do - it_behaves_like "#ancestor?" + let(:commit) { repository.commit } + let(:ancestor) { commit.parents.first } + let(:cache_key) { "ancestor:#{ancestor.id}:#{commit.id}" } + + it_behaves_like '#ancestor?' + + context 'caching', :request_store, :clean_gitlab_redis_cache do + it 'only calls out to Gitaly once' do + expect(repository.raw_repository).to receive(:ancestor?).once + + 2.times { repository.ancestor?(commit.id, ancestor.id) } + end + + it 'increments a counter with cache hits' do + counter = Gitlab::Metrics.counter(:repository_ancestor_calls_total, 'Repository ancestor calls') + + expect do + 2.times { repository.ancestor?(commit.id, ancestor.id) } + end.to change { counter.get(cache_hit: 'true') }.by(1) + .and change { counter.get(cache_hit: 'false') }.by(1) + end + + it 'returns the value from the request store' do + repository.__send__(:request_store_cache).write(cache_key, "it's apparent") + + expect(repository.ancestor?(ancestor.id, commit.id)).to eq("it's apparent") + end + + it 'returns the value from the redis cache' do + expect(repository.__send__(:cache)).to receive(:fetch).with(cache_key).and_return("it's apparent") + + expect(repository.ancestor?(ancestor.id, commit.id)).to eq("it's apparent") + end + end end describe '#ancestor? with Rugged enabled', :enable_rugged do diff --git a/spec/tasks/gitlab/import_export/import_rake_spec.rb b/spec/tasks/gitlab/import_export/import_rake_spec.rb new file mode 100644 index 00000000000..e8507e63bf5 --- /dev/null +++ b/spec/tasks/gitlab/import_export/import_rake_spec.rb @@ -0,0 +1,106 @@ +# frozen_string_literal: true + +require 'rake_helper' + +describe 'gitlab:import_export:import rake task' do + let(:username) { 'root' } + let(:namespace_path) { username } + let!(:user) { create(:user, username: username) } + let(:task_params) { [username, namespace_path, project_name, archive_path] } + let(:project) { Project.find_by_full_path("#{namespace_path}/#{project_name}") } + + before do + Rake.application.rake_require('tasks/gitlab/import_export/import') + allow(Settings.uploads.object_store).to receive(:[]=).and_call_original + end + + around do |example| + old_direct_upload_setting = Settings.uploads.object_store['direct_upload'] + old_background_upload_setting = Settings.uploads.object_store['background_upload'] + + Settings.uploads.object_store['direct_upload'] = true + Settings.uploads.object_store['background_upload'] = true + + example.run + + Settings.uploads.object_store['direct_upload'] = old_direct_upload_setting + Settings.uploads.object_store['background_upload'] = old_background_upload_setting + end + + subject { run_rake_task('gitlab:import_export:import', task_params) } + + context 'when project import is valid' do + let(:project_name) { 'import_rake_test_project' } + let(:archive_path) { 'spec/fixtures/gitlab/import_export/lightweight_project_export.tar.gz' } + + it 'performs project import successfully' do + expect { subject }.to output(/Done!/).to_stdout + expect { subject }.not_to raise_error + + expect(project.merge_requests.count).to be > 0 + expect(project.issues.count).to be > 0 + expect(project.milestones.count).to be > 0 + expect(project.import_state.status).to eq('finished') + end + + it 'disables direct & background upload only during project creation' do + expect_next_instance_of(Projects::GitlabProjectsImportService) do |service| + expect(service).to receive(:execute).and_wrap_original do |m| + expect(Settings.uploads.object_store['background_upload']).to eq(false) + expect(Settings.uploads.object_store['direct_upload']).to eq(false) + + m.call + end + end + + expect_next_instance_of(GitlabProjectImport) do |importer| + expect(importer).to receive(:execute_sidekiq_job).and_wrap_original do |m| + expect(Settings.uploads.object_store['background_upload']).to eq(true) + expect(Settings.uploads.object_store['direct_upload']).to eq(true) + expect(Settings.uploads.object_store).not_to receive(:[]=).with('backgroud_upload', false) + expect(Settings.uploads.object_store).not_to receive(:[]=).with('direct_upload', false) + + m.call + end + end + + subject + end + end + + context 'when project import is invalid' do + let(:project_name) { 'import_rake_invalid_test_project' } + let(:archive_path) { 'spec/fixtures/gitlab/import_export/corrupted_project_export.tar.gz' } + let(:not_imported_message) { /Total number of not imported relations: 1/ } + let(:error) { /Validation failed: Notes is invalid/ } + + context 'when import_graceful_failures feature flag is enabled' do + before do + stub_feature_flags(import_graceful_failures: true) + end + + it 'performs project import successfully' do + expect { subject }.to output(not_imported_message).to_stdout + expect { subject }.not_to raise_error + + expect(project.merge_requests).to be_empty + expect(project.import_state.last_error).to be_nil + expect(project.import_state.status).to eq('finished') + end + end + + context 'when import_graceful_failures feature flag is disabled' do + before do + stub_feature_flags(import_graceful_failures: false) + end + + it 'fails project import with an error' do + expect { subject }.to raise_error(SystemExit).and output(error).to_stdout + + expect(project.merge_requests).to be_empty + expect(project.import_state.last_error).to match(error) + expect(project.import_state.status).to eq('failed') + end + end + end +end |