diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-25 18:12:11 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-25 18:12:11 +0300 |
commit | 82c63f420c7fc2229a44aa336d656a9712884f83 (patch) | |
tree | 7ae316f671e194adcbc7712e853e5397bf161a36 /spec/scripts | |
parent | e5d3d8c323872cb5dee33c5388b3157294c30343 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/scripts')
-rw-r--r-- | spec/scripts/failed_tests_spec.rb | 127 | ||||
-rw-r--r-- | spec/scripts/pipeline_test_report_builder_spec.rb | 137 |
2 files changed, 264 insertions, 0 deletions
diff --git a/spec/scripts/failed_tests_spec.rb b/spec/scripts/failed_tests_spec.rb new file mode 100644 index 00000000000..92eae75b3be --- /dev/null +++ b/spec/scripts/failed_tests_spec.rb @@ -0,0 +1,127 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative '../../scripts/failed_tests' + +RSpec.describe FailedTests do + let(:report_file) { 'spec/fixtures/scripts/test_report.json' } + let(:output_directory) { 'tmp/previous_test_results' } + let(:rspec_pg_regex) { /rspec .+ pg12( .+)?/ } + let(:rspec_ee_pg_regex) { /rspec-ee .+ pg12( .+)?/ } + + subject { described_class.new(previous_tests_report_path: report_file, output_directory: output_directory, rspec_pg_regex: rspec_pg_regex, rspec_ee_pg_regex: rspec_ee_pg_regex) } + + describe '#output_failed_test_files' do + it 'writes the file for the suite' do + expect(File).to receive(:open).with(File.join(output_directory, "rspec_failed_files.txt"), 'w').once + + subject.output_failed_test_files + end + end + + describe '#failed_files_for_suite_collection' do + let(:failure_path) { 'path/to/fail_file_spec.rb' } + let(:other_failure_path) { 'path/to/fail_file_spec_2.rb' } + let(:file_contents_as_json) do + { + 'suites' => [ + { + 'failed_count' => 1, + 'name' => 'rspec unit pg12 10/12', + 'test_cases' => [ + { + 'status' => 'failed', + 'file' => failure_path + } + ] + }, + { + 'failed_count' => 1, + 'name' => 'rspec-ee unit pg12', + 'test_cases' => [ + { + 'status' => 'failed', + 'file' => failure_path + } + ] + }, + { + 'failed_count' => 1, + 'name' => 'rspec unit pg13 10/12', + 'test_cases' => [ + { + 'status' => 'failed', + 'file' => other_failure_path + } + ] + } + ] + } + end + + before do + allow(subject).to receive(:file_contents_as_json).and_return(file_contents_as_json) + end + + it 'returns a list of failed file paths for suite collection' do + result = subject.failed_files_for_suite_collection + + expect(result[:rspec].to_a).to match_array(failure_path) + expect(result[:rspec_ee].to_a).to match_array(failure_path) + end + end + + describe 'empty report' do + let(:file_content) do + '{}' + end + + before do + allow(subject).to receive(:file_contents).and_return(file_content) + end + + it 'does not fail for output files' do + subject.output_failed_test_files + end + + it 'returns empty results for suite failures' do + result = subject.failed_files_for_suite_collection + + expect(result.values.flatten).to be_empty + end + end + + describe 'invalid report' do + let(:file_content) do + '' + end + + before do + allow(subject).to receive(:file_contents).and_return(file_content) + end + + it 'does not fail for output files' do + subject.output_failed_test_files + end + + it 'returns empty results for suite failures' do + result = subject.failed_files_for_suite_collection + + expect(result.values.flatten).to be_empty + end + end + + describe 'missing report file' do + let(:report_file) { 'unknownfile.json' } + + it 'does not fail for output files' do + subject.output_failed_test_files + end + + it 'returns empty results for suite failures' do + result = subject.failed_files_for_suite_collection + + expect(result.values.flatten).to be_empty + end + end +end diff --git a/spec/scripts/pipeline_test_report_builder_spec.rb b/spec/scripts/pipeline_test_report_builder_spec.rb new file mode 100644 index 00000000000..8a5388f4db8 --- /dev/null +++ b/spec/scripts/pipeline_test_report_builder_spec.rb @@ -0,0 +1,137 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative '../../scripts/pipeline_test_report_builder' + +RSpec.describe PipelineTestReportBuilder do + let(:report_file) { 'spec/fixtures/scripts/test_report.json' } + let(:output_file_path) { 'tmp/previous_test_results/output_file.json' } + + subject do + described_class.new( + project: 'gitlab-org/gitlab', + mr_id: '999', + instance_base_url: 'https://gitlab.com', + output_file_path: output_file_path + ) + end + + let(:mr_pipelines) do + [ + { + 'status' => 'running', + 'created_at' => DateTime.now.to_s + }, + { + 'status' => 'failed', + 'created_at' => (DateTime.now - 5).to_s + } + ] + end + + let(:failed_builds_for_pipeline) do + [ + { + 'id' => 9999, + 'stage' => 'test' + } + ] + end + + let(:test_report_for_build) do + { + "name": "rspec-ee system pg11 geo", + "failed_count": 41, + "test_cases": [ + { + "status": "failed", + "name": "example", + "classname": "ee.spec.features.geo_node_spec", + "file": "./ee/spec/features/geo_node_spec.rb", + "execution_time": 6.324748, + "system_output": { + "__content__": "\n", + "message": "RSpec::Core::MultipleExceptionError", + "type": "RSpec::Core::MultipleExceptionError" + } + } + ] + } + end + + before do + allow(subject).to receive(:pipelines_for_mr).and_return(mr_pipelines) + allow(subject).to receive(:failed_builds_for_pipeline).and_return(failed_builds_for_pipeline) + allow(subject).to receive(:test_report_for_build).and_return(test_report_for_build) + end + + describe '#test_report_for_latest_pipeline' do + context 'no previous pipeline' do + let(:mr_pipelines) { [] } + + it 'returns empty hash' do + expect(subject.test_report_for_latest_pipeline).to eq("{}") + end + end + + context 'first pipeline scenario' do + let(:mr_pipelines) do + [ + { + 'status' => 'running', + 'created_at' => DateTime.now.to_s + } + ] + end + + it 'returns empty hash' do + expect(subject.test_report_for_latest_pipeline).to eq("{}") + end + end + + context 'no previous failed pipeline' do + let(:mr_pipelines) do + [ + { + 'status' => 'running', + 'created_at' => DateTime.now.to_s + }, + { + 'status' => 'success', + 'created_at' => (DateTime.now - 5).to_s + } + ] + end + + it 'returns empty hash' do + expect(subject.test_report_for_latest_pipeline).to eq("{}") + end + end + + context 'no failed test builds' do + let(:failed_builds_for_pipeline) do + [ + { + 'id' => 9999, + 'stage' => 'prepare' + } + ] + end + + it 'returns empty hash' do + expect(subject.test_report_for_latest_pipeline).to eq("{}") + end + end + + context 'failed pipeline and failed test builds' do + it 'returns populated test list for suites' do + actual = subject.test_report_for_latest_pipeline + expected = { + 'suites' => [test_report_for_build] + }.to_json + + expect(actual).to eq(expected) + end + end + end +end |