/wondercap/tests/deployment/core/snapshot_transaction_test.rb
Ruby | 259 lines | 192 code | 67 blank | 0 comment | 1 complexity | 977a51b90bcd9408aa796190888b4998 MD5 | raw file
- require "enumerator"
- require "rubygems"
- require "mocha"
- require "test/unit"
- require "testutils/method_stub"
- require "wondercap/lib/deployment/core/snapshot_transaction"
- class SnapshotTransactionCleanerFindUnfinishedDeploymentsMethodTest < Test::Unit::TestCase
- def setup
- @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransactionCleaner, :find_unfinished_deployments)
- end
-
- def test_returns_all_unfinished_deployments_when_there_is_no_current_snapshot
- dep = stub("dep")
- unfinished_snapshot = stub("unfinished_snapshot", :deployments => [dep])
-
- assert_equal([OpenStruct.new(:cur_dep => nil, :unfinished_dep => dep)], @method.call(unfinished_snapshot, nil))
- end
-
- def test_returns_deployments_not_present_in_current_snapshot
- dep1 = stub("dep1", :name => "dep1")
- dep2 = stub("dep2", :name => "dep2")
- current_snapshot = stub("current_snapshot")
- current_snapshot.stubs(:find_deployment_by_name).with("dep1").returns(dep1)
- current_snapshot.stubs(:find_deployment_by_name).with("dep2").returns(nil)
- unfinished_snapshot = stub("unfinished_snapshot", :deployments => [dep1, dep2])
-
- assert_equal([OpenStruct.new(:cur_dep => nil, :unfinished_dep => dep2)], @method.call(unfinished_snapshot, current_snapshot))
- end
-
- def test_returns_deployments_which_names_are_found_in_current_snapshot_but_are_not_equal_to_ones_found
- dep = stub("dep", :name => "dep")
- cur_dep = stub("cur_dep")
- current_snapshot = stub("current_snapshot")
- current_snapshot.stubs(:find_deployment_by_name).with("dep").returns(cur_dep)
- unfinished_snapshot = stub("unfinished_snapshot", :deployments => [dep])
- assert_equal([OpenStruct.new(:cur_dep => cur_dep, :unfinished_dep => dep)], @method.call(unfinished_snapshot, current_snapshot))
- end
-
- end
- class SnapshotTransactionCleanerCleanUnfinishedSnapshotTest < Test::Unit::TestCase
- def setup
- @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransactionCleaner, :clean_unfinished_snapshot)
- end
-
- def test_does_nothing_when_there_is_no_unfinished_snapshot
- area = stub("area", :unfinished_snapshot => nil)
-
- @obj.expects(:find_unfinished_deployments).never
-
- @method.call(area)
- end
-
- def test_uses_find_unfinished_deployments_method_when_there_is_unfinished_snapshot
- unfinished_snapshot = stub("unfinished_snapshot")
- current_snapshot = stub("current_snapshot")
- area = stub_everything("area", :unfinished_snapshot => unfinished_snapshot, :current_snapshot => current_snapshot)
-
- @obj.expects(:find_unfinished_deployments).with(unfinished_snapshot, current_snapshot).returns([])
-
- @method.call(area)
- end
-
- def test_nullifies_and_deletes_unfinished_snapshot_after_cleaning
- unfinished_snapshot = stub("unfinished_snapshot")
- area = stub_everything("area", :unfinished_snapshot => unfinished_snapshot)
- @obj.stubs(:find_unfinished_deployments).returns([])
-
- area.expects(:unfinished_snapshot=).with(nil)
- area.expects(:delete_snapshot).with(unfinished_snapshot)
- area.expects(:save!)
-
- @method.call(area)
- end
-
- def test_unlinks_each_unfinished_deployment_when_current_snapshot_is_empty
- dep = stub_everything("deployment")
- unfinished_snapshot = stub_everything("unfinished_snapshot")
- area = stub_everything("area", :unfinished_snapshot => unfinished_snapshot)
- @obj.stubs(:find_unfinished_deployments).returns([OpenStruct.new(:unfinished_dep => dep)])
-
- @obj.expects(:clean_unfinished_deployment).with(unfinished_snapshot, dep, nil)
-
- @method.call(area)
- end
- end
- class SnapshotTransactionDeployNewSnapshotMethodTest < Test::Unit::TestCase
- def setup
- @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransaction, :deploy_new_snapshot)
- end
-
- def bypass_transaction_error
- yield
- rescue Wondercap::Deployment::SnapshotTransactionError => e
- raise e.errors.values.first if e.errors.values.first.kind_of? Mocha::ExpectationError
- end
-
- def test_prepares_new_snapshot_deploys_new_deployment_and_finishes_new_snapshot_when_there_are_no_errors
- current_snapshot = stub("current_snapshot")
- area = stub("area", :current_snapshot => current_snapshot)
- new_snapshot = stub("new_snapshot")
- dep = stub_everything("deployment")
-
- @obj.expects(:prepare_and_save_new_snapshot).with(area).returns(new_snapshot)
- @obj.expects(:deploy_single_deployment).with(dep, new_snapshot, current_snapshot)
- @obj.expects(:finish_new_snapshot).with(new_snapshot, area)
-
- @method.call(area, [dep])
- end
-
- def test_stops_processing_after_an_error
- area = stub_everything("area")
- dep1 = stub_everything("deployment1")
- dep2 = stub_everything("deployment2")
- @obj.stub_everything
- @obj.stubs(:deploy_single_deployment).with(dep1, anything, anything).raises(RuntimeError.new("some error"))
- @obj.expects(:deploy_single_deployment).with(dep2, anything, anything).never
-
- bypass_transaction_error { @method.call(area, [dep1, dep2]) }
- end
-
- def test_processes_all_deployments_if_ignore_errors_is_specified
- area = stub_everything("area")
- dep1 = stub_everything("deployment1")
- dep2 = stub_everything("deployment2")
- @obj.stub_everything
- @obj.stubs(:deploy_single_deployment).with(dep1, anything, anything).raises(RuntimeError.new("some error"))
- @obj.expects(:deploy_single_deployment).with(dep2, anything, anything)
-
- bypass_transaction_error { @method.call(area, [dep1, dep2], :ignore_errors => true) }
- end
-
- def test_collects_all_exceptions_if_ignore_errors_is_specified
- area = stub_everything("area")
- dep1 = stub_everything("dep1", :name => "dep1")
- dep2 = stub_everything("dep2", :name => "dep2")
- err1 = RuntimeError.new
- err2 = RuntimeError.new
- @obj.stub_everything
- @obj.stubs(:deploy_single_deployment).with(dep1, anything, anything).raises(err1)
- @obj.stubs(:deploy_single_deployment).with(dep2, anything, anything).raises(err2)
-
- begin
- @method.call(area, [dep1, dep2], :ignore_errors => true)
- rescue Wondercap::Deployment::SnapshotTransactionError => e
- assert_same(err1, e["dep1"])
- assert_same(err2, e["dep2"])
- end
- end
-
- def test_does_not_clean_transaction_if_ignore_errors_is_specified
- area = stub_everything("area")
- dep = stub_everything("deployment")
-
- @obj.stub_everything
- @obj.expects(:clean_unfinished_snapshot).with(area).never
-
- bypass_transaction_error { @method.call(area, [dep]) }
- end
- def test_cleans_up_transaction_after_an_error
- area = stub_everything("area")
- dep = stub_everything("deployment")
-
- @obj.stub_everything
- @obj.stubs(:deploy_single_deployment).raises(RuntimeError.new("some error"))
-
- @obj.expects(:clean_unfinished_snapshot).with(area)
-
- bypass_transaction_error { @method.call(area, [dep]) }
- end
-
- end
- class SnapshotTransactionDeploySingleDeploymentMethodTest < Test::Unit::TestCase
- def setup
- @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransaction, :deploy_single_deployment)
-
- @getter = stub("getter")
- @obj.stubs(:configuration_getter).returns(@getter)
-
- Wondercap::Deployment::Deployment.stubs(:transaction).yields
- Wondercap::Deployment::Deployment.stubs(:inspect)
- end
-
- def test_deploys_and_links_new_deployment
- conf = stub_everything("conf")
- dep = stub_everything("deployment")
- new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
- @getter.stubs(:call).with(dep).returns(conf)
-
- conf.expects(:deploy)
- conf.expects(:link_deployment)
-
- @method.call(dep, new_snapshot, nil)
- end
-
- def test_unlinks_previous_deployment
- dep = stub_everything("deployment")
- new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
-
- prev_dep = stub_everything("prev_dep")
- prev_snapshot = stub_everything("prev_snapshot", :find_deployment_by_name => prev_dep)
- prev_conf = stub_everything("prev_conf")
-
- @getter.stubs(:call).with(dep).returns(stub_everything("conf"))
- @getter.stubs(:call).with(prev_dep).returns(prev_conf)
-
- prev_conf.expects(:unlink_deployment)
-
- @method.call(dep, new_snapshot, prev_snapshot)
- end
-
- def test_raises_deploy_errors
- conf = stub_everything("conf")
- dep = stub_everything("deployment")
- new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
- @getter.stubs(:call).with(dep).returns(conf)
-
- conf.stubs(:deploy).raises(RuntimeError.new)
-
- assert_raise(RuntimeError) { @method.call(dep, new_snapshot, nil) }
- end
-
- def test_raises_link_errors
- conf = stub_everything("conf")
- dep = stub_everything("deployment")
- new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
- @getter.stubs(:call).with(dep).returns(conf)
-
- conf.stubs(:link_deployment).raises(RuntimeError.new)
-
- assert_raise(RuntimeError) { @method.call(dep, new_snapshot, nil) }
- end
-
- def test_does_not_raise_unlink_errors
- dep = stub_everything("deployment")
- new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
-
- prev_strategy = stub_everything("prev_strategy")
- prev_dep = stub_everything("prev_dep")
- prev_snapshot = stub_everything("prev_snapshot", :find_deployment_by_name => prev_dep)
-
- @getter.stubs(:call).with(dep).returns(stub_everything("conf", :strategy => stub_everything("strategy")))
- @getter.stubs(:call).with(prev_dep).returns(stub_everything("prev_conf", :strategy => prev_strategy))
- prev_strategy.stubs(:unlink).raises(RuntimeError.new)
-
- assert_nothing_thrown { @method.call(dep, new_snapshot, prev_snapshot) }
- end
-
- end