diff options
author | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
commit | 6438df3a1e0fb944485cebf07976160184697d72 (patch) | |
tree | 00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /spec/lib/atlassian | |
parent | 42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff) |
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'spec/lib/atlassian')
4 files changed, 469 insertions, 15 deletions
diff --git a/spec/lib/atlassian/jira_connect/client_spec.rb b/spec/lib/atlassian/jira_connect/client_spec.rb index 6a161854dfb..21ee40f22fe 100644 --- a/spec/lib/atlassian/jira_connect/client_spec.rb +++ b/spec/lib/atlassian/jira_connect/client_spec.rb @@ -8,6 +8,15 @@ RSpec.describe Atlassian::JiraConnect::Client do subject { described_class.new('https://gitlab-test.atlassian.net', 'sample_secret') } let_it_be(:project) { create_default(:project, :repository) } + let_it_be(:mrs_by_title) { create_list(:merge_request, 4, :unique_branches, :jira_title) } + let_it_be(:mrs_by_branch) { create_list(:merge_request, 2, :jira_branch) } + let_it_be(:red_herrings) { create_list(:merge_request, 1, :unique_branches) } + + let_it_be(:pipelines) do + (red_herrings + mrs_by_branch + mrs_by_title).map do |mr| + create(:ci_pipeline, merge_request: mr) + end + end around do |example| freeze_time { example.run } @@ -22,13 +31,25 @@ RSpec.describe Atlassian::JiraConnect::Client do end describe '#send_info' do - it 'calls store_build_info and store_dev_info as appropriate' do + it 'calls more specific methods as appropriate' do + expect(subject).to receive(:store_ff_info).with( + project: project, + update_sequence_id: :x, + feature_flags: :r + ).and_return(:ff_stored) + expect(subject).to receive(:store_build_info).with( project: project, update_sequence_id: :x, pipelines: :y ).and_return(:build_stored) + expect(subject).to receive(:store_deploy_info).with( + project: project, + update_sequence_id: :x, + deployments: :q + ).and_return(:deploys_stored) + expect(subject).to receive(:store_dev_info).with( project: project, update_sequence_id: :x, @@ -43,10 +64,13 @@ RSpec.describe Atlassian::JiraConnect::Client do commits: :a, branches: :b, merge_requests: :c, - pipelines: :y + pipelines: :y, + deployments: :q, + feature_flags: :r } - expect(subject.send_info(**args)).to contain_exactly(:dev_stored, :build_stored) + expect(subject.send_info(**args)) + .to contain_exactly(:dev_stored, :build_stored, :deploys_stored, :ff_stored) end it 'only calls methods that we need to call' do @@ -83,31 +107,263 @@ RSpec.describe Atlassian::JiraConnect::Client do } end - describe '#store_build_info' do - let_it_be(:mrs_by_title) { create_list(:merge_request, 4, :unique_branches, :jira_title) } - let_it_be(:mrs_by_branch) { create_list(:merge_request, 2, :jira_branch) } - let_it_be(:red_herrings) { create_list(:merge_request, 1, :unique_branches) } + describe '#handle_response' do + let(:errors) { [{ 'message' => 'X' }, { 'message' => 'Y' }] } + let(:processed) { subject.send(:handle_response, response, 'foo') { |x| [:data, x] } } + + context 'the response is 200 OK' do + let(:response) { double(code: 200, parsed_response: :foo) } + + it 'yields to the block' do + expect(processed).to eq [:data, :foo] + end + end + + context 'the response is 400 bad request' do + let(:response) { double(code: 400, parsed_response: errors) } + + it 'extracts the errors messages' do + expect(processed).to eq('errorMessages' => %w(X Y)) + end + end + + context 'the response is 401 forbidden' do + let(:response) { double(code: 401, parsed_response: nil) } + + it 'reports that our JWT is wrong' do + expect(processed).to eq('errorMessages' => ['Invalid JWT']) + end + end + + context 'the response is 403' do + let(:response) { double(code: 403, parsed_response: nil) } + + it 'reports that the App is misconfigured' do + expect(processed).to eq('errorMessages' => ['App does not support foo']) + end + end + + context 'the response is 413' do + let(:response) { double(code: 413, parsed_response: errors) } + + it 'extracts the errors messages' do + expect(processed).to eq('errorMessages' => ['Data too large', 'X', 'Y']) + end + end + + context 'the response is 429' do + let(:response) { double(code: 429, parsed_response: nil) } + + it 'reports that we exceeded the rate limit' do + expect(processed).to eq('errorMessages' => ['Rate limit exceeded']) + end + end + + context 'the response is 503' do + let(:response) { double(code: 503, parsed_response: nil) } - let_it_be(:pipelines) do - (red_herrings + mrs_by_branch + mrs_by_title).map do |mr| - create(:ci_pipeline, merge_request: mr) + it 'reports that the service is unavailable' do + expect(processed).to eq('errorMessages' => ['Service unavailable']) end end + context 'the response is anything else' do + let(:response) { double(code: 1000, parsed_response: :something) } + + it 'reports that this was unanticipated' do + expect(processed).to eq('errorMessages' => ['Unknown error'], 'response' => :something) + end + end + end + + describe '#store_deploy_info' do + let_it_be(:environment) { create(:environment, name: 'DEV', project: project) } + let_it_be(:deployments) do + pipelines.map do |p| + build = create(:ci_build, environment: environment.name, pipeline: p, project: project) + create(:deployment, deployable: build, environment: environment) + end + end + + let(:schema) do + Atlassian::Schemata.deploy_info_payload + end + + let(:body) do + matcher = be_valid_json.and match_schema(schema) + + ->(text) { matcher.matches?(text) } + end + + let(:rejections) { [] } + let(:response_body) do + { + acceptedDeployments: [], + rejectedDeployments: rejections, + unknownIssueKeys: [] + }.to_json + end + + before do + path = '/rest/deployments/0.1/bulk' + stub_full_request('https://gitlab-test.atlassian.net' + path, method: :post) + .with(body: body, headers: expected_headers(path)) + .to_return(body: response_body, headers: { 'Content-Type': 'application/json' }) + end + + it "calls the API with auth headers" do + subject.send(:store_deploy_info, project: project, deployments: deployments) + end + + it 'only sends information about relevant MRs' do + expect(subject).to receive(:post).with('/rest/deployments/0.1/bulk', { deployments: have_attributes(size: 6) }).and_call_original + + subject.send(:store_deploy_info, project: project, deployments: deployments) + end + + it 'does not call the API if there is nothing to report' do + expect(subject).not_to receive(:post) + + subject.send(:store_deploy_info, project: project, deployments: deployments.take(1)) + end + + context 'there are errors' do + let(:rejections) do + [{ errors: [{ message: 'X' }, { message: 'Y' }] }, { errors: [{ message: 'Z' }] }] + end + + it 'reports the errors' do + response = subject.send(:store_deploy_info, project: project, deployments: deployments) + + expect(response['errorMessages']).to eq(%w(X Y Z)) + end + end + + it 'does not call the API if the feature flag is not enabled' do + stub_feature_flags(jira_sync_deployments: false) + + expect(subject).not_to receive(:post) + + subject.send(:store_deploy_info, project: project, deployments: deployments) + end + + it 'does call the API if the feature flag enabled for the project' do + stub_feature_flags(jira_sync_deployments: project) + + expect(subject).to receive(:post).with('/rest/deployments/0.1/bulk', { deployments: Array }).and_call_original + + subject.send(:store_deploy_info, project: project, deployments: deployments) + end + end + + describe '#store_ff_info' do + let_it_be(:feature_flags) { create_list(:operations_feature_flag, 3, project: project) } + + let(:schema) do + Atlassian::Schemata.ff_info_payload + end + + let(:body) do + matcher = be_valid_json.and match_schema(schema) + + ->(text) { matcher.matches?(text) } + end + + let(:failures) { {} } + let(:response_body) do + { + acceptedFeatureFlags: [], + failedFeatureFlags: failures, + unknownIssueKeys: [] + }.to_json + end + + before do + feature_flags.first.update!(description: 'RELEVANT-123') + feature_flags.second.update!(description: 'RELEVANT-123') + path = '/rest/featureflags/0.1/bulk' + stub_full_request('https://gitlab-test.atlassian.net' + path, method: :post) + .with(body: body, headers: expected_headers(path)) + .to_return(body: response_body, headers: { 'Content-Type': 'application/json' }) + end + + it "calls the API with auth headers" do + subject.send(:store_ff_info, project: project, feature_flags: feature_flags) + end + + it 'only sends information about relevant MRs' do + expect(subject).to receive(:post).with('/rest/featureflags/0.1/bulk', { + flags: have_attributes(size: 2), properties: Hash + }).and_call_original + + subject.send(:store_ff_info, project: project, feature_flags: feature_flags) + end + + it 'does not call the API if there is nothing to report' do + expect(subject).not_to receive(:post) + + subject.send(:store_ff_info, project: project, feature_flags: [feature_flags.last]) + end + + context 'there are errors' do + let(:failures) do + { + a: [{ message: 'X' }, { message: 'Y' }], + b: [{ message: 'Z' }] + } + end + + it 'reports the errors' do + response = subject.send(:store_ff_info, project: project, feature_flags: feature_flags) + + expect(response['errorMessages']).to eq(['a: X', 'a: Y', 'b: Z']) + end + end + + it 'does not call the API if the feature flag is not enabled' do + stub_feature_flags(jira_sync_feature_flags: false) + + expect(subject).not_to receive(:post) + + subject.send(:store_ff_info, project: project, feature_flags: feature_flags) + end + + it 'does call the API if the feature flag enabled for the project' do + stub_feature_flags(jira_sync_feature_flags: project) + + expect(subject).to receive(:post).with('/rest/featureflags/0.1/bulk', { + flags: Array, properties: Hash + }).and_call_original + + subject.send(:store_ff_info, project: project, feature_flags: feature_flags) + end + end + + describe '#store_build_info' do let(:build_info_payload_schema) do Atlassian::Schemata.build_info_payload end let(:body) do - matcher = be_valid_json.according_to_schema(build_info_payload_schema) + matcher = be_valid_json.and match_schema(build_info_payload_schema) ->(text) { matcher.matches?(text) } end + let(:failures) { [] } + let(:response_body) do + { + acceptedBuilds: [], + rejectedBuilds: failures, + unknownIssueKeys: [] + }.to_json + end + before do path = '/rest/builds/0.1/bulk' stub_full_request('https://gitlab-test.atlassian.net' + path, method: :post) .with(body: body, headers: expected_headers(path)) + .to_return(body: response_body, headers: { 'Content-Type': 'application/json' }) end it "calls the API with auth headers" do @@ -115,7 +371,9 @@ RSpec.describe Atlassian::JiraConnect::Client do end it 'only sends information about relevant MRs' do - expect(subject).to receive(:post).with('/rest/builds/0.1/bulk', { builds: have_attributes(size: 6) }) + expect(subject).to receive(:post) + .with('/rest/builds/0.1/bulk', { builds: have_attributes(size: 6) }) + .and_call_original subject.send(:store_build_info, project: project, pipelines: pipelines) end @@ -137,12 +395,28 @@ RSpec.describe Atlassian::JiraConnect::Client do it 'does call the API if the feature flag enabled for the project' do stub_feature_flags(jira_sync_builds: project) - expect(subject).to receive(:post).with('/rest/builds/0.1/bulk', { builds: Array }) + expect(subject).to receive(:post) + .with('/rest/builds/0.1/bulk', { builds: Array }) + .and_call_original subject.send(:store_build_info, project: project, pipelines: pipelines) end + context 'there are errors' do + let(:failures) do + [{ errors: [{ message: 'X' }, { message: 'Y' }] }, { errors: [{ message: 'Z' }] }] + end + + it 'reports the errors' do + response = subject.send(:store_build_info, project: project, pipelines: pipelines) + + expect(response['errorMessages']).to eq(%w(X Y Z)) + end + end + it 'avoids N+1 database queries' do + pending 'https://gitlab.com/gitlab-org/gitlab/-/issues/292818' + baseline = ActiveRecord::QueryRecorder.new do subject.send(:store_build_info, project: project, pipelines: pipelines) end diff --git a/spec/lib/atlassian/jira_connect/serializers/build_entity_spec.rb b/spec/lib/atlassian/jira_connect/serializers/build_entity_spec.rb index 52e475d20ca..4bbd654655d 100644 --- a/spec/lib/atlassian/jira_connect/serializers/build_entity_spec.rb +++ b/spec/lib/atlassian/jira_connect/serializers/build_entity_spec.rb @@ -23,7 +23,7 @@ RSpec.describe Atlassian::JiraConnect::Serializers::BuildEntity do end it 'is invalid, since it has no issue keys' do - expect(subject.to_json).not_to be_valid_json.according_to_schema(Atlassian::Schemata.build_info) + expect(subject.to_json).not_to match_schema(Atlassian::Schemata.build_info) end end end @@ -43,7 +43,7 @@ RSpec.describe Atlassian::JiraConnect::Serializers::BuildEntity do describe '#to_json' do it 'is valid according to the build info schema' do - expect(subject.to_json).to be_valid_json.according_to_schema(Atlassian::Schemata.build_info) + expect(subject.to_json).to be_valid_json.and match_schema(Atlassian::Schemata.build_info) end end end diff --git a/spec/lib/atlassian/jira_connect/serializers/deployment_entity_spec.rb b/spec/lib/atlassian/jira_connect/serializers/deployment_entity_spec.rb new file mode 100644 index 00000000000..82bcbdc4561 --- /dev/null +++ b/spec/lib/atlassian/jira_connect/serializers/deployment_entity_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Atlassian::JiraConnect::Serializers::DeploymentEntity do + let_it_be(:user) { create_default(:user) } + let_it_be(:project) { create_default(:project, :repository) } + let_it_be(:environment) { create(:environment, name: 'prod', project: project) } + let_it_be_with_reload(:deployment) { create(:deployment, environment: environment) } + + subject { described_class.represent(deployment) } + + context 'when the deployment does not belong to any Jira issue' do + describe '#issue_keys' do + it 'is empty' do + expect(subject.issue_keys).to be_empty + end + end + + describe '#to_json' do + it 'can encode the object' do + expect(subject.to_json).to be_valid_json + end + + it 'is invalid, since it has no issue keys' do + expect(subject.to_json).not_to match_schema(Atlassian::Schemata.deployment_info) + end + end + end + + context 'this is an external deployment' do + before do + deployment.update!(deployable: nil) + end + + it 'does not raise errors when serializing' do + expect { subject.to_json }.not_to raise_error + end + + it 'returns an empty list of issue keys' do + expect(subject.issue_keys).to be_empty + end + end + + describe 'environment type' do + using RSpec::Parameterized::TableSyntax + + where(:env_name, :env_type) do + 'prod' | 'production' + 'test' | 'testing' + 'staging' | 'staging' + 'dev' | 'development' + 'review/app' | 'development' + 'something-else' | 'unmapped' + end + + with_them do + before do + environment.update!(name: env_name) + end + + let(:exposed_type) { subject.send(:environment_entity).send(:type) } + + it 'has the correct environment type' do + expect(exposed_type).to eq(env_type) + end + end + end + + context 'when the deployment can be linked to a Jira issue' do + let(:pipeline) { create(:ci_pipeline, merge_request: merge_request) } + + before do + subject.deployable.update!(pipeline: pipeline) + end + + %i[jira_branch jira_title].each do |trait| + context "because it belongs to an MR with a #{trait}" do + let(:merge_request) { create(:merge_request, trait) } + + describe '#issue_keys' do + it 'is not empty' do + expect(subject.issue_keys).not_to be_empty + end + end + + describe '#to_json' do + it 'is valid according to the deployment info schema' do + expect(subject.to_json).to be_valid_json.and match_schema(Atlassian::Schemata.deployment_info) + end + end + end + end + end +end diff --git a/spec/lib/atlassian/jira_connect/serializers/feature_flag_entity_spec.rb b/spec/lib/atlassian/jira_connect/serializers/feature_flag_entity_spec.rb new file mode 100644 index 00000000000..964801338cf --- /dev/null +++ b/spec/lib/atlassian/jira_connect/serializers/feature_flag_entity_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Atlassian::JiraConnect::Serializers::FeatureFlagEntity do + let_it_be(:user) { create_default(:user) } + let_it_be(:project) { create_default(:project) } + + subject { described_class.represent(feature_flag) } + + context 'when the feature flag does not belong to any Jira issue' do + let_it_be(:feature_flag) { create(:operations_feature_flag) } + + describe '#issue_keys' do + it 'is empty' do + expect(subject.issue_keys).to be_empty + end + end + + describe '#to_json' do + it 'can encode the object' do + expect(subject.to_json).to be_valid_json + end + + it 'is invalid, since it has no issue keys' do + expect(subject.to_json).not_to match_schema(Atlassian::Schemata.feature_flag_info) + end + end + end + + context 'when the feature flag does belong to a Jira issue' do + let(:feature_flag) do + create(:operations_feature_flag, description: 'THING-123') + end + + describe '#issue_keys' do + it 'is not empty' do + expect(subject.issue_keys).not_to be_empty + end + end + + describe '#to_json' do + it 'is valid according to the feature flag info schema' do + expect(subject.to_json).to be_valid_json.and match_schema(Atlassian::Schemata.feature_flag_info) + end + end + + context 'it has a percentage strategy' do + let!(:scopes) do + strat = create(:operations_strategy, + feature_flag: feature_flag, + name: ::Operations::FeatureFlags::Strategy::STRATEGY_GRADUALROLLOUTUSERID, + parameters: { 'percentage' => '50', 'groupId' => 'abcde' }) + + [ + create(:operations_scope, strategy: strat, environment_scope: 'production in live'), + create(:operations_scope, strategy: strat, environment_scope: 'staging'), + create(:operations_scope, strategy: strat) + ] + end + + let(:entity) { Gitlab::Json.parse(subject.to_json) } + + it 'is valid according to the feature flag info schema' do + expect(subject.to_json).to be_valid_json.and match_schema(Atlassian::Schemata.feature_flag_info) + end + + it 'has the correct summary' do + expect(entity.dig('summary', 'status')).to eq( + 'enabled' => true, + 'defaultValue' => '', + 'rollout' => { 'percentage' => 50.0, 'text' => 'Percent of users' } + ) + end + + it 'includes the correct environments' do + expect(entity['details']).to contain_exactly( + include('environment' => { 'name' => 'production in live', 'type' => 'production' }), + include('environment' => { 'name' => 'staging', 'type' => 'staging' }), + include('environment' => { 'name' => scopes.last.environment_scope }) + ) + end + end + end +end |