logstash-output-monasca_log_api 0.3.3 → 0.4

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.
@@ -1,16 +1,16 @@
1
- =begin
2
- Copyright 2015 FUJITSU LIMITED
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
- in compliance with the License. You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software distributed under the License
10
- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
- or implied. See the License for the specific language governing permissions and limitations under
12
- the License.
13
- =end
1
+ # Copyright 2016 FUJITSU LIMITED
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4
+ # use this file except in compliance with the License. You may obtain a copy of
5
+ # the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+ # License for the specific language governing permissions and limitations under
13
+ # the License.
14
14
 
15
15
  # encoding: utf-8
16
16
 
@@ -18,21 +18,26 @@ require_relative '../spec_helper'
18
18
 
19
19
  describe LogStash::Outputs::Keystone::Token do
20
20
 
21
- describe "#new" do
22
- it "takes two parameters and returns a Token object" do
23
- token = LogStash::Outputs::Keystone::Token.new('token-id', DateTime.now)
24
- expect(token).to be_a(LogStash::Outputs::Keystone::Token)
25
- end
26
- end
21
+ describe "when initializing" do
22
+ it "should be a singleton" do
23
+ expect{LogStash::Outputs::Keystone::Token.new}
24
+ .to raise_exception(NoMethodError)
25
+ end
27
26
 
28
- describe "#==" do
29
- it "should equal to another token instance" do
30
- token = LogStash::Outputs::Keystone::Token.new('token-id', DateTime.parse("2015-05-26T08:55:36.774122Z"))
31
- second_token = LogStash::Outputs::Keystone::Token.new('token-id', DateTime.parse("2015-05-26T08:55:36.774122Z"))
32
- third_token = LogStash::Outputs::Keystone::Token.new('token-id', DateTime.parse("2015-05-30T08:55:36.774122Z"))
27
+ it "should always return the same object" do
28
+ token = LogStash::Outputs::Keystone::Token.instance
29
+ another_token = token = LogStash::Outputs::Keystone::Token.instance
30
+ expect(token).to eq(another_token)
31
+ end
32
+ end
33
33
 
34
- expect(token).to eq(second_token)
35
- expect(third_token).to_not eq(second_token)
34
+ describe "when changing properties" do
35
+ it "should change for each instance" do
36
+ token = LogStash::Outputs::Keystone::Token.instance
37
+ another_token = token = LogStash::Outputs::Keystone::Token.instance
38
+ another_token.set_token(1, 2)
39
+ expect(token).to eq(another_token)
36
40
  end
37
41
  end
42
+
38
43
  end
