commit_spec.rb 13.7 KB
Newer Older
1 2
# == Schema Information
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3
# Table name: ci_commits
4
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6 7 8 9 10 11 12 13 14 15
#  id            :integer          not null, primary key
#  project_id    :integer
#  ref           :string(255)
#  sha           :string(255)
#  before_sha    :string(255)
#  push_data     :text
#  created_at    :datetime
#  updated_at    :datetime
#  tag           :boolean          default(FALSE)
#  yaml_errors   :text
#  committed_at  :datetime
Stan Hu's avatar
Stan Hu committed
16
#  gl_project_id :integer
17 18 19 20
#

require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
21
describe Ci::Commit, models: true do
22 23
  let(:project) { FactoryGirl.create :empty_project }
  let(:commit) { FactoryGirl.create :ci_commit, project: project }
24

25
  it { is_expected.to belong_to(:project) }
26
  it { is_expected.to have_many(:statuses) }
27
  it { is_expected.to have_many(:trigger_requests) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28 29
  it { is_expected.to have_many(:builds) }
  it { is_expected.to validate_presence_of :sha }
30 31
  it { is_expected.to validate_presence_of :status }
  it { is_expected.to delegate_method(:stages).to(:statuses) }
32

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33 34 35
  it { is_expected.to respond_to :git_author_name }
  it { is_expected.to respond_to :git_author_email }
  it { is_expected.to respond_to :short_sha }
36 37 38 39 40 41 42 43

  describe :valid_commit_sha do
    context 'commit.sha can not start with 00000000' do
      before do
        commit.sha = '0' * 40
        commit.valid_commit_sha
      end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44
      it('commit errors should not be empty') { expect(commit.errors).not_to be_empty }
45 46 47 48 49 50
    end
  end

  describe :short_sha do
    subject { commit.short_sha }

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
51 52 53 54
    it 'has 8 items' do
      expect(subject.size).to eq(8)
    end
    it { expect(commit.sha).to start_with(subject) }
55 56 57
  end

  describe :create_next_builds do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
58 59
  end

60 61 62 63 64 65 66 67 68 69 70 71 72
  describe :retried do
    subject { commit.retried }

    before do
      @commit1 = FactoryGirl.create :ci_build, commit: commit, name: 'deploy'
      @commit2 = FactoryGirl.create :ci_build, commit: commit, name: 'deploy'
    end

    it 'returns old builds' do
      is_expected.to contain_exactly(@commit1)
    end
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
73
  describe :create_builds do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74
    let!(:commit) { FactoryGirl.create :ci_commit, project: project, ref: 'master', tag: false }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
75 76

    def create_builds(trigger_request = nil)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
77
      commit.create_builds(nil, trigger_request)
78 79
    end

80 81
    def create_next_builds
      commit.create_next_builds(commit.builds.order(:id).last)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
82 83 84 85
    end

    it 'creates builds' do
      expect(create_builds).to be_truthy
86 87
      commit.builds.update_all(status: "success")
      expect(commit.builds.count(:all)).to eq(2)
88

Kamil Trzcinski's avatar
Kamil Trzcinski committed
89
      expect(create_next_builds).to be_truthy
90 91
      commit.builds.update_all(status: "success")
      expect(commit.builds.count(:all)).to eq(4)
92

Kamil Trzcinski's avatar
Kamil Trzcinski committed
93
      expect(create_next_builds).to be_truthy
94 95
      commit.builds.update_all(status: "success")
      expect(commit.builds.count(:all)).to eq(5)
96

Kamil Trzcinski's avatar
Kamil Trzcinski committed
97
      expect(create_next_builds).to be_falsey
98 99
    end

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    context 'custom stage with first job allowed to fail' do
      let(:yaml) do
        {
          stages: ['clean', 'test'],
          clean_job: {
            stage: 'clean',
            allow_failure: true,
            script: 'BUILD',
          },
          test_job: {
            stage: 'test',
            script: 'TEST',
          },
        }
      end

      before do
        stub_ci_commit_yaml_file(YAML.dump(yaml))
        create_builds
      end

      it 'properly schedules builds' do
        expect(commit.builds.pluck(:status)).to contain_exactly('pending')
        commit.builds.running_or_pending.each(&:drop)
        expect(commit.builds.pluck(:status)).to contain_exactly('pending', 'failed')
      end
    end

128
    context 'properly creates builds when "when" is defined' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
129
      let(:yaml) do
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
        {
          stages: ["build", "test", "test_failure", "deploy", "cleanup"],
          build: {
            stage: "build",
            script: "BUILD",
          },
          test: {
            stage: "test",
            script: "TEST",
          },
          test_failure: {
            stage: "test_failure",
            script: "ON test failure",
            when: "on_failure",
          },
          deploy: {
            stage: "deploy",
            script: "PUBLISH",
          },
          cleanup: {
            stage: "cleanup",
            script: "TIDY UP",
            when: "always",
          }
        }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
155
      end
156 157 158 159 160

      before do
        stub_ci_commit_yaml_file(YAML.dump(yaml))
      end

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      context 'when builds are successful' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(commit.builds.pluck(:name)).to contain_exactly('build')
          expect(commit.builds.pluck(:status)).to contain_exactly('pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'success', 'pending')
          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy', 'cleanup')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'success', 'success', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'success', 'success', 'success')
          commit.reload
          expect(commit.status).to eq('success')
        end
184 185
      end

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      context 'when test job fails' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(commit.builds.pluck(:name)).to contain_exactly('build')
          expect(commit.builds.pluck(:status)).to contain_exactly('pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'pending')
          commit.builds.running_or_pending.each(&:drop)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'failed', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'failed', 'success', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'failed', 'success', 'success')
          commit.reload
          expect(commit.status).to eq('failed')
        end
209 210
      end

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      context 'when test and test_failure jobs fail' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(commit.builds.pluck(:name)).to contain_exactly('build')
          expect(commit.builds.pluck(:status)).to contain_exactly('pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'pending')
          commit.builds.running_or_pending.each(&:drop)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'failed', 'pending')
          commit.builds.running_or_pending.each(&:drop)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'failed', 'failed', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'test_failure', 'cleanup')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'failed', 'failed', 'success')
          commit.reload
          expect(commit.status).to eq('failed')
        end
