Commit d4290995 authored by Shinya Maeda's avatar Shinya Maeda

Add tests for Trace::Stream

parent b94c84e5
...@@ -24,7 +24,7 @@ module Ci ...@@ -24,7 +24,7 @@ module Ci
raw_data raw_data
else else
raise 'Unsupported data store' raise 'Unsupported data store'
end end&.force_encoding(Encoding::BINARY)
end end
def set_data(value) def set_data(value)
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Trace::Stream do describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
set(:job) { create(:ci_build, :running) }
before do
stub_feature_flags(ci_enable_live_trace: true)
end
describe 'delegates' do describe 'delegates' do
subject { described_class.new { nil } } subject { described_class.new { nil } }
...@@ -15,12 +21,7 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -15,12 +21,7 @@ describe Gitlab::Ci::Trace::Stream do
end end
describe '#limit' do describe '#limit' do
let(:stream) do shared_examples_for 'limits' do
described_class.new do
StringIO.new((1..8).to_a.join("\n"))
end
end
it 'if size is larger we start from beginning' do it 'if size is larger we start from beginning' do
stream.limit(20) stream.limit(20)
...@@ -70,21 +71,32 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -70,21 +71,32 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
describe '#append' do context 'when stream is StringIO' do
let(:tempfile) { Tempfile.new } let(:stream) do
described_class.new do
StringIO.new((1..8).to_a.join("\n"))
end
end
it_behaves_like 'limits'
end
context 'when stream is ChunkedIO' do
let(:stream) do let(:stream) do
described_class.new do described_class.new do
tempfile.write("12345678") Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
tempfile.rewind chunked_io.write((1..8).to_a.join("\n"))
tempfile chunked_io.seek(0, IO::SEEK_SET)
end
end end
end end
after do it_behaves_like 'limits'
tempfile.unlink end
end end
describe '#append' do
shared_examples_for 'appends' do
it "truncates and append content" do it "truncates and append content" do
stream.append("89", 4) stream.append("89", 4)
stream.seek(0) stream.seek(0)
...@@ -105,13 +117,40 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -105,13 +117,40 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
describe '#set' do context 'when stream is StringIO' do
let(:tempfile) { Tempfile.new }
let(:stream) do let(:stream) do
described_class.new do described_class.new do
StringIO.new("12345678") tempfile.write("12345678")
tempfile.rewind
tempfile
end end
end end
after do
tempfile.unlink
end
it_behaves_like 'appends'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write('12345678')
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'appends'
end
end
describe '#set' do
shared_examples_for 'sets' do
before do before do
stream.set("8901") stream.set("8901")
end end
...@@ -124,15 +163,32 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -124,15 +163,32 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
describe '#raw' do context 'when stream is StringIO' do
let(:path) { __FILE__ }
let(:lines) { File.readlines(path) }
let(:stream) do let(:stream) do
described_class.new do described_class.new do
File.open(path) StringIO.new("12345678")
end
end
it_behaves_like 'sets'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write('12345678')
chunked_io.seek(0, IO::SEEK_SET)
end
end end
end end
it_behaves_like 'sets'
end
end
describe '#raw' do
shared_examples_for 'sets' do
it 'returns all contents if last_lines is not specified' do it 'returns all contents if last_lines is not specified' do
result = stream.raw result = stream.raw
...@@ -163,13 +219,33 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -163,13 +219,33 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
describe '#html_with_state' do let(:path) { __FILE__ }
let(:lines) { File.readlines(path) }
context 'when stream is File' do
let(:stream) do let(:stream) do
described_class.new do described_class.new do
StringIO.new("1234") File.open(path)
end
end end
end end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write(File.binread(path))
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'sets'
end
end
describe '#html_with_state' do
shared_examples_for 'html_with_states' do
it 'returns html content with state' do it 'returns html content with state' do
result = stream.html_with_state result = stream.html_with_state
...@@ -193,13 +269,32 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -193,13 +269,32 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
describe '#html' do context 'when stream is StringIO' do
let(:stream) do let(:stream) do
described_class.new do described_class.new do
StringIO.new("12\n34\n56") StringIO.new("1234")
end
end
it_behaves_like 'html_with_states'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write("1234")
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'html_with_states'
end end
end end
describe '#html' do
shared_examples_for 'htmls' do
it "returns html" do it "returns html" do
expect(stream.html).to eq("12<br>34<br>56") expect(stream.html).to eq("12<br>34<br>56")
end end
...@@ -209,15 +304,32 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -209,15 +304,32 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
describe '#extract_coverage' do context 'when stream is StringIO' do
let(:stream) do let(:stream) do
described_class.new do described_class.new do
StringIO.new(data) StringIO.new("12\n34\n56")
end end
end end
subject { stream.extract_coverage(regex) } it_behaves_like 'htmls'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write("12\n34\n56")
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'htmls'
end
end
describe '#extract_coverage' do
shared_examples_for 'extract_coverages' do
context 'valid content & regex' do context 'valid content & regex' do
let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered' } let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered' }
let(:regex) { '\(\d+.\d+\%\) covered' } let(:regex) { '\(\d+.\d+\%\) covered' }
...@@ -344,4 +456,30 @@ describe Gitlab::Ci::Trace::Stream do ...@@ -344,4 +456,30 @@ describe Gitlab::Ci::Trace::Stream do
end end
end end
end end
subject { stream.extract_coverage(regex) }
context 'when stream is StringIO' do
let(:stream) do
described_class.new do
StringIO.new(data)
end
end
it_behaves_like 'extract_coverages'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write(data)
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'extract_coverages'
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