diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 04:45:44 +0300 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /spec/lib/gitlab/utils | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'spec/lib/gitlab/utils')
-rw-r--r-- | spec/lib/gitlab/utils/gzip_spec.rb | 58 | ||||
-rw-r--r-- | spec/lib/gitlab/utils/markdown_spec.rb | 32 | ||||
-rw-r--r-- | spec/lib/gitlab/utils/usage_data_spec.rb | 149 |
3 files changed, 229 insertions, 10 deletions
diff --git a/spec/lib/gitlab/utils/gzip_spec.rb b/spec/lib/gitlab/utils/gzip_spec.rb new file mode 100644 index 00000000000..5d1c62e03d3 --- /dev/null +++ b/spec/lib/gitlab/utils/gzip_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +RSpec.describe Gitlab::Utils::Gzip do + before do + example_class = Class.new do + include Gitlab::Utils::Gzip + + def lorem_ipsum + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "\ + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "\ + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "\ + "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "\ + "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat "\ + "cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id "\ + "est laborum." + end + end + + stub_const('ExampleClass', example_class) + end + + subject { ExampleClass.new } + + let(:sample_string) { subject.lorem_ipsum } + let(:compressed_string) { subject.gzip_compress(sample_string) } + + describe "#gzip_compress" do + it "compresses data passed to it" do + expect(compressed_string.length).to be < sample_string.length + end + + it "returns uncompressed data when encountering Zlib::GzipFile::Error" do + expect(ActiveSupport::Gzip).to receive(:compress).and_raise(Zlib::GzipFile::Error) + + expect(compressed_string.length).to eq sample_string.length + end + end + + describe "#gzip_decompress" do + let(:decompressed_string) { subject.gzip_decompress(compressed_string) } + + it "decompresses encoded data" do + expect(decompressed_string).to eq sample_string + end + + it "returns compressed data when encountering Zlib::GzipFile::Error" do + expect(ActiveSupport::Gzip).to receive(:decompress).and_raise(Zlib::GzipFile::Error) + + expect(decompressed_string).not_to eq sample_string.length + end + + it "returns unmodified data when it is determined to be uncompressed" do + expect(subject.gzip_decompress(sample_string)).to eq sample_string + end + end +end diff --git a/spec/lib/gitlab/utils/markdown_spec.rb b/spec/lib/gitlab/utils/markdown_spec.rb index 001ff5bc487..93d91f7ed90 100644 --- a/spec/lib/gitlab/utils/markdown_spec.rb +++ b/spec/lib/gitlab/utils/markdown_spec.rb @@ -52,6 +52,38 @@ RSpec.describe Gitlab::Utils::Markdown do end end + context 'when string has a product suffix' do + let(:string) { 'My Header (ULTIMATE)' } + + it 'ignores a product suffix' do + is_expected.to eq 'my-header' + end + + context 'with only modifier' do + let(:string) { 'My Header (STARTER ONLY)' } + + it 'ignores a product suffix' do + is_expected.to eq 'my-header' + end + end + + context 'with "*" around a product suffix' do + let(:string) { 'My Header **(STARTER)**' } + + it 'ignores a product suffix' do + is_expected.to eq 'my-header' + end + end + + context 'with "*" around a product suffix and only modifier' do + let(:string) { 'My Header **(STARTER ONLY)**' } + + it 'ignores a product suffix' do + is_expected.to eq 'my-header' + end + end + end + context 'when string is empty' do let(:string) { '' } diff --git a/spec/lib/gitlab/utils/usage_data_spec.rb b/spec/lib/gitlab/utils/usage_data_spec.rb index 4675cbd7fa1..362cbaa78e9 100644 --- a/spec/lib/gitlab/utils/usage_data_spec.rb +++ b/spec/lib/gitlab/utils/usage_data_spec.rb @@ -37,6 +37,28 @@ RSpec.describe Gitlab::Utils::UsageData do end end + describe '#sum' do + let(:relation) { double(:relation) } + + it 'returns the count when counting succeeds' do + allow(Gitlab::Database::BatchCount) + .to receive(:batch_sum) + .with(relation, :column, batch_size: 100, start: 2, finish: 3) + .and_return(1) + + expect(described_class.sum(relation, :column, batch_size: 100, start: 2, finish: 3)).to eq(1) + end + + it 'returns the fallback value when counting fails' do + stub_const("Gitlab::Utils::UsageData::FALLBACK", 15) + allow(Gitlab::Database::BatchCount) + .to receive(:batch_sum) + .and_raise(ActiveRecord::StatementInvalid.new('')) + + expect(described_class.sum(relation, :column)).to eq(15) + end + end + describe '#alt_usage_data' do it 'returns the fallback when it gets an error' do expect(described_class.alt_usage_data { raise StandardError } ).to eq(-1) @@ -76,26 +98,19 @@ RSpec.describe Gitlab::Utils::UsageData do end describe '#with_prometheus_client' do - context 'when Prometheus is enabled' do + shared_examples 'query data from Prometheus' do it 'yields a client instance and returns the block result' do - expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(true) - expect(Gitlab::Prometheus::Internal).to receive(:uri).and_return('http://prom:9090') - result = described_class.with_prometheus_client { |client| client } expect(result).to be_an_instance_of(Gitlab::PrometheusClient) end end - context 'when Prometheus is disabled' do - before do - expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false) - end - + shared_examples 'does not query data from Prometheus' do it 'returns nil by default' do result = described_class.with_prometheus_client { |client| client } - expect(result).to be nil + expect(result).to be_nil end it 'returns fallback if provided' do @@ -104,6 +119,74 @@ RSpec.describe Gitlab::Utils::UsageData do expect(result).to eq([]) end end + + shared_examples 'try to query Prometheus with given address' do + context 'Prometheus is ready' do + before do + stub_request(:get, /\/-\/ready/) + .to_return(status: 200, body: 'Prometheus is Ready.\n') + end + + context 'Prometheus is reachable through HTTPS' do + it_behaves_like 'query data from Prometheus' + end + + context 'Prometheus is not reachable through HTTPS' do + before do + stub_request(:get, /https:\/\/.*/).to_raise(Errno::ECONNREFUSED) + end + + context 'Prometheus is reachable through HTTP' do + it_behaves_like 'query data from Prometheus' + end + + context 'Prometheus is not reachable through HTTP' do + before do + stub_request(:get, /http:\/\/.*/).to_raise(Errno::ECONNREFUSED) + end + + it_behaves_like 'does not query data from Prometheus' + end + end + end + + context 'Prometheus is not ready' do + before do + stub_request(:get, /\/-\/ready/) + .to_return(status: 503, body: 'Service Unavailable') + end + + it_behaves_like 'does not query data from Prometheus' + end + end + + context 'when Prometheus server address is available from settings' do + before do + expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(true) + expect(Gitlab::Prometheus::Internal).to receive(:server_address).and_return('prom:9090') + end + + it_behaves_like 'try to query Prometheus with given address' + end + + context 'when Prometheus server address is available from Consul service discovery' do + before do + expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false) + expect(Gitlab::Consul::Internal).to receive(:api_url).and_return('http://localhost:8500') + expect(Gitlab::Consul::Internal).to receive(:discover_prometheus_server_address).and_return('prom:9090') + end + + it_behaves_like 'try to query Prometheus with given address' + end + + context 'when Prometheus server address is not available' do + before do + expect(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false) + expect(Gitlab::Consul::Internal).to receive(:api_url).and_return(nil) + end + + it_behaves_like 'does not query data from Prometheus' + end end describe '#measure_duration' do @@ -126,4 +209,50 @@ RSpec.describe Gitlab::Utils::UsageData do end end end + + describe '#track_usage_event' do + let(:value) { '9f302fea-f828-4ca9-aef4-e10bd723c0b3' } + let(:event_name) { 'my_event' } + let(:unknown_event) { 'unknown' } + let(:feature) { "usage_data_#{event_name}" } + + context 'with feature enabled' do + before do + stub_feature_flags(feature => true) + end + + it 'tracks redis hll event' do + stub_application_setting(usage_ping_enabled: true) + + expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event).with(value, event_name) + + described_class.track_usage_event(event_name, value) + end + + it 'does not track event when usage ping is not enabled' do + stub_application_setting(usage_ping_enabled: false) + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + described_class.track_usage_event(event_name, value) + end + + it 'raise an error for unknown event' do + stub_application_setting(usage_ping_enabled: true) + + expect { described_class.track_usage_event(unknown_event, value) }.to raise_error(Gitlab::UsageDataCounters::HLLRedisCounter::UnknownEvent) + end + end + + context 'with feature disabled' do + before do + stub_feature_flags(feature => false) + end + + it 'does not track event' do + expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event) + + described_class.track_usage_event(event_name, value) + end + end + end end |