@@ -0,0 +1,80 @@
1
+ # Copyright 2016 FUJITSU LIMITED
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4
+ # use this file except in compliance with the License. You may obtain a copy of
5
+ # the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+ # License for the specific language governing permissions and limitations under
13
+ # the License.
14
+
15
+ # encoding: utf-8
16
+
17
+ require_relative '../spec_helper'
18
+
19
+ describe LogStash::Outputs::Monasca::MonascaLogApiClient do
20
+
21
+ let (:version) { '3.0' }
22
+ let (:valid_date) { DateTime.now + Rational(1, 1440) }
23
+ let (:token) { LogStash::Outputs::Keystone::Token
24
+ .instance.set_token('abc', valid_date) }
25
+ let (:logs) { }
26
+ let (:header) { header = { 'X-Auth-Token' => token,
27
+ 'Content-Type' => 'application/json' } }
28
+
29
+ before(:each) do
30
+ stub_const(
31
+ "LogStash::Outputs::Monasca::MonascaLogApiClient::SUPPORTED_API_VERSION",
32
+ [version])
33
+ end
34
+
35
+ context 'when initializing' do
36
+ it 'then it should register without exceptions' do
37
+ expect {LogStash::Outputs::Monasca::MonascaLogApiClient
38
+ .new('hostname:8080', 'v3.0')}.to_not raise_error
39
+ end
40
+ end
41
+
42
+ context 'when requesting to monasca-log-api' do
43
+ it 'sends x-auth-token and content-type in header, logs in body' do
44
+ client = LogStash::Outputs::Monasca::MonascaLogApiClient
45
+ .new('hostname:8080', 'v3.0')
46
+ expect_any_instance_of(RestClient::Resource).to receive(:post)
47
+ .with(logs.to_json, header)
48
+ client.send_logs(logs, token)
49
+ end
50
+ end
51
+
52
+ context 'when request failed' do
53
+ it 'rescued the exception and logs a failure' do
54
+ client = LogStash::Outputs::Monasca::MonascaLogApiClient
55
+ .new('hostname:8080', 'v3.0')
56
+ expect_any_instance_of(Cabin::Channel).to receive(:warn)
57
+ expect_any_instance_of(RestClient::Resource).to receive(:post)
58
+ .and_raise(Errno::ECONNREFUSED)
59
+ client.send_logs(logs, token)
60
+ end
61
+ end
62
+
63
+ context 'api version checking' do
64
+ it 'should pass for correct version' do
65
+ expect {LogStash::Outputs::Monasca::MonascaLogApiClient
66
+ .new('hostname:8080', 'v3.0')}.to_not raise_error
67
+ end
68
+
69
+ it 'should pass if version does not specify v' do
70
+ expect {LogStash::Outputs::Monasca::MonascaLogApiClient
71
+ .new('hostname:8080', '3.0')}.to_not raise_error
72
+ end
73
+
74
+ it 'should fail for unsupported version' do
75
+ expect {LogStash::Outputs::Monasca::MonascaLogApiClient
76
+ .new('hostname:8080', 'v4.0')}.to raise_exception
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,433 @@
1
+ # Copyright 2016 FUJITSU LIMITED
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4
+ # use this file except in compliance with the License. You may obtain a copy of
5
+ # the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+ # License for the specific language governing permissions and limitations under
13
+ # the License.
14
+
15
+ # encoding: utf-8
16
+
17
+ require_relative 'spec_helper'
18
+
19
+ describe 'outputs/monasca_log_api' do
20
+
21
+ let (:event) { LogStash::Event.new(
22
+ {
23
+ 'message' => '2015-08-13 08:36:59,316 INFO monasca_notification '\
24
+ 'graceful shutdown.',
25
+ '@version' => '1',
26
+ '@timestamp' => '2015-08-13T08:37:00.287Z',
27
+ 'path' => '/opt/logstash-2.2.0/test.log',
28
+ 'host' => 'kamil-choroba',
29
+ 'type' => 'test-type',
30
+ 'tags' => ['test-service', 'high']
31
+ })
32
+ }
33
+
34
+ let (:long_event) { LogStash::Event.new(
35
+ {
36
+ 'message' => 'A veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'\
37
+ 'eeery loooooooooooooooooooooooooooooooooooooooooong messsssssssssssssss'\
38
+ 'sssssssssssssssssage ...... A veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'\
39
+ 'eeeeeeeeeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooong mess'\
40
+ 'ssssssssssssssssssssssssssssssssage ...... A veeeeeeeeeeeeeeeeeeeeeeeee'\
41
+ 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery loooooooooooooooooooooooooooooooooo'\
42
+ 'oooooooong messssssssssssssssssssssssssssssssssage ..... A veeeeeeeeeee'\
43
+ 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery loooooooooooooooooooo'\
44
+ 'oooooooooooooooooooooong messssssssssssssssssssssssssssssssssage ..... '\
45
+ 'A veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery loooooo'\
46
+ 'oooooooooooooooooooooooooooooooooooong messssssssssssssssssssssssssssss'\
47
+ 'ssssage ..... A veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'\
48
+ 'eeeery loooooooooooooooooooooooooooooooooooooooooong messssssssssssssss'\
49
+ 'ssssssssssssssssssage ..... A veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'\
50
+ 'eeeeeeeeeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooong mess'\
51
+ 'ssssssssssssssssssssssssssssssssage ..... A veeeeeeeeeeeeeeeeeeeeeeeeee'\
52
+ 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery looooooooooooooooooooooooooooooooooo'\
53
+ 'ooooooong messssssssssssssssssssssssssssssssssage ..... 2015-08-13 08:'\
54
+ '36:59,316 INFO monasca_notification.main Received signal 17, beginning '\
55
+ 'graceful shutdown.',
56
+ '@version' => '1',
57
+ '@timestamp' => '2015-08-13T08:37:00.287Z',
58
+ 'path' => '/opt/logstash-2.2.0/test.log',
59
+ 'host' => 'kamil-choroba',
60
+ 'type' => 'test-type',
61
+ 'dimensions' => '["[\"service\", \"nova\"]", "[\"priority\", \"high\"]"]',
62
+ 'tags' => ['test-service', 'high']
63
+ })
64
+ }
65
+
66
+ let (:event_without_dims) { LogStash::Event.new(
67
+ {
68
+ 'message' => 'A graceful shutdown.',
69
+ '@version' => '1',
70
+ '@timestamp' => '2015-08-13T08:37:00.287Z',
71
+ 'path' => '/opt/logstash-2.2.0/test.log',
72
+ 'host' => 'kamil-choroba',
73
+ 'type' => 'test-type',
74
+ 'tags' => ['test-service', 'high']
75
+ })
76
+ }
77
+
78
+ let (:event_with_one_dim) { LogStash::Event.new(
79
+ {
80
+ 'message' => 'A graceful shutdown.',
81
+ '@version' => '1',
82
+ '@timestamp' => '2015-08-13T08:37:00.287Z',
83
+ 'path' => '/opt/logstash-2.2.0/test.log',
84
+ 'host' => 'kamil-choroba',
85
+ 'type' => 'test-type',
86
+ 'dimensions' => '["service", "nova"]',
87
+ 'tags' => ['test-service', 'high']
88
+ })
89
+ }
90
+
91
+ let (:event_with_more_dims) { LogStash::Event.new(
92
+ {
93
+ 'message' => 'A graceful shutdown.',
94
+ '@version' => '1',
95
+ '@timestamp' => '2015-08-13T08:37:00.287Z',
96
+ 'path' => '/opt/logstash-2.2.0/test.log',
97
+ 'host' => 'kamil-choroba',
98
+ 'type' => 'test-type',
99
+ 'dimensions' => '["[\"service\", \"nova\"]", "[\"priority\", \"high\"]"]',
100
+ 'tags' => ['test-service', 'high']
101
+ })
102
+ }
103
+
104
+ let (:project_name) { 'monasca' }
105
+ let (:username) { 'operator' }
106
+ let (:password) { 'qweqwe' }
107
+
108
+ let (:complete_config) {
109
+ {
110
+ 'monasca_log_api' => 'http://192.168.10.4:8080',
111
+ 'monasca_log_api_version' => 'v3.0',
112
+ 'keystone_api' => 'http://192.168.10.5:5000',
113
+ 'project_name' => project_name,
114
+ 'username' => username,
115
+ 'password' => password,
116
+ 'domain_id' => 'abadcf984cf7401e88579d393317b0d9',
117
+ 'dimensions' => ['service:test'],
118
+ 'num_of_logs' => 3,
119
+ 'elapsed_time_sec' => 5000,
120
+ 'delay' => 1,
121
+ 'max_data_size_kb' => 4
122
+ }
123
+ }
124
+
125
+ let (:complete_config_short_elapsed_time) {
126
+ {
127
+ 'monasca_log_api' => 'http://192.168.10.4:8080',
128
+ 'monasca_log_api_version' => 'v3.0',
129
+ 'keystone_api' => 'http://192.168.10.5:5000',
130
+ 'project_name' => project_name,
131
+ 'username' => username,
132
+ 'password' => password,
133
+ 'domain_id' => 'abadcf984cf7401e88579d393317b0d9',
134
+ 'dimensions' => ['service:test'],
135
+ 'num_of_logs' => 3,
136
+ 'elapsed_time_sec' => 1,
137
+ 'delay' => 1,
138
+ 'max_data_size_kb' => 4
139
+ }
140
+ }
141
+
142
+ let (:simple_config) {
143
+ {
144
+ 'monasca_log_api' => 'http://192.168.10.4:8080',
145
+ 'keystone_api' => 'http://192.168.10.5:5000',
146
+ 'project_name' => project_name,
147
+ 'username' => username,
148
+ 'password' => password,
149
+ 'domain_id' => 'abadcf984cf7401e88579d393317b0d9',
150
+ }
151
+ }
152
+
153
+ let (:empty_config) { {} }
154
+
155
+ let (:valid_date) { DateTime.now + Rational(5, 1440) }
156
+ let (:expired_date) { DateTime.now - Rational(5, 1440) }
157
+ let (:token_id) { "f8cdafb7dce94444ad781a53ddaff693" }
158
+ let (:old_token_id) { "553ae6ea7d074f00a12750e4aa1dad50" }
159
+
160
+ let (:valid_token) { {:token => token_id,
161
+ :expires_at => valid_date } }
162
+
163
+ let (:expired_token) { {:token => old_token_id,
164
+ :expires_at => expired_date } }
165
+
166
+ after(:each) do
167
+ token = LogStash::Outputs::Keystone::Token.instance
168
+ token.set_token nil, nil
169
+ end
170
+
171
+ context 'when initializing' do
172
+
173
+ it 'without configuration, then raise error' do
174
+ expect {LogStash::Plugin.lookup('output', 'monasca_log_api')
175
+ .new(empty_config)}.to raise_error(LogStash::ConfigurationError)
176
+ end
177
+
178
+ it 'with minimal configuration, then use defaults' do
179
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
180
+ .new(simple_config)
181
+
182
+ expect(monasca_log_api.monasca_log_api_version).to be_instance_of(String)
183
+ expect(monasca_log_api.monasca_log_api_version).not_to be_empty
184
+ expect(monasca_log_api.dimensions).to be_nil
185
+ expect(monasca_log_api.num_of_logs).to be_instance_of(Fixnum)
186
+ expect(monasca_log_api.elapsed_time_sec).to be_instance_of(Fixnum)
187
+ expect(monasca_log_api.delay).to be_instance_of(Fixnum)
188
+ expect(monasca_log_api.max_data_size_kb).to be_instance_of(Fixnum)
189
+ end
190
+
191
+ it 'with complete configuration, then override settings' do
192
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
193
+ .new(complete_config)
194
+
195
+ expect(monasca_log_api.monasca_log_api_version)
196
+ .to eq(complete_config['monasca_log_api_version'])
197
+ expect(monasca_log_api.dimensions).to eq(complete_config['dimensions'])
198
+ expect(monasca_log_api.num_of_logs).to eq(complete_config['num_of_logs'])
199
+ expect(monasca_log_api.elapsed_time_sec)
200
+ .to eq(complete_config['elapsed_time_sec'])
201
+ expect(monasca_log_api.delay).to eq(complete_config['delay'])
202
+ expect(monasca_log_api.max_data_size_kb)
203
+ .to eq(complete_config['max_data_size_kb'])
204
+ end
205
+ end
206
+
207
+ context 'when registering' do
208
+ it 'should initialize the token' do
209
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
210
+ .to receive(:authenticate).and_return(valid_token)
211
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
212
+ .new(complete_config)
213
+ allow(monasca_log_api).to receive(:start_time_check)
214
+ expect {monasca_log_api.register}.to_not raise_error
215
+ expect(LogStash::Outputs::Keystone::Token.instance.id).not_to be_nil
216
+ end
217
+
218
+ it 'should start time check thread' do
219
+ expect_any_instance_of(LogStash::Outputs::MonascaLogApi)
220
+ .to receive(:start_time_check)
221
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
222
+ .new(complete_config)
223
+ allow(monasca_log_api).to receive(:init_token)
224
+ expect {monasca_log_api.register}.to_not raise_error
225
+ end
226
+ end
227
+
228
+ context 'initialize log body' do
229
+ it 'with dimensions' do
230
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
231
+ .to receive(:authenticate).and_return(valid_token)
232
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
233
+ .new(complete_config)
234
+ monasca_log_api.register
235
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'])
236
+ .not_to be_nil
237
+ expect(monasca_log_api.instance_variable_get(:@logs)['dimensions'])
238
+ .not_to be_nil
239
+ expect(monasca_log_api.instance_variable_get(:@logs)['dimensions'])
240
+ .not_to be_empty
241
+ end
242
+
243
+ it 'without dimensions' do
244
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
245
+ .to receive(:authenticate).and_return(valid_token)
246
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
247
+ .new(simple_config)
248
+ monasca_log_api.register
249
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'])
250
+ .not_to be_nil
251
+ expect(monasca_log_api.instance_variable_get(:@logs)['dimensions'])
252
+ .to be_nil
253
+ end
254
+ end
255
+
256
+ context 'when receiving messages' do
257
+ it 'collect messages' do
258
+ expect_any_instance_of(LogStash::Outputs::Monasca::MonascaLogApiClient)
259
+ .to_not receive(:send_logs)
260
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
261
+ .new(complete_config)
262
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
263
+ .to receive(:authenticate).and_return(valid_token)
264
+ allow(monasca_log_api).to receive(:start_time_check)
265
+
266
+ monasca_log_api.register
267
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
268
+ .to eq(0)
269
+ monasca_log_api.multi_receive([event])
270
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
271
+ .to eq(1)
272
+ monasca_log_api.multi_receive([event])
273
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
274
+ .to eq(2)
275
+ end
276
+
277
+ it 'collect messages up to certain amount and send logs' do
278
+ expect_any_instance_of(LogStash::Outputs::Monasca::MonascaLogApiClient)
279
+ .to receive(:send_logs)
280
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
281
+ .new(complete_config)
282
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
283
+ .to receive(:authenticate).and_return(valid_token)
284
+ allow(monasca_log_api).to receive(:start_time_check)
285
+
286
+ monasca_log_api.register
287
+ monasca_log_api.multi_receive([event])
288
+ monasca_log_api.multi_receive([event])
289
+ monasca_log_api.multi_receive([event])
290
+
291
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
292
+ .to eq(0)
293
+ end
294
+
295
+ it 'collect messages up to certain bytesize and send logs' do
296
+
297
+ expect_any_instance_of(LogStash::Outputs::Monasca::MonascaLogApiClient)
298
+ .to receive(:send_logs)
299
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
300
+ .new(complete_config)
301
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
302
+ .to receive(:authenticate).and_return(valid_token)
303
+ allow(monasca_log_api).to receive(:start_time_check)
304
+
305
+ monasca_log_api.register
306
+ monasca_log_api.multi_receive([long_event, long_event])
307
+ monasca_log_api.multi_receive([long_event])
308
+
309
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
310
+ .to eq(1)
311
+ end
312
+
313
+ it 'sends logs after a specific time' do
314
+
315
+ expect_any_instance_of(LogStash::Outputs::Monasca::MonascaLogApiClient)
316
+ .to receive(:send_logs)
317
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
318
+ .new(complete_config_short_elapsed_time)
319
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
320
+ .to receive(:authenticate).and_return(valid_token)
321
+
322
+ monasca_log_api.register
323
+ monasca_log_api.multi_receive([event])
324
+
325
+ sleep(3)
326
+
327
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
328
+ .to eq(0)
329
+ end
330
+ end
331
+
332
+ context 'when parsing events' do
333
+ it 'without dimensions' do
334
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
335
+ .new(complete_config)
336
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
337
+ .to receive(:authenticate).and_return(valid_token)
338
+ allow(monasca_log_api).to receive(:start_time_check)
339
+
340
+ monasca_log_api.register
341
+ monasca_log_api.multi_receive([event_without_dims])
342
+
343
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'])
344
+ .to eq([{"message"=>"A graceful shutdown.",
345
+ "dimensions"=>{"path"=>"/opt/logstash-2.2.0/test.log",
346
+ "type"=>"test-type"}}])
347
+ end
348
+
349
+ it 'with one dimensions' do
350
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
351
+ .new(complete_config)
352
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
353
+ .to receive(:authenticate).and_return(valid_token)
354
+ allow(monasca_log_api).to receive(:start_time_check)
355
+
356
+ monasca_log_api.register
357
+ monasca_log_api.multi_receive([event_with_one_dim])
358
+
359
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'])
360
+ .to eq([{"message"=>"A graceful shutdown.",
361
+ "dimensions"=>{"path"=>"/opt/logstash-2.2.0/test.log",
362
+ "type"=>"test-type", "service"=>"nova"}}])
363
+ end
364
+
365
+ it 'with more dimensions' do
366
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
367
+ .new(complete_config)
368
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
369
+ .to receive(:authenticate).and_return(valid_token)
370
+ allow(monasca_log_api).to receive(:start_time_check)
371
+
372
+ monasca_log_api.register
373
+ monasca_log_api.multi_receive([event_with_more_dims])
374
+
375
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'])
376
+ .to eq([{"message"=>"A graceful shutdown.",
377
+ "dimensions"=>{"path"=>"/opt/logstash-2.2.0/test.log",
378
+ "type"=>"test-type", "service"=>"nova", "priority"=>"high"}}])
379
+ end
380
+ end
381
+
382
+ context 'after sending logs' do
383
+ it 'clears collected logs' do
384
+ expect_any_instance_of(LogStash::Outputs::Monasca::MonascaLogApiClient)
385
+ .to receive(:send_logs)
386
+ monasca_log_api = LogStash::Plugin.lookup('output', 'monasca_log_api')
387
+ .new(complete_config)
388
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
389
+ .to receive(:authenticate).and_return(valid_token)
390
+ allow(monasca_log_api).to receive(:start_time_check)
391
+
392
+ monasca_log_api.register
393
+ monasca_log_api.multi_receive([event])
394
+ monasca_log_api.multi_receive([event])
395
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
396
+ .to eq(2)
397
+ monasca_log_api.multi_receive([event])
398
+
399
+ expect(monasca_log_api.instance_variable_get(:@logs)['logs'].size)
400
+ .to eq(0)
401
+ end
402
+ end
403
+
404
+ context 'check token' do
405
+ it 'if expired, renew it' do
406
+ expect_any_instance_of(LogStash::Outputs::Monasca::MonascaLogApiClient)
407
+ .to receive(:send_logs)
408
+ expect_any_instance_of(LogStash::Outputs::Keystone::KeystoneClient)
409
+ .to receive(:authenticate).with(complete_config['domain_id'],
410
+ complete_config['username'],
411
+ complete_config['password'],
412
+ complete_config['project_name'])
413
+ .and_return(expired_token, valid_token)
414
+ monasca_log_api = LogStash::Outputs::MonascaLogApi.new(complete_config)
415
+ allow(monasca_log_api).to receive(:start_time_check)
416
+ monasca_log_api.register
417
+ expect(LogStash::Outputs::Keystone::Token.instance.id).to eq(old_token_id)
418
+ monasca_log_api.multi_receive([event, event, event])
419
+ expect(LogStash::Outputs::Keystone::Token.instance.id).to eq(token_id)
420
+ end
421
+ end
422
+
423
+ context 'when stopping plugin' do
424
+ it 'then it should kill time thread' do
425
+ expect_any_instance_of(Thread).to receive(:kill)
426
+ monasca_log_api = LogStash::Outputs::MonascaLogApi.new(simple_config)
427
+ allow(monasca_log_api).to receive(:init_token)
428
+ monasca_log_api.register
429
+ monasca_log_api.close
430
+ end
431
+ end
432
+
433
+ end