summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb
blob: 877c061d11b18cbee2329e0be9468f50d987c256 (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
require 'spec_helper'

describe Gitlab::BackgroundMigration::ArchiveLegacyTraces, :migration, schema: 20180529152628 do
  include TraceHelpers

  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:builds) { table(:ci_builds) }
  let(:job_artifacts) { table(:ci_job_artifacts) }

  before do
    namespaces.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
    projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1', namespace_id: 123)
    @build = builds.create!(id: 1, project_id: 123, status: 'success', type: 'Ci::Build')
  end

  context 'when trace file exsits at the right place' do
    before do
      create_legacy_trace(@build, 'trace in file')
    end

    it 'correctly archive legacy traces' do
      expect(job_artifacts.count).to eq(0)
      expect(File.exist?(legacy_trace_path(@build))).to be_truthy

      described_class.new.perform(1, 1)

      expect(job_artifacts.count).to eq(1)
      expect(File.exist?(legacy_trace_path(@build))).to be_falsy
      expect(File.read(archived_trace_path(job_artifacts.first))).to eq('trace in file')
    end
  end

  context 'when trace file does not exsits at the right place' do
    it 'does not raise errors nor create job artifact' do
      expect { described_class.new.perform(1, 1) }.not_to raise_error

      expect(job_artifacts.count).to eq(0)
    end
  end

  context 'when trace data exsits in database' do
    before do
      create_legacy_trace_in_db(@build, 'trace in db')
    end

    it 'correctly archive legacy traces' do
      expect(job_artifacts.count).to eq(0)
      expect(@build.read_attribute(:trace)).not_to be_empty

      described_class.new.perform(1, 1)

      @build.reload
      expect(job_artifacts.count).to eq(1)
      expect(@build.read_attribute(:trace)).to be_nil
      expect(File.read(archived_trace_path(job_artifacts.first))).to eq('trace in db')
    end
  end
end