summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/build/artifacts/metadata_spec.rb
blob: ff189c4701e90043b077d817d59abba44ea72891 (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
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Build::Artifacts::Metadata do
  def metadata(path = '', **opts)
    described_class.new(metadata_file_stream, path, **opts)
  end

  let(:metadata_file_path) do
    Rails.root + 'spec/fixtures/ci_build_artifacts_metadata.gz'
  end

  let(:metadata_file_stream) do
    File.open(metadata_file_path) if metadata_file_path
  end

  after do
    metadata_file_stream&.close
  end

  context 'metadata file exists' do
    describe '#find_entries! empty string' do
      subject { metadata('').find_entries! }

      it 'matches correct paths' do
        expect(subject.keys).to contain_exactly 'ci_artifacts.txt',
                                                'other_artifacts_0.1.2/',
                                                'rails_sample.jpg',
                                                'tests_encoding/'
      end

      it 'matches metadata for every path' do
        expect(subject.keys.count).to eq 4
      end

      it 'return Hashes for each metadata' do
        expect(subject.values).to all(be_kind_of(Hash))
      end
    end

    describe '#find_entries! other_artifacts_0.1.2/' do
      subject { metadata('other_artifacts_0.1.2/').find_entries! }

      it 'matches correct paths' do
        expect(subject.keys)
          .to contain_exactly 'other_artifacts_0.1.2/',
                             'other_artifacts_0.1.2/doc_sample.txt',
                             'other_artifacts_0.1.2/another-subdirectory/'
      end
    end

    describe '#find_entries! other_artifacts_0.1.2/another-subdirectory/' do
      subject { metadata('other_artifacts_0.1.2/another-subdirectory/').find_entries! }

      it 'matches correct paths' do
        expect(subject.keys)
          .to contain_exactly 'other_artifacts_0.1.2/another-subdirectory/',
                             'other_artifacts_0.1.2/another-subdirectory/empty_directory/',
                             'other_artifacts_0.1.2/another-subdirectory/banana_sample.gif'
      end
    end

    describe '#find_entries! recursively for other_artifacts_0.1.2/' do
      subject { metadata('other_artifacts_0.1.2/', recursive: true).find_entries! }

      it 'matches correct paths' do
        expect(subject.keys)
          .to contain_exactly 'other_artifacts_0.1.2/',
                             'other_artifacts_0.1.2/doc_sample.txt',
                             'other_artifacts_0.1.2/another-subdirectory/',
                             'other_artifacts_0.1.2/another-subdirectory/empty_directory/',
                             'other_artifacts_0.1.2/another-subdirectory/banana_sample.gif'
      end
    end

    describe '#to_entry' do
      subject { metadata('').to_entry }
      it { is_expected.to be_an_instance_of(Gitlab::Ci::Build::Artifacts::Metadata::Entry) }
    end

    describe '#full_version' do
      subject { metadata('').full_version }
      it { is_expected.to eq 'GitLab Build Artifacts Metadata 0.0.1' }
    end

    describe '#version' do
      subject { metadata('').version }
      it { is_expected.to eq '0.0.1' }
    end

    describe '#errors' do
      subject { metadata('').errors }
      it { is_expected.to eq({}) }
    end
  end

  context 'metadata file does not exist' do
    let(:metadata_file_path) { nil }

    describe '#find_entries!' do
      it 'raises error' do
        expect { metadata.find_entries! }.to raise_error(described_class::InvalidStreamError, /Invalid stream/)
      end
    end
  end

  context 'metadata file is invalid' do
    let(:metadata_file_path) { Rails.root + 'spec/fixtures/ci_build_artifacts.zip' }

    describe '#find_entries!' do
      it 'raises error' do
        expect { metadata.find_entries! }.to raise_error(described_class::InvalidStreamError, /not in gzip format/)
      end
    end
  end

  context 'generated metadata' do
    let(:tmpfile) { Tempfile.new('test-metadata') }
    let(:generator) { CiArtifactMetadataGenerator.new(tmpfile) }
    let(:entry_count) { 5 }

    before do
      tmpfile.binmode

      (1..entry_count).each do |index|
        generator.add_entry("public/test-#{index}.txt")
      end

      generator.write
    end

    after do
      File.unlink(tmpfile.path)
    end

    describe '#find_entries!' do
      it 'reads expected number of entries' do
        stream = File.open(tmpfile.path)

        metadata = described_class.new(stream, 'public', { recursive: true })

        expect(metadata.find_entries!.count).to eq entry_count
      end
    end
  end
end