2020-03-11 11:09:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Elasticsearch::Logs::Lines do
|
2020-03-11 11:09:37 -04:00
|
|
|
let(:client) { Elasticsearch::Transport::Client }
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
let(:es_message_1) { { timestamp: "2019-12-13T14:35:34.034Z", pod: "production-6866bc8974-m4sk4", message: "10.8.2.1 - - [25/Oct/2019:08:03:22 UTC] \"GET / HTTP/1.1\" 200 13" } }
|
|
|
|
let(:es_message_2) { { timestamp: "2019-12-13T14:35:35.034Z", pod: "production-6866bc8974-m4sk4", message: "10.8.2.1 - - [27/Oct/2019:23:49:54 UTC] \"GET / HTTP/1.1\" 200 13" } }
|
|
|
|
let(:es_message_3) { { timestamp: "2019-12-13T14:35:36.034Z", pod: "production-6866bc8974-m4sk4", message: "10.8.2.1 - - [04/Nov/2019:23:09:24 UTC] \"GET / HTTP/1.1\" 200 13" } }
|
|
|
|
let(:es_message_4) { { timestamp: "2019-12-13T14:35:37.034Z", pod: "production-6866bc8974-m4sk4", message: "- -\u003e /" } }
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
let(:es_response) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/logs_response.json')) }
|
2020-03-11 11:09:37 -04:00
|
|
|
|
|
|
|
subject { described_class.new(client) }
|
|
|
|
|
|
|
|
let(:namespace) { "autodevops-deploy-9-production" }
|
|
|
|
let(:pod_name) { "production-6866bc8974-m4sk4" }
|
|
|
|
let(:container_name) { "auto-deploy-app" }
|
|
|
|
let(:search) { "foo +bar "}
|
|
|
|
let(:start_time) { "2019-12-13T14:35:34.034Z" }
|
|
|
|
let(:end_time) { "2019-12-13T14:35:34.034Z" }
|
2020-03-12 20:09:34 -04:00
|
|
|
let(:cursor) { "9999934,1572449784442" }
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
let(:body) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query.json')) }
|
|
|
|
let(:body_with_container) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_container.json')) }
|
|
|
|
let(:body_with_search) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_search.json')) }
|
|
|
|
let(:body_with_times) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_times.json')) }
|
|
|
|
let(:body_with_start_time) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_start_time.json')) }
|
|
|
|
let(:body_with_end_time) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_end_time.json')) }
|
|
|
|
let(:body_with_cursor) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_cursor.json')) }
|
|
|
|
let(:body_with_filebeat_6) { Gitlab::Json.parse(fixture_file('lib/elasticsearch/query_with_filebeat_6.json')) }
|
2020-03-11 11:09:37 -04:00
|
|
|
|
|
|
|
RSpec::Matchers.define :a_hash_equal_to_json do |expected|
|
|
|
|
match do |actual|
|
|
|
|
actual.as_json == expected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#pod_logs' do
|
|
|
|
it 'returns the logs as an array' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can further filter the logs by container name' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_container)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, container_name: container_name)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can further filter the logs by search' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_search)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, search: search)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can further filter the logs by start_time and end_time' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_times)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, start_time: start_time, end_time: end_time)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can further filter the logs by only start_time' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_start_time)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, start_time: start_time)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can further filter the logs by only end_time' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_end_time)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, end_time: end_time)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can search after a cursor' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_cursor)).and_return(es_response)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, cursor: cursor)
|
2020-03-12 20:09:34 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
it 'can search on filebeat 6' do
|
|
|
|
expect(client).to receive(:search).with(body: a_hash_equal_to_json(body_with_filebeat_6)).and_return(es_response)
|
|
|
|
|
2020-05-12 20:07:50 -04:00
|
|
|
result = subject.pod_logs(namespace, pod_name: pod_name, chart_above_v2: false)
|
2020-04-21 11:21:10 -04:00
|
|
|
expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
|
|
|
|
end
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
end
|