some refactoring

This commit is contained in:
Hauke Klement
2015-03-19 14:33:51 +01:00
parent 8787a21c3d
commit ba3476cfec
6 changed files with 121 additions and 62 deletions

View File

@@ -125,6 +125,7 @@ describe SubmissionsController do
describe 'GET #run' do
let(:filename) { submission.collect_files.detect(&:main_file?).name_with_extension }
let(:request) { get :run, filename: filename, id: submission.id }
before(:each) do
expect_any_instance_of(ActionController::Live::SSE).to receive(:write).at_least(3).times
@@ -133,27 +134,27 @@ describe SubmissionsController do
context 'when no errors occur during execution' do
before(:each) do
expect_any_instance_of(DockerClient).to receive(:execute_run_command).with(submission, filename).and_return({})
get :run, filename: filename, id: submission.id
request
end
expect_assigns(docker_client: DockerClient)
expect_assigns(server_sent_event: ActionController::Live::SSE)
expect_assigns(submission: :submission)
expect_content_type('text/event-stream')
expect_status(200)
end
context 'when an error occurs during execution' do
let(:hint) { "Your object 'main' of class 'Object' does not understand the method 'foo'." }
let(:stderr) { "undefined method `foo' for main:Object (NoMethodError)" }
before(:each) do
expect_any_instance_of(DockerClient).to receive(:execute_run_command).with(submission, filename).and_yield(:stderr, stderr)
end
after(:each) { get :run, filename: filename, id: submission.id }
after(:each) { request }
context 'when the error is covered by a hint' do
let(:hint) { "Your object 'main' of class 'Object' does not understand the method 'foo'." }
before(:each) do
expect_any_instance_of(Whistleblower).to receive(:generate_hint).with(stderr).and_return(hint)
end
@@ -236,4 +237,53 @@ describe SubmissionsController do
expect_json
expect_status(200)
end
describe '#with_server_sent_events' do
let(:response) { ActionController::TestResponse.new }
before(:each) { allow(controller).to receive(:response).and_return(response) }
context 'when no error occurs' do
after(:each) { controller.send(:with_server_sent_events) }
it 'uses server-sent events' do
expect(ActionController::Live::SSE).to receive(:new).and_call_original
end
it "writes a 'start' event" do
allow_any_instance_of(ActionController::Live::SSE).to receive(:write)
expect_any_instance_of(ActionController::Live::SSE).to receive(:write).with(nil, event: 'start')
end
it "writes a 'close' event" do
allow_any_instance_of(ActionController::Live::SSE).to receive(:write)
expect_any_instance_of(ActionController::Live::SSE).to receive(:write).with({code: 200}, event: 'close')
end
it 'closes the stream' do
expect_any_instance_of(ActionController::Live::SSE).to receive(:close).and_call_original
end
end
context 'when an error occurs' do
after(:each) { controller.send(:with_server_sent_events) { fail } }
it 'uses server-sent events' do
expect(ActionController::Live::SSE).to receive(:new).and_call_original
end
it "writes a 'start' event" do
allow_any_instance_of(ActionController::Live::SSE).to receive(:write)
expect_any_instance_of(ActionController::Live::SSE).to receive(:write).with(nil, event: 'start')
end
it "writes a 'close' event" do
allow_any_instance_of(ActionController::Live::SSE).to receive(:write)
expect_any_instance_of(ActionController::Live::SSE).to receive(:write).with({code: 500}, event: 'close')
end
it 'closes the stream' do
expect_any_instance_of(ActionController::Live::SSE).to receive(:close).and_call_original
end
end
end
end

View File

@@ -118,15 +118,14 @@ describe DockerClient, docker: true do
end
end
describe '#create_workspace' do
describe '#create_workspace_files' do
let(:container) { double }
before(:each) do
docker_client.instance_variable_set(:@submission, submission)
expect(container).to receive(:binds).at_least(:once).and_return(["#{workspace_path}:#{DockerClient::CONTAINER_WORKSPACE_PATH}"])
end
after(:each) { docker_client.send(:create_workspace, container) }
after(:each) { docker_client.send(:create_workspace_files, container, submission) }
it 'creates submission-specific directories' do
expect(Dir).to receive(:mkdir).at_least(:once)
@@ -233,8 +232,8 @@ describe DockerClient, docker: true do
expect(DockerContainerPool).to receive(:get_container).with(submission.execution_environment).and_call_original
end
it 'creates the workspace' do
expect(docker_client).to receive(:create_workspace)
it 'creates the workspace files' do
expect(docker_client).to receive(:create_workspace_files)
end
it 'executes the run command' do
@@ -251,8 +250,8 @@ describe DockerClient, docker: true do
expect(DockerContainerPool).to receive(:get_container).with(submission.execution_environment).and_call_original
end
it 'creates the workspace' do
expect(docker_client).to receive(:create_workspace)
it 'creates the workspace files' do
expect(docker_client).to receive(:create_workspace_files)
end
it 'executes the test command' do

View File

@@ -1,18 +1,30 @@
require 'rails_helper'
describe DockerContainerMixin do
[:binds, :port_bindings].each do |method|
describe "##{method}" do
let(:data) { [] }
describe '#binds' do
let(:binds) { [] }
it 'is defined for Docker::Container' do
expect(Docker::Container.instance_methods).to include(method)
end
it 'is defined for Docker::Container' do
expect(Docker::Container.instance_methods).to include(:binds)
end
it 'returns the correct information' do
expect(CONTAINER).to receive(:json).and_return('HostConfig' => {method.to_s.camelize => data})
expect(CONTAINER.send(method)).to eq(data)
end
it 'returns the correct information' do
expect(CONTAINER).to receive(:json).and_return('HostConfig' => {'Binds' => binds})
expect(CONTAINER.binds).to eq(binds)
end
end
describe '#port_bindings' do
let(:port) { 1234 }
let(:port_bindings) { {"#{port}/tcp" => [{'HostIp' => '', 'HostPort' => port.to_s}]} }
it 'is defined for Docker::Container' do
expect(Docker::Container.instance_methods).to include(:port_bindings)
end
it 'returns the correct information' do
expect(CONTAINER).to receive(:json).and_return('HostConfig' => {'PortBindings' => port_bindings})
expect(CONTAINER.port_bindings).to eq(port => port)
end
end
end