BigW Consortium Gitlab

migrate_process_commit_worker_jobs_spec.rb 5.6 KB
Newer Older
1 2
# encoding: utf-8

3 4 5 6
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20161124141322_migrate_process_commit_worker_jobs.rb')

describe MigrateProcessCommitWorkerJobs do
7
  let(:project) { create(:project, :repository) }
8 9 10 11 12 13
  let(:user) { create(:user) }
  let(:commit) { project.commit.raw.raw_commit }

  describe 'Project' do
    describe 'find_including_path' do
      it 'returns Project instances' do
14 15
        expect(described_class::Project.find_including_path(project.id)).
          to be_an_instance_of(described_class::Project)
16 17 18
      end

      it 'selects the full path for every Project' do
19 20
        migration_project = described_class::Project.
          find_including_path(project.id)
21

22 23
        expect(migration_project[:path_with_namespace]).
          to eq(project.path_with_namespace)
24 25 26 27 28
      end
    end

    describe '#repository_storage_path' do
      it 'returns the storage path for the repository' do
29 30
        migration_project = described_class::Project.
          find_including_path(project.id)
31

32 33
        expect(File.directory?(migration_project.repository_storage_path)).
          to eq(true)
34 35 36 37 38
      end
    end

    describe '#repository_path' do
      it 'returns the path to the repository' do
39 40
        migration_project = described_class::Project.
          find_including_path(project.id)
41 42 43 44 45 46 47

        expect(File.directory?(migration_project.repository_path)).to eq(true)
      end
    end

    describe '#repository' do
      it 'returns a Rugged::Repository' do
48 49
        migration_project = described_class::Project.
          find_including_path(project.id)
50

51 52
        expect(migration_project.repository).
          to be_an_instance_of(Rugged::Repository)
53 54 55 56 57 58 59 60 61 62 63
      end
    end
  end

  describe '#up', :redis do
    let(:migration) { described_class.new }

    def job_count
      Sidekiq.redis { |r| r.llen('queue:process_commit') }
    end

64
    def pop_job
Douwe Maan committed
65
      JSON.parse(Sidekiq.redis { |r| r.lpop('queue:process_commit') })
66 67
    end

68 69 70 71 72 73 74 75
    before do
      Sidekiq.redis do |redis|
        job = JSON.dump(args: [project.id, user.id, commit.oid])
        redis.lpush('queue:process_commit', job)
      end
    end

    it 'skips jobs using a project that no longer exists' do
76 77 78
      allow(described_class::Project).to receive(:find_including_path).
        with(project.id).
        and_return(nil)
79 80 81 82 83 84 85

      migration.up

      expect(job_count).to eq(0)
    end

    it 'skips jobs using commits that no longer exist' do
86 87 88
      allow_any_instance_of(Rugged::Repository).to receive(:lookup).
        with(commit.oid).
        and_raise(Rugged::OdbError)
89 90 91 92 93 94 95 96 97 98 99 100

      migration.up

      expect(job_count).to eq(0)
    end

    it 'inserts migrated jobs back into the queue' do
      migration.up

      expect(job_count).to eq(1)
    end

101
    it 'encodes data to UTF-8' do
102 103 104
      allow_any_instance_of(Rugged::Repository).to receive(:lookup).
        with(commit.oid).
        and_return(commit)
105

106 107
      allow(commit).to receive(:message).
        and_return('김치'.force_encoding('BINARY'))
108 109 110 111 112 113 114 115 116 117 118

      migration.up

      job = pop_job

      # We don't care so much about what is being stored, instead we just want
      # to make sure the encoding is right so that JSON encoding the data
      # doesn't produce any errors.
      expect(job['args'][2]['message'].encoding).to eq(Encoding::UTF_8)
    end

119 120 121
    context 'a migrated job' do
      let(:job) do
        migration.up
122
        pop_job
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      end

      let(:commit_hash) do
        job['args'][2]
      end

      it 'includes the project ID' do
        expect(job['args'][0]).to eq(project.id)
      end

      it 'includes the user ID' do
        expect(job['args'][1]).to eq(user.id)
      end

      it 'includes the commit ID' do
        expect(commit_hash['id']).to eq(commit.oid)
      end

      it 'includes the commit message' do
        expect(commit_hash['message']).to eq(commit.message)
      end

      it 'includes the parent IDs' do
        expect(commit_hash['parent_ids']).to eq(commit.parent_ids)
      end

      it 'includes the author date' do
        expect(commit_hash['authored_date']).to eq(commit.author[:time].to_s)
      end

      it 'includes the author name' do
        expect(commit_hash['author_name']).to eq(commit.author[:name])
      end

      it 'includes the author Email' do
        expect(commit_hash['author_email']).to eq(commit.author[:email])
      end

      it 'includes the commit date' do
        expect(commit_hash['committed_date']).to eq(commit.committer[:time].to_s)
      end

      it 'includes the committer name' do
        expect(commit_hash['committer_name']).to eq(commit.committer[:name])
      end

      it 'includes the committer Email' do
        expect(commit_hash['committer_email']).to eq(commit.committer[:email])
      end
    end
  end

  describe '#down', :redis do
    let(:migration) { described_class.new }

    def job_count
      Sidekiq.redis { |r| r.llen('queue:process_commit') }
    end

    before do
      Sidekiq.redis do |redis|
        job = JSON.dump(args: [project.id, user.id, commit.oid])
        redis.lpush('queue:process_commit', job)

        migration.up
      end
    end

    it 'pushes migrated jobs back into the queue' do
      migration.down

      expect(job_count).to eq(1)
    end

    context 'a migrated job' do
      let(:job) do
        migration.down

Douwe Maan committed
201
        JSON.parse(Sidekiq.redis { |r| r.lpop('queue:process_commit') })
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
      end

      it 'includes the project ID' do
        expect(job['args'][0]).to eq(project.id)
      end

      it 'includes the user ID' do
        expect(job['args'][1]).to eq(user.id)
      end

      it 'includes the commit SHA' do
        expect(job['args'][2]).to eq(commit.oid)
      end
    end
  end
end