blob: 86d9507f2ab88a635494a1c3495637a81197994c [file] [log] [blame]
Éric Lemoine71272712016-11-08 12:53:51 +00001-- Copyright 2015 Mirantis, Inc.
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- 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
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14
15EXPORT_ASSERT_TO_GLOBALS=true
16require('luaunit')
17require('os')
18package.path = package.path .. ";../heka/files/lua/common/?.lua;lua/mocks/?.lua"
19
20local patt = require('patterns')
21local l = require('lpeg')
22
23TestPatterns = {}
24
25 function TestPatterns:test_Uuid()
26 assertEquals(patt.Uuid:match('be6876f2-c1e6-42ea-ad95-792a5500f0fa'),
27 'be6876f2-c1e6-42ea-ad95-792a5500f0fa')
28 assertEquals(patt.Uuid:match('be6876f2c1e642eaad95792a5500f0fa'),
29 'be6876f2-c1e6-42ea-ad95-792a5500f0fa')
30 assertEquals(patt.Uuid:match('ze6876f2c1e642eaad95792a5500f0fa'),
31 nil)
32 assertEquals(patt.Uuid:match('be6876f2-c1e642eaad95792a5500f0fa'),
33 nil)
34 end
35
36 function TestPatterns:test_Timestamp()
37 -- note that Timestamp:match() returns the number of nanosecs since the
38 -- Epoch in the local timezone
39 local_epoch = os.time(os.date("!*t",0)) * 1e9
40 assertEquals(patt.Timestamp:match('1970-01-01 00:00:01+00:00'),
41 local_epoch + 1e9)
42 assertEquals(patt.Timestamp:match('1970-01-01 00:00:02'),
43 local_epoch + 2e9)
44 assertEquals(patt.Timestamp:match('1970-01-01 00:00:03'),
45 local_epoch + 3e9)
46 assertEquals(patt.Timestamp:match('1970-01-01T00:00:04-00:00'),
47 local_epoch + 4e9)
48 assertEquals(patt.Timestamp:match('1970-01-01 01:00:05+01:00'),
49 local_epoch + 5e9)
50 assertEquals(patt.Timestamp:match('1970-01-01 00:00:00.123456+00:00'),
51 local_epoch + 0.123456 * 1e9)
52 assertEquals(patt.Timestamp:match('1970-01-01 00:01'),
53 nil)
54 end
55
56 function TestPatterns:test_programname()
57 assertEquals(l.C(patt.programname):match('nova-api'), 'nova-api')
58 assertEquals(l.C(patt.programname):match('nova-api foo'), 'nova-api')
59 end
60
61 function TestPatterns:test_anywhere()
62 assertEquals(patt.anywhere(l.C(patt.dash)):match(' - '), '-')
63 assertEquals(patt.anywhere(patt.dash):match(' . '), nil)
64 end
65
66 function TestPatterns:test_openstack()
67 local_epoch = os.time(os.date("!*t",0)) * 1e9
68 assertEquals(patt.openstack:match(
69 '1970-01-01 00:00:02 3434 INFO oslo_service.periodic_task [-] Blabla...'),
70 {Timestamp = local_epoch + 2e9, Pid = '3434', SeverityLabel = 'INFO',
71 PythonModule = 'oslo_service.periodic_task', Message = '[-] Blabla...'})
72 end
73
74 function TestPatterns:test_openstack_request_context()
75 assertEquals(patt.openstack_request_context:match('[-]'), nil)
76 assertEquals(patt.openstack_request_context:match(
77 "[req-4db318af-54c9-466d-b365-fe17fe4adeed - - - - -]"),
78 {RequestId = '4db318af-54c9-466d-b365-fe17fe4adeed'})
79 assertEquals(patt.openstack_request_context:match(
80 "[req-4db318af-54c9-466d-b365-fe17fe4adeed 8206d40abcc3452d8a9c1ea629b4a8d0 112245730b1f4858ab62e3673e1ee9e2 - - -]"),
81 {RequestId = '4db318af-54c9-466d-b365-fe17fe4adeed',
82 UserId = '8206d40a-bcc3-452d-8a9c-1ea629b4a8d0',
83 TenantId = '11224573-0b1f-4858-ab62-e3673e1ee9e2'})
84 end
85
86 function TestPatterns:test_openstack_http()
87 assertEquals(patt.openstack_http:match(
88 '"OPTIONS / HTTP/1.0" status: 200 len: 497 time: 0.0006731'),
89 {http_method = 'OPTIONS', http_url = '/', http_version = '1.0',
90 http_status = '200', http_response_size = 497,
91 http_response_time = 0.0006731})
92 assertEquals(patt.openstack_http:match(
93 'foo "OPTIONS / HTTP/1.0" status: 200 len: 497 time: 0.0006731 bar'),
94 {http_method = 'OPTIONS', http_url = '/', http_version = '1.0',
95 http_status = '200', http_response_size = 497,
96 http_response_time = 0.0006731})
97 end
98
99 function TestPatterns:test_openstack_http_with_extra_space()
100 assertEquals(patt.openstack_http:match(
101 '"OPTIONS / HTTP/1.0" status: 200 len: 497 time: 0.0006731'),
102 {http_method = 'OPTIONS', http_url = '/', http_version = '1.0',
103 http_status = '200', http_response_size = 497,
104 http_response_time = 0.0006731})
105 assertEquals(patt.openstack_http:match(
106 'foo "OPTIONS / HTTP/1.0" status: 200 len: 497 time: 0.0006731 bar'),
107 {http_method = 'OPTIONS', http_url = '/', http_version = '1.0',
108 http_status = '200', http_response_size = 497,
109 http_response_time = 0.0006731})
110 end
111
112 function TestPatterns:test_ip_address()
113 assertEquals(patt.ip_address:match('192.168.1.2'),
114 {ip_address = '192.168.1.2'})
115 assertEquals(patt.ip_address:match('foo 192.168.1.2 bar'),
116 {ip_address = '192.168.1.2'})
117 assertEquals(patt.ip_address:match('192.1688.1.2'), nil)
118 end
119
120lu = LuaUnit
121lu:setVerbosity( 1 )
122os.exit( lu:run() )