debian-mirror-gitlab/spec/services/pod_logs/base_service_spec.rb

148 lines
3.6 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ::PodLogs::BaseService do
2020-04-08 14:13:33 +05:30
include KubernetesHelpers
let_it_be(:cluster) { create(:cluster, :provided_by_gcp, environment_scope: '*') }
2021-09-30 23:02:18 +05:30
2020-04-08 14:13:33 +05:30
let(:namespace) { 'autodevops-deploy-9-production' }
let(:pod_name) { 'pod-1' }
let(:pod_name_2) { 'pod-2' }
let(:container_name) { 'container-0' }
let(:params) { {} }
let(:raw_pods) do
2020-04-22 19:07:51 +05:30
[
{
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-04-08 14:13:33 +05:30
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-04-22 19:07:51 +05:30
result = subject.send(:check_arguments, {})
2020-04-08 14:13:33 +05:30
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-24 23:13:21 +05:30
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-04-08 14:13:33 +05:30
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)
expect(result[:pods]).to eq([pod_name, pod_name_2])
end
end
end