diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 00:09:30 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 00:09:30 +0000 |
commit | 060c842402c00f830a810702600cbe39dfa6cf62 (patch) | |
tree | 743bd65ac0c1d4d6518ae8cdd4af5718ec7fb890 /spec | |
parent | 6867eff1f997a881cd3ea64109f7ba2d4b42fde4 (diff) | |
download | gitlab-ce-060c842402c00f830a810702600cbe39dfa6cf62.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/environments.rb | 8 | ||||
-rw-r--r-- | spec/factories/users_statistics.rb | 6 | ||||
-rw-r--r-- | spec/features/markdown/metrics_spec.rb | 30 | ||||
-rw-r--r-- | spec/finders/environments_finder_spec.rb | 63 | ||||
-rw-r--r-- | spec/graphql/resolvers/environments_resolver_spec.rb | 18 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb | 11 | ||||
-rw-r--r-- | spec/models/users_statistics_spec.rb | 43 | ||||
-rw-r--r-- | spec/services/metrics/dashboard/transient_embed_service_spec.rb | 72 | ||||
-rw-r--r-- | spec/workers/users/create_statistics_worker_spec.rb | 33 |
9 files changed, 274 insertions, 10 deletions
diff --git a/spec/factories/environments.rb b/spec/factories/environments.rb index 998672ebe7c..050cb8f8e6c 100644 --- a/spec/factories/environments.rb +++ b/spec/factories/environments.rb @@ -7,6 +7,14 @@ FactoryBot.define do association :project, :repository sequence(:external_url) { |n| "https://env#{n}.example.gitlab.com" } + trait :available do + state { :available } + end + + trait :stopped do + state { :stopped } + end + trait :with_review_app do |environment| transient do ref { 'master' } diff --git a/spec/factories/users_statistics.rb b/spec/factories/users_statistics.rb new file mode 100644 index 00000000000..5b0871f2262 --- /dev/null +++ b/spec/factories/users_statistics.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :users_statistics do + end +end diff --git a/spec/features/markdown/metrics_spec.rb b/spec/features/markdown/metrics_spec.rb index 0d8858a7afd..dadb9571c54 100644 --- a/spec/features/markdown/metrics_spec.rb +++ b/spec/features/markdown/metrics_spec.rb @@ -136,6 +136,36 @@ describe 'Metrics rendering', :js, :use_clean_rails_memory_store_caching, :sidek end end + context 'transient metrics embeds' do + let(:metrics_url) { urls.metrics_project_environment_url(project, environment, embed_json: embed_json) } + let(:title) { 'Important Metrics' } + let(:embed_json) do + { + panel_groups: [{ + panels: [{ + type: "line-graph", + title: title, + y_label: "metric", + metrics: [{ + query_range: "metric * 0.5 < 1" + }] + }] + }] + }.to_json + end + + before do + stub_any_prometheus_request_with_response + end + + it 'shows embedded metrics' do + visit project_issue_path(project, issue) + + expect(page).to have_css('div.prometheus-graph') + expect(page).to have_text(title) + end + end + def import_common_metrics ::Gitlab::DatabaseImporters::CommonMetrics::Importer.new.execute end diff --git a/spec/finders/environments_finder_spec.rb b/spec/finders/environments_finder_spec.rb index 850d160506c..66e404f5236 100644 --- a/spec/finders/environments_finder_spec.rb +++ b/spec/finders/environments_finder_spec.rb @@ -3,15 +3,15 @@ require 'spec_helper' describe EnvironmentsFinder do - describe '#execute' do - let(:project) { create(:project, :repository) } - let(:user) { project.creator } - let(:environment) { create(:environment, project: project) } + let(:project) { create(:project, :repository) } + let(:user) { project.creator } + let(:environment) { create(:environment, :available, project: project) } - before do - project.add_maintainer(user) - end + before do + project.add_maintainer(user) + end + describe '#execute' do context 'tagged deployment' do let(:environment_two) { create(:environment, project: project) } # Environments need to include commits, so rewind two commits to fit @@ -124,4 +124,53 @@ describe EnvironmentsFinder do end end end + + describe '#find' do + context 'with states parameter' do + let(:stopped_environment) { create(:environment, :stopped, project: project) } + + it 'returns environments with the requested state' do + result = described_class.new(project, user, states: 'available').find + + expect(result).to contain_exactly(environment) + end + + it 'returns environments with any of the requested states' do + result = described_class.new(project, user, states: %w(available stopped)).find + + expect(result).to contain_exactly(environment, stopped_environment) + end + + it 'raises exception when requested state is invalid' do + expect { described_class.new(project, user, states: %w(invalid stopped)).find }.to( + raise_error(described_class::InvalidStatesError, 'Requested states are invalid') + ) + end + + context 'works with symbols' do + it 'returns environments with the requested state' do + result = described_class.new(project, user, states: :available).find + + expect(result).to contain_exactly(environment) + end + + it 'returns environments with any of the requested states' do + result = described_class.new(project, user, states: [:available, :stopped]).find + + expect(result).to contain_exactly(environment, stopped_environment) + end + end + end + + context 'with search and states' do + let(:environment2) { create(:environment, :stopped, name: 'test2', project: project) } + let(:environment3) { create(:environment, :available, name: 'test3', project: project) } + + it 'searches environments by name and state' do + result = described_class.new(project, user, search: 'test', states: :available).find + + expect(result).to contain_exactly(environment3) + end + end + end end diff --git a/spec/graphql/resolvers/environments_resolver_spec.rb b/spec/graphql/resolvers/environments_resolver_spec.rb index 35d53c29760..75fd7aff39c 100644 --- a/spec/graphql/resolvers/environments_resolver_spec.rb +++ b/spec/graphql/resolvers/environments_resolver_spec.rb @@ -10,9 +10,9 @@ describe Resolvers::EnvironmentsResolver do context "with a group" do let(:group) { create(:group) } let(:project) { create(:project, :public, group: group) } - let!(:environment1) { create(:environment, name: 'production', project: project) } - let!(:environment2) { create(:environment, name: 'test', project: project) } - let!(:environment3) { create(:environment, name: 'test2', project: project) } + let!(:environment1) { create(:environment, :available, name: 'production', project: project) } + let!(:environment2) { create(:environment, :stopped, name: 'test', project: project) } + let!(:environment3) { create(:environment, :available, name: 'test2', project: project) } before do group.add_developer(current_user) @@ -41,6 +41,18 @@ describe Resolvers::EnvironmentsResolver do end end + context 'with states' do + it 'searches environments by state' do + expect(resolve_environments(states: ['available'])).to contain_exactly(environment1, environment3) + end + + it 'returns error if requested state is invalid' do + expect { resolve_environments(states: ['invalid']) }.to( + raise_error(Gitlab::Graphql::Errors::ArgumentError) + ) + end + end + context 'when project is nil' do subject { resolve(described_class, obj: nil, args: {}, ctx: { current_user: current_user }) } diff --git a/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb b/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb index 387baf1ee53..245c98cdd00 100644 --- a/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/service_selector_spec.rb @@ -98,6 +98,17 @@ describe Gitlab::Metrics::Dashboard::ServiceSelector do it { is_expected.to be Metrics::Dashboard::GrafanaMetricEmbedService } end + + context 'with the embed defined in the arguments' do + let(:arguments) do + { + embedded: true, + embed_json: '{}' + } + end + + it { is_expected.to be Metrics::Dashboard::TransientEmbedService } + end end end end diff --git a/spec/models/users_statistics_spec.rb b/spec/models/users_statistics_spec.rb new file mode 100644 index 00000000000..fc23bed711f --- /dev/null +++ b/spec/models/users_statistics_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe UsersStatistics do + describe '.create_current_stats!' do + before do + create_list(:user_highest_role, 4) + create_list(:user_highest_role, 2, :guest) + create_list(:user_highest_role, 3, :reporter) + create_list(:user_highest_role, 4, :developer) + create_list(:user_highest_role, 3, :maintainer) + create_list(:user_highest_role, 2, :owner) + create_list(:user, 2, :bot) + create_list(:user, 1, :blocked) + + allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) + end + + context 'when successful' do + it 'creates an entry with the current statistics values' do + expect(described_class.create_current_stats!).to have_attributes( + without_groups_and_projects: 4, + with_highest_role_guest: 2, + with_highest_role_reporter: 3, + with_highest_role_developer: 4, + with_highest_role_maintainer: 3, + with_highest_role_owner: 2, + bots: 2, + blocked: 1 + ) + end + end + + context 'when unsuccessful' do + it 'raises an ActiveRecord::RecordInvalid exception' do + allow(UsersStatistics).to receive(:create!).and_raise(ActiveRecord::RecordInvalid) + + expect { described_class.create_current_stats! }.to raise_error(ActiveRecord::RecordInvalid) + end + end + end +end diff --git a/spec/services/metrics/dashboard/transient_embed_service_spec.rb b/spec/services/metrics/dashboard/transient_embed_service_spec.rb new file mode 100644 index 00000000000..fddfbe15281 --- /dev/null +++ b/spec/services/metrics/dashboard/transient_embed_service_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::Dashboard::TransientEmbedService, :use_clean_rails_memory_store_caching do + let_it_be(:project) { build(:project) } + let_it_be(:user) { create(:user) } + let_it_be(:environment) { create(:environment, project: project) } + + before do + project.add_maintainer(user) + end + + describe '.valid_params?' do + let(:params) { { embedded: 'true', embed_json: '{}' } } + + subject { described_class.valid_params?(params) } + + it { is_expected.to be_truthy } + + context 'missing embedded' do + let(:params) { { embed_json: '{}' } } + + it { is_expected.to be_falsey } + end + + context 'not embedded' do + let(:params) { { embedded: 'false', embed_json: '{}' } } + + it { is_expected.to be_falsey } + end + + context 'missing embed_json' do + let(:params) { { embedded: 'true' } } + + it { is_expected.to be_falsey } + end + end + + describe '#get_dashboard' do + let(:embed_json) do + { + panel_groups: [{ + panels: [{ + type: 'line-graph', + title: 'title', + y_label: 'y_label', + metrics: [{ + query_range: 'up', + label: 'y_label' + }] + }] + }] + }.to_json + end + let(:service_params) { [project, user, { environment: environment, embedded: 'true', embed_json: embed_json }] } + let(:service_call) { described_class.new(*service_params).get_dashboard } + + it_behaves_like 'valid embedded dashboard service response' + it_behaves_like 'raises error for users with insufficient permissions' + + it 'caches the unprocessed dashboard for subsequent calls' do + expect_any_instance_of(described_class) + .to receive(:get_raw_dashboard) + .once + .and_call_original + + described_class.new(*service_params).get_dashboard + described_class.new(*service_params).get_dashboard + end + end +end diff --git a/spec/workers/users/create_statistics_worker_spec.rb b/spec/workers/users/create_statistics_worker_spec.rb new file mode 100644 index 00000000000..3b2b72a832d --- /dev/null +++ b/spec/workers/users/create_statistics_worker_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Users::CreateStatisticsWorker do + describe '#perform' do + subject { described_class.new.perform } + + before do + allow(ActiveRecord::Base.connection).to receive(:transaction_open?).and_return(false) + end + + context 'when successful' do + it 'create an users statistics entry' do + expect { subject }.to change { UsersStatistics.count }.from(0).to(1) + end + end + + context 'when unsuccessful' do + it 'logs an error' do + users_statistics = build(:users_statistics) + users_statistics.errors.add(:base, 'This is an error') + exception = ActiveRecord::RecordInvalid.new(users_statistics) + + allow(UsersStatistics).to receive(:create_current_stats!).and_raise(exception) + + expect(Gitlab::ErrorTracking).to receive(:track_exception).with(exception).and_call_original + + subject + end + end + end +end |