1 | <?php |
||
15 | class SnapshotTest extends Base |
||
16 | { |
||
17 | private const SNAPSHOT_PATH = '/usr/share/elasticsearch/repository/'; |
||
18 | private const REPOSITORY_NAME = 'repo-name'; |
||
19 | |||
20 | /** |
||
21 | * @var Snapshot |
||
22 | */ |
||
23 | protected $snapshot; |
||
24 | |||
25 | /** |
||
26 | * @var Index |
||
27 | */ |
||
28 | protected $index; |
||
29 | |||
30 | /** |
||
31 | * @var Document[] |
||
32 | */ |
||
33 | protected $docs; |
||
34 | |||
35 | protected function setUp(): void |
||
36 | { |
||
37 | parent::setUp(); |
||
38 | $this->snapshot = new Snapshot($this->_getClient()); |
||
39 | |||
40 | $this->index = $this->_createIndex(); |
||
41 | $this->docs = [ |
||
42 | new Document('1', ['city' => 'San Diego']), |
||
43 | new Document('2', ['city' => 'San Luis Obispo']), |
||
44 | new Document('3', ['city' => 'San Francisco']), |
||
45 | ]; |
||
46 | $this->index->addDocuments($this->docs); |
||
47 | $this->index->refresh(); |
||
48 | } |
||
49 | |||
50 | public function testRegisterRepository(): void |
||
51 | { |
||
52 | $location = $this->registerRepository('backup1'); |
||
53 | |||
54 | $response = $this->snapshot->getRepository(self::REPOSITORY_NAME); |
||
55 | $this->assertEquals($location, $response['settings']['location']); |
||
56 | |||
57 | // attempt to retrieve a repository which does not exist |
||
58 | $this->expectException(NotFoundException::class); |
||
59 | $this->snapshot->getRepository('foobar'); |
||
60 | } |
||
61 | |||
62 | public function testSnapshotAndRestore(): void |
||
63 | { |
||
64 | $this->registerRepository('backup2'); |
||
65 | |||
66 | // create a snapshot of our test index |
||
67 | $snapshotName = 'test_snapshot_1'; |
||
68 | $response = $this->snapshot->createSnapshot(self::REPOSITORY_NAME, $snapshotName, ['indices' => $this->index->getName()], true); |
||
69 | |||
70 | // ensure that the snapshot was created properly |
||
71 | $this->assertTrue($response->isOk()); |
||
72 | $this->assertArrayHasKey('snapshot', $response->getData()); |
||
73 | $data = $response->getData(); |
||
74 | $this->assertContains($this->index->getName(), $data['snapshot']['indices']); |
||
75 | $this->assertCount(1, $data['snapshot']['indices']); // only the specified index should be present |
||
76 | $this->assertEquals($snapshotName, $data['snapshot']['snapshot']); |
||
77 | |||
78 | // retrieve data regarding the snapshot |
||
79 | $response = $this->snapshot->getSnapshot(self::REPOSITORY_NAME, $snapshotName); |
||
80 | $this->assertContains($this->index->getName(), $response['indices']); |
||
81 | |||
82 | // delete our test index |
||
83 | $this->index->delete(); |
||
84 | |||
85 | // restore the index from our snapshot |
||
86 | $response = $this->snapshot->restoreSnapshot(self::REPOSITORY_NAME, $snapshotName, [], true); |
||
87 | $this->assertTrue($response->isOk()); |
||
88 | |||
89 | $this->index->refresh(); |
||
90 | $this->index->forcemerge(); |
||
91 | |||
92 | // ensure that the index has been restored |
||
93 | $count = $this->index->count(); |
||
94 | $this->assertEquals(\count($this->docs), $count); |
||
95 | |||
96 | // delete the snapshot |
||
97 | $response = $this->snapshot->deleteSnapshot(self::REPOSITORY_NAME, $snapshotName); |
||
98 | $this->assertTrue($response->isOk()); |
||
99 | |||
100 | // ensure that the snapshot has been deleted |
||
101 | $this->expectException(NotFoundException::class); |
||
102 | $this->snapshot->getSnapshot(self::REPOSITORY_NAME, $snapshotName); |
||
103 | } |
||
104 | |||
105 | private function registerRepository(string $name): string |
||
114 | } |
||
115 |