forked from Normation/rudder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestQueryProcessor.scala
689 lines (595 loc) · 24.1 KB
/
TestQueryProcessor.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
*************************************************************************************
* Copyright 2011 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/
package com.normation.rudder.services.queries
import org.junit._
import org.junit.Assert._
import org.junit.runner.RunWith
import org.junit.runners.BlockJUnit4ClassRunner
import com.normation.rudder.repository.ldap.LDAPEntityMapper
import com.normation.rudder.domain.queries._
import net.liftweb.common._
import com.normation.rudder.domain._
import com.unboundid.ldap.sdk.DN
import com.normation.ldap.ldif._
import com.normation.ldap.listener.InMemoryDsConnectionProvider
import com.normation.ldap.sdk._
import com.normation.inventory.ldap.core._
import com.normation.inventory.domain.NodeId
import com.normation.utils.HashcodeCaching
import com.normation.rudder.services.nodes.NaiveNodeInfoServiceCachedImpl
/*
* Test query parsing.
*
* These test doesn't test JSON syntax error, as we rely on
* a JSON parser for that part.
*/
@RunWith(classOf[BlockJUnit4ClassRunner])
class TestQueryProcessor extends Loggable {
val ldifLogger = new DefaultLDIFFileLogger("TestQueryProcessor","/tmp/normation/rudder/ldif")
//init of in memory LDAP directory
val schemaLDIFs = (
"00-core" ::
"01-pwpolicy" ::
"04-rfc2307bis" ::
"05-rfc4876" ::
"099-0-inventory" ::
"099-1-rudder" ::
Nil
) map { name =>
this.getClass.getClassLoader.getResource("ldap-data/schema/" + name + ".ldif").getPath
}
val bootstrapLDIFs = ("ldap/bootstrap.ldif" :: "ldap-data/inventory-sample-data.ldif" :: Nil) map { name =>
this.getClass.getClassLoader.getResource(name).getPath
}
val ldap = InMemoryDsConnectionProvider[RoLDAPConnection](
baseDNs = "cn=rudder-configuration" :: Nil
, schemaLDIFPaths = schemaLDIFs
, bootstrapLDIFPaths = bootstrapLDIFs
, ldifLogger
)
//end inMemory ds
val DIT = new InventoryDit(new DN("ou=Accepted Inventories,ou=Inventories,cn=rudder-configuration"),new DN("ou=Inventories,cn=rudder-configuration"),"test")
val removedDIT = new InventoryDit(new DN("ou=Removed Inventories,ou=Inventories,cn=rudder-configuration"),new DN("ou=Inventories,cn=rudder-configuration"),"test")
val pendingDIT = new InventoryDit(new DN("ou=Pending Inventories,ou=Inventories,cn=rudder-configuration"),new DN("ou=Inventories,cn=rudder-configuration"),"test")
val ditService = new InventoryDitServiceImpl(pendingDIT, DIT, removedDIT)
val nodeDit = new NodeDit(new DN("cn=rudder-configuration"))
val rudderDit = new RudderDit(new DN("ou=Rudder, cn=rudder-configuration"))
val ditQueryData = new DitQueryData(DIT, nodeDit)
val inventoryMapper = new InventoryMapper(ditService, pendingDIT, DIT, removedDIT)
val ldapMapper = new LDAPEntityMapper(rudderDit, nodeDit, DIT, null, inventoryMapper)
val internalLDAPQueryProcessor = new InternalLDAPQueryProcessor(ldap,DIT,ditQueryData,ldapMapper)
val nodeInfoService = new NaiveNodeInfoServiceCachedImpl(ldap, nodeDit, DIT, removedDIT, pendingDIT, ldapMapper, inventoryMapper)
val queryProcessor = new AcceptedNodesLDAPQueryProcessor(
nodeDit,
DIT,
internalLDAPQueryProcessor,
nodeInfoService
)
val parser = new CmdbQueryParser with
DefaultStringQueryParser with
JsonQueryLexer {
override val criterionObjects = Map[String,ObjectCriterion]() ++ ditQueryData.criteriaMap
}
case class TestQuery(name:String,query:Query,awaited:Seq[NodeId]) extends HashcodeCaching
val s = Seq(
new NodeId("node0"),new NodeId("node1"),new NodeId("node2"),
new NodeId("node3"),new NodeId("node4"),new NodeId("node5"),
new NodeId("node6"),new NodeId("node7")
)
val sr = NodeId("root") +: s
@Test def ensureNodeLoaded() {
//just check that we correctly loaded demo data in serve
val s = (for {
con <- ldap
} yield {
con.search("cn=rudder-configuration", Sub, BuildFilter.ALL).size
}).openOrThrowException("For tests")
val expected = 41+30 //bootstrap + inventory-sample
assert(expected == s, s"Not found the expected number of entries in test LDAP directory [expected: ${expected}, found: ${s}], perhaps the demo entries where not correctly loaded")
}
@Test def basicQueriesOnId() {
/* find back all server */
val q0 = TestQuery(
"q0",
parser("""
{ "select":"node", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"exists" }
] }
""").openOrThrowException("For tests"),
s)
/* find back server 1 and 5 by id */
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "composition":"or", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"eq", "value":"node1" }
, { "objectType":"node" , "attribute":"nodeId" , "comparator":"eq", "value":"node5" }
] }
""").openOrThrowException("For tests"),
s(1) :: s(5) :: Nil)
/* find back neither server 1 and 5 by id because of the and */
val q2 = TestQuery(
"q2",
parser("""
{ "select":"node", "composition":"and", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"eq", "value":"node1" }
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"eq", "value":"node5" }
] }
""").openOrThrowException("For tests"),
Nil)
testQueries( q0 :: q1 :: q2 :: Nil)
}
@Test def basicQueriesOnOneNodeParameter() {
// only two servers have RAM: server1(RAM) = 10000000, server2(RAM) = 1
val q2_0 = TestQuery(
"q2_0",
parser("""
{ "select":"node", "where":[
{ "objectType":"node", "attribute":"ram", "comparator":"gt", "value":"1" }
] }
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q2_0_ = TestQuery("q2_0_", query = q2_0.query.copy(composition = Or), q2_0.awaited)
val q2_1 = TestQuery(
"q2_1",
parser("""
{ "select":"node", "where":[
{ "objectType":"node", "attribute":"ram", "comparator":"gteq", "value":"1" }
] }
""").openOrThrowException("For tests"),
s(1) :: s(2) :: Nil)
val q2_1_ = TestQuery("q2_1_", query = q2_1.query.copy(composition = Or), q2_1.awaited)
val q2_2 = TestQuery(
"q2_2",
parser("""
{ "select":"node", "where":[
{ "objectType":"node", "attribute":"ram", "comparator":"lteq", "value":"1" }
] }
""").openOrThrowException("For tests"),
s(2) :: Nil)
val q2_2_ = TestQuery("q2_2_", query = q2_2.query.copy(composition = Or), q2_2.awaited)
testQueries(q2_0 :: q2_0_ :: q2_1 :: q2_1_ :: q2_2 :: q2_2_ :: Nil)
}
@Test def machineComponentQueries() {
val q3 = TestQuery(
"q3",
parser("""
{ "select":"node", "where":[
{ "objectType":"biosPhysicalElement", "attribute":"softwareVersion", "comparator":"eq", "value":"6.00" }
] }
""").openOrThrowException("For tests"),
s(6) :: s(7) :: Nil)
testQueries(q3 :: Nil)
}
@Test def softwareQueries() {
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "where":[
{ "objectType":"software", "attribute":"softwareVersion", "comparator":"eq", "value":"1.0.0" }
] }
""").openOrThrowException("For tests"),
s(2) :: s(7) :: Nil)
val q2 = TestQuery(
"q2",
parser("""
{ "select":"node", "where":[
{ "objectType":"software", "attribute":"softwareVersion", "comparator":"eq", "value":"2.0-rc" }
] }
""").openOrThrowException("For tests"),
s(2) :: Nil)
testQueries(q1 :: q2 :: Nil)
}
@Test def logicalElementQueries() {
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "where":[
{ "objectType":"fileSystemLogicalElement", "attribute":"fileSystemFreeSpace", "comparator":"gteq", "value":"1" }
] }
""").openOrThrowException("For tests"),
s(3) :: s(7) :: Nil)
val q2 = TestQuery(
"q2",
parser("""
{ "select":"node", "where":[
{ "objectType":"fileSystemLogicalElement", "attribute":"fileSystemFreeSpace", "comparator":"gteq", "value":"100" }
] }
""").openOrThrowException("For tests"),
s(3) :: Nil)
testQueries(q1 :: q2 :: Nil)
}
@Test def regexQueries() {
//regex and "subqueries" for logical elements should not be contradictory
//here, we have to *only* search for logical elements with the regex
//and cn is both on node and logical elements
val q0 = TestQuery(
"q0",
parser("""
{ "select":"node", "composition":"or" , "where":[
, { "objectType":"fileSystemLogicalElement", "attribute":"description", "comparator":"regex", "value":"matchOnM[e]" }
] }
""").openOrThrowException("For tests"),
s(3) :: Nil)
//on node
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "where":[
{ "objectType":"node" , "attribute":"ram" , "comparator":"regex", "value":"[0-9]{9}" }
, { "objectType":"node" , "attribute":"osKernelVersion" , "comparator":"regex", "value":"[0-9.-]+-(gen)eric" }
, { "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[01]" }
] }
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q1_ = TestQuery("q1_", query = q1.query.copy(composition = Or), s(0) :: s(1) :: Nil)
//on node software, machine, machine element, node element
val q2 = TestQuery(
"q2",
parser("""
{ "select":"nodeAndPolicyServer", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[017]" }
, { "objectType":"software", "attribute":"cn", "comparator":"regex" , "value":"Software [0-9]" }
, { "objectType":"machine", "attribute":"machineId", "comparator":"regex" , "value":"machine[0-2]" }
, { "objectType":"fileSystemLogicalElement", "attribute":"fileSystemFreeSpace", "comparator":"regex", "value":"[01]{2}" }
, { "objectType":"biosPhysicalElement", "attribute":"softwareVersion", "comparator":"regex", "value":"[6.0]+" }
] }
""").openOrThrowException("For tests"),
s(7) :: Nil)
val q2_ = TestQuery("q2_", query = q2.query.copy(composition = Or),
(s(0) :: s(1) :: s(7) :: //nodeId
s(2) :: s(7) :: //software
s(4) :: s(5) :: s(6) :: s(7) :: //machine
s(2) :: sr(0) :: // free space
s(2) :: //bios
Nil).distinct)
//on node and or for regex
val q3 = TestQuery(
"q3",
parser("""
{ "select":"node", "composition":"or", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[01]" }
, { "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[12]" }
] }
""").openOrThrowException("For tests"),
s(0) :: s(1) :: s(2) :: Nil)
//on node and or for regex, testing #3340
val q3_2 = TestQuery(
"q3_2",
parser("""
{ "select":"node", "composition":"or", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[01]" }
, { "objectType":"node" , "attribute":"nodeId" , "comparator":"eq" , "value":"node5" }
, { "objectType":"software", "attribute":"softwareVersion", "comparator":"eq", "value":"1.0.0" }
] }
""").openOrThrowException("For tests"),
s(0) :: s(1) :: s(2) :: s(5) :: s(7) :: Nil)
//same as q3 with and
val q4 = TestQuery(
"q3",
parser("""
{ "select":"node", "composition":"and", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[01]" }
, { "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"[nN]ode[12]" }
] }
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q5 = TestQuery(
"q5",
parser("""
{ "select":"nodeAndPolicyServer","composition":"or", "where":[
, { "objectType":"fileSystemLogicalElement" , "attribute":"mountPoint" , "comparator":"regex", "value":"[/]" }
] }
""").openOrThrowException("For tests"),
s(3) :: s(7) :: sr(0) :: Nil)
//test regex for "not containing word", see http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word
//here, we don't want to have node0 or node1
val q6 = TestQuery(
"q5",
parser("""
{ "select":"node", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"regex", "value":"((?!node0|node1).)*" }
] }
""").openOrThrowException("For tests"),
s.tail.tail)
//same as q5, but with "not regex"
val q7 = TestQuery(
"q6",
parser("""
{ "select":"node", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"notRegex", "value":"node0" }
] }
""").openOrThrowException("For tests"),
s.tail)
//same as q5 on IP, to test with escaping
//192.168.56.101 is for node3
val q8 = TestQuery(
"q7",
parser("""
{ "select":"node", "where":[
{ "objectType":"node" , "attribute":"ipHostNumber" , "comparator":"notRegex", "value":"192.168.56.101" }
] }
""").openOrThrowException("For tests"),
s.filterNot( _ == s(1)) )
//typical use case for server on internal/dmz/both: want intenal (but not both)
//that test a match regex and not regex
val q9 = TestQuery(
"q8",
parser("""
{ "select":"node", "where":[
{ "objectType":"node" , "attribute":"ipHostNumber" , "comparator":"regex", "value":"127.0.0.*" }
, { "objectType":"node" , "attribute":"ipHostNumber" , "comparator":"notRegex", "value":"192.168.56.10[23]" }
] }
""").openOrThrowException("For tests"),
Seq(s(1), s(4)) )
//s0,5,6,7,8 not ok because no 127.0.0.1
//s1 ok because not in "not regex" pattern
//s2,s3 not ok because in the "not regex" pattern
//s4 ok because only 127.0.0.1
testQueries(q0 :: q1 :: q1_ :: q2 :: q2_ :: q3 :: q3_2 :: q4 :: q5 :: q6 :: q7 :: q8 :: q9 :: Nil)
}
@Test def dateQueries() {
//the node inventory date is 15/05/2013
def q(name: String, comp: String, day: Int, expects: Seq[NodeId] ) = TestQuery(
name
, parser("""
{ "select":"node", "where":[
{ "objectType":"node", "attribute":"inventoryDate", "comparator":"%s" , "value":"%s/05/2013" }
] }
""".format(comp, day)).openOrThrowException("For tests")
, expects
)
def query(name: String, comp: String, day: Int, valid: Boolean) = q(name, comp, day, { if(valid) s(0) :: Nil else Nil })
val q12 = q("q12", "notEq", 15, s.filterNot( _ == s(0)))
val q13 = q("q13", "notEq", 14, s )
val q14 = q("q14", "notEq", 16, s )
testQueries(
query("q1", "eq", 15, valid = true)
:: query("q2", "eq", 14, valid = false)
:: query("q3", "eq", 16, valid = false)
:: query("q4", "gteq", 15, valid = true)
:: query("q5", "gteq", 16, valid = false)
:: query("q6", "lteq", 15, valid = true)
:: query("q7", "lteq", 14, valid = false)
:: query("q8", "lt", 15, valid = false)
:: query("q9", "lt", 16, valid = true)
:: query("q10", "gt", 15, valid = false)
:: query("q11", "gt", 14, valid = true)
:: q12 :: q13 :: q14
:: Nil
)
}
@Test def policyServerQueriesOnId() {
val q0 = TestQuery(
"q0",
parser("""
{ "select":"nodeAndPolicyServer", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"exists" }
] }
""").openOrThrowException("For tests"),
sr)
val q1 = TestQuery(
"q1",
parser("""
{ "select":"nodeAndPolicyServer", "composition":"or", "where":[
{ "objectType":"node" , "attribute":"nodeId" , "comparator":"exists" }
] }
""").openOrThrowException("For tests"),
sr)
testQueries( q0 :: q1 :: Nil)
}
/**
* Test environment variable
*/
@Test def nodeJsonFixedKeyQueries() {
val q1 = TestQuery(
"q1",
parser("""
{"select":"node","composition":"And","where":[
{"objectType":"process","attribute":"started","comparator":"eq","value":"2015-01-21 17:24"}
]}
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q2 = TestQuery(
"q2",
parser("""
{"select":"node","composition":"And","where":[
{"objectType":"process","attribute":"commandName","comparator":"regex","value":".*vtmp.*"}
]}
""").openOrThrowException("For tests"),
s(1) :: Nil)
testQueries(q1 :: q2 :: Nil)
}
/**
* Test environment variable and nodeProperty
*/
@Test def nodeNameValueQueries() {
val q1 = TestQuery(
"q1",
parser("""
{"select":"node","composition":"And","where":[
{"objectType":"environmentVariable","attribute":"name.value","comparator":"eq","value":"SHELL=/bin/sh"}
]}
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q2 = TestQuery(
"q2",
parser("""
{"select":"node","composition":"And","where":[
{"objectType":"environmentVariable","attribute":"name.value","comparator":"regex","value":".+=/.*/rudder.*"}
]}
""").openOrThrowException("For tests"),
s(2) :: s(3) :: Nil)
testQueries(q1 :: q2 :: Nil)
}
@Test def nodeProperties() {
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"eq", "value":"foo=bar" }
] }
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q2 = TestQuery(
"q2",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"foo?=.*ar" }
] }
""").openOrThrowException("For tests"),
s(1) :: Nil)
val q3 = TestQuery(
"q3",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"hasKey", "value":"datacenter" }
] }
""").openOrThrowException("For tests"),
s(2) :: s(3) :: Nil)
// same as "haskey"
val q4 = TestQuery(
"q4",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"datacenter=.*" }
] }
""").openOrThrowException("For tests"),
s(2) :: s(3) :: Nil)
// kind of matching sub-keys
val q5 = TestQuery(
"q5",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"datacenter=.*\"id\":1234,.*" }
] }
""").openOrThrowException("For tests"),
s(2) :: Nil)
// matching unquoted number
val q6 = TestQuery(
"q6",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"number=42" }
] }
""").openOrThrowException("For tests"),
s(3) :: s(4) :: Nil)
// matching provider, but the user data only
val q7 = TestQuery(
"q7",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"datacenter=.*provider.*" }
] }
""").openOrThrowException("For tests"),
s(3) :: Nil)
testQueries(q1 :: q2 :: q3 :: q4 :: q5 :: q6 :: q7 :: Nil)
}
@Test def nodePropertiesMissingEqualsReq() {
// Failing request, see #10570
// if there is no "=", we must only find node with empty value for the key
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"foo" }
] }
""").openOrThrowException("For tests"),
s(4) :: Nil)
testQueries(q1 :: Nil)
}
@Test def nodePropertiesFailingReq() {
// Failing request, see #10570
val failingRegexRequest =
parser("""
{ "select":"node", "where":[
{ "objectType":"serializedNodeProperty", "attribute":"name.value", "comparator":"regex", "value":"f{o}o" }
] }""")
failingRegexRequest match {
case Full(query) =>
val result =
queryProcessor.process(query) match {
case Full(_) => true
case _ => false
}
assertFalse("regex Query with wrong data for node properties should fail", result)
case eb:EmptyBox =>
throw new Exception("Failing regexp query is not a valid query")
}
}
@Test def unsortedQueries() {
val q1 = TestQuery(
"q1",
parser("""
{ "select":"node", "where":[
{ "objectType":"software", "attribute":"cn", "comparator":"eq" , "value":"aalib-libs.i586" },
{ "objectType":"machine", "attribute":"cn", "comparator":"exists" },
{ "objectType":"node" , "attribute":"ram" , "comparator":"gt", "value":"1000" }
] }
""").openOrThrowException("For tests"),
Nil)
testQueries(q1 :: Nil)
}
private def testQueries(queries:Seq[TestQuery]) : Unit = {
queries foreach { q =>
logger.debug("Processing: " + q.name)
testQueryResultProcessor(q.name,q.query,q.awaited)
}
}
private def testQueryResultProcessor(name:String,query:Query, nodes:Seq[NodeId]) = {
val ids = nodes.sortBy( _.value )
val found = queryProcessor.process(query).openOrThrowException("For tests").map { nodeInfo =>
nodeInfo.id
}.sortBy( _.value )
//also test with requiring only the expected node to check consistancy
//(that should not change anything)
assertEquals("[%s]Duplicate entries in result: %s".format(name,found),
found.size.toLong,found.distinct.size.toLong)
assertEquals("[%s]Size differ between awaited and found entry set (process) Found: %s Wants: %s".
format(name,found,ids),ids.size.toLong,found.size.toLong)
assertTrue("[%s]Entries differ between awaited and found entry set (process) Found: %s Wants: %s".
format(name,found,ids),found.forall { f => ids.exists( f == _) })
logger.debug("Testing with expected entries")
val foundWithLimit = (internalLDAPQueryProcessor.internalQueryProcessor(query, limitToNodeIds = Some(ids)).openOrThrowException("For tests").map { entry =>
NodeId(entry("nodeId").get)
}).distinct.sortBy( _.value )
assertEquals("[%s]Size differ between awaited entry and found entry set when setting expected entries (process)\n Found: %s\n Wants: %s".
format(name,foundWithLimit,ids),ids.size.toLong,foundWithLimit.size.toLong)
}
@After def after() {
ldap.server.shutDown(true)
}
}