235 236
      end

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      context 'when deploy job fails' do
        it 'properly creates builds' do
          expect(create_builds).to be_truthy
          expect(commit.builds.pluck(:name)).to contain_exactly('build')
          expect(commit.builds.pluck(:status)).to contain_exactly('pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'success', 'pending')
          commit.builds.running_or_pending.each(&:drop)

          expect(commit.builds.pluck(:name)).to contain_exactly('build', 'test', 'deploy', 'cleanup')
          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'success', 'failed', 'pending')
          commit.builds.running_or_pending.each(&:success)

          expect(commit.builds.pluck(:status)).to contain_exactly('success', 'success', 'failed', 'success')
          commit.reload
          expect(commit.status).to eq('failed')
        end
260 261
      end
    end
262 263 264
  end

  describe "#finished_at" do
Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
265
    let(:commit) { FactoryGirl.create :ci_commit }
266 267

    it "returns finished_at of latest build" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
268
      build = FactoryGirl.create :ci_build, commit: commit, finished_at: Time.now - 60
269
      FactoryGirl.create :ci_build, commit: commit, finished_at: Time.now - 120
270

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
271
      expect(commit.finished_at.to_i).to eq(build.finished_at.to_i)
272 273 274
    end

    it "returns nil if there is no finished build" do
275
      FactoryGirl.create :ci_not_started_build, commit: commit
276

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
277
      expect(commit.finished_at).to be_nil
278 279 280 281
    end
  end

  describe "coverage" do
