summaryrefslogtreecommitdiff
path: root/spec/workers/packages/helm/extraction_worker_spec.rb
blob: daebbda307759e4b89f6cbecbaf740d389f73db9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Helm::ExtractionWorker, type: :worker 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_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),
          project_id: package_file.package.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')
      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 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'
    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'
    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', ActiveRecord::RecordInvalid
    end
  end
end