From 6b73fc7b30444b1f98a77c2edb1afbc2d9746bd6 Mon Sep 17 00:00:00 2001 From: Hauke Klement Date: Wed, 11 Feb 2015 10:04:21 +0100 Subject: [PATCH] create a container's specific directory before starting the container --- lib/docker_client.rb | 13 ++++++++----- spec/lib/docker_client_spec.rb | 18 +++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/docker_client.rb b/lib/docker_client.rb index d1a20e71..5552d8a9 100644 --- a/lib/docker_client.rb +++ b/lib/docker_client.rb @@ -28,7 +28,9 @@ class DockerClient def self.create_container(execution_environment) container = Docker::Container.create('Image' => find_image_by_tag(execution_environment.docker_image).info['RepoTags'].first, 'OpenStdin' => true, 'StdinOnce' => true) - container.start('Binds' => mapped_directories, 'PortBindings' => mapped_ports(execution_environment)) + local_workspace_path = generate_local_workspace_path + FileUtils.mkdir(local_workspace_path) + container.start('Binds' => mapped_directories(local_workspace_path), 'PortBindings' => mapped_ports(execution_environment)) container end @@ -82,8 +84,8 @@ class DockerClient Docker::Image.all.detect { |image| image.info['RepoTags'].flatten.include?(tag) } end - def self.generate_remote_workspace_path - File.join(config[:workspace_root], SecureRandom.uuid) + def self.generate_local_workspace_path + File.join(LOCAL_WORKSPACE_ROOT, SecureRandom.uuid) end def self.image_tags @@ -110,8 +112,9 @@ class DockerClient Pathname.new(container.binds.first.split(':').first.sub(config[:workspace_root], LOCAL_WORKSPACE_ROOT.to_s)) end - def self.mapped_directories - ["#{generate_remote_workspace_path}:#{CONTAINER_WORKSPACE_PATH}"] + def self.mapped_directories(local_workspace_path) + remote_workspace_path = local_workspace_path.sub(LOCAL_WORKSPACE_ROOT.to_s, config[:workspace_root]) + ["#{remote_workspace_path}:#{CONTAINER_WORKSPACE_PATH}"] end def self.mapped_ports(execution_environment) diff --git a/spec/lib/docker_client_spec.rb b/spec/lib/docker_client_spec.rb index 71b490a5..7d1b7093 100644 --- a/spec/lib/docker_client_spec.rb +++ b/spec/lib/docker_client_spec.rb @@ -32,6 +32,11 @@ describe DockerClient, docker: true do expect(DockerClient).to receive(:find_image_by_tag).with(execution_environment.docker_image).and_call_original end + it 'creates a unique directory' do + expect(DockerClient).to receive(:generate_local_workspace_path).and_call_original + expect(FileUtils).to receive(:mkdir).with(kind_of(String)).and_call_original + end + it 'creates a container waiting for input' do expect(Docker::Container).to receive(:create).with('Image' => kind_of(String), 'OpenStdin' => true, 'StdinOnce' => true).and_call_original end @@ -84,7 +89,7 @@ describe DockerClient, docker: true do after(:each) { File.delete(file_path) } it 'creates a file' do - expect(DockerClient).to receive(:local_workspace_path).and_return(workspace_path) + expect(DockerClient).to receive(:local_workspace_path).at_least(:once).and_return(workspace_path) docker_client.send(:create_workspace_file, container: CONTAINER, file: file) expect(File.exist?(file_path)).to be true expect(File.new(file_path, 'r').read).to eq(file.content) @@ -92,7 +97,7 @@ describe DockerClient, docker: true do end describe '.destroy_container' do - let(:container) { DockerClient.send(:create_container, execution_environment) } + let(:container) { DockerClient.create_container(execution_environment) } after(:each) { DockerClient.destroy_container(container) } it 'stops the container' do @@ -166,14 +171,14 @@ describe DockerClient, docker: true do end end - describe '.generate_remote_workspace_path' do + describe '.generate_local_workspace_path' do it 'includes the correct workspace root' do - expect(DockerClient.generate_remote_workspace_path).to start_with(DockerClient.config[:workspace_root]) + expect(DockerClient.generate_local_workspace_path).to start_with(DockerClient::LOCAL_WORKSPACE_ROOT.to_s) end it 'includes a UUID' do expect(SecureRandom).to receive(:uuid).and_call_original - DockerClient.generate_remote_workspace_path + DockerClient.generate_local_workspace_path end end @@ -209,8 +214,7 @@ describe DockerClient, docker: true do describe '.mapped_directories' do it 'returns a unique mapping' do - expect(DockerClient).to receive(:generate_remote_workspace_path).and_return(workspace_path) - mapping = DockerClient.send(:mapped_directories).first + mapping = DockerClient.mapped_directories(workspace_path).first expect(mapping).to start_with(workspace_path) expect(mapping).to end_with(DockerClient::CONTAINER_WORKSPACE_PATH) end