File: extraction_worker_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (120 lines) | stat: -rw-r--r-- 4,321 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Helm::ExtractionWorker, type: :worker, feature_category: :package_registry do
  describe '#perform' do
    let_it_be(:package) { create(:helm_package, without_package_files: true, status: 'processing') }

    let!(:package_file) { create(:helm_package_file, without_loaded_metadatum: true, package: package) }
    let(:package_file_id) { package_file.id }
    let(:channel) { 'stable' }

    let(:expected_metadata) do
      {
        'apiVersion' => 'v2',
        'description' => 'File, Block, and Object Storage Services for your Cloud-Native Environment',
        'icon' => 'https://rook.io/images/rook-logo.svg',
        'name' => 'rook-ceph',
        'sources' => ['https://github.com/rook/rook'],
        'version' => 'v1.5.8'
      }
    end

    subject { described_class.new.perform(channel, package_file_id) }

    shared_examples 'handling error' do |error_message:,
      error_class: Packages::Helm::ExtractFileMetadataService::ExtractionError|
      it 'mark the package as errored', :aggregate_failures do
        expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
          instance_of(error_class),
          {
            package_file_id: package_file.id,
            project_id: package_file.project_id
          }
        )
        expect { subject }
          .to not_change { Packages::Package.count }
          .and not_change { Packages::PackageFile.count }
          .and change { package.reload.status }.from('processing').to('error')

        expect(package.status_message).to match(error_message)
      end
    end

    context 'with valid package file' do
      it_behaves_like 'an idempotent worker' do
        let(:job_args) { [channel, package_file_id] }

        it 'updates package and package file', :aggregate_failures do
          expect(Gitlab::ErrorTracking).not_to receive(:log_exception)

          expect { subject }
            .to not_change { Packages::Package.count }
            .and not_change { Packages::PackageFile.count }
            .and change { Packages::Helm::FileMetadatum.count }.from(0).to(1)
            .and change { package.reload.status }.from('processing').to('default')

          helm_file_metadatum = package_file.helm_file_metadatum

          expect(helm_file_metadatum.channel).to eq(channel)
          expect(helm_file_metadatum.metadata).to eq(expected_metadata)
        end
      end
    end

    context 'with invalid package file id' do
      let(:package_file_id) { 5555 }

      it "doesn't update helm_file_metadatum", :aggregate_failures do
        expect { subject }
          .to not_change { Packages::Package.count }
          .and not_change { Packages::PackageFile.count }
          .and not_change { Packages::Helm::FileMetadatum.count }
          .and not_change { package.reload.status }
      end
    end

    context 'with controlled errors' do
      context 'with an empty package file' do
        before do
          expect_next_instance_of(Gem::Package::TarReader) do |tar_reader|
            expect(tar_reader).to receive(:each).and_return([])
          end
        end

        it_behaves_like 'handling error', error_message: /Chart.yaml not found/
      end

      context 'with an invalid YAML' do
        before do
          expect_next_instance_of(Gem::Package::TarReader::Entry) do |entry|
            expect(entry).to receive(:read).and_return('{')
          end
        end

        it_behaves_like 'handling error', error_message: /Error while parsing Chart.yaml/
      end

      context 'with an invalid Chart.yaml' do
        before do
          expect_next_instance_of(Gem::Package::TarReader::Entry) do |entry|
            expect(entry).to receive(:read).and_return('{}')
          end
        end

        it_behaves_like 'handling error', error_class: ActiveRecord::RecordInvalid, error_message: /Validation failed/
      end
    end

    context 'with uncontrolled errors' do
      before do
        allow_next_instance_of(::Packages::Helm::ProcessFileService) do |instance|
          allow(instance).to receive(:execute).and_raise(StandardError.new('Boom'))
        end
      end

      it_behaves_like 'handling error', error_class: StandardError, error_message: 'Unexpected error: StandardError'
    end
  end
end