fake_sensu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.0.1
2
+ * basic support of major sensu api actions
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'sinatra'
4
+ gem 'rest-client'
5
+ gem 'thin'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alan Sebastian
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Fake Sensu
2
+ A sensu api without all the dependencies. For testing.
3
+
4
+ ## Usage
5
+ Include it in your spec helper, and start it
6
+
7
+ ```ruby
8
+ require 'fake_sensu'
9
+
10
+ RSpec.configure do |config|
11
+ config.before :suite do
12
+ FakeSensu.version = '0.9.12' # optional
13
+ FakeSensu.start!
14
+ end
15
+ end
16
+ ```
17
+
18
+ ## Adding a new verision
19
+ If you want to add support for an additional sensu api version, just point the add_version script to a sensu api with default seed data loaded. If successful, the version will become available.
20
+
21
+ ```bash
22
+ ./add_version http://foo:bar@0.0.0.0/4567
23
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ require 'json'
3
+ require 'rest_client'
4
+ require 'fileutils'
5
+
6
+ if ARGV[0]
7
+ # TODO: ensure password/user/port is included in addition to uri
8
+ @sensu_path = ARGV[0]
9
+ else
10
+ abort("USAGE:\n\nadd_version http://foo:bar@localhost:4567\n\n")
11
+ end
12
+
13
+ class FakeSensu
14
+ class GenerateResponses
15
+
16
+ def initialize(args={})
17
+ @sensu_path = args[:sensu_path]
18
+ @version = get_api_version
19
+ @response_file = create_response_file
20
+ generate_index_responses
21
+ generate_show_responses
22
+ write_out
23
+ end
24
+
25
+ def get_api_version
26
+ req = JSON.parse(RestClient.get("#{@sensu_path}/info"))
27
+ api_version = req["sensu"]["version"]
28
+ end
29
+
30
+ def create_response_file
31
+ filename = "#{@version}.json"
32
+ file_path = File.expand_path "./responses/#{filename}"
33
+ unless File.exists? file_path
34
+ FileUtils.touch file_path
35
+ end
36
+ file_path
37
+ end
38
+
39
+ def generate_index_responses
40
+ @index_responses = {"index" => {}}
41
+ %w{info clients checks events stashes}.each do |path|
42
+ begin
43
+ puts "generating index responses for #{path}"
44
+ resp = RestClient.get("#{@sensu_path}/#{path}")
45
+ @index_responses["index"][path] = resp
46
+ rescue Exception => e
47
+ puts e
48
+ next
49
+ end
50
+ end
51
+ end
52
+
53
+ def generate_show_responses
54
+ @show_responses = {"show" => {}}
55
+ paths = %w{
56
+ clients/i-424242
57
+ }
58
+ paths.each do |path|
59
+ begin
60
+ puts "generating show response for #{path}"
61
+ resp = RestClient.get("#{@sensu_path}/#{path}")
62
+ @show_responses["show"][path] = resp
63
+ rescue Exception => e
64
+ next
65
+ end
66
+ end
67
+ end
68
+
69
+ def write_out
70
+ responses = @index_responses.merge(@show_responses)
71
+ File.open(@response_file, 'w') { |f| f.write(JSON.pretty_generate(responses)) }
72
+ end
73
+
74
+ end
75
+ end
76
+ FakeSensu::GenerateResponses.new(:sensu_path => @sensu_path)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fake_sensu/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fake_sensu"
8
+ spec.version = FakeSensu::VERSION
9
+ spec.authors = ["Alan Sebastian"]
10
+ spec.email = ["alan.sebastian@sonian.net"]
11
+ spec.description = %q{Mocking library for sensu}
12
+ spec.summary = %q{Mocking library/server for sensu}
13
+ spec.homepage = "http://www.github.com/asebastian/fake_sensu"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib", "lib/fake_sensu/responses"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ require 'json'
3
+ require 'rest_client'
4
+ require 'fileutils'
5
+
6
+ if ARGV[0]
7
+ # TODO: ensure password/user/port is included in addition to uri
8
+ @sensu_path = ARGV[0]
9
+ else
10
+ abort("USAGE:\n\nadd_version http://foo:bar@localhost:4567\n\n")
11
+ end
12
+
13
+ class FakeSensu
14
+ class GenerateResponses
15
+
16
+ def initialize(args={})
17
+ @sensu_path = args[:sensu_path]
18
+ @version = get_api_version
19
+ @response_file = create_response_file
20
+ generate_index_responses
21
+ generate_show_responses
22
+ write_out
23
+ end
24
+
25
+ def get_api_version
26
+ req = JSON.parse(RestClient.get("#{@sensu_path}/info"))
27
+ api_version = req["sensu"]["version"]
28
+ end
29
+
30
+ def create_response_file
31
+ filename = "#{@version}.json"
32
+ file_path = File.expand_path "./responses/#{filename}"
33
+ unless File.exists? file_path
34
+ FileUtils.touch file_path
35
+ end
36
+ file_path
37
+ end
38
+
39
+ def generate_index_responses
40
+ @index_responses = {"index" => {}}
41
+ %w{info clients checks events stashes}.each do |path|
42
+ begin
43
+ puts "generating index responses for #{path}"
44
+ resp = RestClient.get("#{@sensu_path}/#{path}")
45
+ @index_responses["index"][path] = resp
46
+ rescue Exception => e
47
+ puts e
48
+ next
49
+ end
50
+ end
51
+ end
52
+
53
+ def generate_show_responses
54
+ @show_responses = {"show" => {}}
55
+ paths = %w{
56
+ clients/i-424242
57
+ }
58
+ paths.each do |path|
59
+ begin
60
+ puts "generating show response for #{path}"
61
+ resp = RestClient.get("#{@sensu_path}/#{path}")
62
+ @show_responses["show"][path] = resp
63
+ rescue Exception => e
64
+ next
65
+ end
66
+ end
67
+ end
68
+
69
+ def write_out
70
+ responses = @index_responses.merge(@show_responses)
71
+ File.open(@response_file, 'w') { |f| f.write(JSON.pretty_generate(responses)) }
72
+ end
73
+
74
+ end
75
+ end
76
+ FakeSensu::GenerateResponses.new(:sensu_path => @sensu_path)
@@ -0,0 +1,172 @@
1
+ require 'json'
2
+ require 'sinatra'
3
+
4
+ module FakeSensu
5
+ class Api < Sinatra::Base
6
+
7
+ configure do
8
+ version = "0.9.12"
9
+ response_path = File.dirname(__FILE__) + "/responses/#{version}.json"
10
+ responses = JSON.parse(File.read(response_path))["index"]
11
+ set :info, responses["info"]
12
+ set :clients, responses["clients"]
13
+ set :checks, responses["checks"]
14
+ set :events, responses["events"]
15
+ set :stashes, responses["stashes"]
16
+ set :immutable_info, info.freeze
17
+ set :immutable_clients, clients.freeze
18
+ set :immutable_checks, checks.freeze
19
+ set :immutable_events, events.freeze
20
+ set :immutable_stashes, stashes.freeze
21
+ end
22
+
23
+ get '/info' do
24
+ content_type :json
25
+ body settings.info
26
+ end
27
+
28
+ get '/clients' do
29
+ content_type :json
30
+ settings.clients
31
+ end
32
+
33
+ get %r{/clients/([\w\.-]+)$} do |client_name|
34
+ content_type :json
35
+ clients = JSON.parse(settings.clients)
36
+ clients.each do |client|
37
+ clients.each do |client|
38
+ if client.has_value? client_name
39
+ @body = client.reject! {|k| k == "name"}.to_json
40
+ end
41
+ end
42
+ end
43
+ @body ? "#{@body}" : ""
44
+ end
45
+
46
+ get '/checks' do
47
+ content_type :json
48
+ settings.checks
49
+ end
50
+
51
+ get %r{/checks?/([\w\.-]+)$} do |check_name|
52
+ content_type :json
53
+ checks = JSON.parse(settings.checks)
54
+ checks.each do |check|
55
+ if check.has_value? check_name
56
+ @body = check.to_json
57
+ end
58
+ end
59
+ @body ? "#{@body}" : ""
60
+ end
61
+
62
+ post %r{/(?:check/)?request$} do
63
+ post_body = JSON.parse(request.body.read)
64
+ check_name = post_body["check"]
65
+ check = post_body["check"]
66
+ subscribers = check["subscribers"].to_a
67
+ command = check["command"]
68
+ payload = {
69
+ :name => check_name,
70
+ :command => command,
71
+ :issued => Time.now.to_i
72
+ }
73
+ body payload
74
+ end
75
+
76
+ get '/events' do
77
+ content_type :json
78
+ body settings.events
79
+ end
80
+
81
+ get %r{/events/([\w\.-]+)$} do |client_name|
82
+ content_type :json
83
+ events = JSON.parse(settings.events)
84
+ events.each do |event|
85
+ if event.has_value? client_name
86
+ @body = event.to_json
87
+ end
88
+ end
89
+ @body ? "#{@body}" : ""
90
+ end
91
+
92
+ get %r{/events?/([\w\.-]+)/([\w\.-]+)$} do |client_name, check_name|
93
+ content_type :json
94
+ event_json = settings.events[client_name]
95
+ unless event_json.nil?
96
+ event_json[:client_name] = check
97
+ event_json[:check_name] = check_name
98
+ @body = event_json
99
+ end
100
+ end
101
+
102
+ delete %r{/events?/([\w\.-]+)/([\w\.-]+)$} do |client_name, check_name|
103
+ content_type :json
104
+ events = JSON.parse(settings.events)
105
+ events.each do |event|
106
+ if event["client"] == client_name && event["check"] == check_name
107
+ events.delete_if {|e| e.has_value? check_name}
108
+ # settings.events = events.to_s
109
+ end
110
+ end
111
+ body ''
112
+ end
113
+
114
+ get '/stashes' do
115
+ content_type :json
116
+ body settings.stashes
117
+ end
118
+
119
+ get %r{/stash(?:es)?/(.*)} do |path|
120
+ content_type :json
121
+ stashes = JSON.parse(settings.stashes)
122
+ stashes.each do |stash|
123
+ if stash.has_value? path
124
+ @body = stash["content"]
125
+ end
126
+ end
127
+ @body ? "#{@body}" : ""
128
+ end
129
+
130
+ post %r{/stash(?:es)?/(.*)} do |path|
131
+ content_type :json
132
+ stashes = JSON.parse(settings.stashes)
133
+ stashes = stashes + [{"path" => path, "content" => {"timestamp" => Time.now.to_i}}]
134
+ settings.stashes = stashes.to_json
135
+ body ''
136
+ end
137
+
138
+ post '/stashes' do
139
+ content_type :json
140
+ post_body = JSON.parse(request.body.read)
141
+ path = post_body["path"]
142
+ content = post_body["content"]
143
+ stashes = JSON.parse(settings.stashes)
144
+ stashes = stashes + [{"path" => path, "content" => content}]
145
+ settings.stashes = stashes.to_json
146
+ end
147
+
148
+ delete %r{/stash(?:es)?/(.*)} do |path|
149
+ content_type :json
150
+ stashes = JSON.parse(settings.stashes)
151
+ stashes.each do |stash|
152
+ if stash.has_value? path
153
+ stashes.delete_if {|stash| stash.has_value? path}
154
+ settings.stashes = stashes.to_json
155
+ body = ''
156
+ else
157
+ body = 'not found'
158
+ end
159
+ end
160
+ body
161
+ end
162
+
163
+ get '/reset' do
164
+ settings.info = settings.immutable_info
165
+ settings.clients = settings.immutable_clients
166
+ settings.checks = settings.immutable_checks
167
+ settings.events = settings.immutable_events
168
+ settings.stashes = settings.immutable_stashes
169
+ body ''
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), "api")
2
+ run FakeSensu::Api
@@ -0,0 +1,9 @@
1
+ module FakeSensu
2
+ module FakeSensuMacros
3
+
4
+ def reset_fake_sensu!
5
+ RestClient.get("http://localhost:4567/reset")
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ {
2
+ "index": {
3
+ "info": "{\"sensu\":{\"version\":\"0.9.12.beta.6\"},\"rabbitmq\":{\"keepalives\":{\"messages\":null,\"consumers\":null},\"results\":{\"messages\":null,\"consumers\":null},\"connected\":false},\"redis\":{\"connected\":true}}",
4
+ "clients": "[{\"name\":\"i-424242\",\"address\":\"127.0.0.1\",\"subscriptions\":[\"test\"],\"nested\":{\"attribute\":true},\"timestamp\":1364343737}]",
5
+ "checks": "[{\"command\":\"echo -n OK\",\"subscribers\":[\"test\"],\"interval\":60,\"name\":\"test\"}]",
6
+ "events": "[{\"output\":\"i-424242 true\",\"status\":2,\"issued\":1364343741,\"handlers\":[\"default\"],\"flapping\":false,\"occurrences\":11828,\"client\":\"i-424242\",\"check\":\"tokens\"},{\"output\":\"foobar\",\"status\":1,\"issued\":1364343741,\"handlers\":[\"default\"],\"flapping\":false,\"occurrences\":11828,\"client\":\"i-424242\",\"check\":\"standalone\"}]",
7
+ "stashes": "[]"
8
+ },
9
+ "show": {
10
+ "clients/i-424242": "{\"name\":\"i-424242\",\"address\":\"127.0.0.1\",\"subscriptions\":[\"test\"],\"nested\":{\"attribute\":true},\"timestamp\":1364343737}"
11
+ }
12
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "index": {
3
+ "info": "{\"sensu\":{\"version\":\"0.9.12\"},\"rabbitmq\":{\"keepalives\":{\"messages\":null,\"consumers\":null},\"results\":{\"messages\":null,\"consumers\":null},\"connected\":false},\"redis\":{\"connected\":true}}",
4
+ "clients": "[{\"name\":\"i-424242\",\"address\":\"127.0.0.1\",\"subscriptions\":[\"test\"],\"nested\":{\"attribute\":true},\"timestamp\":1364343737}]",
5
+ "checks": "[{\"command\":\"echo -n OK\",\"subscribers\":[\"test\"],\"interval\":60,\"name\":\"test\"},{\"command\":\"echo -n OK\",\"subscribers\":[\"tokens\"],\"interval\":60,\"name\":\"tokens\"}]",
6
+ "events": "[{\"output\":\"i-424242 true\",\"status\":2,\"issued\":1364343741,\"handlers\":[\"default\"],\"flapping\":false,\"occurrences\":11828,\"client\":\"i-424242\",\"check\":\"tokens\"},{\"output\":\"foobar\",\"status\":1,\"issued\":1364343741,\"handlers\":[\"default\"],\"flapping\":false,\"occurrences\":11828,\"client\":\"i-424242\",\"check\":\"standalone\"}]",
7
+ "stashes": "[{\"path\":\"silence/i-424242/tokens\",\"content\":{\"timestamp\":1364332102}},{\"path\":\"silence/i-424242/standalone\",\"content\":{\"timestamp\":1364332111}}]"
8
+ },
9
+ "show": {
10
+ }
11
+ }
@@ -0,0 +1,28 @@
1
+ module FakeSensu
2
+ class Server
3
+
4
+ def initialize(*args)
5
+ inject_rspec_suite
6
+ start_api
7
+ end
8
+
9
+ def inject_rspec_suite
10
+ RSpec.configure do |config|
11
+ config.after :suite do
12
+ puts "\nstopping fake sensu api @ #{$fake_sensu_pid}!"
13
+ Process.kill 9, $fake_sensu_pid
14
+ end
15
+
16
+ end
17
+ end
18
+
19
+ def start_api
20
+ puts "starting fake sensu api!"
21
+ ru_path = File.join(File.dirname(__FILE__), "config.ru")
22
+ $fake_sensu_pid = Process.spawn("rackup --env production #{ru_path}", :out => "/dev/stdout")
23
+ sleep 4
24
+ end
25
+
26
+ end
27
+ end
28
+
@@ -0,0 +1,3 @@
1
+ module FakeSensu
2
+ VERSION = "0.0.1"
3
+ end
data/lib/fake_sensu.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "fake_sensu/version"
2
+ require "fake_sensu/api"
3
+ require "fake_sensu/macros"
4
+ require "fake_sensu/server"
5
+
6
+ module FakeSensu
7
+
8
+ def self.version(*args)
9
+ args.first || '0.9.12'
10
+ end
11
+
12
+ def self.start!
13
+ FakeSensu::Server.new
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fake_sensu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alan Sebastian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Mocking library for sensu
47
+ email:
48
+ - alan.sebastian@sonian.net
49
+ executables:
50
+ - generate_responses
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - CHANGELOG.md
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - bin/generate_responses
61
+ - fake_sensu.gemspec
62
+ - generate_responses
63
+ - lib/fake_sensu.rb
64
+ - lib/fake_sensu/api.rb
65
+ - lib/fake_sensu/config.ru
66
+ - lib/fake_sensu/macros.rb
67
+ - lib/fake_sensu/responses/0.9.12.beta.6.json
68
+ - lib/fake_sensu/responses/0.9.12.json
69
+ - lib/fake_sensu/server.rb
70
+ - lib/fake_sensu/version.rb
71
+ homepage: http://www.github.com/asebastian/fake_sensu
72
+ licenses:
73
+ - MIT
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ - lib/fake_sensu/responses
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.25
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Mocking library/server for sensu
97
+ test_files: []