summaryrefslogtreecommitdiff
path: root/spec/migrations/migrate_legacy_artifacts_to_job_artifacts_spec.rb
blob: 381369599af047374e264f54ae857b9804be7d24 (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
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20180430161409_migrate_legacy_artifacts_to_job_artifacts.rb')

describe MigrateLegacyArtifactsToJobArtifacts, :migration, :sidekiq do
  let(:migration_class) { Gitlab::BackgroundMigration::MigrateLegacyArtifacts }
  let(:migration_name)  { migration_class.to_s.demodulize }

  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:pipelines) { table(:ci_pipelines) }
  let(:jobs) { table(:ci_builds) }
  let(:job_artifacts) { table(:ci_job_artifacts) }

  before do
    namespaces.create!(id: 1, name: 'gitlab', path: 'gitlab-org')
    projects.create!(id: 1, name: 'gitlab', path: 'gitlab-ce', namespace_id: 1)
    pipelines.create!(id: 1, project_id: 1, ref: 'master', sha: 'adf43c3a')
  end

  context 'when legacy artifacts exist' do
    before do
      jobs.create!(id: 1, commit_id: 1, project_id: 1, status: :success, artifacts_file: 'archive.zip')
      jobs.create!(id: 2, commit_id: 1, project_id: 1, status: :failed, artifacts_metadata: 'metadata.gz')
      jobs.create!(id: 3, commit_id: 1, project_id: 1, status: :failed, artifacts_file: 'archive.zip', artifacts_metadata: 'metadata.gz')
      jobs.create!(id: 4, commit_id: 1, project_id: 1, status: :running)
      jobs.create!(id: 5, commit_id: 1, project_id: 1, status: :success, artifacts_file: 'archive.zip', artifacts_metadata: 'metadata.gz')
      jobs.create!(id: 6, commit_id: 1, project_id: 1, status: :failed, artifacts_file: 'archive.zip', artifacts_metadata: 'metadata.gz')
    end

    it 'schedules a background migration' do
      Sidekiq::Testing.fake! do
        Timecop.freeze do
          migrate!

          expect(migration_name).to be_scheduled_delayed_migration(5.minutes, 1, 6)
          expect(BackgroundMigrationWorker.jobs.size).to eq 1
        end
      end
    end

    context 'when new artifacts has already existed' do
      before do
        job_artifacts.create!(job_id: 1, id: 1, project_id: 1, file_type: 1, size: 123, file: 'archive.zip')
        job_artifacts.create!(job_id: 6, id: 2, project_id: 1, file_type: 1, size: 123, file: 'archive.zip')
      end

      it 'schedules a background migration to specific targets' do
        Sidekiq::Testing.fake! do
          Timecop.freeze do
            migrate!

            expect(migration_name).to be_scheduled_delayed_migration(5.minutes, 3, 5)
            expect(BackgroundMigrationWorker.jobs.size).to eq 1
          end
        end
      end
    end
  end

  context 'when legacy artifacts do not exist' do
    before do
      jobs.create!(id: 1, commit_id: 1, project_id: 1, status: :success)
      jobs.create!(id: 2, commit_id: 1, project_id: 1, status: :failed, artifacts_metadata: 'metadata.gz')
    end

    it 'does not schedule background migrations' do
      Sidekiq::Testing.fake! do
        Timecop.freeze do
          migrate!

          expect(BackgroundMigrationWorker.jobs.size).to eq 0
        end
      end
    end
  end
end