diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-16 21:08:00 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-16 21:08:00 +0000 |
commit | 69d6d3ca2013e97cfd2d89449669ea7bf475f4e9 (patch) | |
tree | 2cc4227ebfc52b7603691f06b0b8e09e030e8428 /spec | |
parent | 01fdcf49b1553c22ae116fe96cedd7b91d02225c (diff) | |
download | gitlab-ce-69d6d3ca2013e97cfd2d89449669ea7bf475f4e9.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
10 files changed, 557 insertions, 20 deletions
diff --git a/spec/controllers/projects/environments/sample_metrics_controller_spec.rb b/spec/controllers/projects/environments/sample_metrics_controller_spec.rb new file mode 100644 index 00000000000..4faa3ecb567 --- /dev/null +++ b/spec/controllers/projects/environments/sample_metrics_controller_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Projects::Environments::SampleMetricsController do + include StubENV + + let_it_be(:project) { create(:project) } + let_it_be(:environment) { create(:environment, project: project) } + let_it_be(:user) { create(:user) } + + before(:context) do + RSpec::Mocks.with_temporary_scope do + stub_env('USE_SAMPLE_METRICS', 'true') + Rails.application.reload_routes! + end + end + + after(:context) do + Rails.application.reload_routes! + end + + before do + project.add_reporter(user) + sign_in(user) + end + + describe 'GET #query' do + context 'when the file is not found' do + before do + get :query, params: environment_params + end + + it 'returns a 404' do + expect(response).to have_gitlab_http_status(:not_found) + end + end + + context 'when the sample data is found' do + before do + allow_next_instance_of(Metrics::SampleMetricsService) do |service| + allow(service).to receive(:query).and_return([]) + end + get :query, params: environment_params + end + + it 'returns JSON with a message and a 200 status code' do + expect(json_response.keys).to contain_exactly('status', 'data') + expect(response).to have_gitlab_http_status(:ok) + end + end + end + + private + + def environment_params(params = {}) + { + id: environment.id.to_s, + namespace_id: project.namespace.full_path, + project_id: project.name, + identifier: 'sample_metric_query_result' + }.merge(params) + end +end diff --git a/spec/fixtures/gitlab/sample_metrics/sample_metric_query_result.yml b/spec/fixtures/gitlab/sample_metrics/sample_metric_query_result.yml new file mode 100644 index 00000000000..ba074912b3b --- /dev/null +++ b/spec/fixtures/gitlab/sample_metrics/sample_metric_query_result.yml @@ -0,0 +1,151 @@ +--- +- metric: {} + values: + - - 1573560714.209 + - '0.02361297607421875' + - - 1573560774.209 + - '0.02361297607421875' + - - 1573560834.209 + - '0.02362823486328125' + - - 1573560894.209 + - '0.02361297607421875' + - - 1573560954.209 + - '0.02385711669921875' + - - 1573561014.209 + - '0.02361297607421875' + - - 1573561074.209 + - '0.02361297607421875' + - - 1573561134.209 + - '0.02362060546875' + - - 1573561194.209 + - '0.02362060546875' + - - 1573561254.209 + - '0.02362060546875' + - - 1573561314.209 + - '0.02362060546875' + - - 1573561374.209 + - '0.023624420166015625' + - - 1573561434.209 + - '0.023651123046875' + - - 1573561494.209 + - '0.02362060546875' + - - 1573561554.209 + - '0.0236358642578125' + - - 1573561614.209 + - '0.02362060546875' + - - 1573561674.209 + - '0.02362060546875' + - - 1573561734.209 + - '0.02362060546875' + - - 1573561794.209 + - '0.02362060546875' + - - 1573561854.209 + - '0.02362060546875' + - - 1573561914.209 + - '0.023651123046875' + - - 1573561974.209 + - '0.02362060546875' + - - 1573562034.209 + - '0.02362060546875' + - - 1573562094.209 + - '0.02362060546875' + - - 1573562154.209 + - '0.02362060546875' + - - 1573562214.209 + - '0.023624420166015625' + - - 1573562274.209 + - '0.02362060546875' + - - 1573562334.209 + - '0.023868560791015625' + - - 1573562394.209 + - '0.02374267578125' + - - 1573562454.209 + - '0.02362060546875' + - - 1573562514.209 + - '0.02362060546875' + - - 1573562574.209 + - '0.02362060546875' + - - 1573562634.209 + - '0.02362060546875' + - - 1573562694.209 + - '0.023639678955078125' + - - 1573562754.209 + - '0.0236358642578125' + - - 1573562814.209 + - '0.02362060546875' + - - 1573562874.209 + - '0.0236358642578125' + - - 1573562934.209 + - '0.023651123046875' + - - 1573562994.209 + - '0.02362060546875' + - - 1573563054.209 + - '0.023624420166015625' + - - 1573563114.209 + - '0.02362060546875' + - - 1573563174.209 + - '0.02362060546875' + - - 1573563234.209 + - '0.02362060546875' + - - 1573563294.209 + - '0.02362060546875' + - - 1573563354.209 + - '0.02362060546875' + - - 1573563414.209 + - '0.023651123046875' + - - 1573563474.209 + - '0.023651123046875' + - - 1573563534.209 + - '0.023651123046875' + - - 1573563594.209 + - '0.023773193359375' + - - 1573563654.209 + - '0.023681640625' + - - 1573563714.209 + - '0.023895263671875' + - - 1573563774.209 + - '0.023651123046875' + - - 1573563834.209 + - '0.023651123046875' + - - 1573563894.209 + - '0.023651123046875' + - - 1573563954.209 + - '0.0236663818359375' + - - 1573564014.209 + - '0.023651123046875' + - - 1573564074.209 + - '0.023681640625' + - - 1573564134.209 + - '0.0236663818359375' + - - 1573564194.209 + - '0.0236663818359375' + - - 1573564254.209 + - '0.023651123046875' + - - 1573564314.209 + - '0.023651123046875' + - - 1573564374.209 + - '0.023651123046875' + - - 1573564434.209 + - '0.023773193359375' + - - 1573564494.209 + - '0.023651123046875' + - - 1573564554.209 + - '0.023681640625' + - - 1573564614.209 + - '0.023773193359375' + - - 1573564674.209 + - '0.023651123046875' + - - 1573564734.209 + - '0.023651123046875' + - - 1573564794.209 + - '0.023651123046875' + - - 1573564854.209 + - '0.023651123046875' + - - 1573564914.209 + - '0.023651123046875' + - - 1573564974.209 + - '0.023651123046875' + - - 1573565034.209 + - '0.023651123046875' + - - 1573565094.209 + - '0.023895263671875'
\ No newline at end of file diff --git a/spec/frontend/create_cluster/init_create_cluster_spec.js b/spec/frontend/create_cluster/init_create_cluster_spec.js index e7b9a7adde4..1fdcb57492d 100644 --- a/spec/frontend/create_cluster/init_create_cluster_spec.js +++ b/spec/frontend/create_cluster/init_create_cluster_spec.js @@ -3,6 +3,11 @@ import initGkeDropdowns from '~/create_cluster/gke_cluster'; import initGkeNamespace from '~/create_cluster/gke_cluster_namespace'; import PersistentUserCallout from '~/persistent_user_callout'; +// This import is loaded dynamically in `init_create_cluster`. +// Let's eager import it here so that the first spec doesn't timeout. +// https://gitlab.com/gitlab-org/gitlab/issues/118499 +import '~/create_cluster/eks_cluster'; + jest.mock('~/create_cluster/gke_cluster', () => jest.fn()); jest.mock('~/create_cluster/gke_cluster_namespace', () => jest.fn()); jest.mock('~/persistent_user_callout', () => ({ @@ -20,10 +25,9 @@ describe('initCreateCluster', () => { }; gon = { features: {} }; }); + afterEach(() => { - initGkeDropdowns.mockReset(); - initGkeNamespace.mockReset(); - PersistentUserCallout.factory.mockReset(); + jest.clearAllMocks(); }); describe.each` diff --git a/spec/helpers/diff_helper_spec.rb b/spec/helpers/diff_helper_spec.rb index def3078c652..7f988c60817 100644 --- a/spec/helpers/diff_helper_spec.rb +++ b/spec/helpers/diff_helper_spec.rb @@ -259,7 +259,7 @@ describe DiffHelper do end context '#render_overflow_warning?' do - let(:diffs_collection) { instance_double(Gitlab::Diff::FileCollection::MergeRequestDiff, diff_files: diff_files) } + let(:diffs_collection) { instance_double(Gitlab::Diff::FileCollection::MergeRequestDiff, raw_diff_files: diff_files) } let(:diff_files) { Gitlab::Git::DiffCollection.new(files) } let(:safe_file) { { too_large: false, diff: '' } } let(:large_file) { { too_large: true, diff: '' } } diff --git a/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb b/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb index 745b9133529..e2615ea5069 100644 --- a/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb +++ b/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb @@ -55,11 +55,29 @@ describe Banzai::Filter::InlineMetricsRedactorFilter do it_behaves_like 'a supported metrics dashboard url' end - context 'for an internal non-dashboard url' do - let(:url) { urls.project_url(project) } + context 'the user has requisite permissions' do + let(:user) { create(:user) } + let(:doc) { filter(input, current_user: user) } - it 'leaves the placeholder' do - expect(doc.to_s).to be_empty + before do + project.add_maintainer(user) + end + + context 'for an internal non-dashboard url' do + let(:url) { urls.project_url(project) } + + it 'leaves the placeholder' do + expect(doc.to_s).to be_empty + end + end + + context 'with over 100 embeds' do + let(:embed) { %(<div class="js-render-metrics" data-dashboard-url="#{url}"></div>) } + let(:input) { embed * 150 } + + it 'redacts ill-advised embeds' do + expect(doc.to_s.length).to eq(embed.length * 100) + end end end end diff --git a/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb b/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb new file mode 100644 index 00000000000..064efebdb96 --- /dev/null +++ b/spec/lib/gitlab/kubernetes/helm/patch_command_spec.rb @@ -0,0 +1,218 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Kubernetes::Helm::PatchCommand do + let(:files) { { 'ca.pem': 'some file content' } } + let(:repository) { 'https://repository.example.com' } + let(:rbac) { false } + let(:version) { '1.2.3' } + + subject(:patch_command) do + described_class.new( + name: 'app-name', + chart: 'chart-name', + rbac: rbac, + files: files, + version: version, + repository: repository + ) + end + + context 'when local tiller feature is disabled' do + before do + stub_feature_flags(managed_apps_local_tiller: false) + end + + let(:tls_flags) do + <<~EOS.squish + --tls + --tls-ca-cert /data/helm/app-name/config/ca.pem + --tls-cert /data/helm/app-name/config/cert.pem + --tls-key /data/helm/app-name/config/key.pem + EOS + end + + it_behaves_like 'helm commands' do + let(:commands) do + <<~EOS + helm init --upgrade + for i in $(seq 1 30); do helm version #{tls_flags} && s=0 && break || s=$?; sleep 1s; echo \"Retrying ($i)...\"; done; (exit $s) + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_comand} + EOS + end + + let(:helm_upgrade_comand) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + #{tls_flags} + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + it_behaves_like 'helm commands' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_comand} + EOS + end + + let(:helm_upgrade_comand) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + + context 'when rbac is true' do + let(:rbac) { true } + + it_behaves_like 'helm commands' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_command} + EOS + end + + let(:helm_upgrade_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + context 'when there is no ca.pem file' do + let(:files) { { 'file.txt': 'some content' } } + + it_behaves_like 'helm commands' do + let(:commands) do + <<~EOS + export HELM_HOST="localhost:44134" + tiller -listen ${HELM_HOST} -alsologtostderr & + helm init --client-only + helm repo add app-name https://repository.example.com + helm repo update + #{helm_upgrade_command} + EOS + end + + let(:helm_upgrade_command) do + <<~EOS.squish + helm upgrade app-name chart-name + --reuse-values + --version 1.2.3 + --namespace gitlab-managed-apps + -f /data/helm/app-name/config/values.yaml + EOS + end + end + end + + describe '#pod_name' do + subject { patch_command.pod_name } + + it { is_expected.to eq 'install-app-name' } + end + + context 'when there is no version' do + let(:version) { nil } + + it { expect { patch_command }.to raise_error(ArgumentError, 'version is required') } + end + + describe '#rbac?' do + subject { patch_command.rbac? } + + context 'rbac is enabled' do + let(:rbac) { true } + + it { is_expected.to be_truthy } + end + + context 'rbac is not enabled' do + let(:rbac) { false } + + it { is_expected.to be_falsey } + end + end + + describe '#pod_resource' do + subject { patch_command.pod_resource } + + context 'rbac is enabled' do + let(:rbac) { true } + + it 'generates a pod that uses the tiller serviceAccountName' do + expect(subject.spec.serviceAccountName).to eq('tiller') + end + end + + context 'rbac is not enabled' do + let(:rbac) { false } + + it 'generates a pod that uses the default serviceAccountName' do + expect(subject.spec.serviceAcccountName).to be_nil + end + end + end + + describe '#config_map_resource' do + let(:metadata) do + { + name: "values-content-configuration-app-name", + namespace: 'gitlab-managed-apps', + labels: { name: "values-content-configuration-app-name" } + } + end + + let(:resource) { ::Kubeclient::Resource.new(metadata: metadata, data: files) } + + subject { patch_command.config_map_resource } + + it 'returns a KubeClient resource with config map content for the application' do + is_expected.to eq(resource) + end + end + + describe '#service_account_resource' do + subject { patch_command.service_account_resource } + + it 'returns nothing' do + is_expected.to be_nil + end + end + + describe '#cluster_role_binding_resource' do + subject { patch_command.cluster_role_binding_resource } + + it 'returns nothing' do + is_expected.to be_nil + end + end +end diff --git a/spec/models/clusters/applications/prometheus_spec.rb b/spec/models/clusters/applications/prometheus_spec.rb index ce4d817b1d7..d588ce3bc38 100644 --- a/spec/models/clusters/applications/prometheus_spec.rb +++ b/spec/models/clusters/applications/prometheus_spec.rb @@ -206,21 +206,19 @@ describe Clusters::Applications::Prometheus do end end - describe '#upgrade_command' do + describe '#patch_command' do + subject(:patch_command) { prometheus.patch_command(values) } + let(:prometheus) { build(:clusters_applications_prometheus) } let(:values) { prometheus.values } - it 'returns an instance of Gitlab::Kubernetes::Helm::InstallCommand' do - expect(prometheus.upgrade_command(values)).to be_an_instance_of(::Gitlab::Kubernetes::Helm::InstallCommand) - end + it { is_expected.to be_an_instance_of(::Gitlab::Kubernetes::Helm::PatchCommand) } it 'is initialized with 3 arguments' do - command = prometheus.upgrade_command(values) - - expect(command.name).to eq('prometheus') - expect(command.chart).to eq('stable/prometheus') - expect(command.version).to eq('6.7.3') - expect(command.files).to eq(prometheus.files) + expect(patch_command.name).to eq('prometheus') + expect(patch_command.chart).to eq('stable/prometheus') + expect(patch_command.version).to eq('6.7.3') + expect(patch_command.files).to eq(prometheus.files) end end diff --git a/spec/serializers/diffs_metadata_entity_spec.rb b/spec/serializers/diffs_metadata_entity_spec.rb index aaca393ec27..0fa643d37b3 100644 --- a/spec/serializers/diffs_metadata_entity_spec.rb +++ b/spec/serializers/diffs_metadata_entity_spec.rb @@ -36,8 +36,16 @@ describe DiffsMetadataEntity do describe 'diff_files' do it 'returns diff files metadata' do - payload = - DiffFileMetadataEntity.represent(merge_request_diff.diffs.diff_files).as_json + raw_diff_files = merge_request_diff.diffs.raw_diff_files + + expect_next_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiff) do |instance| + # Use lightweight version instead. Several methods delegate to it, so putting a 5 + # calls limit. + expect(instance).to receive(:raw_diff_files).at_most(5).times.and_call_original + expect(instance).not_to receive(:diff_files) + end + + payload = DiffFileMetadataEntity.represent(raw_diff_files).as_json expect(subject[:diff_files]).to eq(payload) end diff --git a/spec/services/metrics/sample_metrics_service_spec.rb b/spec/services/metrics/sample_metrics_service_spec.rb new file mode 100644 index 00000000000..8574674ebc4 --- /dev/null +++ b/spec/services/metrics/sample_metrics_service_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Metrics::SampleMetricsService do + describe 'query' do + subject { described_class.new(identifier).query } + + context 'when the file is not found' do + let(:identifier) { nil } + + it { is_expected.to be_nil } + end + + context 'when the file is found' do + let(:identifier) { 'sample_metric_query_result' } + let(:source) { File.join(Rails.root, 'spec/fixtures/gitlab/sample_metrics', "#{identifier}.yml") } + let(:destination) { File.join(Rails.root, Metrics::SampleMetricsService::DIRECTORY, "#{identifier}.yml") } + + around do |example| + FileUtils.mkdir_p(Metrics::SampleMetricsService::DIRECTORY) + FileUtils.cp(source, destination) + + example.run + ensure + FileUtils.rm(destination) + end + + subject { described_class.new(identifier).query } + + it 'loads data from the sample file correctly' do + expect(subject).to eq(YAML.load_file(source)) + end + end + + context 'when the identifier is for a path outside of sample_metrics' do + let(:identifier) { '../config/secrets' } + + it { is_expected.to be_nil } + end + end +end diff --git a/spec/tasks/gitlab/generate_sample_prometheus_data_spec.rb b/spec/tasks/gitlab/generate_sample_prometheus_data_spec.rb new file mode 100644 index 00000000000..72e61f5c524 --- /dev/null +++ b/spec/tasks/gitlab/generate_sample_prometheus_data_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'rake_helper' + +describe 'gitlab:generate_sample_prometheus_data rake task' do + let(:cluster) { create(:cluster, :provided_by_user, :project) } + let(:environment) { create(:environment, project: cluster.project) } + let(:sample_query_file) { File.join(Rails.root, Metrics::SampleMetricsService::DIRECTORY, 'test_query_result.yml') } + let!(:metric) { create(:prometheus_metric, project: cluster.project, identifier: 'test_query_result') } + + around do |example| + example.run + ensure + FileUtils.rm(sample_query_file) + end + + it 'creates the file correctly' do + Rake.application.rake_require 'tasks/gitlab/generate_sample_prometheus_data' + allow(Environment).to receive(:find).and_return(environment) + allow(environment).to receive_message_chain(:prometheus_adapter, :prometheus_client, :query_range) { sample_query_result } + run_rake_task('gitlab:generate_sample_prometheus_data', [environment.id]) + + expect(File.exist?(sample_query_file)).to be true + + query_file_content = YAML.load_file(sample_query_file) + + expect(query_file_content).to eq(sample_query_result) + end +end + +def sample_query_result + file = File.join(Rails.root, 'spec/fixtures/gitlab/sample_metrics', 'sample_metric_query_result.yml') + YAML.load_file(File.expand_path(file, __dir__)) +end |