2020-04-01 08:08:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Terraform::State do
|
2020-06-16 11:08:32 -04:00
|
|
|
include HttpBasicAuthHelpers
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:developer) { create(:user, developer_projects: [project]) }
|
|
|
|
let_it_be(:maintainer) { create(:user, maintainer_projects: [project]) }
|
|
|
|
|
|
|
|
let!(:state) { create(:terraform_state, :with_file, project: project) }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
let(:current_user) { maintainer }
|
2020-06-16 11:08:32 -04:00
|
|
|
let(:auth_header) { user_basic_auth_header(current_user) }
|
2020-04-21 11:21:10 -04:00
|
|
|
let(:project_id) { project.id }
|
|
|
|
let(:state_name) { state.name }
|
|
|
|
let(:state_path) { "/projects/#{project_id}/terraform/state/#{state_name}" }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
|
|
|
before do
|
2020-04-21 11:21:10 -04:00
|
|
|
stub_terraform_state_object_storage(Terraform::StateUploader)
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/terraform/state/:name' do
|
2020-04-21 11:21:10 -04:00
|
|
|
subject(:request) { get api(state_path), headers: auth_header }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'without authentication' do
|
2020-06-16 11:08:32 -04:00
|
|
|
let(:auth_header) { basic_auth_header('bad', 'token') }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'returns 401 if user is not authenticated' do
|
|
|
|
request
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
context 'personal acceess token authentication' do
|
|
|
|
context 'with maintainer permissions' do
|
|
|
|
let(:current_user) { maintainer }
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
it 'returns terraform state belonging to a project of given state name' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(state.file.read)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a project that does not exist' do
|
|
|
|
let(:project_id) { '0000' }
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
it 'returns not found' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
context 'with developer permissions' do
|
|
|
|
let(:current_user) { developer }
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
it 'returns forbidden if the user cannot access the state' do
|
2020-04-21 11:21:10 -04:00
|
|
|
request
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
context 'job token authentication' do
|
|
|
|
let(:auth_header) { job_basic_auth_header(job) }
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
context 'with maintainer permissions' do
|
|
|
|
let(:job) { create(:ci_build, project: project, user: maintainer) }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
it 'returns terraform state belonging to a project of given state name' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.body).to eq(state.file.read)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a project that does not exist' do
|
|
|
|
let(:project_id) { '0000' }
|
|
|
|
|
|
|
|
it 'returns not found' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with developer permissions' do
|
|
|
|
let(:job) { create(:ci_build, project: project, user: developer) }
|
|
|
|
|
|
|
|
it 'returns forbidden if the user cannot access the state' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/terraform/state/:name' do
|
2020-04-21 11:21:10 -04:00
|
|
|
let(:params) { { 'instance': 'example-instance' } }
|
|
|
|
|
|
|
|
subject(:request) { post api(state_path), headers: auth_header, as: :json, params: params }
|
|
|
|
|
2020-04-01 08:08:00 -04:00
|
|
|
context 'when terraform state with a given name is already present' do
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'with maintainer permissions' do
|
|
|
|
let(:current_user) { maintainer }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'updates the state' do
|
|
|
|
expect { request }.to change { Terraform::State.count }.by(0)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
2020-04-27 11:10:16 -04:00
|
|
|
|
|
|
|
context 'on Unicorn', :unicorn do
|
|
|
|
it 'updates the state' do
|
|
|
|
expect { request }.to change { Terraform::State.count }.by(0)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'without body' do
|
|
|
|
let(:params) { nil }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'returns no content if no body is provided' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with developer permissions' do
|
|
|
|
let(:current_user) { developer }
|
|
|
|
|
|
|
|
it 'returns forbidden' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no terraform state of a given name' do
|
2020-04-21 11:21:10 -04:00
|
|
|
let(:state_name) { 'example2' }
|
|
|
|
|
|
|
|
context 'with maintainer permissions' do
|
|
|
|
let(:current_user) { maintainer }
|
|
|
|
|
|
|
|
it 'creates a new state' do
|
|
|
|
expect { request }.to change { Terraform::State.count }.by(1)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
2020-04-27 11:10:16 -04:00
|
|
|
|
|
|
|
context 'on Unicorn', :unicorn do
|
|
|
|
it 'creates a new state' do
|
|
|
|
expect { request }.to change { Terraform::State.count }.by(1)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'without body' do
|
|
|
|
let(:params) { nil }
|
|
|
|
|
|
|
|
it 'returns no content if no body is provided' do
|
|
|
|
request
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with developer permissions' do
|
|
|
|
let(:current_user) { developer }
|
|
|
|
|
|
|
|
it 'returns forbidden' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/terraform/state/:name' do
|
2020-04-21 11:21:10 -04:00
|
|
|
subject(:request) { delete api(state_path), headers: auth_header }
|
|
|
|
|
|
|
|
context 'with maintainer permissions' do
|
|
|
|
let(:current_user) { maintainer }
|
|
|
|
|
|
|
|
it 'deletes the state' do
|
|
|
|
expect { request }.to change { Terraform::State.count }.by(-1)
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with developer permissions' do
|
|
|
|
let(:current_user) { developer }
|
|
|
|
|
|
|
|
it 'returns forbidden' do
|
|
|
|
expect { request }.to change { Terraform::State.count }.by(0)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /projects/:id/terraform/state/:name/lock' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
ID: '123-456',
|
|
|
|
Version: '0.1',
|
|
|
|
Operation: 'OperationTypePlan',
|
|
|
|
Info: '',
|
|
|
|
Who: "#{current_user.username}",
|
|
|
|
Created: Time.now.utc.iso8601(6),
|
|
|
|
Path: ''
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:request) { post api("#{state_path}/lock"), headers: auth_header, params: params }
|
|
|
|
|
|
|
|
it 'locks the terraform state' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:id/terraform/state/:name/lock' do
|
|
|
|
before do
|
|
|
|
state.lock_xid = '123-456'
|
|
|
|
state.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:request) { delete api("#{state_path}/lock"), headers: auth_header, params: params }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
context 'with the correct lock id' do
|
|
|
|
let(:params) { { ID: '123-456' } }
|
2020-04-01 08:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'removes the terraform state lock' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with no lock id (force-unlock)' do
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it 'removes the terraform state lock' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an incorrect lock id' do
|
|
|
|
let(:params) { { ID: '456-789' } }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:conflict)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a longer than 255 character lock id' do
|
|
|
|
let(:params) { { ID: '0' * 256 } }
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
end
|
2020-04-01 08:08:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|