PageRenderTime 32ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/chef/spec/unit/provider/subversion_spec.rb

https://github.com/pcross616/chef
Ruby | 230 lines | 198 code | 15 blank | 17 comment | 10 complexity | 7bc2012fdeb0f825aabf7a50eb987212 MD5 | raw file
  1. #
  2. # Author:: Daniel DeLeo (<dan@kallistec.com>)
  3. # Copyright:: Copyright (c) 2008 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
  19. describe Chef::Provider::Subversion do
  20. before do
  21. @resource = Chef::Resource::Subversion.new("my app")
  22. @resource.repository "http://svn.example.org/trunk/"
  23. @resource.destination "/my/deploy/dir"
  24. @resource.revision "12345"
  25. @node = Chef::Node.new
  26. @provider = Chef::Provider::Subversion.new(@node, @resource)
  27. end
  28. it "converts resource attributes to options for run_command and popen4" do
  29. @provider.run_options.should == {}
  30. @resource.user 'deployninja'
  31. @provider.run_options.should == {:user => "deployninja"}
  32. end
  33. context "determining the revision of the currently deployed code" do
  34. before do
  35. @stdout = mock("stdout")
  36. @stderr = mock("stderr")
  37. @exitstatus = mock("exitstatus")
  38. end
  39. it "sets the revision to nil if there isn't any deployed code yet" do
  40. ::File.should_receive(:exist?).with("/my/deploy/dir").and_return(false)
  41. @provider.find_current_revision.should be_nil
  42. end
  43. it "determines the current revision if there's a checkout with svn data available" do
  44. example_svn_info = "Path: .\n" +
  45. "URL: http://svn.example.org/trunk/myapp\n" +
  46. "Repository Root: http://svn.example.org\n" +
  47. "Repository UUID: d62ff500-7bbc-012c-85f1-0026b0e37c24\n" +
  48. "Revision: 11739\nNode Kind: directory\n" +
  49. "Schedule: normal\n" +
  50. "Last Changed Author: codeninja\n" +
  51. "Last Changed Rev: 11410\n" + # Last Changed Rev is preferred to Revision
  52. "Last Changed Date: 2009-03-25 06:09:56 -0600 (Wed, 25 Mar 2009)\n\n"
  53. ::File.should_receive(:exist?).with("/my/deploy/dir").and_return(true)
  54. ::File.should_receive(:directory?).with("/my/deploy/dir").and_return(true)
  55. ::Dir.should_receive(:chdir).with("/my/deploy/dir").and_yield
  56. @stdout.stub!(:string).and_return(example_svn_info)
  57. @stderr.stub!(:string).and_return("")
  58. @exitstatus.stub!(:exitstatus).and_return(0)
  59. expected_command = ["svn info", {:cwd=>"/my/deploy/dir"}]
  60. @provider.should_receive(:popen4).with(*expected_command).
  61. and_yield("no-pid", "no-stdin", @stdout,@stderr).
  62. and_return(@exitstatus)
  63. @provider.find_current_revision.should eql("11410")
  64. end
  65. it "gives nil as the current revision if the deploy dir isn't a SVN working copy" do
  66. example_svn_info = "svn: '/tmp/deploydir' is not a working copy\n"
  67. ::File.should_receive(:exist?).with("/my/deploy/dir").and_return(true)
  68. ::File.should_receive(:directory?).with("/my/deploy/dir").and_return(true)
  69. ::Dir.should_receive(:chdir).with("/my/deploy/dir").and_yield
  70. @stdout.stub!(:string).and_return(example_svn_info)
  71. @stderr.stub!(:string).and_return("")
  72. @exitstatus.stub!(:exitstatus).and_return(1)
  73. @provider.should_receive(:popen4).and_yield("no-pid", "no-stdin", @stdout,@stderr).
  74. and_return(@exitstatus)
  75. @provider.find_current_revision.should be_nil
  76. end
  77. end
  78. it "creates the current_resource object and sets its revision to the current deployment's revision" do
  79. @provider.stub!(:find_current_revision).and_return("11410")
  80. @provider.load_current_resource
  81. @provider.current_resource.name.should eql(@resource.name)
  82. @provider.current_resource.revision.should eql("11410")
  83. end
  84. context "resolving revisions to an integer" do
  85. before do
  86. @stdout = mock("stdout")
  87. @stderr = mock("stderr")
  88. end
  89. it "returns the revision number as is if it's already an integer" do
  90. @provider.revision_int.should eql("12345")
  91. end
  92. it "queries the server and resolves the revision if it's not an integer (i.e. 'HEAD')" do
  93. example_svn_info = "Path: .\n" +
  94. "URL: http://svn.example.org/trunk/myapp\n" +
  95. "Repository Root: http://svn.example.org\n" +
  96. "Repository UUID: d62ff500-7bbc-012c-85f1-0026b0e37c24\n" +
  97. "Revision: 11739\nNode Kind: directory\n" +
  98. "Schedule: normal\n" +
  99. "Last Changed Author: codeninja\n" +
  100. "Last Changed Rev: 11410\n" + # Last Changed Rev is preferred to Revision
  101. "Last Changed Date: 2009-03-25 06:09:56 -0600 (Wed, 25 Mar 2009)\n\n"
  102. exitstatus = mock("exitstatus")
  103. exitstatus.stub!(:exitstatus).and_return(0)
  104. @resource.revision "HEAD"
  105. @stdout.stub!(:string).and_return(example_svn_info)
  106. @stderr.stub!(:string).and_return("")
  107. @provider.should_receive(:popen4).and_yield("no-pid","no-stdin",@stdout,@stderr).
  108. and_return(exitstatus)
  109. @provider.revision_int.should eql("11410")
  110. end
  111. it "returns a helpful message if data from `svn info` can't be parsed" do
  112. example_svn_info = "some random crap from an error message\n"
  113. exitstatus = mock("exitstatus")
  114. exitstatus.stub!(:exitstatus).and_return(0)
  115. @resource.revision "HEAD"
  116. @stdout.stub!(:string).and_return(example_svn_info)
  117. @stderr.stub!(:string).and_return("")
  118. @provider.should_receive(:popen4).and_yield("no-pid","no-stdin",@stdout,@stderr).
  119. and_return(exitstatus)
  120. lambda {@provider.revision_int}.should raise_error(RuntimeError, "Could not parse `svn info` data: some random crap from an error message")
  121. end
  122. it "responds to :revision_slug as an alias for revision_sha" do
  123. @provider.should respond_to(:revision_slug)
  124. end
  125. end
  126. it "generates a checkout command with default options" do
  127. @provider.checkout_command.should eql("svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir")
  128. end
  129. it "generates a checkout command with authentication" do
  130. @resource.svn_username "deployNinja"
  131. @resource.svn_password "vanish!"
  132. @provider.checkout_command.should eql("svn checkout -q --username deployNinja --password vanish! " +
  133. "-r12345 http://svn.example.org/trunk/ /my/deploy/dir")
  134. end
  135. it "generates a checkout command with arbitrary options" do
  136. @resource.svn_arguments "--no-auth-cache"
  137. @provider.checkout_command.should eql("svn checkout --no-auth-cache -q -r12345 "+
  138. "http://svn.example.org/trunk/ /my/deploy/dir")
  139. end
  140. it "generates a sync command with default options" do
  141. @provider.sync_command.should eql("svn update -q -r12345 /my/deploy/dir")
  142. end
  143. it "generates an export command with default options" do
  144. @provider.export_command.should eql("svn export -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir")
  145. end
  146. it "generates an export command with the --force option" do
  147. expected = "svn export --force -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir"
  148. @provider.export_command(:force => true).should == expected
  149. end
  150. it "runs an export with the --force option" do
  151. expected_cmd = "svn export --force -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir"
  152. @provider.should_receive(:run_command).with(:command => expected_cmd)
  153. @provider.action_force_export
  154. end
  155. it "runs the checkout command for action_checkout" do
  156. expected_cmd = "svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir"
  157. @provider.should_receive(:run_command).with(:command => expected_cmd)
  158. @resource.should_receive(:updated=).at_least(1).times.with(true)
  159. @provider.action_checkout
  160. end
  161. it "runs commands with the user and group specified in the resource" do
  162. @resource.user "whois"
  163. @resource.group "thisis"
  164. expected_cmd = "svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir"
  165. @provider.should_receive(:run_command).with(:command => expected_cmd, :user => "whois", :group => "thisis")
  166. @resource.should_receive(:updated=).at_least(1).times.with(true)
  167. @provider.action_checkout
  168. end
  169. it "does a checkout for action_sync if there's no deploy dir" do
  170. ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").and_return(false)
  171. @provider.should_receive(:action_checkout)
  172. @resource.should_receive(:updated=).at_least(1).times.with(true)
  173. @provider.action_sync
  174. end
  175. it "does a checkout for action_sync if the deploy dir exists but is empty" do
  176. ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").and_return(true)
  177. ::Dir.should_receive(:entries).with("/my/deploy/dir").and_return(['.','..'])
  178. @provider.should_receive(:action_checkout)
  179. @resource.should_receive(:updated=).at_least(1).times.with(true)
  180. @provider.action_sync
  181. end
  182. it "runs the sync_command on action_sync if the deploy dir exists and isn't empty" do
  183. ::File.should_receive(:exist?).with("/my/deploy/dir/.svn").and_return(true)
  184. ::Dir.should_receive(:entries).with("/my/deploy/dir").and_return(['.','..','the','app','exists'])
  185. expected_cmd = "svn update -q -r12345 /my/deploy/dir"
  186. @provider.should_receive(:run_command).with(:command => expected_cmd)
  187. @resource.should_receive(:updated=).at_least(1).times.with(true)
  188. @provider.action_sync
  189. end
  190. it "runs the export_command on action_export" do
  191. expected_cmd = "svn export -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir"
  192. @provider.should_receive(:run_command).with(:command => expected_cmd)
  193. @resource.should_receive(:updated=).at_least(1).times.with(true)
  194. @provider.action_export
  195. end
  196. end