Commit 075bbd4c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'sh-pages-eof-error' into 'master'

Fix EOF detection with CI artifacts metadata

Closes #36954

See merge request gitlab-org/gitlab-ce!22479
parents 6691d5f9 9065599e
---
title: Fix EOF detection with CI artifacts metadata
merge_request: 22479
author:
type: fixed
......@@ -59,9 +59,12 @@ module Gitlab
until gz.eof?
begin
path = read_string(gz).force_encoding('UTF-8')
meta = read_string(gz).force_encoding('UTF-8')
path = read_string(gz)&.force_encoding('UTF-8')
meta = read_string(gz)&.force_encoding('UTF-8')
# We might hit an EOF while reading either value, so we should
# abort if we don't get any data.
next unless path && meta
next unless path.valid_encoding? && meta.valid_encoding?
next unless path =~ match_pattern
next if path =~ INVALID_PATH_PATTERN
......
......@@ -112,4 +112,34 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
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
# 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