fake_servicebus 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.md +20 -0
  7. data/README.md +41 -0
  8. data/Rakefile +31 -0
  9. data/bin/fake_servicebus +65 -0
  10. data/fake_servicebus.gemspec +33 -0
  11. data/lib/fake_servicebus.rb +90 -0
  12. data/lib/fake_servicebus/actions/create_queue.rb +64 -0
  13. data/lib/fake_servicebus/actions/delete_message.rb +19 -0
  14. data/lib/fake_servicebus/actions/delete_queue.rb +18 -0
  15. data/lib/fake_servicebus/actions/get_queue.rb +20 -0
  16. data/lib/fake_servicebus/actions/list_queues.rb +28 -0
  17. data/lib/fake_servicebus/actions/receive_message.rb +44 -0
  18. data/lib/fake_servicebus/actions/renew_lock_message.rb +19 -0
  19. data/lib/fake_servicebus/actions/send_message.rb +22 -0
  20. data/lib/fake_servicebus/actions/unlock_message.rb +19 -0
  21. data/lib/fake_servicebus/api.rb +71 -0
  22. data/lib/fake_servicebus/catch_errors.rb +19 -0
  23. data/lib/fake_servicebus/collection_view.rb +20 -0
  24. data/lib/fake_servicebus/daemonize.rb +30 -0
  25. data/lib/fake_servicebus/databases/file.rb +129 -0
  26. data/lib/fake_servicebus/databases/memory.rb +30 -0
  27. data/lib/fake_servicebus/error_response.rb +55 -0
  28. data/lib/fake_servicebus/error_responses.yml +32 -0
  29. data/lib/fake_servicebus/message.rb +59 -0
  30. data/lib/fake_servicebus/queue.rb +201 -0
  31. data/lib/fake_servicebus/queue_factory.rb +16 -0
  32. data/lib/fake_servicebus/queues.rb +72 -0
  33. data/lib/fake_servicebus/responder.rb +41 -0
  34. data/lib/fake_servicebus/server.rb +19 -0
  35. data/lib/fake_servicebus/show_output.rb +21 -0
  36. data/lib/fake_servicebus/test_integration.rb +122 -0
  37. data/lib/fake_servicebus/version.rb +3 -0
  38. data/lib/fake_servicebus/web_interface.rb +74 -0
  39. data/spec/acceptance/message_actions_spec.rb +452 -0
  40. data/spec/acceptance/queue_actions_spec.rb +82 -0
  41. data/spec/integration_spec_helper.rb +23 -0
  42. data/spec/spec_helper.rb +3 -0
  43. data/spec/unit/api_spec.rb +76 -0
  44. data/spec/unit/catch_errors_spec.rb +43 -0
  45. data/spec/unit/collection_view_spec.rb +41 -0
  46. data/spec/unit/error_response_spec.rb +65 -0
  47. data/spec/unit/message_spec.rb +76 -0
  48. data/spec/unit/queue_factory_spec.rb +13 -0
  49. data/spec/unit/queue_spec.rb +204 -0
  50. data/spec/unit/queues_spec.rb +102 -0
  51. data/spec/unit/responder_spec.rb +44 -0
  52. data/spec/unit/show_output_spec.rb +22 -0
  53. data/spec/unit/web_interface_spec.rb +15 -0
  54. metadata +266 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: acdda194a4fa43f3e50829075b48842060759a9ab380216a1c827cd07ef599e6
