Commit 9065599e authored by Stan Hu's avatar Stan Hu

Add a spec that generates the metadata dynamically

parent 4c4d1b79
...@@ -112,4 +112,34 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do ...@@ -112,4 +112,34 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
end end
end 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 end
# frozen_sting_literal: true
# This generates fake CI metadata .gz for testing
# Based off https://gitlab.com/gitlab-org/gitlab-workhorse/blob/master/internal/zipartifacts/metadata.go
class CiArtifactMetadataGenerator
attr_accessor :entries, :output
ARTIFACT_METADATA = "GitLab Build Artifacts Metadata 0.0.2\n".freeze
def initialize(stream)
@entries = {}
@output = Zlib::GzipWriter.new(stream)
end
def add_entry(filename)
@entries[filename] = { CRC: rand(0xfffffff), Comment: FFaker::Lorem.sentence(10) }
end
def write
write_version
write_errors
write_entries
output.close
end
private
def write_version
write_string(ARTIFACT_METADATA)
end
def write_errors
write_string('{}')
end
def write_entries
entries.each do |filename, metadata|
write_string(filename)
write_string(metadata.to_json + "\n")
end
end
def write_string(data)
bytes = [data.length].pack('L>')
output.write(bytes)
output.write(data)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment