spec/task_spec.rb in screwcap-0.3.5 vs spec/task_spec.rb in screwcap-0.5

- old
+ new

@@ -1,87 +1,150 @@ require 'spec_helper' describe "Tasks" do - before(:each) do - @stdout = [] - @stderr = [] - Runner.stubs(:log).with() { |msg,opts| @stdout << msg } - Runner.stubs(:errorlog).with() { |msg,opts| @stderr << msg } - @deployer = Deployer.new(:recipe_file => "./test/config/simple_recipe.rb", :silent => true) - end + it "should have a task-like structure" do + task = Task.new :name => :test do + run "test" + end - before(:all) do - Net::SSH.stubs(:start).yields(SSHObject.new(:return_stream => :stdout, :return_data => "hostname = asdf\n")) - Net::SCP.stubs(:upload!).returns(nil) - Runner.stubs(:ssh_exec!).returns(["ok","",0,nil]) - end + task.__commands.should have(0).__commands + task.__build_commands + task.__commands.should_not be_nil + task.should have(1).__commands - it "should be able to create variables" do - task = @deployer.__tasks.find {|t| t.name == :task1 } - task.bango.should == "bongo" + task.__commands[0][:type].should == :remote + task.__commands[0][:from].should == :test + task.__commands[0][:command].should == "test" end - it "should compile run statements" do - task = @deployer.__tasks.find {|t| t.name == :task1 } - task.should have(6).__commands + it "should be able to build commands" do + unknown = Task.new :name => :unknown_action do + run "unknown" + end + + task = Task.new :name => :test do + run "test" + unknown_action + end + + commands = task.__build_commands([unknown]) + commands.size.should == 2 + + commands[0][:type].should == :remote + commands[0][:from].should == :test + commands[0][:command].should == "test" + + commands[1][:type].should == :remote + commands[1][:from].should == :unknown_action + commands[1][:command].should == "unknown" end - it "should be able to execute statements on a remote server" do - task = @deployer.__tasks.find {|t| t.name == :task1 } - Runner.execute! task, @deployer.__options - @stderr.size.should == 0 - @stdout.size.should == 28 + it "should throw an error if we cannot find a command" do + task = Task.new :name => :test do + run "test" + unknown_action + end + + lambda {task.__build_commands([task]) }.should raise_error(NoMethodError) end - it "should be able to use variables in the run statement" do - task = @deployer.__tasks.find {|t| t.name == :task1 } - command = task.__commands.map{|c| c[:command] }.find {|c| c.index "deploy dir" } - command.should == "deploy dir = tester" + it "should be able to create variables" do + task = Task.new :name => :test do + set :blaster, "stun" + run "fire #{blaster}" + end + task.__build_commands + task.blaster.should == "stun" + task.__commands.first[:command].should == "fire stun" end - it "should be able to override a globally set variable" do - @deployer.deploy_var_2.should == "bongo" - @deployer.deploy_var_3.should == %w(one two) + it "command sets should inherit the parent's variables" do + subsub = Task.new :name => :subsubtask do + set :from, "venus" + run "fly to #{where} from #{from}" + end - task = @deployer.__tasks.find {|t| t.name == :task1 } - task.deploy_var_2.should == "shasta" + sub = Task.new :name => :subtask do + set :from, "mars" + run "fly to #{where} from #{from}" + subsubtask + end - task = @deployer.__tasks.find {|t| t.name == :task2 } - task.deploy_var_2.should == "purple" - task.deploy_var_3.should == "mountain dew" - end + task = Task.new :name => :task do + set :where, "the moon" + set :from, "earth" + run "fly to #{where} from #{from}" + subtask + end - it "should complain if you do not pass the task a server argument" do - lambda { Deployer.new(:recipe_file => "./test/config/no_server.rb", :silent => true)}.should raise_error(Screwcap::ConfigurationError) - end + commands = task.__build_commands([sub, subsub]) + commands[0][:from].should == :task + commands[0][:command].should == "fly to the moon from earth" - it "should complain if you pass a server that is not defined" do - lambda { Deployer.new(:recipe_file => "./test/config/undefined_server.rb", :silent => true)}.should raise_error(Screwcap::ConfigurationError) - end + commands[1][:from].should == :subtask + commands[1][:command].should == "fly to the moon from mars" - it "should be able to disable parallel running" do - # this is hard to test. with threads and stuff - lambda { @deployer.run! :non_parallel }.should_not raise_error + commands[2][:from].should == :subsubtask + commands[2][:command].should == "fly to the moon from venus" end - it "should be able to run local commands" do - lambda { @deployer.run! :task3 }.should_not raise_error - end + it "should respond to :before or before_ calls" do + before = Task.new :name => :do_before do + run "before" + end + task = Task.new :name => :test, :before => :do_before do + run "task" + end - it "should be able to upload files using the scp command" do - deployer = Deployer.new(:recipe_file => "./test/config/upload.rb", :silent => true) - deployer.run! :upload + before2 = Task.new :name => :before_deploy do + run "before" + end + + task2 = Task.new :name => :deploy do + run "deploy" + end + + commands = task.__build_commands([before]) + commands.map {|c| c[:command] }.should == ["before","task"] + + commands = task2.__build_commands([before2]) + commands.map {|c| c[:command] }.should == ["before","deploy"] end - it "should respond to onfailure" do - deployer = Deployer.new(:recipe_file => "./test/config/expect.rb", :silent => true) - t = deployer.__tasks.find {|t| t.__name == :expect } - Runner.stubs(:ssh_exec!).returns(["","fail",1,nil]).then.returns(["ok","",0,nil]) - Runner.execute! t, deployer.__options - t.__commands.map {|c| [c[:command], c[:from]] }.first.should == ["echo 'we failed'", :failover] + it "should respond to :after or after_ calls" do + after = Task.new :name => :do_after do + run "after" + end + task = Task.new :name => :test, :after => :do_after do + run "task" + end + + after2 = Task.new :name => :after_deploy do + run "after" + end + + task2 = Task.new :name => :deploy do + run "deploy" + end + + commands = task.__build_commands([after]) + commands.map {|c| c[:command] }.should == ["task","after"] + + commands = task2.__build_commands([after2]) + commands.map {|c| c[:command] }.should == ["deploy", "after"] end - it "should be able to create local tasks" do - # TODO better testing on this - lambda { Deployer.new(:recipe_file => "./test/config/local_task.rb", :silent => true).run! :local }.should_not raise_error + it "should validate" do + task = Task.new :name => :test + lambda { task.validate([]) }.should raise_error(Screwcap::ConfigurationError) + + server = Server.new :name => :server, :address => "none", :user => "yeah" + other_server = Server.new :name => :server2, :address => "none", :user => "yeah" + task = Task.new :name => :test, :server => :server + lambda { task.validate([server]) }.should_not raise_error + lambda { task.validate([other_server]) }.should raise_error(Screwcap::ConfigurationError) + task = Task.new :name => :test, :servers => :server + lambda { task.validate([server]) }.should_not raise_error + task = Task.new :name => :test, :servers => [:server, :server2] + lambda { task.validate([server,other_server]) }.should_not raise_error end end