PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wondercap/tests/deployment/core/snapshot_transaction_test.rb

http://wondercap.googlecode.com/
Ruby | 259 lines | 192 code | 67 blank | 0 comment | 1 complexity | 977a51b90bcd9408aa796190888b4998 MD5 | raw file
  1. require "enumerator"
  2. require "rubygems"
  3. require "mocha"
  4. require "test/unit"
  5. require "testutils/method_stub"
  6. require "wondercap/lib/deployment/core/snapshot_transaction"
  7. class SnapshotTransactionCleanerFindUnfinishedDeploymentsMethodTest < Test::Unit::TestCase
  8. def setup
  9. @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransactionCleaner, :find_unfinished_deployments)
  10. end
  11. def test_returns_all_unfinished_deployments_when_there_is_no_current_snapshot
  12. dep = stub("dep")
  13. unfinished_snapshot = stub("unfinished_snapshot", :deployments => [dep])
  14. assert_equal([OpenStruct.new(:cur_dep => nil, :unfinished_dep => dep)], @method.call(unfinished_snapshot, nil))
  15. end
  16. def test_returns_deployments_not_present_in_current_snapshot
  17. dep1 = stub("dep1", :name => "dep1")
  18. dep2 = stub("dep2", :name => "dep2")
  19. current_snapshot = stub("current_snapshot")
  20. current_snapshot.stubs(:find_deployment_by_name).with("dep1").returns(dep1)
  21. current_snapshot.stubs(:find_deployment_by_name).with("dep2").returns(nil)
  22. unfinished_snapshot = stub("unfinished_snapshot", :deployments => [dep1, dep2])
  23. assert_equal([OpenStruct.new(:cur_dep => nil, :unfinished_dep => dep2)], @method.call(unfinished_snapshot, current_snapshot))
  24. end
  25. def test_returns_deployments_which_names_are_found_in_current_snapshot_but_are_not_equal_to_ones_found
  26. dep = stub("dep", :name => "dep")
  27. cur_dep = stub("cur_dep")
  28. current_snapshot = stub("current_snapshot")
  29. current_snapshot.stubs(:find_deployment_by_name).with("dep").returns(cur_dep)
  30. unfinished_snapshot = stub("unfinished_snapshot", :deployments => [dep])
  31. assert_equal([OpenStruct.new(:cur_dep => cur_dep, :unfinished_dep => dep)], @method.call(unfinished_snapshot, current_snapshot))
  32. end
  33. end
  34. class SnapshotTransactionCleanerCleanUnfinishedSnapshotTest < Test::Unit::TestCase
  35. def setup
  36. @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransactionCleaner, :clean_unfinished_snapshot)
  37. end
  38. def test_does_nothing_when_there_is_no_unfinished_snapshot
  39. area = stub("area", :unfinished_snapshot => nil)
  40. @obj.expects(:find_unfinished_deployments).never
  41. @method.call(area)
  42. end
  43. def test_uses_find_unfinished_deployments_method_when_there_is_unfinished_snapshot
  44. unfinished_snapshot = stub("unfinished_snapshot")
  45. current_snapshot = stub("current_snapshot")
  46. area = stub_everything("area", :unfinished_snapshot => unfinished_snapshot, :current_snapshot => current_snapshot)
  47. @obj.expects(:find_unfinished_deployments).with(unfinished_snapshot, current_snapshot).returns([])
  48. @method.call(area)
  49. end
  50. def test_nullifies_and_deletes_unfinished_snapshot_after_cleaning
  51. unfinished_snapshot = stub("unfinished_snapshot")
  52. area = stub_everything("area", :unfinished_snapshot => unfinished_snapshot)
  53. @obj.stubs(:find_unfinished_deployments).returns([])
  54. area.expects(:unfinished_snapshot=).with(nil)
  55. area.expects(:delete_snapshot).with(unfinished_snapshot)
  56. area.expects(:save!)
  57. @method.call(area)
  58. end
  59. def test_unlinks_each_unfinished_deployment_when_current_snapshot_is_empty
  60. dep = stub_everything("deployment")
  61. unfinished_snapshot = stub_everything("unfinished_snapshot")
  62. area = stub_everything("area", :unfinished_snapshot => unfinished_snapshot)
  63. @obj.stubs(:find_unfinished_deployments).returns([OpenStruct.new(:unfinished_dep => dep)])
  64. @obj.expects(:clean_unfinished_deployment).with(unfinished_snapshot, dep, nil)
  65. @method.call(area)
  66. end
  67. end
  68. class SnapshotTransactionDeployNewSnapshotMethodTest < Test::Unit::TestCase
  69. def setup
  70. @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransaction, :deploy_new_snapshot)
  71. end
  72. def bypass_transaction_error
  73. yield
  74. rescue Wondercap::Deployment::SnapshotTransactionError => e
  75. raise e.errors.values.first if e.errors.values.first.kind_of? Mocha::ExpectationError
  76. end
  77. def test_prepares_new_snapshot_deploys_new_deployment_and_finishes_new_snapshot_when_there_are_no_errors
  78. current_snapshot = stub("current_snapshot")
  79. area = stub("area", :current_snapshot => current_snapshot)
  80. new_snapshot = stub("new_snapshot")
  81. dep = stub_everything("deployment")
  82. @obj.expects(:prepare_and_save_new_snapshot).with(area).returns(new_snapshot)
  83. @obj.expects(:deploy_single_deployment).with(dep, new_snapshot, current_snapshot)
  84. @obj.expects(:finish_new_snapshot).with(new_snapshot, area)
  85. @method.call(area, [dep])
  86. end
  87. def test_stops_processing_after_an_error
  88. area = stub_everything("area")
  89. dep1 = stub_everything("deployment1")
  90. dep2 = stub_everything("deployment2")
  91. @obj.stub_everything
  92. @obj.stubs(:deploy_single_deployment).with(dep1, anything, anything).raises(RuntimeError.new("some error"))
  93. @obj.expects(:deploy_single_deployment).with(dep2, anything, anything).never
  94. bypass_transaction_error { @method.call(area, [dep1, dep2]) }
  95. end
  96. def test_processes_all_deployments_if_ignore_errors_is_specified
  97. area = stub_everything("area")
  98. dep1 = stub_everything("deployment1")
  99. dep2 = stub_everything("deployment2")
  100. @obj.stub_everything
  101. @obj.stubs(:deploy_single_deployment).with(dep1, anything, anything).raises(RuntimeError.new("some error"))
  102. @obj.expects(:deploy_single_deployment).with(dep2, anything, anything)
  103. bypass_transaction_error { @method.call(area, [dep1, dep2], :ignore_errors => true) }
  104. end
  105. def test_collects_all_exceptions_if_ignore_errors_is_specified
  106. area = stub_everything("area")
  107. dep1 = stub_everything("dep1", :name => "dep1")
  108. dep2 = stub_everything("dep2", :name => "dep2")
  109. err1 = RuntimeError.new
  110. err2 = RuntimeError.new
  111. @obj.stub_everything
  112. @obj.stubs(:deploy_single_deployment).with(dep1, anything, anything).raises(err1)
  113. @obj.stubs(:deploy_single_deployment).with(dep2, anything, anything).raises(err2)
  114. begin
  115. @method.call(area, [dep1, dep2], :ignore_errors => true)
  116. rescue Wondercap::Deployment::SnapshotTransactionError => e
  117. assert_same(err1, e["dep1"])
  118. assert_same(err2, e["dep2"])
  119. end
  120. end
  121. def test_does_not_clean_transaction_if_ignore_errors_is_specified
  122. area = stub_everything("area")
  123. dep = stub_everything("deployment")
  124. @obj.stub_everything
  125. @obj.expects(:clean_unfinished_snapshot).with(area).never
  126. bypass_transaction_error { @method.call(area, [dep]) }
  127. end
  128. def test_cleans_up_transaction_after_an_error
  129. area = stub_everything("area")
  130. dep = stub_everything("deployment")
  131. @obj.stub_everything
  132. @obj.stubs(:deploy_single_deployment).raises(RuntimeError.new("some error"))
  133. @obj.expects(:clean_unfinished_snapshot).with(area)
  134. bypass_transaction_error { @method.call(area, [dep]) }
  135. end
  136. end
  137. class SnapshotTransactionDeploySingleDeploymentMethodTest < Test::Unit::TestCase
  138. def setup
  139. @method, @obj = method_stub(Wondercap::Deployment::SnapshotTransaction, :deploy_single_deployment)
  140. @getter = stub("getter")
  141. @obj.stubs(:configuration_getter).returns(@getter)
  142. Wondercap::Deployment::Deployment.stubs(:transaction).yields
  143. Wondercap::Deployment::Deployment.stubs(:inspect)
  144. end
  145. def test_deploys_and_links_new_deployment
  146. conf = stub_everything("conf")
  147. dep = stub_everything("deployment")
  148. new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
  149. @getter.stubs(:call).with(dep).returns(conf)
  150. conf.expects(:deploy)
  151. conf.expects(:link_deployment)
  152. @method.call(dep, new_snapshot, nil)
  153. end
  154. def test_unlinks_previous_deployment
  155. dep = stub_everything("deployment")
  156. new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
  157. prev_dep = stub_everything("prev_dep")
  158. prev_snapshot = stub_everything("prev_snapshot", :find_deployment_by_name => prev_dep)
  159. prev_conf = stub_everything("prev_conf")
  160. @getter.stubs(:call).with(dep).returns(stub_everything("conf"))
  161. @getter.stubs(:call).with(prev_dep).returns(prev_conf)
  162. prev_conf.expects(:unlink_deployment)
  163. @method.call(dep, new_snapshot, prev_snapshot)
  164. end
  165. def test_raises_deploy_errors
  166. conf = stub_everything("conf")
  167. dep = stub_everything("deployment")
  168. new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
  169. @getter.stubs(:call).with(dep).returns(conf)
  170. conf.stubs(:deploy).raises(RuntimeError.new)
  171. assert_raise(RuntimeError) { @method.call(dep, new_snapshot, nil) }
  172. end
  173. def test_raises_link_errors
  174. conf = stub_everything("conf")
  175. dep = stub_everything("deployment")
  176. new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
  177. @getter.stubs(:call).with(dep).returns(conf)
  178. conf.stubs(:link_deployment).raises(RuntimeError.new)
  179. assert_raise(RuntimeError) { @method.call(dep, new_snapshot, nil) }
  180. end
  181. def test_does_not_raise_unlink_errors
  182. dep = stub_everything("deployment")
  183. new_snapshot = stub_everything("new_snapshot", :find_or_initialize_deployment_by_name => stub_everything("new_deployment"))
  184. prev_strategy = stub_everything("prev_strategy")
  185. prev_dep = stub_everything("prev_dep")
  186. prev_snapshot = stub_everything("prev_snapshot", :find_deployment_by_name => prev_dep)
  187. @getter.stubs(:call).with(dep).returns(stub_everything("conf", :strategy => stub_everything("strategy")))
  188. @getter.stubs(:call).with(prev_dep).returns(stub_everything("prev_conf", :strategy => prev_strategy))
  189. prev_strategy.stubs(:unlink).raises(RuntimeError.new)
  190. assert_nothing_thrown { @method.call(dep, new_snapshot, prev_snapshot) }
  191. end
  192. end