observed 0.1.1

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 (119) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +20 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +204 -0
  7. data/Rakefile +4 -0
  8. data/examples/00readme/Gemfile +5 -0
  9. data/examples/00readme/clockwork.rb +16 -0
  10. data/examples/00readme/observed.rb +17 -0
  11. data/examples/observed.rb +10 -0
  12. data/exe/observed-oneshot +11 -0
  13. data/features/oneshot.feature +24 -0
  14. data/features/support/env.rb +8 -0
  15. data/features/test_in_single_ruby_source.feature +32 -0
  16. data/integrations/observed-clockwork/.gitignore +17 -0
  17. data/integrations/observed-clockwork/Gemfile +4 -0
  18. data/integrations/observed-clockwork/LICENSE.txt +22 -0
  19. data/integrations/observed-clockwork/README.md +53 -0
  20. data/integrations/observed-clockwork/Rakefile +1 -0
  21. data/integrations/observed-clockwork/bin/clockwork +16 -0
  22. data/integrations/observed-clockwork/bin/clockworkd +16 -0
  23. data/integrations/observed-clockwork/bin/observed-clockwork +16 -0
  24. data/integrations/observed-clockwork/bin/rake +16 -0
  25. data/integrations/observed-clockwork/examples/clockwork/clockwork.rb +9 -0
  26. data/integrations/observed-clockwork/examples/clockwork/foo_plugin.rb +19 -0
  27. data/integrations/observed-clockwork/examples/clockwork/observed.conf +6 -0
  28. data/integrations/observed-clockwork/features/run_observed_inside_clockwork.feature +59 -0
  29. data/integrations/observed-clockwork/features/support/env.rb +8 -0
  30. data/integrations/observed-clockwork/lib/observed/clockwork/version.rb +5 -0
  31. data/integrations/observed-clockwork/lib/observed/clockwork.rb +20 -0
  32. data/integrations/observed-clockwork/observed-clockwork.gemspec +29 -0
  33. data/lib/observed/application/oneshot.rb +114 -0
  34. data/lib/observed/application.rb +1 -0
  35. data/lib/observed/builtin_plugins/file.rb +49 -0
  36. data/lib/observed/builtin_plugins/stdout.rb +35 -0
  37. data/lib/observed/builtin_plugins.rb +2 -0
  38. data/lib/observed/config.rb +27 -0
  39. data/lib/observed/config_builder.rb +167 -0
  40. data/lib/observed/config_dsl.rb +77 -0
  41. data/lib/observed/configurable.rb +56 -0
  42. data/lib/observed/default/observer.rb +16 -0
  43. data/lib/observed/default/reporter.rb +17 -0
  44. data/lib/observed/default.rb +2 -0
  45. data/lib/observed/hash/builder.rb +24 -0
  46. data/lib/observed/hash/fetcher.rb +19 -0
  47. data/lib/observed/hash/key_path_encoding.rb +62 -0
  48. data/lib/observed/hash.rb +3 -0
  49. data/lib/observed/observer.rb +18 -0
  50. data/lib/observed/observer_helpers/timer.rb +37 -0
  51. data/lib/observed/pluggable.rb +34 -0
  52. data/lib/observed/reader.rb +14 -0
  53. data/lib/observed/reporter/regexp_matching.rb +11 -0
  54. data/lib/observed/reporter.rb +25 -0
  55. data/lib/observed/system.rb +109 -0
  56. data/lib/observed/version.rb +3 -0
  57. data/lib/observed/writer.rb +14 -0
  58. data/lib/observed.rb +77 -0
  59. data/observed.gemspec +30 -0
  60. data/plugins/observed-fluentd/.gitignore +20 -0
  61. data/plugins/observed-fluentd/Gemfile +12 -0
  62. data/plugins/observed-fluentd/LICENSE.txt +22 -0
  63. data/plugins/observed-fluentd/README.md +99 -0
  64. data/plugins/observed-fluentd/Rakefile +1 -0
  65. data/plugins/observed-fluentd/features/plugin.feature +61 -0
  66. data/plugins/observed-fluentd/features/support/env.rb +32 -0
  67. data/plugins/observed-fluentd/fluent.d/fluent.conf +93 -0
  68. data/plugins/observed-fluentd/fluentd.pid +1 -0
  69. data/plugins/observed-fluentd/lib/observed/fluentd/version.rb +5 -0
  70. data/plugins/observed-fluentd/lib/observed/fluentd.rb +30 -0
  71. data/plugins/observed-fluentd/observe.d/clockwork.rb +9 -0
  72. data/plugins/observed-fluentd/observe.d/observed.conf +17 -0
  73. data/plugins/observed-fluentd/observed-fluentd.gemspec +26 -0
  74. data/plugins/observed-gauge/.gitignore +18 -0
  75. data/plugins/observed-gauge/Gemfile +4 -0
  76. data/plugins/observed-gauge/LICENSE.txt +22 -0
  77. data/plugins/observed-gauge/README.md +29 -0
  78. data/plugins/observed-gauge/Rakefile +1 -0
  79. data/plugins/observed-gauge/lib/observed/gauge/version.rb +5 -0
  80. data/plugins/observed-gauge/lib/observed/gauge.rb +117 -0
  81. data/plugins/observed-gauge/observed-gauge.gemspec +30 -0
  82. data/plugins/observed-gauge/spec/gauge_spec.rb +195 -0
  83. data/plugins/observed-gauge/spec/spec_helper.rb +25 -0
  84. data/plugins/observed-http/.gitignore +17 -0
  85. data/plugins/observed-http/Gemfile +4 -0
  86. data/plugins/observed-http/LICENSE.txt +22 -0
  87. data/plugins/observed-http/README.md +37 -0
  88. data/plugins/observed-http/Rakefile +1 -0
  89. data/plugins/observed-http/features/observe_web_services_via_http.feature +32 -0
  90. data/plugins/observed-http/features/support/env.rb +8 -0
  91. data/plugins/observed-http/lib/observed/http/version.rb +5 -0
  92. data/plugins/observed-http/lib/observed/http.rb +60 -0
  93. data/plugins/observed-http/observed-http.gemspec +31 -0
  94. data/plugins/observed-http/spec/fixtures/observed.conf +10 -0
  95. data/plugins/observed-http/spec/http_spec.rb +14 -0
  96. data/plugins/observed-http/spec/spec_helper.rb +25 -0
  97. data/spec/builtin_plugins/file_spec.rb +145 -0
  98. data/spec/builtin_plugins/stdout_spec.rb +56 -0
  99. data/spec/config_builder_spec.rb +146 -0
  100. data/spec/config_dsl_spec.rb +50 -0
  101. data/spec/configurable_spec.rb +65 -0
  102. data/spec/fixtures/configure_by_conf/foo_plugin.rb +12 -0
  103. data/spec/fixtures/configure_by_conf/observed.conf +6 -0
  104. data/spec/fixtures/configure_by_conf_dot_d/foo_plugin.rb +18 -0
  105. data/spec/fixtures/configure_by_conf_dot_d/observed.conf.d/check_foo_1.rb +1 -0
  106. data/spec/fixtures/configure_by_conf_dot_d/observed.conf.d/plugins.rb +1 -0
  107. data/spec/fixtures/configure_by_require/observed_conf.rb +6 -0
  108. data/spec/hash/builder_spec.rb +36 -0
  109. data/spec/hash/fetcher_spec.rb +29 -0
  110. data/spec/input_helpers/timer_spec.rb +54 -0
  111. data/spec/observed_spec.rb +79 -0
  112. data/spec/observer_spec.rb +123 -0
  113. data/spec/oneshot_spec.rb +58 -0
  114. data/spec/reader_spec.rb +15 -0
  115. data/spec/reporter_spec.rb +19 -0
  116. data/spec/spec_helper.rb +65 -0
  117. data/spec/system_spec.rb +75 -0
  118. data/spec/writer_spec.rb +16 -0
  119. metadata +299 -0
