spec/shelly/cli/main_spec.rb in shelly-0.0.57 vs spec/shelly/cli/main_spec.rb in shelly-0.0.58

- old
+ new

@@ -551,24 +551,16 @@ @client.stub(:token).and_return("abc") FileUtils.mkdir_p("/projects/foo") Dir.chdir("/projects/foo") File.open("Cloudfile", 'w') {|f| f.write("foo-production:\n") } Shelly::User.stub(:new).and_return(@user) - @client.stub(:apps).and_return([{"code_name" => "foo-production"}, {"code_name" => "foo-staging"}]) + @client.stub(:apps).and_return([{"code_name" => "foo-production", "state" => "running"}, + {"code_name" => "foo-staging", "state" => "no_code"}]) @app = Shelly::App.new Shelly::App.stub(:new).and_return(@app) end - # This spec tests cloudfile_present? hook - it "should exit if there is no Cloudfile" do - File.delete("Cloudfile") - $stdout.should_receive(:puts).with("\e[31mNo Cloudfile found\e[0m") - lambda { - invoke(@main, :start) - }.should raise_error(SystemExit) - end - it "should ensure user has logged in" do hooks(@main, :start).should include(:logged_in?) end it "should exit if user doesn't have access to clouds in Cloudfile" do @@ -586,10 +578,33 @@ $stdout.should_receive(:puts).with("Check status with: `shelly list`") invoke(@main, :start) end end + # this tests multiple_clouds method used in majority of tasks + context "without Cloudfile" do + it "should use cloud from params" do + Dir.chdir("/projects") + @client.stub(:start_cloud) + $stdout.should_receive(:puts).with(green "Starting cloud foo-production.") + @main.options = {:cloud => "foo-production"} + invoke(@main, :start) + end + + it "should ask user to specify cloud, list all clouds and exit" do + Dir.chdir("/projects") + @client.stub(:start_cloud) + $stdout.should_receive(:puts).with(red "You have to specify cloud.") + $stdout.should_receive(:puts).with("Select cloud using `shelly start --cloud CLOUD_NAME`") + $stdout.should_receive(:puts).with(green "You have following clouds available:") + $stdout.should_receive(:puts).with(" foo-production | running") + $stdout.should_receive(:puts).with(" foo-staging | no code") + lambda { invoke(@main, :start) }.should raise_error(SystemExit) + end + end + + # this tests multiple_clouds method used in majority of tasks context "multiple clouds in Cloudfile" do before do File.open("Cloudfile", 'w') {|f| f.write("foo-staging:\nfoo-production:\n") } end @@ -690,12 +705,17 @@ it "should ensure user has logged in" do hooks(@main, :stop).should include(:logged_in?) end - it "should ensure that Cloudfile is present" do - hooks(@main, :stop).should include(:cloudfile_present?) + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @client.stub(:stop_cloud) + @main.should_receive(:multiple_clouds).and_return(@app) + fake_stdin(["yes"]) do + invoke(@main, :stop) + end end it "should exit if user doesn't have access to clouds in Cloudfile" do @client.stub(:stop_cloud).and_raise(Shelly::Client::NotFoundException.new("resource" => "cloud")) $stdout.should_receive(:puts).with(red "You have no access to 'foo-production' cloud defined in Cloudfile") @@ -704,61 +724,38 @@ invoke(@main, :stop) end }.should raise_error(SystemExit) end - context "single cloud in Cloudfile" do - it "should stop the cloud" do - @client.stub(:stop_cloud) - $stdout.should_receive(:print).with("Are you sure you want to shut down your application (yes/no): ") - $stdout.should_receive(:puts).with("\n") - $stdout.should_receive(:puts).with("Cloud 'foo-production' stopped") - fake_stdin(["yes"]) do - invoke(@main, :stop) - end + it "should stop the cloud" do + @client.stub(:stop_cloud) + $stdout.should_receive(:print).with("Are you sure you want to shut down your application (yes/no): ") + $stdout.should_receive(:puts).with("\n") + $stdout.should_receive(:puts).with("Cloud 'foo-production' stopped") + fake_stdin(["yes"]) do + invoke(@main, :stop) end end - context "multiple clouds in Cloudfile" do - before do - File.open("Cloudfile", 'w') {|f| f.write("foo-staging:\nfoo-production:\n") } - end - - it "should show information to stop specific cloud and exit" do - $stdout.should_receive(:puts).with(red "You have multiple clouds in Cloudfile.") - $stdout.should_receive(:puts).with("Select cloud using `shelly stop --cloud foo-production`") - $stdout.should_receive(:puts).with("Available clouds:") - $stdout.should_receive(:puts).with(" * foo-production") - $stdout.should_receive(:puts).with(" * foo-staging") - lambda { invoke(@main, :stop) }.should raise_error(SystemExit) - end - - it "should fetch from command line which cloud to stop" do - @client.should_receive(:stop_cloud).with("foo-staging") - $stdout.should_receive(:print).with("Are you sure you want to shut down your application (yes/no): ") - $stdout.should_receive(:puts).with("\n") - $stdout.should_receive(:puts).with("Cloud 'foo-staging' stopped") - @main.options = {:cloud => "foo-staging"} - fake_stdin(["yes"]) do - invoke(@main, :stop) - end - end - end end describe "#ip" do before do - File.open("Cloudfile", 'w') {|f| f.write("foo-staging:\nfoo-production:\n") } + File.open("Cloudfile", 'w') {|f| f.write("foo-production:\n") } @main.stub(:logged_in?).and_return(true) end it "should ensure user has logged in" do hooks(@main, :ip).should include(:logged_in?) end - it "should ensure that Cloudfile is present" do - hooks(@main, :ip).should include(:cloudfile_present?) + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @app.should_receive(:web_server_ip).and_return("11.11") + @app.should_receive(:mail_server_ip).and_return("22.22") + @main.should_receive(:multiple_clouds).and_return(@app) + invoke(@main, :ip) end context "on success" do it "should display mail and web server ip's" do @client.stub(:app).and_return(response) @@ -775,12 +772,12 @@ context "on failure" do it "should raise an error if user does not have access to cloud" do exception = Shelly::Client::NotFoundException.new("resource" => "cloud") @client.stub(:app).and_raise(exception) - $stdout.should_receive(:puts).with(red "You have no access to 'foo-staging' cloud defined in Cloudfile") - invoke(@main, :ip) + $stdout.should_receive(:puts).with(red "You have no access to 'foo-production' cloud defined in Cloudfile") + lambda { invoke(@main, :ip) }.should raise_error(SystemExit) end end end describe "#setup" do @@ -801,17 +798,18 @@ it "should ensure that user is inside git repo" do hooks(@main, :setup).should include(:inside_git_repository?) end - it "should ensure that cloudfile is present" do - hooks(@main, :setup).should include(:cloudfile_present?) + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @main.should_receive(:multiple_clouds).and_return(@app) + invoke(@main, :setup) end it "should show info about adding remote and branch" do - $stdout.should_receive(:puts).with("Investigating Cloudfile") - $stdout.should_receive(:puts).with(green "Adding foo-staging cloud") + $stdout.should_receive(:puts).with(green "Setting up foo-staging cloud") $stdout.should_receive(:puts).with("git remote add foo-staging git_url") $stdout.should_receive(:puts).with("git fetch production") $stdout.should_receive(:puts).with("git checkout -b foo-staging --track foo-staging/master") $stdout.should_receive(:puts).with(green "Your application is set up.") invoke(@main, :setup) @@ -847,11 +845,11 @@ end end end context "and user answers no" do - it "should display commands to perform manually " do + it "should display commands to perform manually" do @app.should_not_receive(:add_git_remote) @app.should_not_receive(:git_fetch_remote) @app.should_not_receive(:git_add_tracking_branch) fake_stdin(["no"]) do invoke(@main, :setup) @@ -875,10 +873,19 @@ it "should ensure user has logged in" do hooks(@main, :delete).should include(:logged_in?) end + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @client.stub(:delete) + @main.should_receive(:multiple_clouds).and_return(@app) + fake_stdin(["yes", "yes", "yes"]) do + invoke(@main, :delete) + end + end + context "when cloud is given" do before do File.open("Cloudfile", 'w') {|f| f.write("foo-staging:\nfoo-production:\n") } end @@ -1019,58 +1026,33 @@ it "should ensure user has logged in" do hooks(@main, :logs).should include(:logged_in?) end - it "should ensure that Cloudfile is present" do - hooks(@main, :logs).should include(:cloudfile_present?) + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @client.stub(:application_logs).and_return(["log1"]) + @main.should_receive(:multiple_clouds).and_return(@app) + invoke(@main, :logs) end it "should exit if user doesn't have access to clouds in Cloudfile" do exception = Shelly::Client::NotFoundException.new("resource" => "cloud") @client.stub(:application_logs).and_raise(exception) $stdout.should_receive(:puts). with(red "You have no access to 'foo-production' cloud defined in Cloudfile") lambda { invoke(@main, :logs) }.should raise_error(SystemExit) end - context "single cloud in Cloudfile" do - it "should show logs for the cloud" do - @client.stub(:application_logs).and_return(["log1"]) - $stdout.should_receive(:puts).with(green "Cloud foo-production:") - $stdout.should_receive(:puts).with(green "Instance 1:") - $stdout.should_receive(:puts).with("log1") - invoke(@main, :logs) - end + it "should show logs for the cloud" do + @client.stub(:application_logs).and_return(["log1"]) + $stdout.should_receive(:puts).with(green "Cloud foo-production:") + $stdout.should_receive(:puts).with(green "Instance 1:") + $stdout.should_receive(:puts).with("log1") + invoke(@main, :logs) end - context "multiple clouds in Cloudfile" do - before do - File.open("Cloudfile", 'w') {|f| - f.write("foo-staging:\nfoo-production:\n") } - end - - it "should show information to print logs for specific cloud and exit" do - $stdout.should_receive(:puts).with(red "You have multiple clouds in Cloudfile.") - $stdout.should_receive(:puts).with("Select cloud using `shelly logs --cloud foo-production`") - $stdout.should_receive(:puts).with("Available clouds:") - $stdout.should_receive(:puts).with(" * foo-production") - $stdout.should_receive(:puts).with(" * foo-staging") - lambda { invoke(@main, :logs) }.should raise_error(SystemExit) - end - - it "should fetch from command line which cloud to start" do - @client.should_receive(:application_logs).with("foo-staging"). - and_return(["log1"]) - $stdout.should_receive(:puts).with(green "Cloud foo-staging:") - $stdout.should_receive(:puts).with(green "Instance 1:") - $stdout.should_receive(:puts).with("log1") - @main.options = {:cloud => "foo-staging"} - invoke(@main, :logs) - end - end - context "multiple instances" do it "should show logs from each instance" do @client.stub(:application_logs).and_return(["log1", "log2"]) $stdout.should_receive(:puts).with(green "Cloud foo-production:") $stdout.should_receive(:puts).with(green "Instance 1:") @@ -1090,64 +1072,39 @@ @user = Shelly::User.new @user.stub(:token) Shelly::User.stub(:new).and_return(@user) @client.stub(:apps).and_return([{"code_name" => "foo-production"}, {"code_name" => "foo-staging"}]) - @app = Shelly::App.new + @app = Shelly::App.new("foo-production") Shelly::App.stub(:new).and_return(@app) File.open("to_execute.rb", 'w') {|f| f.write("User.count") } end it "should ensure user has logged in" do hooks(@main, :execute).should include(:logged_in?) end - it "should ensure cloudfile present" do - hooks(@main, :execute).should include(:cloudfile_present?) + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @client.should_receive(:command).with("foo-production", "User.count", :ruby). + and_return({"result" => "3"}) + @main.should_receive(:multiple_clouds).and_return(@app) + invoke(@main, :execute, "to_execute.rb") end - context "single cloud in Cloudfile" do - it "should execute code for the cloud" do - @client.should_receive(:command).with("foo-production", "User.count", :ruby). - and_return({"result" => "3"}) - $stdout.should_receive(:puts).with("3") - invoke(@main, :execute, "to_execute.rb") - end + it "should execute code for the cloud" do + @client.should_receive(:command).with("foo-production", "User.count", :ruby). + and_return({"result" => "3"}) + $stdout.should_receive(:puts).with("3") + invoke(@main, :execute, "to_execute.rb") end - context "multiple clouds in Cloudfile" do - before do - File.open("Cloudfile", 'w') {|f| - f.write("foo-staging:\nfoo-production:\n") } - end - - it "should show information to print logs for specific cloud and exit" do - $stdout.should_receive(:puts). - with(red "You have multiple clouds in Cloudfile.") - $stdout.should_receive(:puts). - with("Select cloud using `shelly execute --cloud foo-production`") - $stdout.should_receive(:puts).with("Available clouds:") - $stdout.should_receive(:puts).with(" * foo-production") - $stdout.should_receive(:puts).with(" * foo-staging") - lambda { invoke(@main, :execute, "to_execute.rb") }.should raise_error(SystemExit) - end - - it "should fetch from command line which cloud to start" do - @client.should_receive(:command).with("foo-staging", "User.count", :ruby). - and_return({"result" => "3"}) - $stdout.should_receive(:puts).with("3") - @main.options = {:cloud => "foo-staging"} - invoke(@main, :execute, "to_execute.rb") - end - - it "should run code when no file from parameter is found" do - @client.should_receive(:command).with("foo-staging", "2 + 2", :ruby). - and_return({"result" => "4"}) - $stdout.should_receive(:puts).with("4") - @main.options = {:cloud => "foo-staging"} - invoke(@main, :execute, "2 + 2") - end + it "should run code when no file from parameter is found" do + @client.should_receive(:command).with("foo-production", "2 + 2", :ruby). + and_return({"result" => "4"}) + $stdout.should_receive(:puts).with("4") + invoke(@main, :execute, "2 + 2") end context "cloud is not in running state" do it "should print error" do @client.should_receive(:command).with("foo-staging", "2 + 2", :ruby). @@ -1174,21 +1131,25 @@ Dir.chdir("/projects/foo") File.open("Cloudfile", 'w') {|f| f.write("foo-production:\n") } @user = Shelly::User.new @user.stub(:token) Shelly::User.stub(:new).and_return(@user) - @app = Shelly::App.new + @app = Shelly::App.new("foo-production") Shelly::App.stub(:new).and_return(@app) @main.stub(:rake_args).and_return(%w(db:migrate)) end it "should ensure user has logged in" do hooks(@main, :rake).should include(:logged_in?) end - it "should ensure cloudfile present" do - hooks(@main, :execute).should include(:cloudfile_present?) + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @client.should_receive(:command).with("foo-production", "db:migrate", :rake). + and_return({"result" => "OK"}) + @main.should_receive(:multiple_clouds).and_return(@app) + invoke(@main, :rake, "db:migrate") end it "should invoke :command on app with rake task" do @client.should_receive(:command).with("foo-production", "db:migrate", :rake).and_return("result" => "OK") $stdout.should_receive(:puts).with("OK") @@ -1225,49 +1186,30 @@ with(red "Cloud foo-staging is not running. Cannot run rake task.") @main.options = {:cloud => "foo-staging"} lambda { invoke(@main, :rake, "db:migrate") }.should raise_error(SystemExit) end end - - context "multiple clouds in Cloudfile" do - before do - File.open("Cloudfile", 'w') {|f| - f.write("foo-staging:\nfoo-production:\n") } - end - - it "should show information to run rake task for specific cloud and exit" do - $stdout.should_receive(:puts). - with(red "You have multiple clouds in Cloudfile.") - $stdout.should_receive(:puts). - with("Select cloud using `shelly rake db:migrate --cloud foo-production`") - $stdout.should_receive(:puts).with("Available clouds:") - $stdout.should_receive(:puts).with(" * foo-production") - $stdout.should_receive(:puts).with(" * foo-staging") - lambda { invoke(@main, :rake, "db:migrate") }.should raise_error(SystemExit) - end - - it "should fetch from command line for which cloud run rake task" do - @client.should_receive(:command).with("foo-staging", "db:migrate", :rake). - and_return({"result" => "3"}) - $stdout.should_receive(:puts).with("3") - @main.options = {:cloud => "foo-staging"} - invoke(@main, :rake, "db:migrate") - end - end end describe "#redeploy" do before do @user = Shelly::User.new @client.stub(:token).and_return("abc") - @app = Shelly::App.new + @app = Shelly::App.new("foo-production") Shelly::App.stub(:new).and_return(@app) FileUtils.mkdir_p("/projects/foo") Dir.chdir("/projects/foo") File.open("Cloudfile", 'w') { |f| f.write("foo-production:\n") } end + # multiple_clouds is tested in main_spec.rb in describe "#start" block + it "should ensure multiple_clouds check" do + @client.stub(:redeploy) + @main.should_receive(:multiple_clouds).and_return(@app) + invoke(@main, :redeploy) + end + it "should redeploy the application" do $stdout.should_receive(:puts).with(green "Redeploying your application for cloud 'foo-production'") @app.should_receive(:redeploy) invoke(@main, :redeploy) end @@ -1304,31 +1246,9 @@ exception = Shelly::Client::ConflictException.new("state" => "doing_something") @client.should_receive(:redeploy).with("foo-production").and_raise(exception) lambda { invoke(@main, :redeploy) }.should raise_error(Shelly::Client::ConflictException) - end - end - - context "on multiple clouds in Cloudfile" do - before do - File.open("Cloudfile", 'w') { |f| f.write("foo-staging:\nfoo-production:\n") } - end - - it "should show information to redeploy application for specific cloud and exit" do - $stdout.should_receive(:puts).with(red "You have multiple clouds in Cloudfile.") - $stdout.should_receive(:puts).with("Select cloud using `shelly redeploy --cloud foo-production`") - $stdout.should_receive(:puts).with("Available clouds:") - $stdout.should_receive(:puts).with(" * foo-production") - $stdout.should_receive(:puts).with(" * foo-staging") - lambda { invoke(@main, :redeploy) }.should raise_error(SystemExit) - end - - it "should fetch from command line which cloud to redeploy application for" do - @client.should_receive(:redeploy).with("foo-staging") - $stdout.should_receive(:puts).with(green "Redeploying your application for cloud 'foo-staging'") - @main.options = {:cloud => "foo-staging"} - invoke(@main, :redeploy) end end end end