2020-03-11 15:09:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ::PodLogs::BaseService do
|
|
|
|
include KubernetesHelpers
|
|
|
|
|
|
|
|
let_it_be(:cluster) { create(:cluster, :provided_by_gcp, environment_scope: '*') }
|
|
|
|
let(:namespace) { 'autodevops-deploy-9-production' }
|
|
|
|
|
|
|
|
let(:pod_name) { 'pod-1' }
|
2020-03-18 06:09:38 +00:00
|
|
|
let(:pod_name_2) { 'pod-2' }
|
2020-03-11 15:09:37 +00:00
|
|
|
let(:container_name) { 'container-0' }
|
|
|
|
let(:params) { {} }
|
|
|
|
let(:raw_pods) do
|
2020-04-15 18:09:36 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: pod_name,
|
|
|
|
container_names: %w(container-0-0 container-0-1)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: pod_name_2,
|
|
|
|
container_names: %w(container-1-0 container-1-1)
|
|
|
|
}
|
|
|
|
]
|
2020-03-11 15:09:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(cluster, namespace, params: params) }
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'container_name' => container_name,
|
|
|
|
'another_param' => 'foo'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters the parameters' do
|
|
|
|
expect(subject.cluster).to eq(cluster)
|
|
|
|
expect(subject.namespace).to eq(namespace)
|
|
|
|
expect(subject.params).to eq({
|
|
|
|
'container_name' => container_name
|
|
|
|
})
|
|
|
|
expect(subject.params.equal?(params)).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#check_arguments' do
|
|
|
|
context 'when cluster and namespace are provided' do
|
|
|
|
it 'returns success' do
|
|
|
|
result = subject.send(:check_arguments, {})
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cluster is nil' do
|
|
|
|
let(:cluster) { nil }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
result = subject.send(:check_arguments, {})
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Cluster does not exist')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace is nil' do
|
|
|
|
let(:namespace) { nil }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
result = subject.send(:check_arguments, {})
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Namespace is empty')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace is empty' do
|
|
|
|
let(:namespace) { '' }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
result = subject.send(:check_arguments, {})
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Namespace is empty')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pod_name and container_name are provided' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'pod_name' => pod_name,
|
|
|
|
'container_name' => container_name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
2020-03-18 03:09:43 +00:00
|
|
|
result = subject.send(:check_arguments, {})
|
2020-03-11 15:09:37 +00:00
|
|
|
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
expect(result[:pod_name]).to eq(pod_name)
|
|
|
|
expect(result[:container_name]).to eq(container_name)
|
|
|
|
end
|
|
|
|
end
|
2020-05-07 09:09:51 +00:00
|
|
|
|
|
|
|
context 'when pod_name is not a string' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'pod_name' => { something_that_is: :not_a_string }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error' do
|
|
|
|
result = subject.send(:check_arguments, {})
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Invalid pod_name')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when container_name is not a string' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'container_name' => { something_that_is: :not_a_string }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error' do
|
|
|
|
result = subject.send(:check_arguments, {})
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Invalid container_name')
|
|
|
|
end
|
|
|
|
end
|
2020-03-11 15:09:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#get_pod_names' do
|
|
|
|
it 'returns success with a list of pods' do
|
|
|
|
result = subject.send(:get_pod_names, raw_pods: raw_pods)
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:success)
|
2020-03-18 06:09:38 +00:00
|
|
|
expect(result[:pods]).to eq([pod_name, pod_name_2])
|
2020-03-11 15:09:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|