forked from leonid-shevtsov/headless
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
169 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in ci_util.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ require 'rake' | |
spec = Gem::Specification.new do |s| | ||
s.author = 'Leonid Shevtsov' | ||
s.email = '[email protected]' | ||
|
||
s.name = 'headless' | ||
s.version = '0.1.0' | ||
s.summary = 'Ruby headless display interface' | ||
|
@@ -16,4 +16,7 @@ spec = Gem::Specification.new do |s| | |
|
||
s.files = FileList['lib/*.rb', '[A-Z]*'].to_a | ||
s.has_rdoc = true | ||
|
||
s.add_development_dependency "rspec", "~> 2.6" | ||
s.add_development_dependency "ruby-debug" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class CliUtil | ||
def self.application_exists?(app) | ||
`which #{app}`.strip == "" | ||
end | ||
|
||
def self.path_to(app) | ||
`which #{app}`.strip | ||
end | ||
|
||
def self.read_pid(file) | ||
pid = (File.read("/tmp/.X#{display}-lock") rescue "").strip.to_i | ||
pid == 0 ? nil : pid | ||
end | ||
|
||
def self.fork_process(command, pid_file) | ||
pid = fork do | ||
exec command | ||
exit! 127 | ||
end | ||
|
||
File.open pid_file, 'w' do |f| | ||
f.puts pid | ||
end | ||
end | ||
|
||
def self.kill_process(pid_file) | ||
if File.exist? pid_file | ||
pid = File.read(pid_file).strip.to_i | ||
Process.kill 'TERM', pid | ||
FileUtils.rm pid_file | ||
else | ||
puts "#{pid_file} not found" | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require 'lib/headless' | ||
|
||
describe Headless do | ||
before do | ||
ENV['DISPLAY'] = ":31337" | ||
end | ||
|
||
context "instaniation" do | ||
context "when Xvfb is not installed" do | ||
before do | ||
CliUtil.stub!(:application_exists?).and_return(false) | ||
end | ||
|
||
it "raises an error" do | ||
lambda { Headless.new }.should raise_error(Headless::Exception) | ||
end | ||
end | ||
|
||
context "when Xvfb not started yet" do | ||
before do | ||
stub_environment | ||
end | ||
|
||
it "starts Xvfb" do | ||
Headless.any_instance.should_receive(:system).with("/usr/bin/Xvfb :99 -screen 0 1280x1024x24 -ac >/dev/null 2>&1 &").and_return(true) | ||
|
||
headless = Headless.new | ||
end | ||
|
||
it "allows setting screen dimensions" do | ||
Headless.any_instance.should_receive(:system).with("/usr/bin/Xvfb :99 -screen 0 1024x768x16 -ac >/dev/null 2>&1 &").and_return(true) | ||
|
||
headless = Headless.new(:dimensions => "1024x768x16") | ||
end | ||
end | ||
|
||
context "when Xvfb is already running" do | ||
before do | ||
stub_environment | ||
CliUtil.stub!(:read_pid).and_return(31337) | ||
end | ||
|
||
it "raises an error if reuse display is not allowed" do | ||
lambda { Headless.new(:reuse => false) }.should raise_error(Headless::Exception) | ||
end | ||
|
||
it "doesn't raise an error if reuse display is allowed" do | ||
lambda { Headless.new(:reuse => true) }.should_not raise_error(Headless::Exception) | ||
lambda { Headless.new }.should_not raise_error(Headless::Exception) | ||
end | ||
end | ||
end | ||
|
||
describe "#start" do | ||
before do | ||
stub_environment | ||
|
||
@headless = Headless.new | ||
end | ||
|
||
it "switches to the headless server" do | ||
ENV['DISPLAY'].should == ":31337" | ||
@headless.start | ||
ENV['DISPLAY'].should == ":99" | ||
end | ||
end | ||
|
||
describe "#stop" do | ||
before do | ||
stub_environment | ||
|
||
@headless = Headless.new | ||
end | ||
|
||
it "switches back from the headless server" do | ||
ENV['DISPLAY'].should == ":31337" | ||
@headless.start | ||
ENV['DISPLAY'].should == ":99" | ||
@headless.stop | ||
ENV['DISPLAY'].should == ":31337" | ||
end | ||
end | ||
|
||
describe "#destroy" do | ||
before do | ||
stub_environment | ||
@headless = Headless.new | ||
|
||
CliUtil.stub!(:read_pid).and_return(4444) | ||
end | ||
|
||
it "switches back from the headless server and terminates the headless session" do | ||
Process.should_receive(:kill).with('TERM', 4444) | ||
|
||
ENV['DISPLAY'].should == ":31337" | ||
@headless.start | ||
ENV['DISPLAY'].should == ":99" | ||
@headless.destroy | ||
ENV['DISPLAY'].should == ":31337" | ||
end | ||
end | ||
|
||
private | ||
|
||
def stub_environment | ||
CliUtil.stub!(:application_exists?).and_return(true) | ||
CliUtil.stub!(:read_pid).and_return(nil) | ||
CliUtil.stub!(:path_to).and_return("/usr/bin/Xvfb") | ||
end | ||
end |