diff options
Diffstat (limited to 'spec')
18 files changed, 247 insertions, 162 deletions
diff --git a/spec/fixtures/lib/elasticsearch/logs_response.json b/spec/fixtures/lib/elasticsearch/logs_response.json index 7a733882089..2dbc6f569a5 100644 --- a/spec/fixtures/lib/elasticsearch/logs_response.json +++ b/spec/fixtures/lib/elasticsearch/logs_response.json @@ -19,7 +19,12 @@ "_score": null, "_source": { "message": "10.8.2.1 - - [25/Oct/2019:08:03:22 UTC] \"GET / HTTP/1.1\" 200 13", - "@timestamp": "2019-12-13T14:35:34.034Z" + "@timestamp": "2019-12-13T14:35:34.034Z", + "kubernetes": { + "pod": { + "name": "production-6866bc8974-m4sk4" + } + } }, "sort": [ 9999998, @@ -33,7 +38,12 @@ "_score": null, "_source": { "message": "10.8.2.1 - - [27/Oct/2019:23:49:54 UTC] \"GET / HTTP/1.1\" 200 13", - "@timestamp": "2019-12-13T14:35:35.034Z" + "@timestamp": "2019-12-13T14:35:35.034Z", + "kubernetes": { + "pod": { + "name": "production-6866bc8974-m4sk4" + } + } }, "sort": [ 9999949, @@ -47,7 +57,12 @@ "_score": null, "_source": { "message": "10.8.2.1 - - [04/Nov/2019:23:09:24 UTC] \"GET / HTTP/1.1\" 200 13", - "@timestamp": "2019-12-13T14:35:36.034Z" + "@timestamp": "2019-12-13T14:35:36.034Z", + "kubernetes": { + "pod": { + "name": "production-6866bc8974-m4sk4" + } + } }, "sort": [ 9999944, @@ -61,7 +76,12 @@ "_score": null, "_source": { "message": "- -\u003e /", - "@timestamp": "2019-12-13T14:35:37.034Z" + "@timestamp": "2019-12-13T14:35:37.034Z", + "kubernetes": { + "pod": { + "name": "production-6866bc8974-m4sk4" + } + } }, "sort": [ 9999934, diff --git a/spec/fixtures/lib/elasticsearch/query.json b/spec/fixtures/lib/elasticsearch/query.json index 565c871b1c7..75164a7439f 100644 --- a/spec/fixtures/lib/elasticsearch/query.json +++ b/spec/fixtures/lib/elasticsearch/query.json @@ -33,7 +33,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/fixtures/lib/elasticsearch/query_with_container.json b/spec/fixtures/lib/elasticsearch/query_with_container.json index 21eac5d7dbe..11bc653441c 100644 --- a/spec/fixtures/lib/elasticsearch/query_with_container.json +++ b/spec/fixtures/lib/elasticsearch/query_with_container.json @@ -40,7 +40,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/fixtures/lib/elasticsearch/query_with_cursor.json b/spec/fixtures/lib/elasticsearch/query_with_cursor.json index 1264fdb0322..c5b81e97d3c 100644 --- a/spec/fixtures/lib/elasticsearch/query_with_cursor.json +++ b/spec/fixtures/lib/elasticsearch/query_with_cursor.json @@ -37,7 +37,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/fixtures/lib/elasticsearch/query_with_end_time.json b/spec/fixtures/lib/elasticsearch/query_with_end_time.json index 2859e6427d4..226e0f115e7 100644 --- a/spec/fixtures/lib/elasticsearch/query_with_end_time.json +++ b/spec/fixtures/lib/elasticsearch/query_with_end_time.json @@ -42,7 +42,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/fixtures/lib/elasticsearch/query_with_search.json b/spec/fixtures/lib/elasticsearch/query_with_search.json index 3c9bed047fa..ca63c12f3b8 100644 --- a/spec/fixtures/lib/elasticsearch/query_with_search.json +++ b/spec/fixtures/lib/elasticsearch/query_with_search.json @@ -42,7 +42,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/fixtures/lib/elasticsearch/query_with_start_time.json b/spec/fixtures/lib/elasticsearch/query_with_start_time.json index 0c5cfca42f7..cb3e37de8a7 100644 --- a/spec/fixtures/lib/elasticsearch/query_with_start_time.json +++ b/spec/fixtures/lib/elasticsearch/query_with_start_time.json @@ -42,7 +42,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/fixtures/lib/elasticsearch/query_with_times.json b/spec/fixtures/lib/elasticsearch/query_with_times.json index 7108d42217e..91d28b28842 100644 --- a/spec/fixtures/lib/elasticsearch/query_with_times.json +++ b/spec/fixtures/lib/elasticsearch/query_with_times.json @@ -43,7 +43,8 @@ ], "_source": [ "@timestamp", - "message" + "message", + "kubernetes.pod.name" ], "size": 500 } diff --git a/spec/frontend/logs/components/environment_logs_spec.js b/spec/frontend/logs/components/environment_logs_spec.js index c638b4c05f9..162aeb1cc56 100644 --- a/spec/frontend/logs/components/environment_logs_spec.js +++ b/spec/frontend/logs/components/environment_logs_spec.js @@ -300,9 +300,10 @@ describe('EnvironmentLogs', () => { const items = findPodsDropdown().findAll(GlDropdownItem); expect(findPodsDropdown().props('text')).toBe(mockPodName); - expect(items.length).toBe(mockPods.length); + expect(items.length).toBe(mockPods.length + 1); + expect(items.at(0).text()).toBe('All pods'); mockPods.forEach((pod, i) => { - const item = items.at(i); + const item = items.at(i + 1); expect(item.text()).toBe(pod); }); }); @@ -345,7 +346,7 @@ describe('EnvironmentLogs', () => { expect(dispatch).not.toHaveBeenCalledWith(`${module}/showPodLogs`, expect.anything()); - items.at(index).vm.$emit('click'); + items.at(index + 1).vm.$emit('click'); expect(dispatch).toHaveBeenCalledWith(`${module}/showPodLogs`, mockPods[index]); }); diff --git a/spec/frontend/logs/mock_data.js b/spec/frontend/logs/mock_data.js index 1a84d6edd12..537582cff5a 100644 --- a/spec/frontend/logs/mock_data.js +++ b/spec/frontend/logs/mock_data.js @@ -32,15 +32,93 @@ export const mockPods = [ ]; export const mockLogsResult = [ - { timestamp: '2019-12-13T13:43:18.2760123Z', message: 'Log 1' }, - { timestamp: '2019-12-13T13:43:18.2760123Z', message: 'Log 2' }, - { timestamp: '2019-12-13T13:43:26.8420123Z', message: 'Log 3' }, + { + timestamp: '2019-12-13T13:43:18.2760123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:29:48 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:18.2760123Z', + message: '- -> /', + pod: 'bar', + }, + { + timestamp: '2019-12-13T13:43:26.8420123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:29:57 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:26.8420123Z', + message: '- -> /', + pod: 'bar', + }, + { + timestamp: '2019-12-13T13:43:28.3710123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:29:58 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:28.3710123Z', + message: '- -> /', + pod: 'bar', + }, + { + timestamp: '2019-12-13T13:43:36.8860123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:30:07 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:36.8860123Z', + message: '- -> /', + pod: 'bar', + }, + { + timestamp: '2019-12-13T13:43:38.4000123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:30:08 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:38.4000123Z', + message: '- -> /', + pod: 'bar', + }, + { + timestamp: '2019-12-13T13:43:46.8420123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:30:17 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:46.8430123Z', + message: '- -> /', + pod: 'bar', + }, + { + timestamp: '2019-12-13T13:43:48.3240123Z', + message: '10.36.0.1 - - [16/Oct/2019:06:30:18 UTC] "GET / HTTP/1.1" 200 13', + pod: 'foo', + }, + { + timestamp: '2019-12-13T13:43:48.3250123Z', + message: '- -> /', + pod: 'bar', + }, ]; export const mockTrace = [ - 'Dec 13 13:43:18.276Z | Log 1', - 'Dec 13 13:43:18.276Z | Log 2', - 'Dec 13 13:43:26.842Z | Log 3', + 'Dec 13 13:43:18.276Z | foo | 10.36.0.1 - - [16/Oct/2019:06:29:48 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:18.276Z | bar | - -> /', + 'Dec 13 13:43:26.842Z | foo | 10.36.0.1 - - [16/Oct/2019:06:29:57 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:26.842Z | bar | - -> /', + 'Dec 13 13:43:28.371Z | foo | 10.36.0.1 - - [16/Oct/2019:06:29:58 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:28.371Z | bar | - -> /', + 'Dec 13 13:43:36.886Z | foo | 10.36.0.1 - - [16/Oct/2019:06:30:07 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:36.886Z | bar | - -> /', + 'Dec 13 13:43:38.400Z | foo | 10.36.0.1 - - [16/Oct/2019:06:30:08 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:38.400Z | bar | - -> /', + 'Dec 13 13:43:46.842Z | foo | 10.36.0.1 - - [16/Oct/2019:06:30:17 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:46.843Z | bar | - -> /', + 'Dec 13 13:43:48.324Z | foo | 10.36.0.1 - - [16/Oct/2019:06:30:18 UTC] "GET / HTTP/1.1" 200 13', + 'Dec 13 13:43:48.325Z | bar | - -> /', ]; export const mockResponse = { diff --git a/spec/frontend/logs/stores/actions_spec.js b/spec/frontend/logs/stores/actions_spec.js index 1512797e1bc..1754931bcaf 100644 --- a/spec/frontend/logs/stores/actions_spec.js +++ b/spec/frontend/logs/stores/actions_spec.js @@ -13,7 +13,7 @@ import { fetchMoreLogsPrepend, } from '~/logs/stores/actions'; -import { defaultTimeRange } from '~/monitoring/constants'; +import { defaultTimeRange } from '~/vue_shared/constants'; import axios from '~/lib/utils/axios_utils'; import flash from '~/flash'; @@ -172,14 +172,13 @@ describe('Logs Store actions', () => { describe('fetchLogs', () => { beforeEach(() => { expectedMutations = [ - { type: types.REQUEST_PODS_DATA }, { type: types.REQUEST_LOGS_DATA }, - { type: types.SET_CURRENT_POD_NAME, payload: mockPodName }, - { type: types.RECEIVE_PODS_DATA_SUCCESS, payload: mockPods }, { type: types.RECEIVE_LOGS_DATA_SUCCESS, payload: { logs: mockLogsResult, cursor: mockNextCursor }, }, + { type: types.SET_CURRENT_POD_NAME, payload: mockPodName }, + { type: types.RECEIVE_PODS_DATA_SUCCESS, payload: mockPods }, ]; expectedActions = []; @@ -364,7 +363,6 @@ describe('Logs Store actions', () => { null, state, [ - { type: types.REQUEST_PODS_DATA }, { type: types.REQUEST_LOGS_DATA }, { type: types.RECEIVE_PODS_DATA_ERROR }, { type: types.RECEIVE_LOGS_DATA_ERROR }, diff --git a/spec/frontend/logs/stores/mutations_spec.js b/spec/frontend/logs/stores/mutations_spec.js index eae838a31d4..37db355af09 100644 --- a/spec/frontend/logs/stores/mutations_spec.js +++ b/spec/frontend/logs/stores/mutations_spec.js @@ -223,17 +223,6 @@ describe('Logs Store Mutations', () => { }); }); - describe('REQUEST_PODS_DATA', () => { - it('receives pods data', () => { - mutations[types.REQUEST_PODS_DATA](state); - - expect(state.pods).toEqual( - expect.objectContaining({ - options: [], - }), - ); - }); - }); describe('RECEIVE_PODS_DATA_SUCCESS', () => { it('receives pods data success', () => { mutations[types.RECEIVE_PODS_DATA_SUCCESS](state, mockPods); diff --git a/spec/frontend/monitoring/components/__snapshots__/dashboard_template_spec.js.snap b/spec/frontend/monitoring/components/__snapshots__/dashboard_template_spec.js.snap index 77f7f2e0609..e37043e5d4d 100644 --- a/spec/frontend/monitoring/components/__snapshots__/dashboard_template_spec.js.snap +++ b/spec/frontend/monitoring/components/__snapshots__/dashboard_template_spec.js.snap @@ -78,6 +78,7 @@ exports[`Dashboard template matches the default snapshot 1`] = ` label-size="sm" > <date-time-picker-stub + customenabled="true" options="[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]" value="[object Object]" /> diff --git a/spec/frontend/monitoring/components/dashboard_url_time_spec.js b/spec/frontend/monitoring/components/dashboard_url_time_spec.js index 161c64dd74b..bf5a11a536e 100644 --- a/spec/frontend/monitoring/components/dashboard_url_time_spec.js +++ b/spec/frontend/monitoring/components/dashboard_url_time_spec.js @@ -7,7 +7,7 @@ import { mockProjectDir } from '../mock_data'; import Dashboard from '~/monitoring/components/dashboard.vue'; import { createStore } from '~/monitoring/stores'; -import { defaultTimeRange } from '~/monitoring/constants'; +import { defaultTimeRange } from '~/vue_shared/constants'; import { propsData } from '../init_utils'; jest.mock('~/flash'); diff --git a/spec/lib/gitlab/elasticsearch/logs_spec.rb b/spec/lib/gitlab/elasticsearch/logs_spec.rb index f82c4acb82b..6b9d1dbef99 100644 --- a/spec/lib/gitlab/elasticsearch/logs_spec.rb +++ b/spec/lib/gitlab/elasticsearch/logs_spec.rb @@ -5,10 +5,10 @@ require 'spec_helper' describe Gitlab::Elasticsearch::Logs do let(:client) { Elasticsearch::Transport::Client } - let(:es_message_1) { { timestamp: "2019-12-13T14:35:34.034Z", 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", 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", 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", message: "- -\u003e /" } } + 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')) } @@ -40,49 +40,49 @@ describe Gitlab::Elasticsearch::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) + 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, container_name: container_name) + 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, search: search) + 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, start_time: start_time, end_time: end_time) + 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, start_time: start_time) + 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, end_time: end_time) + 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, cursor: cursor) + 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 diff --git a/spec/services/pod_logs/base_service_spec.rb b/spec/services/pod_logs/base_service_spec.rb index a18fda544df..d93ea51eae1 100644 --- a/spec/services/pod_logs/base_service_spec.rb +++ b/spec/services/pod_logs/base_service_spec.rb @@ -78,9 +78,7 @@ describe ::PodLogs::BaseService do expect(result[:message]).to eq('Namespace is empty') end end - end - describe '#check_param_lengths' do context 'when pod_name and container_name are provided' do let(:params) do { @@ -90,43 +88,13 @@ describe ::PodLogs::BaseService do end it 'returns success' do - result = subject.send(:check_param_lengths, {}) + result = subject.send(:check_arguments, {}) expect(result[:status]).to eq(:success) expect(result[:pod_name]).to eq(pod_name) expect(result[:container_name]).to eq(container_name) end end - - context 'when pod_name is too long' do - let(:params) do - { - 'pod_name' => "a very long string." * 15 - } - end - - it 'returns an error' do - result = subject.send(:check_param_lengths, {}) - - expect(result[:status]).to eq(:error) - expect(result[:message]).to eq('pod_name cannot be larger than 253 chars') - end - end - - context 'when container_name is too long' do - let(:params) do - { - 'container_name' => "a very long string." * 15 - } - end - - it 'returns an error' do - result = subject.send(:check_param_lengths, {}) - - expect(result[:status]).to eq(:error) - expect(result[:message]).to eq('container_name cannot be larger than 253 chars') - end - end end describe '#get_raw_pods' do @@ -150,80 +118,4 @@ describe ::PodLogs::BaseService do expect(result[:pods]).to eq([pod_name]) end end - - describe '#check_pod_name' do - it 'returns success if pod_name was specified' do - result = subject.send(:check_pod_name, pod_name: pod_name, pods: [pod_name]) - - expect(result[:status]).to eq(:success) - expect(result[:pod_name]).to eq(pod_name) - end - - it 'returns success if pod_name was not specified but there are pods' do - result = subject.send(:check_pod_name, pod_name: nil, pods: [pod_name]) - - expect(result[:status]).to eq(:success) - expect(result[:pod_name]).to eq(pod_name) - end - - it 'returns error if pod_name was not specified and there are no pods' do - result = subject.send(:check_pod_name, pod_name: nil, pods: []) - - expect(result[:status]).to eq(:error) - expect(result[:message]).to eq('No pods available') - end - - it 'returns error if pod_name was specified but does not exist' do - result = subject.send(:check_pod_name, pod_name: 'another_pod', pods: [pod_name]) - - expect(result[:status]).to eq(:error) - expect(result[:message]).to eq('Pod does not exist') - end - end - - describe '#check_container_name' do - it 'returns success if container_name was specified' do - result = subject.send(:check_container_name, - container_name: container_name, - pod_name: pod_name, - raw_pods: raw_pods - ) - - expect(result[:status]).to eq(:success) - expect(result[:container_name]).to eq(container_name) - end - - it 'returns success if container_name was not specified and there are containers' do - result = subject.send(:check_container_name, - pod_name: pod_name, - raw_pods: raw_pods - ) - - expect(result[:status]).to eq(:success) - expect(result[:container_name]).to eq(container_name) - end - - it 'returns error if container_name was not specified and there are no containers on the pod' do - raw_pods.first.spec.containers = [] - - result = subject.send(:check_container_name, - pod_name: pod_name, - raw_pods: raw_pods - ) - - expect(result[:status]).to eq(:error) - expect(result[:message]).to eq('No containers available') - end - - it 'returns error if container_name was specified but does not exist' do - result = subject.send(:check_container_name, - container_name: 'foo', - pod_name: pod_name, - raw_pods: raw_pods - ) - - expect(result[:status]).to eq(:error) - expect(result[:message]).to eq('Container does not exist') - end - end end diff --git a/spec/services/pod_logs/elasticsearch_service_spec.rb b/spec/services/pod_logs/elasticsearch_service_spec.rb index 984a303e9e3..1387d2cfb8e 100644 --- a/spec/services/pod_logs/elasticsearch_service_spec.rb +++ b/spec/services/pod_logs/elasticsearch_service_spec.rb @@ -170,7 +170,7 @@ describe ::PodLogs::ElasticsearchService do .and_return(Elasticsearch::Transport::Client.new) allow_any_instance_of(::Gitlab::Elasticsearch::Logs) .to receive(:pod_logs) - .with(namespace, pod_name, container_name: container_name, search: search, start_time: start_time, end_time: end_time, cursor: cursor) + .with(namespace, pod_name: pod_name, container_name: container_name, search: search, start_time: start_time, end_time: end_time, cursor: cursor) .and_return({ logs: expected_logs, cursor: expected_cursor }) result = subject.send(:pod_logs, result_arg) diff --git a/spec/services/pod_logs/kubernetes_service_spec.rb b/spec/services/pod_logs/kubernetes_service_spec.rb index 9fab88a14f6..8ce79d4c318 100644 --- a/spec/services/pod_logs/kubernetes_service_spec.rb +++ b/spec/services/pod_logs/kubernetes_service_spec.rb @@ -9,13 +9,18 @@ describe ::PodLogs::KubernetesService do let(:namespace) { 'autodevops-deploy-9-production' } let(:pod_name) { 'pod-1' } - let(:container_name) { 'container-1' } + let(:container_name) { 'container-0' } let(:params) { {} } let(:raw_logs) do "2019-12-13T14:04:22.123456Z Log 1\n2019-12-13T14:04:23.123456Z Log 2\n" \ "2019-12-13T14:04:24.123456Z Log 3" end + let(:raw_pods) do + JSON.parse([ + kube_pod(name: pod_name) + ].to_json, object_class: OpenStruct) + end subject { described_class.new(cluster, namespace, params: params) } @@ -140,9 +145,9 @@ describe ::PodLogs::KubernetesService do let(:expected_logs) do [ - { message: "Log 1", timestamp: "2019-12-13T14:04:22.123456Z" }, - { message: "Log 2", timestamp: "2019-12-13T14:04:23.123456Z" }, - { message: "Log 3", timestamp: "2019-12-13T14:04:24.123456Z" } + { message: "Log 1", pod: 'pod-1', timestamp: "2019-12-13T14:04:22.123456Z" }, + { message: "Log 2", pod: 'pod-1', timestamp: "2019-12-13T14:04:23.123456Z" }, + { message: "Log 3", pod: 'pod-1', timestamp: "2019-12-13T14:04:24.123456Z" } ] end @@ -163,4 +168,98 @@ describe ::PodLogs::KubernetesService do end end end + + describe '#check_pod_name' do + it 'returns success if pod_name was specified' do + result = subject.send(:check_pod_name, pod_name: pod_name, pods: [pod_name]) + + expect(result[:status]).to eq(:success) + expect(result[:pod_name]).to eq(pod_name) + end + + it 'returns success if pod_name was not specified but there are pods' do + result = subject.send(:check_pod_name, pod_name: nil, pods: [pod_name]) + + expect(result[:status]).to eq(:success) + expect(result[:pod_name]).to eq(pod_name) + end + + it 'returns error if pod_name was not specified and there are no pods' do + result = subject.send(:check_pod_name, pod_name: nil, pods: []) + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('No pods available') + end + + it 'returns error if pod_name was specified but does not exist' do + result = subject.send(:check_pod_name, pod_name: 'another_pod', pods: [pod_name]) + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('Pod does not exist') + end + + it 'returns error if pod_name is too long' do + result = subject.send(:check_pod_name, pod_name: "a very long string." * 15, pods: [pod_name]) + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('pod_name cannot be larger than 253 chars') + end + end + + describe '#check_container_name' do + it 'returns success if container_name was specified' do + result = subject.send(:check_container_name, + container_name: container_name, + pod_name: pod_name, + raw_pods: raw_pods + ) + + expect(result[:status]).to eq(:success) + expect(result[:container_name]).to eq(container_name) + end + + it 'returns success if container_name was not specified and there are containers' do + result = subject.send(:check_container_name, + pod_name: pod_name, + raw_pods: raw_pods + ) + + expect(result[:status]).to eq(:success) + expect(result[:container_name]).to eq(container_name) + end + + it 'returns error if container_name was not specified and there are no containers on the pod' do + raw_pods.first.spec.containers = [] + + result = subject.send(:check_container_name, + pod_name: pod_name, + raw_pods: raw_pods + ) + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('No containers available') + end + + it 'returns error if container_name was specified but does not exist' do + result = subject.send(:check_container_name, + container_name: 'foo', + pod_name: pod_name, + raw_pods: raw_pods + ) + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('Container does not exist') + end + + it 'returns error if container_name is too long' do + result = subject.send(:check_container_name, + container_name: "a very long string." * 15, + pod_name: pod_name, + raw_pods: raw_pods + ) + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq('container_name cannot be larger than 253 chars') + end + end end |