Generalize method and constant names for runner management

This commit is contained in:
Sebastian Serth
2021-09-13 12:49:56 +02:00
parent e752df1b3c
commit 30603cb7ab
18 changed files with 139 additions and 110 deletions

View File

@ -8,7 +8,7 @@ describe ExecutionEnvironmentsController do
before do
allow(controller).to receive(:current_user).and_return(user)
allow(controller).to receive(:copy_execution_environment_to_poseidon).and_return(nil)
allow(controller).to receive(:sync_to_runner_management).and_return(nil)
end
describe 'POST #create' do
@ -26,8 +26,8 @@ describe ExecutionEnvironmentsController do
expect { perform_request.call }.to change(ExecutionEnvironment, :count).by(1)
end
it 'registers the execution environment with Poseidon' do
expect(controller).to have_received(:copy_execution_environment_to_poseidon)
it 'registers the execution environment with the runner management' do
expect(controller).to have_received(:sync_to_runner_management)
end
expect_redirect(ExecutionEnvironment.last)
@ -40,8 +40,8 @@ describe ExecutionEnvironmentsController do
expect_status(200)
expect_template(:new)
it 'does not register the execution environment with Poseidon' do
expect(controller).not_to have_received(:copy_execution_environment_to_poseidon)
it 'does not register the execution environment with the runner management' do
expect(controller).not_to have_received(:sync_to_runner_management)
end
end
end
@ -167,7 +167,7 @@ describe ExecutionEnvironmentsController do
context 'with a valid execution environment' do
before do
allow(DockerClient).to receive(:image_tags).at_least(:once).and_return([])
allow(controller).to receive(:copy_execution_environment_to_poseidon).and_return(nil)
allow(controller).to receive(:sync_to_runner_management).and_return(nil)
put :update, params: {execution_environment: FactoryBot.attributes_for(:ruby), id: execution_environment.id}
end
@ -175,8 +175,8 @@ describe ExecutionEnvironmentsController do
expect_assigns(execution_environment: ExecutionEnvironment)
expect_redirect(:execution_environment)
it 'updates the execution environment at Poseidon' do
expect(controller).to have_received(:copy_execution_environment_to_poseidon)
it 'updates the execution environment at the runner management' do
expect(controller).to have_received(:sync_to_runner_management)
end
end
@ -187,25 +187,24 @@ describe ExecutionEnvironmentsController do
expect_status(200)
expect_template(:edit)
it 'does not update the execution environment at Poseidon' do
expect(controller).not_to have_received(:copy_execution_environment_to_poseidon)
it 'does not update the execution environment at the runner management' do
expect(controller).not_to have_received(:sync_to_runner_management)
end
end
end
describe '#synchronize_all_to_poseidon' do
describe '#sync_all_to_runner_management' do
let(:execution_environments) { FactoryBot.build_list(:ruby, 3) }
it 'copies all execution environments to Poseidon' do
it 'copies all execution environments to the runner management' do
allow(ExecutionEnvironment).to receive(:all).and_return(execution_environments)
execution_environments.each do |execution_environment|
allow(execution_environment).to receive(:copy_to_poseidon).and_return(true)
allow(Runner::Strategy::Poseidon).to receive(:sync_environment).with(execution_environment).and_return(true)
expect(Runner::Strategy::Poseidon).to receive(:sync_environment).with(execution_environment).once
end
post :synchronize_all_to_poseidon
expect(execution_environments).to all(have_received(:copy_to_poseidon).once)
post :sync_all_to_runner_management
end
end
end

View File

