log2mail 0.0.1.pre2

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.
data/spec/factories.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'tempfile'
2
+
3
+ FactoryGirl.define do
4
+ factory :config, class: Log2mail::Config do
5
+
6
+ trait :valid do
7
+ initialize_with do
8
+ tmp = Tempfile.new('valid_config')
9
+ tmp.write <<-CONFIG
10
+ # sample config file for log2mail
11
+ # comments start with '#'
12
+ # see source code doc/Configuration for additional information
13
+
14
+ defaults
15
+ sendtime = 20
16
+ resendtime = 50
17
+ maxlines = 7
18
+ template = /tmp/mail_template
19
+ fromaddr = log2mail
20
+ sendmail = /usr/sbin/sendmail -oi -t
21
+ mailto = global_default_recipient@example.org # new in log2mail.rb
22
+ file = test.log
23
+ pattern = /any/
24
+ pattern = string match
25
+ mailto = special@recipient
26
+ maxlines = 99
27
+ CONFIG
28
+ tmp.close
29
+ new(tmp.path)
30
+ end
31
+ end
32
+
33
+ trait :valid_without_defaults do
34
+ initialize_with do
35
+ tmp = Tempfile.new('valid_config')
36
+ tmp.write <<-CONFIG
37
+ file = test.log
38
+ pattern = /any/
39
+ pattern = string match
40
+ mailto = special@recipient
41
+ CONFIG
42
+ tmp.close
43
+ new(tmp.path)
44
+ end
45
+ end
46
+
47
+ factory :valid_config, traits: [:valid]
48
+ factory :valid_config_without_defaults, traits: [:valid_without_defaults]
49
+ end
50
+
51
+ factory :hit, class: Log2mail::Hit do
52
+ matched_text "a line with string match in it\n"
53
+ pattern 'string match'
54
+ file 'test.log'
55
+ initialize_with{ new(matched_text, pattern, file) }
56
+ end
57
+
58
+ factory :report, class: Log2mail::Report do
59
+ recipients 'recipient@example.org'
60
+ hit { build(:hit) }
61
+
62
+ template do
63
+ tmp = Tempfile.new('valid_config')
64
+ tmp.write <<-TEMPLATE
65
+ From: %f
66
+ To: %t
67
+ Subject: matched your pattern: %m
68
+
69
+ Hello!
70
+
71
+ We have matched your pattern "%m" in "%F" %n times:
72
+
73
+ %l
74
+
75
+ Yours,
76
+ log2mail.
77
+ TEMPLATE
78
+ tmp.close
79
+ tmp.path
80
+ end
81
+
82
+ end
83
+
84
+ factory :file, class: Log2mail::File do
85
+ initialize_with do
86
+ tmp = Tempfile.new('file')
87
+ tmp.write <<-FILE
88
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
89
+ eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
90
+ ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
91
+ aliquip ex ea commodo consequat. Duis aute irure dolor in
92
+ reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
93
+ pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
94
+ culpa qui officia deserunt mollit anim id est laborum.
95
+ FILE
96
+ tmp.close
97
+ new( tmp.path, ['ut'] )
98
+ end
99
+ end
100
+
101
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ module Log2mail
5
+
6
+ describe Config do
7
+
8
+ subject { build(:valid_config) }
9
+
10
+ describe '#config' do
11
+ it 'logs a message' do
12
+ expect($logger).to receive(:debug).with(/Reading configuration from/)
13
+ subject
14
+ end
15
+
16
+ context 'with defaults' do
17
+ it 'should return a valid config' do
18
+ expect($logger).not_to receive(:warn)
19
+ expect(subject.files).to eql(["test.log"])
20
+ expect(subject.defaults).to eql({
21
+ :sendtime => 20,
22
+ :resendtime => 50,
23
+ :maxlines => 7,
24
+ :template => "/tmp/mail_template",
25
+ :fromaddr => "log2mail",
26
+ :sendmail => "/usr/sbin/sendmail -oi -t",
27
+ :mailtos => ["global_default_recipient@example.org"],
28
+ })
29
+ expect(subject.patterns_for_file('test.log')).to eql(["/any/", "string match"])
30
+ expect(subject.mailtos_for_pattern('test.log', 'string match')).to eql(["special@recipient"])
31
+ expect(subject.mailtos_for_pattern('test.log', '/any/')).to eql(["global_default_recipient@example.org"])
32
+ end
33
+ end
34
+
35
+ context 'without defaults' do
36
+ subject { build(:valid_config_without_defaults) }
37
+ it 'should return a valid config' do
38
+ expect(subject.files).to eql(["test.log"])
39
+ expect(subject.defaults).to eql({})
40
+ expect(subject.patterns_for_file('test.log')).to eql(["/any/", "string match"])
41
+ expect(subject.mailtos_for_pattern('test.log', 'string match')).to eql(["special@recipient"])
42
+ expect(subject.mailtos_for_pattern('test.log', '/any/')).to eql([])
43
+ end
44
+ it 'should log warning when no recipients' do
45
+ expect($logger).to receive(:warn).with(/Pattern.*has no recipients/)
46
+ subject
47
+ end
48
+ end
49
+
50
+ # it { is_expected.to eql build(:config) }
51
+ end
52
+
53
+ describe '#files' do
54
+ it 'should return a list of files to watch' do
55
+ expect(subject.files).to be_kind_of(Array)
56
+ expect(subject.files).to eql(['test.log'])
57
+ end
58
+ end
59
+
60
+ describe '#patterns_for_file' do
61
+ it 'should return a list of patterns for file' do
62
+ expect(subject.patterns_for_file('test.log')).to be_kind_of(Array)
63
+ end
64
+ end
65
+
66
+ describe '#mailtos_for_pattern' do
67
+ it 'should return a list of recipients for file' do
68
+ expect(subject.mailtos_for_pattern('test.log', /any/)).to be_kind_of(Array)
69
+ end
70
+ it 'should return the global default recipient (if no sepcial recipient)' do
71
+ expect(subject.mailtos_for_pattern('test.log', /any/)).to eql(["global_default_recipient@example.org"])
72
+ end
73
+ it 'should return the special recipient' do
74
+ expect(subject.mailtos_for_pattern('test.log', 'string match')).to eql(["special@recipient"])
75
+ end
76
+ end
77
+
78
+ describe '#settings_for_mailto' do
79
+ it 'should return defaults, except for setting present' do
80
+ expect(subject.settings_for_mailto('test.log', 'string match', 'special@recipient')).to eql({
81
+ :sendtime => 20,
82
+ :resendtime => 50,
83
+ :maxlines => 99,
84
+ :template => "/tmp/mail_template",
85
+ :fromaddr => "log2mail",
86
+ :sendmail => "/usr/sbin/sendmail -oi -t",
87
+ })
88
+ end
89
+ it 'should return defaults, if no setting is present' do
90
+ expect(subject.settings_for_mailto('test.log', 'string match', 'global_default_recipient@example.org')).to eql({
91
+ :sendtime => 20,
92
+ :resendtime => 50,
93
+ :maxlines => 7,
94
+ :template => "/tmp/mail_template",
95
+ :fromaddr => "log2mail",
96
+ :sendmail => "/usr/sbin/sendmail -oi -t",
97
+ })
98
+ end
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ module Log2mail
4
+
5
+ describe File::Parser do
6
+
7
+ subject{ build(:file) }
8
+
9
+ describe '#parse' do
10
+
11
+ context 'string pattern' do
12
+ it 'returns the hits' do
13
+ expect($logger).to receive(:log).with(0, /pattern match/)
14
+ hits = subject.parse( IO.read(subject.path) )
15
+ expect(hits.count).to be(3)
16
+ expect(hits[0].matched_text).to eql('eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim')
17
+ end
18
+ it 'returns no hits' do
19
+ expect($logger).not_to receive(:log).with(0, /pattern match/)
20
+ hits = subject.parse( 'no hits here' )
21
+ expect(hits.count).to be(0)
22
+ expect(hits).to eql([])
23
+ end
24
+ end
25
+
26
+ context 'regexp pattern' do
27
+ it 'returns the hits' do
28
+ expect($logger).to receive(:log).with(0, /pattern match/)
29
+ subject.patterns = ['/ut/']
30
+ hits = subject.parse( IO.read(subject.path) )
31
+ expect(hits.count).to be(3)
32
+ expect(hits.map(&:matched_text)).to all( eql('ut') )
33
+ end
34
+ it 'returns no hits' do
35
+ expect($logger).not_to receive(:log).with(0, /pattern match/)
36
+ hits = subject.parse( 'no hits here' )
37
+ expect(hits.count).to be(0)
38
+ expect(hits).to eql([])
39
+ end
40
+ end
41
+
42
+ context 'mixed patterns' do
43
+ it 'returns the hits' do
44
+ expect($logger).to receive(:log).with(0, /pattern match/)
45
+ subject.patterns = ['/ut/i', 'dolor']
46
+ hits = subject.parse( IO.read(subject.path) )
47
+ expect(hits.count).to be(8)
48
+ expect(hits.map(&:matched_text)).to eql([
49
+ "ut", "Ut", "ut", "ut",
50
+ "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do",
51
+ "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim",
52
+ "aliquip ex ea commodo consequat. Duis aute irure dolor in",
53
+ "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla"
54
+ ])
55
+ end
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ module Log2mail
4
+
5
+ describe ReportFactory do
6
+
7
+ subject { ReportFactory.new( build(:valid_config) ) }
8
+ before do
9
+
10
+ end
11
+
12
+ describe '.reports_from_hit' do
13
+ subject { ReportFactory.new( build(:valid_config) ).reports_from_hit( build(:hit) ) }
14
+ it { is_expected.to be_kind_of(Array) }
15
+ it { is_expected.to all( be_kind_of(Log2mail::Report) ) }
16
+ end
17
+
18
+ end
19
+
20
+ describe Report do
21
+ describe '#body_from_template' do
22
+ subject { build(:report).send(:body_from_template) }
23
+ it 'should parse the template' do
24
+ expect(subject).to eql(<<-EOS)
25
+ From: log2mail
26
+ To: recipient@example.org
27
+ Subject: matched your pattern: string match
28
+
29
+ Hello!
30
+
31
+ We have matched your pattern "string match" in "test.log" %n times:
32
+
33
+ a line with string match in it
34
+
35
+ Yours,
36
+ log2mail.
37
+ EOS
38
+ end
39
+ end
40
+ describe '#deliver' do
41
+ include Mail::Matchers
42
+ subject { build(:report).deliver }
43
+ it { should have_sent_email }
44
+ # it 'should send the message' do
45
+ # expect(subject.deliver).to change{Mail::TestMailer.deliveries.length}.by(1)
46
+ # end
47
+ end
48
+ describe '#sendmail_command=' do
49
+ let(:report){ build(:report) }
50
+ it 'parses the sendmail command string with parameters' do
51
+ report.sendmail_command = "/usr/local/sbin/sendmail -oi -t"
52
+ expect(report.sendmail_location).to eql('/usr/local/sbin/sendmail')
53
+ expect(report.sendmail_arguments).to eql('-oi -t')
54
+ end
55
+ it 'parses the sendmail command string without' do
56
+ report.sendmail_command = "/usr/local/sbin/sendmail"
57
+ expect(report.sendmail_location).to eql('/usr/local/sbin/sendmail')
58
+ expect(report.sendmail_arguments).to be(nil)
59
+ end
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ module Log2mail
4
+
5
+ describe Watcher do
6
+ subject { Log2mail::Watcher.new( build(:valid_config ), 99 ) }
7
+
8
+ describe '#new' do
9
+ it 'should fail on invalid configuration' do
10
+ expect{ Log2mail::Watcher.new('invalid config', 99) }.to raise_error(Log2mail::Error, /Invalid configuration/)
11
+ end
12
+ it 'should init @sleeptime' do
13
+ expect(subject.instance_eval('@sleeptime')).to be(99)
14
+ end
15
+ it 'should init @factory' do
16
+ expect(subject.instance_eval('@factory')).to be_instance_of(Log2mail::ReportFactory)
17
+ end
18
+ it 'should init @files' do
19
+ files = subject.instance_eval('@files')
20
+ expect(files).to all( be_instance_of(Log2mail::File))
21
+ expect(files.count).to be(1)
22
+ end
23
+ end
24
+
25
+ describe '#run' do
26
+ let(:files) { subject.instance_eval('@files') }
27
+ before do
28
+ subject.instance_eval('@sleeptime=0')
29
+ expect(subject).to receive(:running?).and_return(true, false)
30
+ end
31
+ it 'should open and seek all files in the beginning' do
32
+ expect(subject).to receive(:open_and_seek_files)
33
+ subject.run
34
+ end
35
+ it 'should should check eof? for each file' do
36
+ files.each { |file| expect(file).to receive(:eof?).and_return(false) }
37
+ subject.run
38
+ end
39
+ it 'should report any hits' do
40
+ files.each do |file|
41
+ expect(file).to receive(:eof?).and_return(false)
42
+ hits = [build(:hit)]
43
+ expect(file).to receive(:parse).and_return(hits)
44
+ expect(subject).to receive(:report).with(hits)
45
+ end
46
+ subject.run
47
+ end
48
+
49
+ context 'rotated file' do
50
+ before do
51
+ allow(subject).to receive(:open_and_seek_files)
52
+ files.each do |file|
53
+ expect(file).to receive(:eof?).and_return(true)
54
+ expect(file).to receive(:rotated?).and_return(true)
55
+ end
56
+ end
57
+ it 'should reopen file if rotated' do
58
+ files.each do |file|
59
+ expect(file).to receive(:open).once
60
+ end
61
+ subject.run
62
+ end
63
+ it 'should report any hits' do
64
+ files.each do |file|
65
+ hits = [build(:hit)]
66
+ expect(file).to receive(:parse).and_return(hits)
67
+ expect(subject).to receive(:report).with(hits)
68
+ end
69
+ subject.run
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ # privates
76
+
77
+ describe '#open_and_seek_files'
78
+
79
+ describe '#log' do
80
+ it 'should not fail' do
81
+ expect{ subject.send(:log, 'a message') }.not_to raise_error
82
+ end
83
+ end
84
+
85
+ describe '#report' do
86
+ it 'should not fail' do
87
+ expect{ subject.send(:report, [build(:hit)]) }.not_to raise_error
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,112 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # RSpec
42
+ # spec/support/factory_girl.rb
43
+ require 'factory_girl'
44
+ config.include FactoryGirl::Syntax::Methods
45
+
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # These two settings work together to allow you to limit a spec run
51
+ # to individual examples or groups you care about by tagging them with
52
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
53
+ # get run.
54
+ config.filter_run :focus
55
+ config.run_all_when_everything_filtered = true
56
+
57
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
58
+ # For more details, see:
59
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
60
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
61
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
62
+ config.disable_monkey_patching!
63
+
64
+ # This setting enables warnings. It's recommended, but in some cases may
65
+ # be too noisy due to issues in dependencies.
66
+ config.warnings = true
67
+
68
+ # Many RSpec users commonly either run the entire suite or an individual
69
+ # file, and it's useful to allow more verbose output when running an
70
+ # individual spec file.
71
+ if config.files_to_run.one?
72
+ # Use the documentation formatter for detailed output,
73
+ # unless a formatter has already been configured
74
+ # (e.g. via a command-line flag).
75
+ config.default_formatter = 'doc'
76
+ end
77
+
78
+ # Print the 10 slowest examples and example groups at the
79
+ # end of the spec run, to help surface which specs are running
80
+ # particularly slow.
81
+ config.profile_examples = 10
82
+
83
+ # Run specs in random order to surface order dependencies. If you find an
84
+ # order dependency and want to debug it, you can fix the order by providing
85
+ # the seed, which is printed after each run.
86
+ # --seed 1234
87
+ config.order = :random
88
+
89
+ # Seed global randomization in this process using the `--seed` CLI option.
90
+ # Setting this allows you to use `--seed` to deterministically reproduce
91
+ # test failures related to randomization by passing the same `--seed` value
92
+ # as the one that triggered the failure.
93
+ Kernel.srand config.seed
94
+ =end
95
+
96
+ config.before do
97
+ # Log2mail.send(:remove_const, 'Logger') if defined?(Log2mail::Logger)
98
+ # Log2mail::Logger = spy('Logger')
99
+ # stub_const('Log2mail::Logger', spy('Logger') )
100
+ $logger = spy('Logger')
101
+ Mail::TestMailer.deliveries.clear
102
+ end
103
+
104
+
105
+ end
106
+
107
+ require 'log2mail'
108
+ require 'factories'
109
+
110
+ Mail.defaults do
111
+ delivery_method :test
112
+ end