282 283
    let(:project) { FactoryGirl.create :empty_project, build_coverage_regex: "/.*/" }
    let(:commit) { FactoryGirl.create :ci_commit, project: project }
284 285

    it "calculates average when there are two builds with coverage" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
286 287
      FactoryGirl.create :ci_build, name: "rspec", coverage: 30, commit: commit
      FactoryGirl.create :ci_build, name: "rubocop", coverage: 40, commit: commit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
288
      expect(commit.coverage).to eq("35.00")
289 290 291
    end

    it "calculates average when there are two builds with coverage and one with nil" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
292 293 294
      FactoryGirl.create :ci_build, name: "rspec", coverage: 30, commit: commit
      FactoryGirl.create :ci_build, name: "rubocop", coverage: 40, commit: commit
      FactoryGirl.create :ci_build, commit: commit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
295
      expect(commit.coverage).to eq("35.00")
296 297 298
    end

    it "calculates average when there are two builds with coverage and one is retried" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
299 300 301
      FactoryGirl.create :ci_build, name: "rspec", coverage: 30, commit: commit
      FactoryGirl.create :ci_build, name: "rubocop", coverage: 30, commit: commit
      FactoryGirl.create :ci_build, name: "rubocop", coverage: 40, commit: commit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
302
      expect(commit.coverage).to eq("35.00")
303 304 305
    end

    it "calculates average when there is one build without coverage" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
306
      FactoryGirl.create :ci_build, commit: commit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
307
      expect(commit.coverage).to be_nil
308 309
    end
  end
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

  describe '#retryable?' do
    subject { commit.retryable? }

    context 'no failed builds' do
      before do
        FactoryGirl.create :ci_build, name: "rspec", commit: commit, status: 'success'
      end

      it 'be not retryable' do
        is_expected.to be_falsey
      end
    end

    context 'with failed builds' do
      before do
        FactoryGirl.create :ci_build, name: "rspec", commit: commit, status: 'running'
        FactoryGirl.create :ci_build, name: "rubocop", commit: commit, status: 'failed'
      end

      it 'be retryable' do
        is_expected.to be_truthy
      end
    end
  end

  describe '#stages' do
    let(:commit2) { FactoryGirl.create :ci_commit, project: project }
    subject { CommitStatus.where(commit: [commit, commit2]).stages }

    before do
      FactoryGirl.create :ci_build, commit: commit2, stage: 'test', stage_idx: 1
      FactoryGirl.create :ci_build, commit: commit, stage: 'build', stage_idx: 0
    end

    it 'return all stages' do
      is_expected.to eq(%w(build test))
    end
  end

  describe '#update_state' do
    it 'execute update_state after touching object' do
      expect(commit).to receive(:update_state).and_return(true)
      commit.touch
    end

    context 'dependent objects' do
      let(:commit_status) { build :commit_status, commit: commit }

      it 'execute update_state after saving dependent object' do
        expect(commit).to receive(:update_state).and_return(true)
        commit_status.save
      end
    end

    context 'update state' do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
366
      let(:current) { Time.now.change(usec: 0) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
367
      let(:build) { FactoryGirl.create :ci_build, :success, commit: commit, started_at: current - 120, finished_at: current - 60 }
368 369 370 371 372 373 374 375 376 377 378 379

      before do
        build
      end

      [:status, :started_at, :finished_at, :duration].each do |param|
        it "update #{param}" do
          expect(commit.send(param)).to eq(build.send(param))
        end
      end
    end
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

  describe '#branch?' do
    subject { commit.branch? }

    context 'is not a tag' do
      before do
        commit.tag = false
      end

      it 'return true when tag is set to false' do
        is_expected.to be_truthy
      end
    end

    context 'is not a tag' do
      before do
        commit.tag = true
      end

      it 'return false when tag is set to true' do
        is_expected.to be_falsey
      end
    end
  end
404
end