4
+ data.tar.gz: 4b7b170a0f0371abf2a2a4671aa781af6eb4ab70aa0a0f252862523ec2aa45a3
5
+ SHA512:
6
+ metadata.gz: 7f4a48c6a4350effaa17430ab73eba40caf1990acdb3ec6e1ad79689e2c316fe2c5c9dd8c14c0b259aea663eedc9312e44c31e03da5cd35e0614306126b05756
7
+ data.tar.gz: decaa02b7d9649176ef802c9dcb81f9f06c73d343d54bd462f257806d6dd23fd5dfb1989cee807d0cdac2a59fdb452fea96b599a351c02b657a3649271b8a9d0
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .DS_Store
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --format documentation
2
+ --color
3
+ --order random
4
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fake_servicebus.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2012 Iain Hecker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Fake Service Bus [![Gem Version](https://badge.fury.io/rb/fake_servicebus.svg)](https://badge.fury.io/rb/fake_servicebus)
2
+
3
+ Fake Service Bus is inspired by [Fake SQS](https://github.com/iain/fake_sqs) and it's also forked from Fake SQS, thanks Fake SQS.
4
+
5
+ Fake Service Bus is a lightweight server that mocks the Azure Service Bus API.
6
+
7
+ It is extremely useful for testing Service Bus applications in a sandbox environment without actually
8
+ making calls to Azure, which not only requires a network connection, but also costs
9
+ money.
10
+
11
+ Currently, only Queue APIs are supported
12
+ * List queues
13
+ * Create queue
14
+ * Get queue
15
+ * Delete queue
16
+ * Send message to queue
17
+ * Receive message from queue with timeout
18
+ * Unlock message
19
+ * Renew message
20
+ * Delete message
21
+
22
+ PRs are welcome.
23
+
24
+ ## Installation
25
+
26
+ ```
27
+ gem install fake_servicebus
28
+ ```
29
+
30
+ ## Running
31
+
32
+ ```
33
+ fake_servicebus --database /path/to/database.yml
34
+ ```
35
+
36
+ ## Development
37
+
38
+ ```
39
+ bundle install
40
+ rake
41
+ ```
@@ -0,0 +1,31 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "tempfile"
4
+ require 'rspec/core/rake_task'
5
+
6
+ namespace :spec do
7
+
8
+ desc "Run only unit specs"
9
+ RSpec::Core::RakeTask.new(:unit) do |t|
10
+ t.pattern = "spec/unit"
11
+ end
12
+
13
+ desc "Run specs with in-memory database"
14
+ RSpec::Core::RakeTask.new(:memory) do |t|
15
+ ENV["SQS_DATABASE"] = ":memory:"
16
+ t.pattern = "spec/acceptance"
17
+ end
18
+
19
+ desc "Run specs with file database"
20
+ RSpec::Core::RakeTask.new(:file) do |t|
21
+ file = Tempfile.new(["rspec-sqs", ".yml"], encoding: "utf-8")
22
+ ENV["SQS_DATABASE"] = file.path
23
+ t.pattern = "spec/acceptance"
24
+ end
25
+
26
+ end
27
+
28
+ desc "Run spec suite with both in-memory and file"
29
+ task :spec => ["spec:unit", "spec:memory", "spec:file"]
30
+
31
+ task :default => :spec
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path("../../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'fake_servicebus'
7
+ require 'optparse'
8
+
9
+ options = {
10
+ :port => 10002,
11
+ :host => "0.0.0.0",
12
+ :verbose => false,
13
+ :daemonize => false,
14
+ :database => ":memory:"
15
+ }
16
+
17
+ parser = OptionParser.new do |o|
18
+
19
+ o.on "--database DATABASE", "Where to store the database (default: #{options[:database]})" do |database|
20
+ options[:database] = database
21
+ end
22
+
23
+ o.on "-p", "--port PORT", Integer, "Port to use (default: #{options[:port]})" do |port|
24
+ options[:port] = port
25
+ end
26
+
27
+ o.on "-o", "--bind HOST", "Host to bind to (default: #{options[:host]})" do |host|
28
+ options[:host] = host
29
+ end
30
+
31
+ o.on "-s", "--server SERVER", ['thin', 'mongrel', 'webrick'], "Server to use: thin, mongrel or webrick (by default Sinatra chooses the best available)" do |server|
32
+ options[:server] = server
33
+ end
34
+
35
+ o.on "-P", "--pid PIDFILE", "Where to write the pid" do |pid|
36
+ options[:pid] = pid
37
+ end
38
+
39
+ o.on "-d", "--[no-]daemonize", "Detaches the process" do |daemonize|
40
+ options[:daemonize] = daemonize
41
+ end
42
+
43
+ o.on "-v", "--[no-]verbose", "Shows input parameters and output XML" do |verbose|
44
+ options[:verbose] = verbose
45
+ end
46
+
47
+ o.on "--log FILE", "Redirect output to this logfile (default: console)" do |logfile|
48
+ options[:log] = logfile
49
+ end
50
+
51
+ o.on_tail "--version", "Shows the version" do
52
+ puts "fake_servicebus version #{FakeServiceBus::VERSION}"
53
+ exit
54
+ end
55
+
56
+ o.on_tail "-h", "--help", "Shows this help page" do
57
+ puts o
58
+ exit
59
+ end
60
+
61
+ end
62
+
63
+ parser.parse!
64
+
65
+ FakeServiceBus.to_rack(options).run!
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fake_servicebus/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fake_servicebus"
8
+ gem.version = FakeServiceBus::VERSION
9
+ gem.authors = ["JonesChi"]
10
+ gem.email = ["duguschi@gmail.com"]
11
+ gem.summary = %q{Provides a fake Service Bus server that you can run locally to test against}
12
+ gem.homepage = "https://github.com/JonesChi/fake_servicebus"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ gem.license = "MIT"
19
+
20
+
21
+ gem.add_dependency "rack", "~> 1.6"
22
+ gem.add_dependency "sinatra", "~> 1.4"
23
+ gem.add_dependency "builder", "~> 3.2"
24
+ gem.add_dependency "nokogiri", "~> 1.11.0.rc3"
25
+ gem.add_dependency "ruby-duration", "~> 3.2"
26
+
27
+ gem.add_development_dependency "rspec", "~> 3.6"
28
+ gem.add_development_dependency "rake", "~> 12.0"
29
+ gem.add_development_dependency "rack-test", "~> 0.7"
30
+ gem.add_development_dependency "thin", "~> 1.7"
31
+ gem.add_development_dependency "verbose_hash_fetch", "~> 0.0"
32
+ gem.add_development_dependency "activesupport", "~> 5.1"
33
+ end
@@ -0,0 +1,90 @@
1
+ require 'fake_servicebus/api'
2
+ require 'fake_servicebus/catch_errors'
3
+ require 'fake_servicebus/collection_view'
4
+ require 'fake_servicebus/error_response'
5
+ require 'fake_servicebus/message'
6
+ require 'fake_servicebus/queue'
7
+ require 'fake_servicebus/queue_factory'
8
+ require 'fake_servicebus/queues'
9
+ require 'fake_servicebus/responder'
10
+ require 'fake_servicebus/server'
11
+ require 'fake_servicebus/version'
12
+ require 'fake_servicebus/databases/file'
13
+ require 'fake_servicebus/databases/memory'
14
+
15
+ module FakeServiceBus
16
+
17
+ def self.to_rack(options)
18
+
19
+ require 'fake_servicebus/web_interface'
20
+ app = FakeServiceBus::WebInterface
21
+
22
+ if (log = options[:log])
23
+ file = File.new(log, "a+")
24
+ file.sync = true
25
+ app.use Rack::CommonLogger, file
26
+ app.set :log_file, file
27
+ app.enable :logging
28
+ end
29
+
30
+ if options[:verbose]
31
+ require 'fake_servicebus/show_output'
32
+ app.use FakeServiceBus::ShowOutput
33
+ app.enable :logging
34
+ end
35
+
36
+ if options[:daemonize]
37
+ require 'fake_servicebus/daemonize'
38
+ Daemonize.new(options).call
39
+ end
40
+
41
+ app.set :port, options[:port] if options[:port]
42
+ app.set :bind, options[:host] if options[:host]
43
+ app.set :server, options[:server] if options[:server]
44
+ server = FakeServiceBus.server(port: options[:port], host: options[:host])
45
+ app.set :api, FakeServiceBus.api(server: server, database: options[:database])
46
+ app
47
+ end
48
+
49
+ def self.server(options = {})
50
+ Server.new(options)
51
+ end
52
+
53
+ def self.api(options = {})
54
+ db = database_for(options.fetch(:database) { ":memory:" })
55
+ API.new(
56
+ server: options.fetch(:server),
57
+ queues: queues(db),
58
+ responder: responder
59
+ )
60
+ end
61
+
62
+ def self.queues(database)
63
+ Queues.new(queue_factory: queue_factory, database: database)
64
+ end
65
+
66
+ def self.responder
67
+ Responder.new
68
+ end
69
+
70
+ def self.queue_factory
71
+ QueueFactory.new(message_factory: message_factory, queue: queue)
72
+ end
73
+
74
+ def self.message_factory
75
+ Message
76
+ end
77
+
78
+ def self.queue
79
+ Queue
80
+ end
81
+
82
+ def self.database_for(name)
83
+ if name == ":memory:"
84
+ MemoryDatabase.new
85
+ else
86
+ FileDatabase.new(name)
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,64 @@
1
+ require 'time'
2
+ require 'nokogiri'
3
+
4
+ module FakeServiceBus
5
+ module Actions
6
+ class CreateQueue
7
+
8
+ def initialize(options = {})
9
+ @server = options.fetch(:server)
10
+ @queues = options.fetch(:queues)
11
+ @responder = options.fetch(:responder)
12
+ @request = options.fetch(:request)
13
+ end
14
+
15
+ def call(queue_name, params)
16
+ attributes = {}
17
+ root = Nokogiri::XML(@request.body.read)
18
+ root.remove_namespaces!
19
+ queue_element = root.xpath('.//content/QueueDescription')[0]
20
+ if not queue_element.nil?
21
+ if elem = queue_element.xpath('LockDuration')[0]
22
+ attributes['LockDuration'] = elem.text
23
+ end
24
+ if elem = queue_element.xpath('MaxSizeInMegabytes')[0]
25
+ attributes['MaxSizeInMegabytes'] = elem.text.to_i
26
+ end
27
+ if elem = queue_element.xpath('RequiresDuplicateDetection')[0]
28
+ attributes['RequiresDuplicateDetection'] = elem.text == "true"
29
+ end
30
+ if elem = queue_element.xpath('RequiresSession')[0]
31
+ attributes['RequiresSession'] = elem.text == "true"
32
+ end
33
+ if elem = queue_element.xpath('DefaultMessageTimeToLive')[0]
34
+ attributes['DefaultMessageTimeToLive'] = elem.text
35
+ end
36
+ if elem = queue_element.xpath('DeadLetteringOnMessageExpiration')[0]
37
+ attributes['DeadLetteringOnMessageExpiration'] = elem.text == "true"
38
+ end
39
+ if elem = queue_element.xpath('DuplicateDetectionHistoryTimeWindow')[0]
40
+ attributes['DuplicateDetectionHistoryTimeWindow'] = elem.text
41
+ end
42
+ if elem = queue_element.xpath('MaxDeliveryCount')[0]
43
+ attributes['MaxDeliveryCount'] = elem.text.to_i
44
+ end
45
+ if elem = queue_element.xpath('EnableBatchedOperations')[0]
46
+ attributes['EnableBatchedOperations'] = elem.text == "true"
47
+ end
48
+ if elem = queue_element.xpath('MessageCount')[0]
49
+ attributes['MessageCount'] = elem.text.to_i
50
+ end
51
+ if elem = queue_element.xpath('SizeInBytes')[0]
52
+ attributes['SizeInBytes'] = elem.text.to_i
53
+ end
54
+ end
55
+
56
+ queue = @queues.create(queue_name, {'Attributes'=>attributes})
57
+ xml = Builder::XmlMarkup.new()
58
+ body = @responder.queue xml, queue
59
+ [201, body]
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,19 @@
1
+ module FakeServiceBus
2
+ module Actions
3
+ class DeleteMessage
4
+
5
+ def initialize(options = {})
6
+ @server = options.fetch(:server)
7
+ @queues = options.fetch(:queues)
8
+ end
9
+
10
+ def call(queue_name, lock_token, params)
11
+ queue = @queues.get(queue_name)
12
+
13
+ queue.delete_message(lock_token)
14
+ 200
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module FakeServiceBus
2
+ module Actions
3
+ class DeleteQueue
4
+
5
+ def initialize(options = {})
6
+ @server = options.fetch(:server)
7
+ @queues = options.fetch(:queues)
8
+ @responder = options.fetch(:responder)
9
+ end
10
+
11
+ def call(queue_name, params = {})
12
+ @queues.delete(queue_name, params)
13
+ #@responder.call queue_name
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module FakeServiceBus
2
+ module Actions
3
+ class GetQueue
4
+
5
+ def initialize(options = {})
6
+ @server = options.fetch(:server)
7
+ @queues = options.fetch(:queues)
8
+ @responder = options.fetch(:responder)
9
+ @request = options.fetch(:request)
10
+ end
11
+
12
+ def call(queue_name, params)
13
+ queue = @queues.get(queue_name, params)
14
+ xml = Builder::XmlMarkup.new()
15
+ @responder.queue xml, queue
16
+ end
17
+
18
+ end
19
+ end
20
+ end