Introduce strategy for runner behavior
The runner model is only a class responsible for storing information now. Based on the configuration it picks a strategy for the runner management. The Poseidon strategy is already implemented and tested. The Docker strategy will follow.
This commit is contained in:

committed by
Sebastian Serth

parent
cf58be97ee
commit
d0d1b1bffd
@ -3,92 +3,17 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Runner do
|
||||
let(:runner) { FactoryBot.create :runner }
|
||||
let(:runner_id) { runner.runner_id }
|
||||
let(:error_message) { 'test error message' }
|
||||
let(:response_body) { nil }
|
||||
let(:user) { FactoryBot.build :external_user }
|
||||
let(:execution_environment) { FactoryBot.create :ruby }
|
||||
|
||||
# All requests handle a BadRequest (400) response the same way.
|
||||
shared_examples 'BadRequest (400) error handling' do
|
||||
let(:response_body) { {message: error_message}.to_json }
|
||||
let(:response_status) { 400 }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::BadRequest, /#{error_message}/)
|
||||
end
|
||||
end
|
||||
|
||||
# All requests handle a Unauthorized (401) response the same way.
|
||||
shared_examples 'Unauthorized (401) error handling' do
|
||||
let(:response_status) { 401 }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::Unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
# All requests except creation and destruction handle a NotFound (404) response the same way.
|
||||
shared_examples 'NotFound (404) error handling' do
|
||||
let(:response_status) { 404 }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::NotFound, /Runner/)
|
||||
end
|
||||
|
||||
it 'destroys the runner locally' do
|
||||
expect { action.call }.to change(described_class, :count).by(-1)
|
||||
.and raise_error(Runner::Error::NotFound)
|
||||
end
|
||||
end
|
||||
|
||||
# All requests handle an InternalServerError (500) response the same way.
|
||||
shared_examples 'InternalServerError (500) error handling' do
|
||||
shared_examples 'InternalServerError (500) with error code' do |error_code, error_class|
|
||||
let(:response_status) { 500 }
|
||||
let(:response_body) { {message: error_message, errorCode: error_code}.to_json }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(error_class) do |error|
|
||||
expect(error.message).to match(/#{error_message}/)
|
||||
expect(error.message).to match(/#{error_code}/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when error code is nomad overload' do
|
||||
include_examples(
|
||||
'InternalServerError (500) with error code',
|
||||
described_class.error_nomad_overload, Runner::Error::NotAvailable
|
||||
)
|
||||
end
|
||||
|
||||
context 'when error code is not nomad overload' do
|
||||
include_examples(
|
||||
'InternalServerError (500) with error code',
|
||||
described_class.error_unknown, Runner::Error::InternalServerError
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# All requests handle an unknown response status the same way.
|
||||
shared_examples 'unknown response status error handling' do
|
||||
let(:response_status) { 1337 }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::Unknown)
|
||||
end
|
||||
end
|
||||
let(:runner_id) { FactoryBot.attributes_for(:runner)[:runner_id] }
|
||||
let(:strategy_class) { described_class.strategy_class }
|
||||
|
||||
describe 'attribute validation' do
|
||||
let(:runner) { FactoryBot.create :runner }
|
||||
|
||||
it 'validates the presence of the runner id' do
|
||||
described_class.skip_callback(:validation, :before, :request_remotely)
|
||||
described_class.skip_callback(:validation, :before, :request_id)
|
||||
runner.update(runner_id: nil)
|
||||
expect(runner.errors[:runner_id]).to be_present
|
||||
described_class.set_callback(:validation, :before, :request_remotely)
|
||||
described_class.set_callback(:validation, :before, :request_id)
|
||||
end
|
||||
|
||||
it 'validates the presence of an execution environment' do
|
||||
@ -102,256 +27,98 @@ describe Runner do
|
||||
end
|
||||
end
|
||||
|
||||
describe 'creation' do
|
||||
let(:action) { -> { described_class.create(user: user, execution_environment: execution_environment) } }
|
||||
let!(:create_stub) do
|
||||
WebMock
|
||||
.stub_request(:post, "#{Runner::BASE_URL}/runners")
|
||||
.with(
|
||||
body: {executionEnvironmentId: execution_environment.id, inactivityTimeout: Runner::UNUSED_EXPIRATION_TIME},
|
||||
headers: {'Content-Type' => 'application/json'}
|
||||
)
|
||||
.to_return(body: response_body, status: response_status)
|
||||
end
|
||||
|
||||
context 'when a runner is created' do
|
||||
let(:response_body) { {runnerId: runner_id}.to_json }
|
||||
let(:response_status) { 200 }
|
||||
|
||||
it 'requests a runner from the runner management' do
|
||||
action.call
|
||||
expect(create_stub).to have_been_requested.once
|
||||
end
|
||||
|
||||
it 'does not call the runner management again when updating' do
|
||||
runner = action.call
|
||||
runner.runner_id = 'another_id'
|
||||
successfully_saved = runner.save
|
||||
expect(successfully_saved).to be_truthy
|
||||
expect(create_stub).to have_been_requested.once
|
||||
describe '::strategy_class' do
|
||||
shared_examples 'uses the strategy defined in the constant' do |strategy, strategy_class|
|
||||
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
|
||||
|
||||
context 'when the runner management returns Ok (200) with an id' do
|
||||
let(:response_body) { {runnerId: runner_id}.to_json }
|
||||
let(:response_status) { 200 }
|
||||
{poseidon: Runner::Strategy::Poseidon, docker: Runner::Strategy::Docker}.each do |strategy, strategy_class|
|
||||
include_examples 'uses the strategy defined in the constant', strategy, strategy_class
|
||||
end
|
||||
|
||||
it 'sets the runner id according to the response' do
|
||||
runner = action.call
|
||||
expect(runner.runner_id).to eq(runner_id)
|
||||
expect(runner).to be_persisted
|
||||
shared_examples 'delegates method sends to its strategy' do |method, *args|
|
||||
context "when sending #{method}" do
|
||||
let(:strategy) { instance_double(strategy_class) }
|
||||
let(:runner) { described_class.create }
|
||||
|
||||
before do
|
||||
allow(strategy_class).to receive(:request_from_management).and_return(runner_id)
|
||||
allow(strategy_class).to receive(:new).and_return(strategy)
|
||||
end
|
||||
|
||||
it "delegates the method #{method}" do
|
||||
expect(strategy).to receive(method)
|
||||
runner.send(method, *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns Ok (200) without an id' do
|
||||
let(:response_body) { {}.to_json }
|
||||
let(:response_status) { 200 }
|
||||
include_examples 'delegates method sends to its strategy', :destroy_at_management
|
||||
include_examples 'delegates method sends to its strategy', :copy_files, nil
|
||||
include_examples 'delegates method sends to its strategy', :attach_to_execution, nil
|
||||
end
|
||||
|
||||
it 'does not save the runner' do
|
||||
runner = action.call
|
||||
expect(runner).not_to be_persisted
|
||||
end
|
||||
describe '#request_id' do
|
||||
it 'requests a runner from the runner management when created' do
|
||||
expect(strategy_class).to receive(:request_from_management)
|
||||
described_class.create
|
||||
end
|
||||
|
||||
context 'when the runner management returns Ok (200) with invalid JSON' do
|
||||
let(:response_body) { '{hello}' }
|
||||
let(:response_status) { 200 }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::Unknown)
|
||||
end
|
||||
it 'sets the runner id when created' do
|
||||
allow(strategy_class).to receive(:request_from_management).and_return(runner_id)
|
||||
runner = described_class.create
|
||||
expect(runner.runner_id).to eq(runner_id)
|
||||
end
|
||||
|
||||
context 'when the runner management returns BadRequest (400)' do
|
||||
include_examples 'BadRequest (400) error handling'
|
||||
it 'sets the strategy when created' do
|
||||
allow(strategy_class).to receive(:request_from_management).and_return(runner_id)
|
||||
runner = described_class.create
|
||||
expect(runner.strategy).to be_present
|
||||
end
|
||||
|
||||
context 'when the runner management returns Unauthorized (401)' do
|
||||
include_examples 'Unauthorized (401) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns NotFound (404)' do
|
||||
let(:response_status) { 404 }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::NotFound, /Execution environment/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns InternalServerError (500)' do
|
||||
include_examples 'InternalServerError (500) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns an unknown response status' do
|
||||
include_examples 'unknown response status error handling'
|
||||
it 'does not call the runner management again when validating the model' do
|
||||
expect(strategy_class).to receive(:request_from_management).and_return(runner_id).once
|
||||
runner = described_class.create
|
||||
runner.valid?
|
||||
end
|
||||
end
|
||||
|
||||
describe 'execute command' do
|
||||
let(:command) { 'ls' }
|
||||
let(:action) { -> { runner.execute_command(command) } }
|
||||
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")
|
||||
.with(
|
||||
body: {command: command, timeLimit: execution_environment.permitted_execution_time},
|
||||
headers: {'Content-Type' => 'application/json'}
|
||||
)
|
||||
.to_return(body: response_body, status: response_status)
|
||||
end
|
||||
describe '::for' do
|
||||
let(:user) { FactoryBot.create :external_user }
|
||||
let(:exercise) { FactoryBot.create :fibonacci }
|
||||
|
||||
context 'when #execute_command is called' do
|
||||
let(:response_status) { 200 }
|
||||
let(:response_body) { {websocketUrl: websocket_url}.to_json }
|
||||
|
||||
it 'schedules an execution in the runner management' do
|
||||
action.call
|
||||
expect(execute_command_stub).to have_been_requested.once
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns Ok (200) with a websocket url' do
|
||||
let(:response_status) { 200 }
|
||||
let(:response_body) { {websocketUrl: websocket_url}.to_json }
|
||||
|
||||
it 'returns the url' do
|
||||
url = action.call
|
||||
expect(url).to eq(websocket_url)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns Ok (200) without a websocket url' do
|
||||
let(:response_body) { {}.to_json }
|
||||
let(:response_status) { 200 }
|
||||
context 'when the runner could not be saved' do
|
||||
before { allow(strategy_class).to receive(:request_from_management).and_return(nil) }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::Unknown)
|
||||
expect { described_class.for(user, exercise) }.to raise_error(Runner::Error::Unknown, /could not be saved/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns Ok (200) with invalid JSON' do
|
||||
let(:response_body) { '{hello}' }
|
||||
let(:response_status) { 200 }
|
||||
context 'when a runner already exists' do
|
||||
let!(:existing_runner) { FactoryBot.create(:runner, user: user, execution_environment: exercise.execution_environment) }
|
||||
|
||||
it 'raises an error' do
|
||||
expect { action.call }.to raise_error(Runner::Error::Unknown)
|
||||
it 'returns the existing runner' do
|
||||
new_runner = described_class.for(user, exercise)
|
||||
expect(new_runner).to eq(existing_runner)
|
||||
end
|
||||
|
||||
it 'sets the strategy' do
|
||||
runner = described_class.for(user, exercise)
|
||||
expect(runner.strategy).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns BadRequest (400)' do
|
||||
include_examples 'BadRequest (400) error handling'
|
||||
end
|
||||
context 'when no runner exists' do
|
||||
before { allow(strategy_class).to receive(:request_from_management).and_return(runner_id) }
|
||||
|
||||
context 'when the runner management returns Unauthorized (401)' do
|
||||
include_examples 'Unauthorized (401) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns NotFound (404)' do
|
||||
include_examples 'NotFound (404) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns InternalServerError (500)' do
|
||||
include_examples 'InternalServerError (500) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns an unknown response status' do
|
||||
include_examples 'unknown response status error handling'
|
||||
end
|
||||
end
|
||||
|
||||
describe 'destruction' do
|
||||
let(:action) { -> { runner.destroy_remotely } }
|
||||
let(:response_status) { 204 }
|
||||
let!(:destroy_stub) do
|
||||
WebMock
|
||||
.stub_request(:delete, "#{Runner::BASE_URL}/runners/#{runner_id}")
|
||||
.to_return(body: response_body, status: response_status)
|
||||
end
|
||||
|
||||
it 'deletes the runner from the runner management' do
|
||||
action.call
|
||||
expect(destroy_stub).to have_been_requested.once
|
||||
end
|
||||
|
||||
it 'does not destroy the runner locally' do
|
||||
expect { action.call }.not_to change(described_class, :count)
|
||||
end
|
||||
|
||||
context 'when the runner management returns NoContent (204)' do
|
||||
it 'does not raise an error' do
|
||||
expect { action.call }.not_to raise_error
|
||||
it 'returns a new runner' do
|
||||
runner = described_class.for(user, exercise)
|
||||
expect(runner).to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns Unauthorized (401)' do
|
||||
include_examples 'Unauthorized (401) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns NotFound (404)' do
|
||||
let(:response_status) { 404 }
|
||||
|
||||
it 'raises an exception' do
|
||||
expect { action.call }.to raise_error(Runner::Error::NotFound, /Runner/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns InternalServerError (500)' do
|
||||
include_examples 'InternalServerError (500) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns an unknown response status' do
|
||||
include_examples 'unknown response status error handling'
|
||||
end
|
||||
end
|
||||
|
||||
describe 'copy files' do
|
||||
let(:filename) { 'main.py' }
|
||||
let(:file_content) { 'print("Hello World!")' }
|
||||
let(:action) { -> { runner.copy_files({filename => file_content}) } }
|
||||
let(:encoded_file_content) { Base64.strict_encode64(file_content) }
|
||||
let(:response_status) { 204 }
|
||||
let!(:copy_files_stub) do
|
||||
WebMock
|
||||
.stub_request(:patch, "#{Runner::BASE_URL}/runners/#{runner_id}/files")
|
||||
.with(
|
||||
body: {copy: [{path: filename, content: encoded_file_content}]},
|
||||
headers: {'Content-Type' => 'application/json'}
|
||||
)
|
||||
.to_return(body: response_body, status: response_status)
|
||||
end
|
||||
|
||||
it 'sends the files to the runner management' do
|
||||
action.call
|
||||
expect(copy_files_stub).to have_been_requested.once
|
||||
end
|
||||
|
||||
context 'when the runner management returns NoContent (204)' do
|
||||
let(:response_status) { 204 }
|
||||
|
||||
it 'does not raise an error' do
|
||||
expect { action.call }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the runner management returns BadRequest (400)' do
|
||||
include_examples 'BadRequest (400) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns Unauthorized (401)' do
|
||||
include_examples 'Unauthorized (401) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns NotFound (404)' do
|
||||
include_examples 'NotFound (404) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns InternalServerError (500)' do
|
||||
include_examples 'InternalServerError (500) error handling'
|
||||
end
|
||||
|
||||
context 'when the runner management returns an unknown response status' do
|
||||
include_examples 'unknown response status error handling'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user