/spec/travis/model/job/queue_spec.rb

https://github.com/travis-ci/travis-core · Ruby · 310 lines · 256 code · 54 blank · 0 comment · 35 complexity · 149846f64d43809eeaf68b235ace2aad MD5 · raw file

  1. require 'spec_helper'
  2. describe 'Job::Queue' do
  3. def queue(*args)
  4. Job::Queue.new(*args)
  5. end
  6. let(:the_past) { Time.parse("1982-06-23") }
  7. let(:recently) { 7.days.ago }
  8. before do
  9. Travis.config.queues = [
  10. { queue: 'builds.rails', slug: 'rails/rails' },
  11. { queue: 'builds.mac_osx', os: 'osx' },
  12. { queue: 'builds.docker', sudo: false },
  13. { queue: 'builds.gce', services: %w(docker) },
  14. { queue: 'builds.gce', dist: 'trusty' },
  15. { queue: 'builds.cloudfoundry', owner: 'cloudfoundry' },
  16. { queue: 'builds.clojure', language: 'clojure' },
  17. { queue: 'builds.erlang', language: 'erlang' },
  18. ]
  19. Job::Queue.instance_variable_set(:@queues, nil)
  20. Job::Queue.instance_variable_set(:@default, nil)
  21. Travis::Features.stubs(:owner_active?).returns(true)
  22. Travis::Github::Education.stubs(:education_queue?).returns(false)
  23. end
  24. after do
  25. Travis.config.default_queue = 'builds.linux'
  26. end
  27. it 'returns builds.linux as the default queue' do
  28. Job::Queue.default.name.should == 'builds.linux'
  29. end
  30. it 'returns builds.common as the default queue if configured to in Travis.config' do
  31. Travis.config.default_queue = 'builds.common'
  32. Job::Queue.default.name.should == 'builds.common'
  33. end
  34. describe 'Queue.sudo_detected?' do
  35. [
  36. [{ script: 'sudo echo' }, true],
  37. [{ bogus: 'sudo echo' }, false],
  38. [{ before_install: ['# no sudo', 'ping -c 1 google.com'] }, true],
  39. [{ before_install: ['docker run busybox echo whatever'] }, true],
  40. [{ before_script: ['echo ; echo ; echo ; sudo echo ; echo'] }, true],
  41. [{ install: '# no sudo needed here' }, false],
  42. [{ install: true }, false],
  43. ].each do |config, expected|
  44. it "returns #{expected} for #{config}" do
  45. Job::Queue.sudo_detected?(config).should == expected
  46. end
  47. end
  48. end
  49. describe 'Queue.for' do
  50. it 'returns the default build queue when neither slug or language match the given configuration hash' do
  51. job = stub('job', :config => {}, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-ci', :owner => stub, :created_at => the_past))
  52. Job::Queue.for(job).name.should == 'builds.linux'
  53. end
  54. it 'returns the queue when slug matches the given configuration hash' do
  55. job = stub('job', :config => {}, :repository => stub('repository', :owner_name => 'rails', :name => 'rails', :owner => stub, :created_at => the_past))
  56. Job::Queue.for(job).name.should == 'builds.rails'
  57. end
  58. it 'returns the queue when language matches the given configuration hash' do
  59. job = stub('job', :config => { :language => 'clojure' }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-ci', :owner => stub, :created_at => the_past))
  60. Job::Queue.for(job).name.should == 'builds.clojure'
  61. end
  62. it 'returns the queue when the owner matches the given configuration hash' do
  63. job = stub('job', :config => {}, :repository => stub('repository', :owner_name => 'cloudfoundry', :name => 'bosh', :owner => stub, :created_at => the_past))
  64. Job::Queue.for(job).name.should == 'builds.cloudfoundry'
  65. end
  66. it 'returns the queue when sudo requirements matches the given configuration hash' do
  67. job = stub('job', :config => { sudo: false }, :repository => stub('repository', :owner_name => 'markronson', :name => 'recordcollection', :owner => stub, :created_at => the_past))
  68. Job::Queue.for(job).name.should == 'builds.docker'
  69. end
  70. it 'returns the docker queue by default for educational repositories' do
  71. Travis::Github::Education.stubs(:education_queue?).returns(true)
  72. owner = stub('owner', :education => true)
  73. job = stub('job', :config => { }, :repository => stub('repository', :owner_name => 'markronson', :name => 'recordcollection', :owner => owner, :created_at => the_past))
  74. Job::Queue.for(job).name.should == 'builds.docker'
  75. end
  76. it 'returns the queue matching configuration for educational repository' do
  77. Travis::Github::Education.stubs(:education_queue?).returns(true)
  78. owner = stub('owner', :education => true)
  79. job = stub('job', :config => { :os => 'osx' }, :repository => stub('repository', :owner_name => 'markronson', :name => 'recordcollection', :owner => owner, :created_at => the_past))
  80. Job::Queue.for(job).name.should == 'builds.mac_osx'
  81. end
  82. it 'handles language being passed as an array gracefully' do
  83. job = stub('job', :config => { :language => ['clojure'] }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-ci', :owner => stub, :created_at => the_past))
  84. Job::Queue.for(job).name.should == 'builds.clojure'
  85. end
  86. context 'when "os" value matches the given configuration hash' do
  87. it 'returns the matching queue' do
  88. job = stub('job', :config => { :os => 'osx'}, :repository => stub('travis-core', :owner_name => 'travis-ci', :name => 'bosh', :owner => stub, :created_at => the_past))
  89. Job::Queue.for(job).name.should == 'builds.mac_osx'
  90. end
  91. it 'returns the matching queue when language is also given' do
  92. job = stub('job', :config => {:language => 'clojure', :os => 'osx'}, :repository => stub('travis-core', :owner_name => 'travis-ci', :name => 'bosh', :owner => stub, :created_at => the_past))
  93. Job::Queue.for(job).name.should == 'builds.mac_osx'
  94. end
  95. end
  96. context 'when "services" value matches the given configuration hash' do
  97. it 'returns the matching queue' do
  98. job = stub('job', config: { services: %w(redis docker postgresql) }, repository: stub('travis-core', owner_name: 'travis-ci', name: 'bosh', owner: stub, created_at: the_past))
  99. Job::Queue.for(job).name.should == 'builds.gce'
  100. end
  101. it 'returns the matching queue when language is also given' do
  102. job = stub('job', config: { language: 'clojure', services: %w(redis docker postgresql) }, repository: stub('travis-core', owner_name: 'travis-ci', name: 'bosh', owner: stub, created_at: the_past))
  103. Job::Queue.for(job).name.should == 'builds.gce'
  104. end
  105. end
  106. context 'when "docker_default_queue" feature is active' do
  107. before do
  108. Travis::Features.stubs(:feature_active?).with(:docker_default_queue).returns(true)
  109. Travis::Features.stubs(:feature_active?).with(:education).returns(true)
  110. end
  111. it 'returns "builds.docker" when sudo: nil and the repo created_at is nil' do
  112. job = stub('job', :config => { }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => nil))
  113. Job::Queue.for(job).name.should == 'builds.docker'
  114. end
  115. it 'returns "builds.docker" when sudo: nil and the repo created_at is after cutoff' do
  116. Travis.config.docker_default_queue_cutoff = recently.to_s
  117. job = stub('job', :config => { }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => Time.now))
  118. Job::Queue.for(job).name.should == 'builds.docker'
  119. end
  120. it 'returns "builds.linux" when sudo: nil and the repo created_at is before cutoff' do
  121. Travis.config.docker_default_queue_cutoff = recently.to_s
  122. job = stub('job', :config => { }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => recently - 7.days))
  123. Job::Queue.for(job).name.should == 'builds.linux'
  124. end
  125. it 'returns "builds.linux" when sudo: nil and the repo created_at is after cutoff and sudo is detected' do
  126. Travis.config.docker_default_queue_cutoff = recently.to_s
  127. job = stub('job', :config => { script: 'sudo echo whatever' }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => recently - 7.days))
  128. Job::Queue.for(job).name.should == 'builds.linux'
  129. end
  130. it 'returns "builds.docker" when sudo: false and the repo created_at is nil' do
  131. job = stub('job', :config => { sudo: false }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => nil))
  132. Job::Queue.for(job).name.should == 'builds.docker'
  133. end
  134. it 'returns "builds.docker" when sudo: false and the repo created_at is after cutoff' do
  135. Travis.config.docker_default_queue_cutoff = recently.to_s
  136. job = stub('job', :config => { sudo: false }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => Time.now))
  137. Job::Queue.for(job).name.should == 'builds.docker'
  138. end
  139. it 'returns "builds.docker" when sudo: false and the repo created_at is before cutoff' do
  140. Travis.config.docker_default_queue_cutoff = recently.to_s
  141. job = stub('job', :config => { sudo: false }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => recently - 7.days))
  142. Job::Queue.for(job).name.should == 'builds.docker'
  143. end
  144. [true, 'required'].each do |sudo|
  145. it %{returns "builds.linux" when sudo: #{sudo} and the repo created_at is nil} do
  146. Travis.config.docker_default_queue_cutoff = recently.to_s
  147. job = stub('job', :config => { sudo: sudo }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => nil))
  148. Job::Queue.for(job).name.should == 'builds.linux'
  149. end
  150. it %{returns "builds.linux" when sudo: #{sudo} and the repo created_at is after cutoff} do
  151. Travis.config.docker_default_queue_cutoff = recently.to_s
  152. job = stub('job', :config => { sudo: sudo }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => nil))
  153. Job::Queue.for(job).name.should == 'builds.linux'
  154. end
  155. it %{returns "builds.linux" when sudo: #{sudo} and the repo created_at is before cutoff} do
  156. Travis.config.docker_default_queue_cutoff = recently.to_s
  157. job = stub('job', :config => { sudo: sudo }, :repository => stub('repository', :owner_name => 'travis-ci', :name => 'travis-core', :owner => stub, :created_at => nil))
  158. Job::Queue.for(job).name.should == 'builds.linux'
  159. end
  160. end
  161. end
  162. end
  163. context 'when "sudo" value matches the given configuration hash' do
  164. it 'returns the matching queue' do
  165. job = stub('job', config: { sudo: false }, repository: stub('travis-core', owner_name: 'travis-ci', name: 'travis-core', owner: stub, :created_at => the_past))
  166. Job::Queue.for(job).name.should == 'builds.docker'
  167. end
  168. it 'returns the matching queue when language is also given' do
  169. job = stub('job', config: { language: 'clojure', sudo: false }, repository: stub('travis-core', owner_name: 'travis-ci', name: 'travis-core', owner: stub, :created_at => the_past))
  170. Job::Queue.for(job).name.should == 'builds.docker'
  171. end
  172. end
  173. describe 'Queue.queues' do
  174. it 'returns an array of Queues for the config hash' do
  175. rails, _, docker, _, _, cloudfoundry, clojure, _ = Job::Queue.send(:queues)
  176. rails.name.should == 'builds.rails'
  177. rails.attrs[:slug].should == 'rails/rails'
  178. docker.name.should == 'builds.docker'
  179. docker.attrs[:sudo].should == false
  180. cloudfoundry.name.should == 'builds.cloudfoundry'
  181. cloudfoundry.attrs[:owner].should == 'cloudfoundry'
  182. clojure.name.should == 'builds.clojure'
  183. clojure.attrs[:language].should == 'clojure'
  184. end
  185. end
  186. describe 'matches?' do
  187. it "returns false when neither of slug or language match" do
  188. queue = queue('builds.linux', {})
  189. queue.matches?(stub('job', repository: stub('repository', owner_name: 'foo', name: 'bar', owner: nil), config: { language: 'COBOL' })).should be_false
  190. end
  191. it "returns true when the given owner matches" do
  192. queue = queue('builds.cloudfoundry', { owner: 'cloudfoundry' })
  193. queue.matches?(stub('job', repository: stub('repository', owner_name: 'cloudfoundry', name: 'bosh', owner: nil), config: {})).should be_true
  194. end
  195. it "returns true when the given slug matches" do
  196. queue = queue('builds.rails', { slug: 'rails/rails' })
  197. queue.matches?(stub('job', repository: stub('repository', owner_name: 'rails', name: 'rails', owner: nil), config: {})).should be_true
  198. end
  199. it "returns true when the given language matches" do
  200. queue = queue('builds.linux', { language: 'clojure' })
  201. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { language: 'clojure' })).should be_true
  202. end
  203. it 'returns true when os is missing' do
  204. queue = queue('builds.linux', { language: 'clojure' })
  205. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { language: 'clojure' })).should be_true
  206. end
  207. it 'returns true when sudo is false' do
  208. queue = queue('builds.docker', { sudo: false })
  209. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { sudo: false })).should be_true
  210. end
  211. it 'returns false when sudo is true' do
  212. queue = queue('builds.docker', { sudo: false })
  213. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { sudo: true })).should be_false
  214. end
  215. it 'returns false when sudo is not specified' do
  216. queue = queue('builds.docker', { sudo: false })
  217. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: {})).should be_false
  218. end
  219. it 'returns true when dist matches' do
  220. queue = queue('builds.gce', { dist: 'trusty' })
  221. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { dist: 'trusty' })).should be_true
  222. end
  223. it 'returns false when dist does not match' do
  224. queue = queue('builds.docker', { dist: 'precise' })
  225. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { dist: 'trusty' })).should be_false
  226. end
  227. it 'returns true when osx_image matches' do
  228. queue = queue('builds.mac_beta', { osx_image: 'beta' })
  229. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { osx_image: 'beta' })).should be_true
  230. end
  231. it 'returns false when osx_image does not match' do
  232. queue = queue('builds.mac_stable', { osx_image: 'stable' })
  233. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { osx_image: 'beta' })).should be_false
  234. end
  235. it 'returns true when services match' do
  236. queue = queue('builds.gce', { services: %w(docker) })
  237. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { services: %w(redis docker postgresql) })).should be_true
  238. end
  239. it 'returns false when services do not match' do
  240. queue = queue('builds.gce', { services: %w(docker) })
  241. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: { services: %w(redis postgresql) })).should be_false
  242. end
  243. it 'returns false if no valid matchers are specified' do
  244. queue = queue('builds.invalid', { foobar_donotmatch: true })
  245. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: {})).should be_false
  246. end
  247. it 'returns true for percentage: 100' do
  248. queue = queue('builds.always', { percentage: 100 })
  249. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: {})).should be_true
  250. end
  251. it 'returns false for percentage: 0' do
  252. queue = queue('builds.always', { percentage: 0 })
  253. queue.matches?(stub('job', repository: stub('repository', owner_name: nil, name: nil, owner: nil), config: {})).should be_false
  254. end
  255. end
  256. end