Commit 7d673308 authored by Reuben Pereira's avatar Reuben Pereira Committed by Rémy Coutable

Refactor environments controller spec

- Now that we've separated the logs HTML and JSON format responses
into 2 APIs, refactor the specs to reflect this.
parent 59371992
......@@ -81,25 +81,6 @@ describe Projects::EnvironmentsController do
before do
stub_licensed_features(pod_logs: true)
create(:cluster, :provided_by_gcp,
environment_scope: '*', projects: [project])
create(:deployment, :success, environment: environment)
allow_any_instance_of(Gitlab::Kubernetes::RolloutStatus).to receive(:instances)
.and_return([{ pod_name: pod_name }])
end
shared_examples 'resource not found' do |message|
it 'returns 400' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq(message)
expect(json_response['pods']).to match_array([pod_name])
expect(json_response['pod_name']).to eq(pod_name)
expect(json_response['container_name']).to eq(container)
end
end
context 'when unlicensed' do
......@@ -122,104 +103,119 @@ describe Projects::EnvironmentsController do
expect(response).to render_template 'logs'
end
end
end
describe 'GET k8s_pod_logs' do
let(:pod_name) { "foo" }
let(:container) { 'container-1' }
let(:service_result) do
{
status: :success,
logs: ['Log 1', 'Log 2', 'Log 3'],
message: 'message',
pods: [pod_name],
pod_name: pod_name,
container_name: container
}
end
before do
stub_licensed_features(pod_logs: true)
allow_any_instance_of(PodLogsService).to receive(:execute).and_return(service_result)
end
shared_examples 'resource not found' do |message|
it 'returns 400', :aggregate_failures do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq(message)
expect(json_response['pods']).to match_array([pod_name])
expect(json_response['pod_name']).to eq(pod_name)
expect(json_response['container_name']).to eq(container)
end
end
it 'returns the logs for a specific pod', :aggregate_failures do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:success)
expect(json_response["logs"]).to match_array(["Log 1", "Log 2", "Log 3"])
expect(json_response["pods"]).to match_array([pod_name])
expect(json_response['message']).to eq(service_result[:message])
expect(json_response['pod_name']).to eq(pod_name)
expect(json_response['container_name']).to eq(container)
end
it 'registers a usage of the endpoint' do
expect(::Gitlab::UsageCounters::PodLogs).to receive(:increment).with(project.id)
context 'when using JSON format' do
let(:container) { 'container-1' }
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
end
context 'when kubernetes API returns error' do
let(:service_result) do
{
status: :success,
logs: ['Log 1', 'Log 2', 'Log 3'],
message: 'message',
status: :error,
message: 'Kubernetes API returned status code: 400',
pods: [pod_name],
pod_name: pod_name,
container_name: container
}
end
before do
allow_any_instance_of(PodLogsService).to receive(:execute).and_return(service_result)
end
it 'returns the logs for a specific pod' do
it 'returns bad request' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:success)
expect(json_response["logs"]).to match_array(["Log 1", "Log 2", "Log 3"])
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response["logs"]).to eq(nil)
expect(json_response["pods"]).to match_array([pod_name])
expect(json_response['message']).to eq(service_result[:message])
expect(json_response["message"]).to eq('Kubernetes API returned status code: 400')
expect(json_response['pod_name']).to eq(pod_name)
expect(json_response['container_name']).to eq(container)
end
end
it 'registers a usage of the endpoint' do
expect(::Gitlab::UsageCounters::PodLogs).to receive(:increment).with(project.id)
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
end
context 'when kubernetes API returns error' do
let(:service_result) do
{
status: :error,
message: 'Kubernetes API returned status code: 400',
pods: [pod_name],
pod_name: pod_name,
container_name: container
}
end
it 'returns bad request' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response["logs"]).to eq(nil)
expect(json_response["pods"]).to match_array([pod_name])
expect(json_response["message"]).to eq('Kubernetes API returned status code: 400')
expect(json_response['pod_name']).to eq(pod_name)
expect(json_response['container_name']).to eq(container)
end
context 'when pod does not exist' do
let(:service_result) do
{
status: :error,
message: 'Pod not found',
pods: [pod_name],
pod_name: pod_name,
container_name: container
}
end
context 'when pod does not exist' do
let(:service_result) do
{
status: :error,
message: 'Pod not found',
pods: [pod_name],
pod_name: pod_name,
container_name: container
}
end
it_behaves_like 'resource not found', 'Pod not found'
end
it_behaves_like 'resource not found', 'Pod not found'
context 'when service returns error without pods, pod_name, container_name' do
let(:service_result) do
{
status: :error,
message: 'No deployment platform'
}
end
context 'when service returns error without pods, pod_name, container_name' do
let(:service_result) do
{
status: :error,
message: 'No deployment platform'
}
end
it 'returns the error without pods, pod_name and container_name' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
it 'returns the error without pods, pod_name and container_name' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('No deployment platform')
expect(json_response.keys).to contain_exactly('message', 'status')
end
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('No deployment platform')
expect(json_response.keys).to contain_exactly('message', 'status')
end
end
context 'when service returns status processing' do
let(:service_result) { { status: :processing } }
context 'when service returns status processing' do
let(:service_result) { { status: :processing } }
it 'renders accepted' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
it 'renders accepted' do
get :k8s_pod_logs, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:accepted)
end
expect(response).to have_gitlab_http_status(:accepted)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment