summaryrefslogtreecommitdiff
path: root/spec/support/helpers/ci_artifact_metadata_generator.rb
blob: e02501565a98d8a2d4bd9b74aafe86b67c78a8d4 (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
# frozen_string_literal: true

# 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