-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathElasticSearchAliasInitTest.php
More file actions
167 lines (141 loc) · 5.87 KB
/
ElasticSearchAliasInitTest.php
File metadata and controls
167 lines (141 loc) · 5.87 KB
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
<?php
namespace Tests\Jobs;
use Tests\TestCase;
use App\Http\Curl\HttpRequest;
use Illuminate\Contracts\Queue\Job;
use App\Jobs\ElasticSearchAliasInit;
use App\WikiDb;
use App\Wiki;
class ElasticSearchAliasInitTest extends TestCase
{
private $wikiId;
private $prefix;
private $dbName;
public function setUp(): void {
parent::setUp();
$this->wikiId = Wiki::factory()->create()->id;
$this->dbName = WikiDb::factory()->create( [ 'wiki_id' => $this->wikiId ] )->name;
$this->prefix = 'testing_1';
putenv( 'ELASTICSEARCH_SHARED_INDEX_PREFIX' );
}
public function tearDown(): void
{
putenv( 'ELASTICSEARCH_SHARED_INDEX_PREFIX' );
Wiki::query()->delete();
WikiDb::query()->delete();
parent::tearDown();
}
private function buildAlias( string $index, string $alias ) {
return [
'add' => [
'index' => $index,
'alias' => $alias,
'routing' => $alias,
'filter' => [ 'prefix' => [ 'wiki' => $this->dbName . '-' ] ]
]
];
}
private function getMockRequest() {
$request = $this->createMock( HttpRequest::class );
$request->expects( $this->once() )
->method('setOptions')
->with(
[
CURLOPT_URL => getenv( 'ELASTICSEARCH_SHARED_INDEX_HOST' ) . '/_aliases',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60 * 15,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ],
CURLOPT_POSTFIELDS => json_encode( [
'actions' => [
$this->buildAlias( $this->prefix . '_content_first', $this->dbName ),
$this->buildAlias( $this->prefix . '_content_first', $this->dbName . '_content' ),
$this->buildAlias( $this->prefix . '_content_first', $this->dbName . '_content_first' ),
$this->buildAlias( $this->prefix . '_general_first', $this->dbName ),
$this->buildAlias( $this->prefix . '_general_first', $this->dbName . '_general' ),
$this->buildAlias( $this->prefix . '_general_first', $this->dbName . '_general_first' )
]
] )
]
);
return $request;
}
public function testSuccess()
{
$request = $this->getMockRequest();
$request->method( 'execute' )
->willReturn( json_encode( [ 'acknowledged' => true ] ) );
$mockJob = $this->createMock( Job::class );
$mockJob->expects( $this->never() )
->method( 'fail' )
->withAnyParameters();
$job = new ElasticSearchAliasInit( $this->wikiId, $this->prefix );
$job->setJob( $mockJob );
$job->handle( $request );
}
public function testFailure()
{
$request = $this->getMockRequest();
$request->method( 'execute' )
->willReturn( json_encode( [ 'acknowledged' => false ] ) );
$mockJob = $this->createMock( Job::class );
$mockJob->expects( $this->once() )
->method( 'fail' )
->with( new \RuntimeException( "Updating Elasticsearch aliases failed for $this->wikiId with {\"acknowledged\":false}" ) );
$job = new ElasticSearchAliasInit( $this->wikiId, $this->prefix );
$job->setJob( $mockJob );
$job->handle( $request );
}
public function testElasticFailure()
{
$error = json_encode( [ 'error' => [ 'root_cause' => [ [ 'type' => 'index_not_found_exception' ] ] ] ] );
$request = $this->getMockRequest();
$request->method( 'execute' )->willReturn( $error );
$mockJob = $this->createMock( Job::class );
$mockJob->expects( $this->once() )
->method( 'fail' )
->with( new \RuntimeException( "Updating Elasticsearch aliases failed for $this->wikiId with " . $error ) );
$job = new ElasticSearchAliasInit( $this->wikiId, $this->prefix );
$job->setJob( $mockJob );
$job->handle( $request );
}
public function testMissingDatabaseFailure()
{
$this->wikiId = -1;
$request = $this->createMock( HttpRequest::class );
$mockJob = $this->createMock( Job::class );
$mockJob->expects( $this->once() )
->method( 'fail' )
->with( new \RuntimeException( "Failed to get database name for $this->wikiId" ) );
$job = new ElasticSearchAliasInit( $this->wikiId, $this->prefix );
$job->setJob( $mockJob );
$job->handle( $request );
}
public function testSuccessWithPrefixEnv()
{
$this->prefix = 'env_1';
putenv( "ELASTICSEARCH_SHARED_INDEX_PREFIX=$this->prefix" );
$request = $this->getMockRequest();
$request->method( 'execute' )
->willReturn( json_encode( [ 'acknowledged' => true ] ) );
$mockJob = $this->createMock( Job::class );
$mockJob->expects( $this->never() )
->method( 'fail' )
->withAnyParameters();
$job = new ElasticSearchAliasInit( $this->wikiId );
$job->setJob( $mockJob );
$job->handle( $request );
}
public function testMissingPrefixFailure()
{
$request = $this->createMock( HttpRequest::class );
$mockJob = $this->createMock( Job::class );
$mockJob->expects( $this->once() )
->method( 'fail' )
->with( new \RuntimeException( "Missing shared index prefix for $this->wikiId" ) );
$job = new ElasticSearchAliasInit( $this->wikiId );
$job->setJob( $mockJob );
$job->handle( $request );
}
}