diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-08 15:08:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-08 15:08:54 +0300 |
commit | 473b876fe3d7e0b36eb6268cc44a4fe0d94f4422 (patch) | |
tree | 2f6d5f2ecad53015024b6b1509896f5933c5c3e6 /spec/support | |
parent | dca8df0c90d8727d69b3501b15b481546897f3cd (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/shared_examples/requests/api/ml_model_packages_shared_examples.rb | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/spec/support/shared_examples/requests/api/ml_model_packages_shared_examples.rb b/spec/support/shared_examples/requests/api/ml_model_packages_shared_examples.rb new file mode 100644 index 00000000000..81ff004779a --- /dev/null +++ b/spec/support/shared_examples/requests/api/ml_model_packages_shared_examples.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'Endpoint not found if read_model_registry not available' do + context 'when read_model_registry disabled for current project' do + before do + allow(Ability).to receive(:allowed?).and_call_original + allow(Ability).to receive(:allowed?) + .with(user, :read_model_registry, project) + .and_return(false) + end + + it "is not found" do + is_expected.to have_gitlab_http_status(:not_found) + end + end +end + +RSpec.shared_examples 'creates model experiments package files' do + it 'creates package files', :aggregate_failures do + expect { api_response } + .to change { project.packages.count }.by(1) + .and change { Packages::PackageFile.count }.by(1) + expect(api_response).to have_gitlab_http_status(:created) + + package_file = project.packages.last.package_files.reload.last + expect(package_file.file_name).to eq(file_name) + end + + it 'returns bad request if package creation fails' do + allow_next_instance_of(::Packages::MlModel::CreatePackageFileService) do |instance| + expect(instance).to receive(:execute).and_return(nil) + end + + expect(api_response).to have_gitlab_http_status(:bad_request) + end + + context 'when file is too large' do + it 'is bad request', :aggregate_failures do + allow_next_instance_of(UploadedFile) do |uploaded_file| + allow(uploaded_file).to receive(:size).and_return(project.actual_limits.ml_model_max_file_size + 1) + end + + expect(api_response).to have_gitlab_http_status(:bad_request) + end + end +end + +RSpec.shared_examples 'process ml model package upload' do + context 'with object storage disabled' do + before do + stub_package_file_object_storage(enabled: false) + end + + context 'without a file from workhorse' do + let(:send_rewritten_field) { false } + + it_behaves_like 'returning response status', :bad_request + end + + context 'with correct params' do + it_behaves_like 'package workhorse uploads' + it_behaves_like 'creates model experiments package files' + # To be reactivated with https://gitlab.com/gitlab-org/gitlab/-/issues/414270 + # it_behaves_like 'a package tracking event', '::API::MlModelPackages', 'push_package' + end + end + + context 'with object storage enabled' do + let(:tmp_object) do + fog_connection.directories.new(key: 'packages').files.create( # rubocop:disable Rails/SaveBang + key: "tmp/uploads/#{file_name}", + body: 'content' + ) + end + + let(:fog_file) { fog_to_uploaded_file(tmp_object) } + let(:params) { { file: fog_file, 'file.remote_id' => file_name } } + + context 'and direct upload enabled' do + let(:fog_connection) do + stub_package_file_object_storage(direct_upload: true) + end + + it_behaves_like 'creates model experiments package files' + + ['123123', '../../123123'].each do |remote_id| + context "with invalid remote_id: #{remote_id}" do + let(:params) do + { + file: fog_file, + 'file.remote_id' => remote_id + } + end + + it { is_expected.to have_gitlab_http_status(:forbidden) } + end + end + end + + context 'and direct upload disabled' do + let(:fog_connection) do + stub_package_file_object_storage(direct_upload: false) + end + + it_behaves_like 'creates model experiments package files' + end + end +end |