@@ -0,0 +1,195 @@
1
+ require 'spec_helper'
2
+
3
+ require 'observed/gauge'
4
+
5
+ shared_examples_for 'the observed-gauge plugin' do
6
+
7
+ subject {
8
+ described_class.new
9
+ }
10
+
11
+ before {
12
+ File.delete(rrd) if File.exist?(rrd)
13
+
14
+ subject.configure rrd: rrd, key_path: key_path
15
+ }
16
+
17
+ it 'matches against the tag using a pattern described in regular expression' do
18
+ expect(subject.match('test.foo')).to be_true
19
+ end
20
+
21
+ it 'reports data with averaged values' do
22
+ subject.prepare_rrd(start: t - 120, rrd: rrd)
23
+ system.expects(:report).with(tag, expected_data.freeze).once
24
+ expect { subject.report('test.foo', t - 120, data) }.to_not raise_error
25
+ expect { subject.report('test.foo', t - 60, data) }.to_not raise_error
26
+ expect { subject.report('test.foo', t, data) }.to_not raise_error
27
+ end
28
+
29
+ it 'creates rrd files automatically on first report' do
30
+ expect { subject.report('test.foo', t, data) }.to_not raise_error
31
+
32
+ expect { File.exist? rrd }.to be_true
33
+ end
34
+ end
35
+
36
+ describe Observed::Plugins::Gauge do
37
+ subject {
38
+ Observed::Plugins::Gauge.new
39
+ }
40
+
41
+ it 'has a name' do
42
+ expect(described_class.plugin_name).to eq('gauge')
43
+ end
44
+
45
+ context 'with configuration' do
46
+
47
+ before {
48
+
49
+ subject.configure(
50
+ system: system,
51
+ tag_pattern: tag_pattern,
52
+ tag: tag,
53
+ step: step,
54
+ period: period
55
+ )
56
+ }
57
+
58
+ after {
59
+ File.delete(rrd) if File.exist?(rrd)
60
+ }
61
+
62
+ let(:t) {
63
+ Time.now
64
+ }
65
+
66
+ let(:system) {
67
+ sys = mock('system')
68
+
69
+ sys.stubs(:now).returns(t)
70
+
71
+ sys
72
+ }
73
+
74
+ let(:tag_pattern) {
75
+ /test.\.*/
76
+ }
77
+
78
+ let(:tag) {
79
+ 'test.out'
80
+ }
81
+
82
+ let(:step) {
83
+ 10
84
+ }
85
+
86
+ let(:period) {
87
+ 60
88
+ }
89
+
90
+ before {
91
+ subject.configure(
92
+ system: system,
93
+ tag_pattern: tag_pattern,
94
+ tag: tag,
95
+ step: step,
96
+ period: period,
97
+ key_path: key_path
98
+ )
99
+ }
100
+
101
+ context 'with an incorrect key path' do
102
+
103
+ let(:key_path) {
104
+ 123
105
+ }
106
+
107
+ let(:rrd) {
108
+ 'gauge_incorrect_key_path.rrd'
109
+ }
110
+
111
+ let(:data) {
112
+ { response: { time: 10 }}
113
+ }
114
+
115
+ let(:t) {
116
+ Time.now
117
+ }
118
+
119
+ it 'raise an error' do
120
+ expect { subject.report('test.foo', t, data) }.to raise_error(/Unexpected type of key_path met/)
121
+ end
122
+ end
123
+
124
+ context 'with a correct key path' do
125
+
126
+ let(:key_path) {
127
+ 'response.time'
128
+ }
129
+
130
+ let(:data_source) {
131
+ 'response_time'
132
+ }
133
+
134
+ context 'with the default coercer' do
135
+
136
+ context 'with data whose keys are symbols' do
137
+ let(:rrd) {
138
+ 'gauge_spec_symbols.rrd'
139
+ }
140
+
141
+ let(:data) {
142
+ { response: { time: 10 }}
143
+ }
144
+
145
+ let(:expected_data) {
146
+ { response: { time: 10 }}
147
+ }
148
+
149
+ it_behaves_like 'the observed-gauge plugin'
150
+ end
151
+
152
+ context 'with data whose keys are strings' do
153
+ let(:rrd) {
154
+ 'gauge_spec_strings.rrd'
155
+ }
156
+
157
+ let(:data) {
158
+ {'response' => {'time' => 10}}
159
+ }
160
+
161
+ let(:expected_data) {
162
+ { 'response' => { 'time' => 10 }}
163
+ }
164
+
165
+ it_behaves_like 'the observed-gauge plugin'
166
+ end
167
+
168
+ end
169
+
170
+ context 'with a string-to-integer coercer for values' do
171
+
172
+ before {
173
+ subject.configure coercer: ->(v){ t.to_i }
174
+ }
175
+
176
+ let(:rrd) {
177
+ 'gauge_spec_coercer.rrd'
178
+ }
179
+
180
+ let(:data) {
181
+ {'response' => {'time' => '10'}}
182
+ }
183
+
184
+ let(:expected_data) {
185
+ { 'response' => { 'time' => 10 }}
186
+ }
187
+
188
+ it_behaves_like 'the observed-gauge plugin'
189
+ end
190
+
191
+ end
192
+
193
+ end
194
+
195
+ end
@@ -0,0 +1,25 @@
1
+ require 'fakefs/spec_helpers'
2
+ require 'rspec'
3
+
4
+ Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].each { |f| require f }
5
+
6
+ puts "Please do not update/create files while tests are running."
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ config.order = :random
11
+ config.filter_run :focus => true
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+
15
+ config.before(:each) do
16
+ @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
17
+ end
18
+
19
+ config.mock_framework = :mocha
20
+ end
21
+
22
+ if RUBY_VERSION =~ /^1.9/
23
+ require 'simplecov'
24
+ SimpleCov.start
25
+ end
@@ -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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in observed-http.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 GREE, Inc.
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.
@@ -0,0 +1,37 @@
1
+ # Observed::Http
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'observed-http'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install observed-http
18
+
19
+ ## Test
20
+
21
+ $ gem build observed.gemspec
22
+ $ gem install observed
23
+ $ cd plugins/observed-http
24
+ $ bundle install
25
+ $ bundle exec rspec
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ Feature: Observe Web services via HTTP
2
+
3
+ In order to observe Web services using Observed,
4
+ I want to the observed-http plugin to send HTTP request and report the result via reporters
5
+
6
+ Scenario: Create a .rb file containing Observed code and run it with the ruby command
7
+ Given a file named "test.rb" with:
8
+ """
9
+ require 'observed'
10
+ require 'observed/http'
11
+
12
+ include Observed
13
+
14
+ observe 'foo_1', via: 'http', with: {
15
+ method: 'get',
16
+ url: 'http://google.com',
17
+ timeout_in_milliseconds: 1000
18
+ }
19
+
20
+ report /foo_\d+/, via: 'stdout'
21
+
22
+ run 'foo_1'
23
+ """
24
+ When I run `ruby test.rb`
25
+ Then the output should contain:
26
+ """
27
+ foo_1.success
28
+ """
29
+ Then the output should contain:
30
+ """
31
+ Get http://google.com
32
+ """
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'aruba/cucumber'
3
+
4
+ World(Aruba::Api)
5
+
6
+ Before do
7
+ @aruba_timeout_seconds = 20
8
+ end
@@ -0,0 +1,5 @@
1
+ module Observed
2
+ module Http
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,60 @@
1
+ require 'observed/http/version'
2
+ require 'observed/observer_helpers/timer'
3
+ require 'timeout'
4
+ require 'net/http'
5
+
6
+ module Observed
7
+ module Plugins
8
+ class HTTP < Observed::Observer
9
+
10
+ include Observed::ObserverHelpers::Timer
11
+
12
+ attribute :timeout_in_milliseconds, default: 5000
13
+
14
+ attribute :method
15
+ attribute :url
16
+
17
+ def observe
18
+ logger.debug "method: #{method}, url: #{url}"
19
+
20
+ uri = URI.parse(url)
21
+
22
+ logger.debug "uri: #{uri}, uri.host: #{uri.host}, uri.port:#{uri.port}, uri.path: #{uri.path}"
23
+
24
+ http_method = method.capitalize
25
+ path = if uri.path.size == 0
26
+ '/'
27
+ else
28
+ uri.path
29
+ end
30
+ req = Net::HTTP::const_get(http_method.intern).new(path)
31
+
32
+ timeout_in_seconds = timeout_in_milliseconds / 1000.0
33
+ if timeout_in_seconds.nan?
34
+ fail "Invalid configuration on timeout: `timeout` must be a number but it was not(=#{timeout_in_seconds})"
35
+ end
36
+
37
+ time_and_report(tag: self.tag, timeout_in_seconds: timeout_in_seconds) do
38
+
39
+ logger.debug "Sending a HTTP request with the timeout of #{timeout_in_seconds} seconds"
40
+
41
+ body = Net::HTTP.start(uri.host, uri.port) {|http|
42
+ http.request(req)
43
+ }.body
44
+
45
+ logger.debug "Response body: #{body}"
46
+
47
+ "#{http_method} #{uri}"
48
+ end
49
+
50
+ end
51
+
52
+ def logger
53
+ @logger ||= Logger.new(STDOUT)
54
+ end
55
+
56
+ plugin_name 'http'
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'observed/http/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "observed-http"
8
+ spec.version = Observed::Http::VERSION
9
+ spec.authors = ["KUOKA Yusuke"]
10
+ spec.email = ["yusuke.kuoka@gree.net"]
11
+ spec.description = %q{observed-http}
12
+ spec.summary = %q{observed-http is a plugin for Observed to run health-check against Web services talking HTTP.}
13
+ spec.homepage = ""
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"]
20
+
21
+ spec.add_dependency "observed", "~> 0.1.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "mocha"
27
+ spec.add_development_dependency "fakefs"
28
+ spec.add_development_dependency "simplecov"
29
+ spec.add_development_dependency "cucumber"
30
+ spec.add_development_dependency "aruba"
31
+ end
@@ -0,0 +1,10 @@
1
+ require 'observed/builtin_plugins'
2
+ require 'observed/http'
3
+
4
+ observe 'foo_1', via: 'http', with: {
5
+ method: 'get',
6
+ url: 'http://google.com',
7
+ timeout_in_milliseconds: 1000
8
+ }
9
+
10
+ report /foo_\d+/, via: 'stdout'
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ require 'observed/application/oneshot'
4
+
5
+ describe Observed::Application::Oneshot do
6
+ subject {
7
+ Observed::Application::Oneshot.create(
8
+ config_file: 'spec/fixtures/observed.conf'
9
+ )
10
+ }
11
+ it 'initializes' do
12
+ expect(subject.run.size).not_to eq(0)
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ require 'fakefs/spec_helpers'
2
+ require 'rspec'
3
+
4
+ Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].each { |f| require f }
5
+
6
+ puts "Please do not update/create files while tests are running."
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ config.order = :random
11
+ config.filter_run :focus => true
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+
15
+ config.before(:each) do
16
+ @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
17
+ end
18
+
19
+ config.mock_framework = :mocha
20
+ end
21
+
22
+ if RUBY_VERSION =~ /^1.9/
23
+ require 'simplecov'
24
+ SimpleCov.start
25
+ end
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+ require 'observed/builtin_plugins/file'
4
+
5
+ describe Observed::BuiltinPlugins::File do
6
+
7
+ include FakeFS::SpecHelpers
8
+
9
+ subject {
10
+ reporter = Observed::BuiltinPlugins::File.new
11
+ reporter.configure config
12
+ reporter
13
+ }
14
+
15
+ let(:config) {
16
+ { tag_pattern: /foo\..+/, format: formatter, path: path }
17
+ }
18
+
19
+ let(:path) {
20
+ 'test.txt'
21
+ }
22
+
23
+ before(:each) {
24
+ File.open(path, 'w') do |f|
25
+ f.write("default content\n")
26
+ end
27
+ }
28
+
29
+ shared_examples_for 'the plugin in the appending mode' do
30
+
31
+ context 'with a specific formatter' do
32
+
33
+ let(:formatter) {
34
+ -> tag, time, data, fetcher { "foo #{time.to_i} #{data[:foo]} #{fetcher['bar.baz']}" }
35
+ }
36
+
37
+ it 'reports the formatted data to the file' do
38
+ time = Time.now
39
+
40
+ expect { subject.report('foo', time, {foo: 1, bar: {baz: 2}}) }.to_not raise_error
41
+
42
+ expect(File.read(path)).to eq("default content\nfoo #{time.to_i} 1 2\n")
43
+ end
44
+
45
+ end
46
+
47
+ context 'without a specific formatter' do
48
+
49
+ let(:formatter) {
50
+ nil
51
+ }
52
+
53
+ it 'reports the data formatted by the default formatter to the stdout' do
54
+ time = Time.now
55
+
56
+ expect { subject.report('foo', time, {foo: 1}) }.to_not raise_error
57
+
58
+ expect(File.read(path)).to eq("default content\n#{time.to_s} foo {:foo=>1}\n")
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+ shared_examples_for 'the plugin in the overwriting mode' do
65
+
66
+ context 'with a specific formatter' do
67
+
68
+ let(:formatter) {
69
+ -> tag, time, data, fetcher { "foo #{time.to_i} #{data[:foo]} #{fetcher['bar.baz']}" }
70
+ }
71
+
72
+ it 'reports the formatted data to the file' do
73
+ time = Time.now
74
+
75
+ expect { subject.report('foo', time, {foo: 1, bar: {baz: 2}}) }.to_not raise_error
76
+
77
+ expect(File.read(path)).to eq("foo #{time.to_i} 1 2\n")
78
+ end
79
+
80
+ end
81
+
82
+ context 'without a specific formatter' do
83
+
84
+ let(:formatter) {
85
+ nil
86
+ }
87
+
88
+ it 'reports the data formatted by the default formatter to the stdout' do
89
+ time = Time.now
90
+
91
+ expect { subject.report('foo', time, {foo: 1}) }.to_not raise_error
92
+
93
+ expect(File.read(path)).to eq("#{time.to_s} foo {:foo=>1}\n")
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+
100
+ context 'with the default mode' do
101
+
102
+ it_behaves_like 'the plugin in the appending mode'
103
+
104
+ context 'with the formatter whose number of parameters is not 3 nor 4' do
105
+
106
+ let(:formatter) {
107
+ -> a, b {}
108
+ }
109
+
110
+ it 'fails while reporting' do
111
+ pattern = /Number of parameters for the function for the key :format must be 3 or 4, but was 2/
112
+ expect { subject.report('foo', Time.now, {}) }.to raise_error(pattern)
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ context 'with the mode :append' do
119
+ before {
120
+ subject.configure mode: :append
121
+ }
122
+ it_behaves_like 'the plugin in the appending mode'
123
+ end
124
+
125
+ context 'with the mode \'a\'' do
126
+ before {
127
+ subject.configure mode: 'a'
128
+ }
129
+ it_behaves_like 'the plugin in the appending mode'
130
+ end
131
+
132
+ context 'with the mode :overwrite' do
133
+ before {
134
+ subject.configure mode: :overwrite
135
+ }
136
+ it_behaves_like 'the plugin in the overwriting mode'
137
+ end
138
+
139
+ context 'with the mode \'w\'' do
140
+ before {
141
+ subject.configure mode: 'w'
142
+ }
143
+ it_behaves_like 'the plugin in the overwriting mode'
144
+ end
145
+ end