Skip to content

Commit

Permalink
some specs for the new rcon stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Arie committed Aug 1, 2013
1 parent 1d62683 commit d0c3889
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
16 changes: 3 additions & 13 deletions app/services/ftp_zip_file_creator.rb
Expand Up @@ -10,25 +10,15 @@ def create_zip

def zip(tmp_dir)
Zip::ZipFile.open(zipfile_name_and_path, Zip::ZipFile::CREATE) do |zipfile|
Dir.glob(File.join(tmp_dir, "*")).each do |filename_with_path|
files_to_zip_in_dir(tmp_dir).each do |filename_with_path|
filename_without_path = File.basename(filename_with_path)
zipfile.add(filename_without_path, filename_with_path)
end
end
end

def zip_file_name
"logs_and_demos_#{reservation.id}.zip"
end

private

def server
reservation.server
end

def shell_escaped_files_to_zip
files_to_zip.collect { |file| file.shellescape }
def files_to_zip_in_dir(dir)
Dir.glob(File.join(dir, "*"))
end

end
10 changes: 0 additions & 10 deletions app/services/ssh_zip_file_creator.rb
Expand Up @@ -23,14 +23,4 @@ def remove_zip_file_on_remote_server
server.execute("rm #{remote_zip_name}")
end

private

def server
reservation.server
end

def shell_escaped_files_to_zip
files_to_zip.collect { |file| file.shellescape }
end

end
10 changes: 10 additions & 0 deletions app/services/zip_file_creator.rb
Expand Up @@ -26,4 +26,14 @@ def zipfile_name_and_path
Rails.root.join("public", "uploads", zipfile_name)
end

def shell_escaped_files_to_zip
files_to_zip.collect { |file| file.shellescape }
end

private

def server
reservation.server
end

end
2 changes: 1 addition & 1 deletion spec/models/local_server_spec.rb
Expand Up @@ -187,7 +187,7 @@
end

it "gives the rcon of the current reservation if there is one" do
subject.stub(:current_reservation => mock_model(Reservation, :rcon => 'foo'))
subject.stub(:current_reservation => mock_model(Reservation, :rcon => 'foo', :provisioned? => true))
subject.stub(:rcon => 'bar')
subject.current_rcon.should eql 'foo'
end
Expand Down
4 changes: 3 additions & 1 deletion spec/models/rcon_ftp_server_spec.rb
Expand Up @@ -89,7 +89,9 @@

it "creates the Net::FTP instance" do
subject.stub(:ip => double, :ftp_username => double, :ftp_password => double)
Net::FTP.should_receive(:new).with(subject.ip, subject.ftp_username, subject.ftp_password)
ftp = stub
Net::FTP.should_receive(:new).with(subject.ip, subject.ftp_username, subject.ftp_password).and_return { ftp }
ftp.should_receive(:passive=).with(true)
subject.ftp
end

Expand Down
41 changes: 41 additions & 0 deletions spec/services/ftp_zip_file_creator_spec.rb
Expand Up @@ -2,5 +2,46 @@

describe FtpZipFileCreator do

let!(:server) { double(:zip_file_creator_class => FtpZipFileCreator) }
let!(:reservation) { double(:server => server) }

describe '#create_zip' do

it "makes a tmpdir locally to store the files to be zipped" do
Dir.should_receive(:mktmpdir)
zip_file = FtpZipFileCreator.new(reservation, ["foo'bar"])
zip_file.stub(:zipfile_name => "/tmp/foo.zip")
zip_file.should_receive(:chmod)
zip_file.create_zip
end

it "gets the files from the server" do
tmp_dir = double
Dir.should_receive(:mktmpdir).and_yield(tmp_dir)
files = ['foo', 'bar']
server.should_receive(:copy_from_server).with(files, tmp_dir)
zip_file = FtpZipFileCreator.new(reservation, files)
zip_file.stub(:zipfile_name => "/tmp/foo.zip")
zip_file.create_zip
end
end

describe '#zip' do

it 'zips the file in the tmp dir' do
zip_file_stub = double
files = ['/tmp/foo']
tmp_dir = double
zipfile_name_and_path = '/tmp/foo.zip'

zip_file_stub.should_receive(:add).with(File.basename(files.first), files.first)
zip_file = FtpZipFileCreator.new(reservation, files)
zip_file.should_receive(:files_to_zip_in_dir).with(tmp_dir).and_return(files)
zip_file.stub(:zipfile_name_and_path => zipfile_name_and_path)
Zip::ZipFile.should_receive(:open).with(zipfile_name_and_path, Zip::ZipFile::CREATE).and_yield(zip_file_stub)
zip_file.zip(tmp_dir)
end
end

end

0 comments on commit d0c3889

Please sign in to comment.