diff options
Diffstat (limited to 'spec/services/pod_logs')
-rw-r--r-- | spec/services/pod_logs/base_service_spec.rb | 110 | ||||
-rw-r--r-- | spec/services/pod_logs/elasticsearch_service_spec.rb | 2 | ||||
-rw-r--r-- | spec/services/pod_logs/kubernetes_service_spec.rb | 107 |
3 files changed, 105 insertions, 114 deletions
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 |