Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ml_model_packages_shared_examples.rb « api « requests « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 30a1398bf94ed4390d6202125cf75e269a801f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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 'Endpoint not found if write_model_registry not available' do
  context 'when write_model_registry is disabled for current project' do
    before do
      allow(Ability).to receive(:allowed?).and_call_original
      allow(Ability).to receive(:allowed?)
                          .with(user, :write_model_registry, project)
                          .and_return(false)
    end

    it { is_expected.to have_gitlab_http_status(:not_found) }
  end
end

RSpec.shared_examples 'Not found when model version does not exist' do
  context 'when model version does not exist' do
    let(:version) { "#{non_existing_record_id}.0.0" }

    it { is_expected.to have_gitlab_http_status(:not_found) }
  end
end

RSpec.shared_examples 'creates package files for model versions' do
  it 'creates package files', :aggregate_failures do
    expect { api_response }
      .to 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
    expect_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 package files for model versions'
      # 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 package files for model versions'

      ['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 package files for model versions'
    end
  end
end

RSpec.shared_examples 'process ml model package download' do
  context 'when package file exists' do
    it { is_expected.to have_gitlab_http_status(:success) }
  end

  it_behaves_like 'Not found when model version does not exist'
end