summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-09 12:09:24 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-09 12:09:24 +0000
commita9ced7da447785c57477b3d8dbccc73a78cface1 (patch)
tree5179d27ab9d801748ee4ed1c64c985974e799812 /spec/lib
parentad0265eead72a624ce7a020847db4f0f0c877e57 (diff)
downloadgitlab-ce-a9ced7da447785c57477b3d8dbccc73a78cface1.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb94
-rw-r--r--spec/lib/gitlab/grafana_embed_usage_data_spec.rb70
-rw-r--r--spec/lib/gitlab/jira_import/base_importer_spec.rb8
-rw-r--r--spec/lib/gitlab/jira_import/issues_importer_spec.rb12
-rw-r--r--spec/lib/gitlab/jira_import/labels_importer_spec.rb12
-rw-r--r--spec/lib/gitlab/usage_data_spec.rb67
-rw-r--r--spec/lib/quality/helm3_client_spec.rb133
7 files changed, 303 insertions, 93 deletions
diff --git a/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb b/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb
new file mode 100644
index 00000000000..37ddb8b569d
--- /dev/null
+++ b/spec/lib/gitlab/background_migration/populate_canonical_emails_spec.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::BackgroundMigration::PopulateCanonicalEmails, :migration, schema: 20200312053852 do
+ let(:migration) { described_class.new }
+
+ let_it_be(:users_table) { table(:users) }
+ let_it_be(:user_canonical_emails_table) { table(:user_canonical_emails) }
+
+ let_it_be(:users) { users_table.all }
+ let_it_be(:user_canonical_emails) { user_canonical_emails_table.all }
+
+ subject { migration.perform(1, 1) }
+
+ describe 'gmail users' do
+ using RSpec::Parameterized::TableSyntax
+
+ where(:original_email, :expected_result) do
+ 'legitimateuser@gmail.com' | 'legitimateuser@gmail.com'
+ 'userwithplus+somestuff@gmail.com' | 'userwithplus@gmail.com'
+ 'user.with.periods@gmail.com' | 'userwithperiods@gmail.com'
+ 'user.with.periods.and.plus+someotherstuff@gmail.com' | 'userwithperiodsandplus@gmail.com'
+ end
+
+ with_them do
+ it 'generates the correct canonical email' do
+ create_user(email: original_email, id: 1)
+
+ subject
+
+ result = canonical_emails
+ expect(result.count).to eq 1
+ expect(result.first).to match({
+ 'user_id' => 1,
+ 'canonical_email' => expected_result
+ })
+ end
+ end
+ end
+
+ describe 'non gmail.com domain users' do
+ %w[
+ legitimateuser@somedomain.com
+ userwithplus+somestuff@other.com
+ user.with.periods@gmail.org
+ user.with.periods.and.plus+someotherstuff@orangmail.com
+ ].each do |non_gmail_address|
+ it 'does not generate a canonical email' do
+ create_user(email: non_gmail_address, id: 1)
+
+ subject
+
+ expect(canonical_emails(user_id: 1).count).to eq 0
+ end
+ end
+ end
+
+ describe 'gracefully handles missing records' do
+ specify { expect { subject }.not_to raise_error }
+ end
+
+ describe 'gracefully handles existing records, some of which may have an already-existing identical canonical_email field' do
+ let_it_be(:user_one) { create_user(email: "example.user@gmail.com", id: 1) }
+ let_it_be(:user_two) { create_user(email: "exampleuser@gmail.com", id: 2) }
+ let_it_be(:user_email_one) { user_canonical_emails.create(canonical_email: "exampleuser@gmail.com", user_id: user_one.id) }
+
+ subject { migration.perform(1, 2) }
+
+ it 'only creates one record' do
+ subject
+
+ expect(canonical_emails.count).not_to be_nil
+ end
+ end
+
+ def create_user(attributes)
+ default_attributes = {
+ projects_limit: 0
+ }
+
+ users.create(default_attributes.merge!(attributes))
+ end
+
+ def canonical_emails(user_id: nil)
+ filter_by_id = user_id ? "WHERE user_id = #{user_id}" : ""
+
+ ApplicationRecord.connection.execute <<~SQL
+ SELECT canonical_email, user_id
+ FROM user_canonical_emails
+ #{filter_by_id};
+ SQL
+ end
+end
diff --git a/spec/lib/gitlab/grafana_embed_usage_data_spec.rb b/spec/lib/gitlab/grafana_embed_usage_data_spec.rb
deleted file mode 100644
index 162db46719b..00000000000
--- a/spec/lib/gitlab/grafana_embed_usage_data_spec.rb
+++ /dev/null
@@ -1,70 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-describe Gitlab::GrafanaEmbedUsageData do
- describe '#issue_count' do
- subject { described_class.issue_count }
-
- let(:project) { create(:project) }
- let(:description_with_embed) { "Some comment\n\nhttps://grafana.example.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
- let(:description_with_unintegrated_embed) { "Some comment\n\nhttps://grafana.exp.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
- let(:description_with_non_grafana_inline_metric) { "Some comment\n\n#{Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url(*['foo', 'bar', 12])}" }
-
- shared_examples "zero count" do
- it "does not count the issue" do
- expect(subject).to eq(0)
- end
- end
-
- context 'with project grafana integration enabled' do
- before do
- create(:grafana_integration, project: project, enabled: true)
- end
-
- context 'with valid and invalid embeds' do
- before do
- # Valid
- create(:issue, project: project, description: description_with_embed)
- create(:issue, project: project, description: description_with_embed)
- # In-Valid
- create(:issue, project: project, description: description_with_unintegrated_embed)
- create(:issue, project: project, description: description_with_non_grafana_inline_metric)
- create(:issue, project: project, description: nil)
- create(:issue, project: project, description: '')
- create(:issue, project: project)
- end
-
- it 'counts only the issues with embeds' do
- expect(subject).to eq(2)
- end
- end
- end
-
- context 'with project grafana integration disabled' do
- before do
- create(:grafana_integration, project: project, enabled: false)
- end
-
- context 'with one issue having a grafana link in the description and one without' do
- before do
- create(:issue, project: project, description: description_with_embed)
- create(:issue, project: project)
- end
-
- it_behaves_like('zero count')
- end
- end
-
- context 'with an un-integrated project' do
- context 'with one issue having a grafana link in the description and one without' do
- before do
- create(:issue, project: project, description: description_with_embed)
- create(:issue, project: project)
- end
-
- it_behaves_like('zero count')
- end
- end
- end
-end
diff --git a/spec/lib/gitlab/jira_import/base_importer_spec.rb b/spec/lib/gitlab/jira_import/base_importer_spec.rb
index 8bc43feb356..f22efcb8743 100644
--- a/spec/lib/gitlab/jira_import/base_importer_spec.rb
+++ b/spec/lib/gitlab/jira_import/base_importer_spec.rb
@@ -37,12 +37,8 @@ describe Gitlab::JiraImport::BaseImporter do
end
context 'when import data exists' do
- let(:jira_import_data) do
- data = JiraImportData.new
- data << JiraImportData::JiraProjectDetails.new('xx', Time.now.strftime('%Y-%m-%d %H:%M:%S'), { user_id: 1, name: 'root' })
- data
- end
- let(:project) { create(:project, import_data: jira_import_data) }
+ let_it_be(:project) { create(:project) }
+ let_it_be(:jira_import) { create(:jira_import_state, project: project) }
let(:subject) { described_class.new(project) }
context 'when #imported_items_cache_key is not implemented' do
diff --git a/spec/lib/gitlab/jira_import/issues_importer_spec.rb b/spec/lib/gitlab/jira_import/issues_importer_spec.rb
index 88e8b195dbe..8e16fd3e978 100644
--- a/spec/lib/gitlab/jira_import/issues_importer_spec.rb
+++ b/spec/lib/gitlab/jira_import/issues_importer_spec.rb
@@ -3,14 +3,10 @@
require 'spec_helper'
describe Gitlab::JiraImport::IssuesImporter do
- let(:user) { create(:user) }
- let(:jira_import_data) do
- data = JiraImportData.new
- data << JiraImportData::JiraProjectDetails.new('XX', Time.now.strftime('%Y-%m-%d %H:%M:%S'), { user_id: user.id, name: user.name })
- data
- end
- let(:project) { create(:project, import_data: jira_import_data) }
- let!(:jira_service) { create(:jira_service, project: project) }
+ let_it_be(:user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+ let_it_be(:jira_import) { create(:jira_import_state, project: project) }
+ let_it_be(:jira_service) { create(:jira_service, project: project) }
subject { described_class.new(project) }
diff --git a/spec/lib/gitlab/jira_import/labels_importer_spec.rb b/spec/lib/gitlab/jira_import/labels_importer_spec.rb
index eaa13d9ed32..2d0e2bc6b53 100644
--- a/spec/lib/gitlab/jira_import/labels_importer_spec.rb
+++ b/spec/lib/gitlab/jira_import/labels_importer_spec.rb
@@ -3,14 +3,10 @@
require 'spec_helper'
describe Gitlab::JiraImport::LabelsImporter do
- let(:user) { create(:user) }
- let(:jira_import_data) do
- data = JiraImportData.new
- data << JiraImportData::JiraProjectDetails.new('XX', Time.now.strftime('%Y-%m-%d %H:%M:%S'), { user_id: user.id, name: user.name })
- data
- end
- let(:project) { create(:project, import_data: jira_import_data) }
- let!(:jira_service) { create(:jira_service, project: project) }
+ let_it_be(:user) { create(:user) }
+ let_it_be(:project) { create(:project) }
+ let_it_be(:jira_import) { create(:jira_import_state, project: project) }
+ let_it_be(:jira_service) { create(:jira_service, project: project) }
subject { described_class.new(project).execute }
diff --git a/spec/lib/gitlab/usage_data_spec.rb b/spec/lib/gitlab/usage_data_spec.rb
index 37d9c5389dd..12199bc6d5a 100644
--- a/spec/lib/gitlab/usage_data_spec.rb
+++ b/spec/lib/gitlab/usage_data_spec.rb
@@ -14,7 +14,7 @@ describe Gitlab::UsageData, :aggregate_failures do
let!(:ud) { build(:usage_data) }
before do
- allow(Gitlab::GrafanaEmbedUsageData).to receive(:issue_count).and_return(2)
+ allow(described_class).to receive(:grafana_embed_usage_data).and_return(2)
end
subject { described_class.data }
@@ -220,6 +220,71 @@ describe Gitlab::UsageData, :aggregate_failures do
end
end
+ describe '#grafana_embed_usage_data' do
+ subject { described_class.grafana_embed_usage_data }
+
+ let(:project) { create(:project) }
+ let(:description_with_embed) { "Some comment\n\nhttps://grafana.example.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
+ let(:description_with_unintegrated_embed) { "Some comment\n\nhttps://grafana.exp.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
+ let(:description_with_non_grafana_inline_metric) { "Some comment\n\n#{Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url(*['foo', 'bar', 12])}" }
+
+ shared_examples "zero count" do
+ it "does not count the issue" do
+ expect(subject).to eq(0)
+ end
+ end
+
+ context 'with project grafana integration enabled' do
+ before do
+ create(:grafana_integration, project: project, enabled: true)
+ end
+
+ context 'with valid and invalid embeds' do
+ before do
+ # Valid
+ create(:issue, project: project, description: description_with_embed)
+ create(:issue, project: project, description: description_with_embed)
+ # In-Valid
+ create(:issue, project: project, description: description_with_unintegrated_embed)
+ create(:issue, project: project, description: description_with_non_grafana_inline_metric)
+ create(:issue, project: project, description: nil)
+ create(:issue, project: project, description: '')
+ create(:issue, project: project)
+ end
+
+ it 'counts only the issues with embeds' do
+ expect(subject).to eq(2)
+ end
+ end
+ end
+
+ context 'with project grafana integration disabled' do
+ before do
+ create(:grafana_integration, project: project, enabled: false)
+ end
+
+ context 'with one issue having a grafana link in the description and one without' do
+ before do
+ create(:issue, project: project, description: description_with_embed)
+ create(:issue, project: project)
+ end
+
+ it_behaves_like('zero count')
+ end
+ end
+
+ context 'with an un-integrated project' do
+ context 'with one issue having a grafana link in the description and one without' do
+ before do
+ create(:issue, project: project, description: description_with_embed)
+ create(:issue, project: project)
+ end
+
+ it_behaves_like('zero count')
+ end
+ end
+ end
+
describe '#count' do
let(:relation) { double(:relation) }
diff --git a/spec/lib/quality/helm3_client_spec.rb b/spec/lib/quality/helm3_client_spec.rb
new file mode 100644
index 00000000000..1144ee9369d
--- /dev/null
+++ b/spec/lib/quality/helm3_client_spec.rb
@@ -0,0 +1,133 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+
+RSpec.describe Quality::Helm3Client do
+ let(:namespace) { 'review-apps-ee' }
+ let(:release_name) { 'my-release' }
+ let(:raw_helm_list_page1) do
+ <<~OUTPUT
+ [
+ {"name":"review-qa-60-reor-1mugd1","namespace":"#{namespace}","revision":1,"updated":"2020-04-03 17:27:10.245952 +0800 +08","status":"failed","chart":"gitlab-1.1.3","app_version":"12.9.2"},
+ {"name":"review-7846-fix-s-261vd6","namespace":"#{namespace}","revision":2,"updated":"2020-04-02 17:27:12.245952 +0800 +08","status":"deployed","chart":"gitlab-1.1.3","app_version":"12.9.2"},
+ {"name":"review-7867-snowp-lzo3iy","namespace":"#{namespace}","revision":1,"updated":"2020-04-02 15:27:12.245952 +0800 +08","status":"deployed","chart":"gitlab-1.1.3","app_version":"12.9.1"},
+ {"name":"review-6709-group-2pzeec","namespace":"#{namespace}","revision":2,"updated":"2020-04-01 21:27:12.245952 +0800 +08","status":"failed","chart":"gitlab-1.1.3","app_version":"12.9.1"}
+ ]
+ OUTPUT
+ end
+ let(:raw_helm_list_page2) do
+ <<~OUTPUT
+ [
+ {"name":"review-6709-group-t40qbv","namespace":"#{namespace}","revision":2,"updated":"2020-04-01 11:27:12.245952 +0800 +08","status":"deployed","chart":"gitlab-1.1.3","app_version":"12.9.1"}
+ ]
+ OUTPUT
+ end
+ let(:raw_helm_list_empty) do
+ <<~OUTPUT
+ []
+ OUTPUT
+ end
+
+ subject { described_class.new(namespace: namespace) }
+
+ describe '#releases' do
+ it 'raises an error if the Helm command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.releases.to_a }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls helm list with default arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ subject.releases.to_a
+ end
+
+ it 'calls helm list with extra arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json --deployed)])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ subject.releases(args: ['--deployed']).to_a
+ end
+
+ it 'returns a list of Release objects' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json --deployed)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true)))
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_empty, '', double(success?: true)))
+
+ releases = subject.releases(args: ['--deployed']).to_a
+
+ expect(releases.size).to eq(1)
+ expect(releases[0]).to have_attributes(
+ name: 'review-6709-group-t40qbv',
+ revision: 2,
+ last_update: Time.parse('2020-04-01 11:27:12.245952 +0800 +08'),
+ status: 'deployed',
+ chart: 'gitlab-1.1.3',
+ app_version: '12.9.1',
+ namespace: namespace
+ )
+ end
+
+ it 'automatically paginates releases' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 0 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page1, '', double(success?: true)))
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 256 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_page2, '', double(success?: true)))
+ expect(Gitlab::Popen).to receive(:popen_with_detail).ordered
+ .with([%(helm list --namespace "#{namespace}" --max 256 --offset 512 --output json)])
+ .and_return(Gitlab::Popen::Result.new([], raw_helm_list_empty, '', double(success?: true)))
+ releases = subject.releases.to_a
+
+ expect(releases.size).to eq(5)
+ expect(releases.last.name).to eq('review-6709-group-t40qbv')
+ end
+ end
+
+ describe '#delete' do
+ it 'raises an error if the Helm command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls helm uninstall with default arguments' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(subject.delete(release_name: release_name)).to eq('')
+ end
+
+ context 'with multiple release names' do
+ let(:release_name) { %w[my-release my-release-2] }
+
+ it 'raises an error if the Helm command fails' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name.join(' ')})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: false)))
+
+ expect { subject.delete(release_name: release_name) }.to raise_error(described_class::CommandFailedError)
+ end
+
+ it 'calls helm uninstall with multiple release names' do
+ expect(Gitlab::Popen).to receive(:popen_with_detail)
+ .with([%(helm uninstall --namespace "#{namespace}" #{release_name.join(' ')})])
+ .and_return(Gitlab::Popen::Result.new([], '', '', double(success?: true)))
+
+ expect(subject.delete(release_name: release_name)).to eq('')
+ end
+ end
+ end
+end