@ -114,13 +114,58 @@ describe Runner::Strategy::Poseidon do
end
end
describe '::sync_environment' do
let(:action) { -> { described_class.sync_environment(execution_environment) } }
let(:execution_environment) { FactoryBot.create(:ruby) }
it 'makes the correct request to Poseidon' do
allow(Faraday).to receive(:put).and_return(Faraday::Response.new(status: 201))
action.call
expect(Faraday).to have_received(:put) do |url, body, headers|
expect(url).to match(%r{execution-environments/#{execution_environment.id}\z})
expect(body).to eq(execution_environment.to_json)
expect(headers).to include({'Content-Type' => 'application/json'})
end
end
shared_examples 'returns true when the api request was successful' do |status|
it "returns true on status #{status}" do
allow(Faraday).to receive(:put).and_return(Faraday::Response.new(status: status))
expect(action.call).to be_truthy
end
end
shared_examples 'returns false when the api request failed' do |status|
it "returns false on status #{status}" do
allow(Faraday).to receive(:put).and_return(Faraday::Response.new(status: status))
expect(action.call).to be_falsey
end
end
[201, 204].each do |status|
include_examples 'returns true when the api request was successful', status
end
[400, 500].each do |status|
include_examples 'returns false when the api request failed', status
end
it 'returns false if Faraday raises an error' do
allow(Faraday).to receive(:put).and_raise(Faraday::TimeoutError)
expect(action.call).to be_falsey
end
end
describe '::request_from_management' do
let(:action) { -> { described_class.request_from_management(execution_environment) } }
let!(:request_runner_stub) do
WebMock
.stub_request(:post, "#{Runner::BASE_URL}/runners")
.stub_request(:post, "#{described_class.config[:url]}/runners")
.with(
body: {executionEnvironmentId: execution_environment.id, inactivityTimeout: Runner::UNUSED_EXPIRATION_TIME},
body: {
executionEnvironmentId: execution_environment.id,
inactivityTimeout: described_class.config[:unused_runner_expiration_time].seconds,
},
headers: {'Content-Type' => 'application/json'}
)
.to_return(body: response_body, status: response_status)
@ -181,7 +226,7 @@ describe Runner::Strategy::Poseidon do
let(:websocket_url) { 'ws://ws.example.com/path/to/websocket' }
let!(:execute_command_stub) do
WebMock
.stub_request(:post, "#{Runner::BASE_URL}/runners/#{runner_id}/execute")
.stub_request(:post, "#{described_class.config[:url]}/runners/#{runner_id}/execute")
.with(
body: {command: command, timeLimit: execution_environment.permitted_execution_time},
headers: {'Content-Type' => 'application/json'}
@ -235,7 +280,7 @@ describe Runner::Strategy::Poseidon do
let(:action) { -> { poseidon.destroy_at_management } }
let!(:destroy_stub) do
WebMock
.stub_request(:delete, "#{Runner::BASE_URL}/runners/#{runner_id}")
.stub_request(:delete, "#{described_class.config[:url]}/runners/#{runner_id}")
.to_return(body: response_body, status: response_status)
end
@ -262,7 +307,7 @@ describe Runner::Strategy::Poseidon do
let(:encoded_file_content) { Base64.strict_encode64(file.content) }
let!(:copy_files_stub) do
WebMock
.stub_request(:patch, "#{Runner::BASE_URL}/runners/#{runner_id}/files")
.stub_request(:patch, "#{described_class.config[:url]}/runners/#{runner_id}/files")
.with(
body: {copy: [{path: file.filepath, content: encoded_file_content}]},
headers: {'Content-Type' => 'application/json'}

View File

@ -192,45 +192,4 @@ describe ExecutionEnvironment do
end
end
end
describe '#copy_to_poseidon' do
let(:execution_environment) { FactoryBot.create(:ruby) }
it 'makes the correct request to Poseidon' do
allow(Faraday).to receive(:put).and_return(Faraday::Response.new(status: 201))
execution_environment.copy_to_poseidon
expect(Faraday).to have_received(:put) do |url, body, headers|
expect(url).to match(%r{execution-environments/#{execution_environment.id}\z})
expect(body).to eq(execution_environment.to_json)
expect(headers).to include({'Content-Type' => 'application/json'})
end
end
shared_examples 'returns true when the api request was successful' do |status|
it "returns true on status #{status}" do
allow(Faraday).to receive(:put).and_return(Faraday::Response.new(status: status))
expect(execution_environment.copy_to_poseidon).to be_truthy
end
end
shared_examples 'returns false when the api request failed' do |status|
it "returns false on status #{status}" do
allow(Faraday).to receive(:put).and_return(Faraday::Response.new(status: status))
expect(execution_environment.copy_to_poseidon).to be_falsey
end
end
[201, 204].each do |status|
include_examples 'returns true when the api request was successful', status
end
[400, 500].each do |status|
include_examples 'returns false when the api request failed', status
end
it 'returns false if Faraday raises an error' do
allow(Faraday).to receive(:put).and_raise(Faraday::TimeoutError)
expect(execution_environment.copy_to_poseidon).to be_falsey
end
end
end

View File

@ -30,8 +30,20 @@ describe Runner do
describe '::strategy_class' do
shared_examples 'uses the strategy defined in the constant' do |strategy, strategy_class|
let(:codeocean_config) { instance_double(CodeOcean::Config) }
let(:runner_management_config) { {runner_management: {enabled: true, strategy: strategy}} }
before do
allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config)
allow(codeocean_config).to receive(:read).and_return(runner_management_config)
end
after do
# Reset the memorized helper
described_class.remove_instance_variable :@strategy_class
end
it "uses #{strategy_class} as strategy class for constant #{strategy}" do
stub_const('Runner::STRATEGY_NAME', strategy)
expect(described_class.strategy_class).to eq(strategy_class)
end
end
@ -41,7 +53,7 @@ describe Runner do
docker_container_pool: Runner::Strategy::DockerContainerPool,
}
available_strategies.each do |strategy, strategy_class|
include_examples 'uses the strategy defined in the constant', strategy, strategy_class
it_behaves_like 'uses the strategy defined in the constant', strategy, strategy_class
end
end

View File

@ -59,7 +59,7 @@ describe ExecutionEnvironmentPolicy do
end
end
permissions(:synchronize_all_to_poseidon?) do
permissions(:sync_all_to_runner_management?) do
it 'grants access to the admin' do
expect(policy).to permit(FactoryBot.build(:admin))
end