diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 5b9754b5c370dd71d9f4505fb6d6ce7e6812ab1a..72c2dc469a9795ad06ecf97729b5f701b7d22c2e 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -48,11 +48,11 @@ module Ci end before_transition [:created, :pending] => :running do |pipeline| - pipeline.started_at = Time.now + pipeline.started_at = Gitlab::Utils.now end before_transition any => [:success, :failed, :canceled] do |pipeline| - pipeline.finished_at = Time.now + pipeline.finished_at = Gitlab::Utils.now end before_transition do |pipeline| diff --git a/app/models/concerns/statuseable.rb b/app/models/concerns/statuseable.rb index 750f937b72407bbdd9e827c41093c4edc86b5bd9..9f73c5c94676fa99ecb4eeda212165225b881c05 100644 --- a/app/models/concerns/statuseable.rb +++ b/app/models/concerns/statuseable.rb @@ -87,7 +87,7 @@ module Statuseable if started_at && finished_at finished_at - started_at elsif started_at - Time.now - started_at + Gitlab::Utils.now - started_at end end end diff --git a/lib/gitlab/utils.rb b/lib/gitlab/utils.rb index d13fe0ef8a9df2c8ac823caa97d0e5d363ae0f9d..4d1bd16eb958ee137be0ea3cbd47b17d7ed3b643 100644 --- a/lib/gitlab/utils.rb +++ b/lib/gitlab/utils.rb @@ -7,11 +7,16 @@ module Gitlab # @param cmd [Array<String>] # @return [Boolean] def system_silent(cmd) - Popen::popen(cmd).last.zero? + Popen.popen(cmd).last.zero? end def force_utf8(str) str.force_encoding(Encoding::UTF_8) end + + # The same as Time.now but using this would make it easier to test + def now + Time.now + end end end diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index 8137e9f8f71abddc964c9da950eb975140ef3f31..67bd23a1ccb5cecd6a43b89f88af00c15934c406 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -129,12 +129,15 @@ describe Ci::Pipeline, models: true do describe '#duration' do before do - build.skip - build2.skip + allow(Gitlab::Utils).to receive(:now). + and_return(current - 120, current) + + pipeline.run + pipeline.succeed end it 'matches sum of builds duration' do - expect(pipeline.reload.duration).to eq(build.duration + build2.duration) + expect(pipeline.reload.duration).to eq(120) end end