summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/elasticsearch/logs_spec.rb
blob: 6b9d1dbef993851315290f201ea06c3c034638dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Elasticsearch::Logs do
  let(:client) { Elasticsearch::Transport::Client }

  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 /" } }

  let(:es_response) { JSON.parse(fixture_file('lib/elasticsearch/logs_response.json')) }

  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" }
  let(:cursor) { "9999934,1572449784442" }

  let(:body) { JSON.parse(fixture_file('lib/elasticsearch/query.json')) }
  let(:body_with_container) { JSON.parse(fixture_file('lib/elasticsearch/query_with_container.json')) }
  let(:body_with_search) { JSON.parse(fixture_file('lib/elasticsearch/query_with_search.json')) }
  let(:body_with_times) { JSON.parse(fixture_file('lib/elasticsearch/query_with_times.json')) }
  let(:body_with_start_time) { JSON.parse(fixture_file('lib/elasticsearch/query_with_start_time.json')) }
  let(:body_with_end_time) { JSON.parse(fixture_file('lib/elasticsearch/query_with_end_time.json')) }
  let(:body_with_cursor) { JSON.parse(fixture_file('lib/elasticsearch/query_with_cursor.json')) }

  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)

      result = subject.pod_logs(namespace, pod_name: pod_name)
      expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
    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)

      result = subject.pod_logs(namespace, pod_name: pod_name, container_name: container_name)
      expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
    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)

      result = subject.pod_logs(namespace, pod_name: pod_name, search: search)
      expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
    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)

      result = subject.pod_logs(namespace, pod_name: pod_name, start_time: start_time, end_time: end_time)
      expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
    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)

      result = subject.pod_logs(namespace, pod_name: pod_name, start_time: start_time)
      expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
    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)

      result = subject.pod_logs(namespace, pod_name: pod_name, end_time: end_time)
      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)

      result = subject.pod_logs(namespace, pod_name: pod_name, cursor: cursor)
      expect(result).to eq(logs: [es_message_4, es_message_3, es_message_2, es_message_1], cursor: cursor)
    end
  end
end