This repository has been archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
EntityTest.php
336 lines (288 loc) · 10.7 KB
/
EntityTest.php
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
<?php
/**
* Copyright 2012 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
require_once 'SplunkTest.php';
/**
* Tests functionality common to all Entity instances.
*
* Since it is not (normally) possible to manipulate an concrete Entity
* directly (as opposed to a subclass), a choice has to be made regarding
* which subclass to use. For now, a "saved search" entity will be the
* concrete subclass used for most tests.
*/
class EntityTest extends SplunkTest
{
// (This search is installed by default on Splunk 4.x.)
const SAVED_SEARCH_NAME = 'Errors in the last 24 hours';
const SAVED_SEARCH_QUERY = 'error OR failed OR severe OR ( sourcetype=access_* ( 404 OR 500 OR 503 ) )';
public function testGetCollectionUsingContext()
{
$context = $this->loginToRealContext();
$response = $context->sendGet('/servicesNS/nobody/search/saved/searches/');
$this->assertContains(
'<title>' . self::SAVED_SEARCH_NAME . '</title>',
$response->body);
}
public function testGetEntity()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->get(self::SAVED_SEARCH_NAME);
return $savedSearch;
}
/** @depends testGetEntity */
public function testGetPropertyOfEntity($savedSearch)
{
$this->assertEquals(self::SAVED_SEARCH_QUERY, $savedSearch['search']);
}
public function testGetEntityReference()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->getReference(
self::SAVED_SEARCH_NAME);
return $savedSearch;
}
/** @depends testGetEntityReference */
public function testGetPropertyOfEntityReference($savedSearch)
{
$this->assertEquals(self::SAVED_SEARCH_QUERY, $savedSearch['search']);
}
public function testGetMissingEntityReference()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->getReference(
'NO_SUCH_SEARCH');
try
{
$this->touch($savedSearch);
$this->assertTrue(FALSE, 'Expected Splunk_HttpException to be thrown.');
}
catch (Splunk_HttpException $e)
{
$this->assertEquals(404, $e->getResponse()->status);
}
}
/**
* @expectedException Splunk_NoSuchEntityException
*/
public function testGetMissingEntityInNamespace()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->get(
self::SAVED_SEARCH_NAME,
Splunk_Namespace::createSystem());
}
public function testGetMissingEntityReferenceInNamespace()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->getReference(
self::SAVED_SEARCH_NAME,
Splunk_Namespace::createSystem());
try
{
$this->touch($savedSearch);
$this->assertTrue(FALSE, 'Expected Splunk_HttpException to be thrown.');
}
catch (Splunk_HttpException $e)
{
$this->assertEquals(404, $e->getResponse()->status);
}
}
public function testGetEntityInNamespace()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->get(
self::SAVED_SEARCH_NAME,
Splunk_Namespace::createUser('admin', 'search'));
}
public function testGetEntityInWildcardNamespace()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->get(
self::SAVED_SEARCH_NAME,
Splunk_Namespace::createUser('admin', NULL));
}
/**
* @expectedException Splunk_AmbiguousEntityNameException
*/
public function testGetAmbiguousEntity()
{
$this->runAmbiguousEntityTest('doGetAmbiguousEntity');
}
private function doGetAmbiguousEntity($service, $entityName)
{
$service->getSavedSearches()->get(
$entityName,
Splunk_Namespace::createUser(NULL, 'search'));
}
/**
* @expectedException Splunk_AmbiguousEntityNameException
*/
public function testGetAmbiguousEntityReference()
{
$this->runAmbiguousEntityTest('doGetAmbiguousEntityReference');
}
private function doGetAmbiguousEntityReference($service, $entityName)
{
$ambigSavedSearch = $service->getSavedSearches()->getReference(
$entityName,
Splunk_Namespace::createUser(NULL, 'search'));
$this->touch($ambigSavedSearch);
}
private function runAmbiguousEntityTest($testBodyFunc)
{
$entityName = $this->createTempName();
$service = $this->loginToRealService();
$savedSearch1 = $service->getSavedSearches()->create(
$entityName,
array(
'namespace' => Splunk_Namespace::createUser('admin', 'search'),
'search' => 'index=_internal | head 1',
));
$savedSearch2 = $service->getSavedSearches()->create(
$entityName,
array(
'namespace' => Splunk_Namespace::createApp('search'),
'search' => 'index=_internal | head 2',
));
try
{
$this->$testBodyFunc($service, $entityName);
}
catch (Exception $e)
{
// Cleanup
$savedSearch1->delete();
$savedSearch2->delete();
throw $e;
}
}
public function testCreateEntity()
{
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->create(
$this->createTempName(),
array(
'search' => 'index=_internal',
));
// Clean up
$savedSearch->delete();
}
public function testDeleteEntity()
{
$entityName = $this->createTempName();
$service = $this->loginToRealService();
$service->getSavedSearches()->create($entityName, array(
'search' => 'index=_internal',
));
$service->getSavedSearches()->delete($entityName);
}
public function testDeleteEntityReferenceMakesNoGets()
{
list($service, $http) = $this->loginToMockService();
$http->expects($this->never())
->method('get');
$http->expects($this->once())
->method('delete')
->will($this->returnValue((object) array(
'status' => 200,
'reason' => 'OK',
'headers' => array(),
'body' => '')));
$service->getSavedSearches()->getReference('IGNORED_NAME')->delete();
}
public function testUpdateEntity()
{
$entityName = $this->createTempName();
$service = $this->loginToRealService();
$savedSearch = $service->getSavedSearches()->create($entityName, array(
'search' => 'index=_internal',
));
$this->assertEquals('index=_internal', $savedSearch['search']);
$savedSearch2 = $savedSearch->update(array(
'search' => 'index=_internal | head 1',
));
$this->assertSame($savedSearch, $savedSearch2);
$this->assertEquals('index=_internal | head 1', $savedSearch['search']);
$savedSearch->delete();
}
public function testUpdateEntityReferenceMakesNoGets()
{
$secondPostReturnValue = (object) array(
'status' => 200,
'reason' => 'OK',
'headers' => array(),
'body' => '');
list($service, $http) = $this->loginToMockService($secondPostReturnValue);
$http->expects($this->never())
->method('get');
$service->getSavedSearches()->getReference('IGNORED_NAME')->update(array(
'search' => 'index=_internal | head 1',
));
}
public function testRefreshEntityInCustomNamespace()
{
$service = $this->loginToRealService();
$views = new Splunk_Collection($service, 'data/ui/views/');
$entity = $views->get(
'charting',
Splunk_Namespace::createApp('search'));
$entity->refresh();
}
public function testNamespaceIsNotAProperty()
{
$service = $this->loginToRealService();
// Setup
$savedSearch = $service->getSavedSearches()->create($this->createTempName(), array(
'namespace' => Splunk_Namespace::createUser('admin', 'launcher'),
'search' => 'search index=_internal | head 1',
));
// Test
$this->assertTrue(isset($savedSearch['search']));
$this->assertFalse(isset($savedSearch['namespace']));
$savedSearch->update(array(
'search' => 'search index=_internal | head 2',
));
$this->assertFalse(isset($savedSearch['namespace']));
$savedSearch->refresh();
$this->assertEquals('search index=_internal | head 2', $savedSearch['search']);
// Teardown
$savedSearch->delete();
}
public function testGetContent()
{
$service = $this->loginToRealService();
$job = $service->search('search index=_internal | head 1', array(
'exec_mode' => 'blocking',
));
$this->assertTrue($job->isDone());
$this->assertTrue('1' === $job['isDone']);
$content = $job->getContent();
$this->assertTrue('1' === $content['isDone']);
}
public function testGetNamespaceOfWildcardedReference()
{
$service = $this->loginToRealService();
$requestNamespace = Splunk_Namespace::createUser(NULL, NULL);
$this->assertFalse($requestNamespace->isExact());
$index = $service->getIndexes()->getReference(
"_internal",
$requestNamespace);
$actualNamespace = $index->getNamespace();
$this->assertTrue($actualNamespace !== NULL);
$this->assertTrue($actualNamespace !== $requestNamespace);
$this->assertTrue($actualNamespace->isExact());
}
}