Commit 9e023666 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve specs for build state update and verification

This commit adds more test coverage for cases when a new metric is being
sent by a runner.
parent 7e06c823
...@@ -65,7 +65,9 @@ module Gitlab ...@@ -65,7 +65,9 @@ module Gitlab
end end
def state_bytesize def state_bytesize
strong_memoize(:state_crc32) { build.pending_state&.trace_bytesize } strong_memoize(:state_bytesize) do
build.pending_state&.trace_bytesize
end
end end
def trace_size def trace_size
...@@ -77,7 +79,8 @@ module Gitlab ...@@ -77,7 +79,8 @@ module Gitlab
end end
def corrupted? def corrupted?
return false if valid? || state_bytesize.nil? return false unless has_bytesize?
return false if valid?
state_bytesize.to_i != trace_size.to_i state_bytesize.to_i != trace_size.to_i
end end
...@@ -86,6 +89,10 @@ module Gitlab ...@@ -86,6 +89,10 @@ module Gitlab
trace_chunks.to_a.size trace_chunks.to_a.size
end end
def has_bytesize?
state_bytesize.present?
end
private private
def chunk_size(chunk) def chunk_size(chunk)
......
...@@ -8,8 +8,12 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -8,8 +8,12 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
subject { described_class.new(build) } subject { described_class.new(build) }
context 'when build pending state exists' do context 'when build pending state exists' do
let(:trace_details) do
{ trace_checksum: 'crc32:d4777540', trace_bytesize: 262161 }
end
before do before do
create(:ci_build_pending_state, build: build, trace_checksum: 'crc32:d4777540') create(:ci_build_pending_state, build: build, **trace_details)
end end
context 'when matching persisted trace chunks exist' do context 'when matching persisted trace chunks exist' do
...@@ -22,6 +26,7 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -22,6 +26,7 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
it 'calculates combined trace chunks CRC32 correctly' do it 'calculates combined trace chunks CRC32 correctly' do
expect(subject.chunks_crc32).to eq 3564598592 expect(subject.chunks_crc32).to eq 3564598592
expect(subject).to be_valid expect(subject).to be_valid
expect(subject).not_to be_corrupted
end end
end end
...@@ -32,8 +37,9 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -32,8 +37,9 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
create_chunk(index: 2, data: 'ccccccccccccccccc') create_chunk(index: 2, data: 'ccccccccccccccccc')
end end
it 'makes trace checksum invalid' do it 'makes trace checksum invalid but not corrupted' do
expect(subject).not_to be_valid expect(subject).not_to be_valid
expect(subject).not_to be_corrupted
end end
end end
...@@ -43,8 +49,9 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -43,8 +49,9 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
create_chunk(index: 2, data: 'ccccccccccccccccc') create_chunk(index: 2, data: 'ccccccccccccccccc')
end end
it 'makes trace checksum invalid' do it 'makes trace checksum invalid and corrupted' do
expect(subject).not_to be_valid expect(subject).not_to be_valid
expect(subject).to be_corrupted
end end
end end
...@@ -55,8 +62,9 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -55,8 +62,9 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
create_chunk(index: 2, data: 'ccccccccccccccccc') create_chunk(index: 2, data: 'ccccccccccccccccc')
end end
it 'makes trace checksum invalid' do it 'makes trace checksum invalid but not corrupted' do
expect(subject).not_to be_valid expect(subject).not_to be_valid
expect(subject).not_to be_corrupted
end end
end end
...@@ -99,6 +107,14 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -99,6 +107,14 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
it 'returns nil' do it 'returns nil' do
expect(subject.last_chunk).to be_nil expect(subject.last_chunk).to be_nil
end end
it 'is not a valid trace' do
expect(subject).not_to be_valid
end
it 'is not a corrupted trace' do
expect(subject).not_to be_corrupted
end
end end
context 'when there are multiple chunks' do context 'when there are multiple chunks' do
...@@ -110,6 +126,14 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do ...@@ -110,6 +126,14 @@ RSpec.describe Gitlab::Ci::Trace::Checksum do
it 'returns chunk with the highest index' do it 'returns chunk with the highest index' do
expect(subject.last_chunk.chunk_index).to eq 1 expect(subject.last_chunk.chunk_index).to eq 1
end end
it 'is not a valid trace' do
expect(subject).not_to be_valid
end
it 'is not a corrupted trace' do
expect(subject).not_to be_corrupted
end
end end
end end
......
...@@ -80,7 +80,11 @@ RSpec.describe Ci::UpdateBuildStateService do ...@@ -80,7 +80,11 @@ RSpec.describe Ci::UpdateBuildStateService do
context 'when build has a checksum' do context 'when build has a checksum' do
let(:params) do let(:params) do
{ checksum: 'crc32:12345678', state: 'failed', failure_reason: 'script_failure' } {
output: { checksum: 'crc32:12345678', bytesize: 123 },
failure_reason: 'script_failure',
state: 'failed'
}
end end
context 'when build does not have associated trace chunks' do context 'when build does not have associated trace chunks' do
...@@ -154,14 +158,74 @@ RSpec.describe Ci::UpdateBuildStateService do ...@@ -154,14 +158,74 @@ RSpec.describe Ci::UpdateBuildStateService do
end end
context 'when trace checksum is valid' do context 'when trace checksum is valid' do
let(:params) { { checksum: 'crc32:ed82cd11', state: 'success' } } let(:params) do
{ output: { checksum: 'crc32:ed82cd11', bytesize: 4 }, state: 'success' }
end
it 'does not increment invalid trace metric' do it 'does not increment invalid or corrupted trace metric' do
execute_with_stubbed_metrics! execute_with_stubbed_metrics!
expect(metrics) expect(metrics)
.not_to have_received(:increment_trace_operation) .not_to have_received(:increment_trace_operation)
.with(operation: :invalid) .with(operation: :invalid)
expect(metrics)
.not_to have_received(:increment_trace_operation)
.with(operation: :corrupted)
end
context 'when using deprecated parameters' do
let(:params) do
{ checksum: 'crc32:ed82cd11', state: 'success' }
end
it 'does not increment invalid or corrupted trace metric' do
execute_with_stubbed_metrics!
expect(metrics)
.not_to have_received(:increment_trace_operation)
.with(operation: :invalid)
expect(metrics)
.not_to have_received(:increment_trace_operation)
.with(operation: :corrupted)
end
end
end
context 'when trace checksum is invalid and the log is corrupted' do
let(:params) do
{ output: { checksum: 'crc32:12345678', bytesize: 1 }, state: 'success' }
end
it 'increments invalid and corrupted trace metrics' do
execute_with_stubbed_metrics!
expect(metrics)
.to have_received(:increment_trace_operation)
.with(operation: :invalid)
expect(metrics)
.to have_received(:increment_trace_operation)
.with(operation: :corrupted)
end
end
context 'when trace checksum is invalid but the log is seems fine' do
let(:params) do
{ output: { checksum: 'crc32:12345678', bytesize: 4 }, state: 'success' }
end
it 'does not increment corrupted trace metric' do
execute_with_stubbed_metrics!
expect(metrics)
.to have_received(:increment_trace_operation)
.with(operation: :invalid)
expect(metrics)
.not_to have_received(:increment_trace_operation)
.with(operation: :corrupted)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment