|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 4 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 5 | +// See the LICENSE file in the project root for more information |
| 6 | + |
| 7 | +namespace Elasticsearch\Tests\Helper; |
| 8 | + |
| 9 | +use Elasticsearch\Helper\ElasticCloudHelper; |
| 10 | +use Elasticsearch\Common\Exceptions\ElasticCloudIdParseException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class ElasticCloudHelperTest |
| 14 | + * |
| 15 | + * @package Elasticsearch\Tests\Helper |
| 16 | + * @author Philip Krauss <[email protected]> |
| 17 | + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2 |
| 18 | + * @link https://elastic.co |
| 19 | + */ |
| 20 | +class ElasticCloudHelperTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + |
| 23 | + /** |
| 24 | + * @return array |
| 25 | + */ |
| 26 | + public function cloudIdsProvider() |
| 27 | + { |
| 28 | + return [ |
| 29 | + ['name' => 'my-cluster-001', 'domain' => 'd-001.com', 'uuids' => 'elasticsearch'], |
| 30 | + ['name' => 'my-cluster-002', 'domain' => 'd-002.net', 'uuids' => 'elasticsearch:kibana'], |
| 31 | + ['name' => 'my-cluster-003', 'domain' => 'd-003.org', 'uuids' => 'elasticsearch:kibana:apm'], |
| 32 | + ]; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return array |
| 37 | + */ |
| 38 | + public function hostArrayProvider() |
| 39 | + { |
| 40 | + return [ |
| 41 | + [ |
| 42 | + [ |
| 43 | + 'host' => 'es.d.co', |
| 44 | + 'port' => '', |
| 45 | + 'scheme' => 'https', |
| 46 | + ] |
| 47 | + ] |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider cloudIdsProvider |
| 53 | + * |
| 54 | + * @covers \ElasticCloudHelperTest::parse |
| 55 | + * @covers \ElasticCloudHelperTest::getCloudId |
| 56 | + * @covers \ElasticCloudHelperTest::getClusterName |
| 57 | + * @covers \ElasticCloudHelperTest::getClusterDns |
| 58 | + */ |
| 59 | + public function testElasticCloudParserAndAccessors($name, $domain, $uuids) |
| 60 | + { |
| 61 | + $cloudId = sprintf('%s:%s', $name, base64_encode(sprintf('%s$%s', $domain, $uuids))); |
| 62 | + $dns = sprintf('elasticsearch.%s', $domain); |
| 63 | + |
| 64 | + $cloud = new ElasticCloudHelper($cloudId); |
| 65 | + |
| 66 | + $this->assertEquals($cloud->getCloudId(), $cloudId); |
| 67 | + $this->assertEquals($cloud->getClusterName(), $name); |
| 68 | + $this->assertEquals($cloud->getClusterDns(), $dns); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @depends testElasticCloudParserAndAccessors |
| 73 | + * |
| 74 | + * @dataProvider hostArrayProvider |
| 75 | + * |
| 76 | + * @covers \ElasticCloudHelperTest::getHost |
| 77 | + */ |
| 78 | + public function testGetHostWithoutBasicAuthentication($expected) |
| 79 | + { |
| 80 | + $cloudId = sprintf('name:%s', base64_encode(sprintf('d.co$es'))); |
| 81 | + |
| 82 | + $cloud = new ElasticCloudHelper($cloudId); |
| 83 | + |
| 84 | + $host = $cloud->getHost(); |
| 85 | + |
| 86 | + $this->assertArrayHasKey(0, $host); |
| 87 | + $this->assertEquals(1, count($host)); |
| 88 | + $this->assertEquals($expected, $host[0]); |
| 89 | + |
| 90 | + $this->assertArrayNotHasKey('user', $host[0]); |
| 91 | + $this->assertArrayNotHasKey('pass', $host[0]); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @depends testGetHostWithoutBasicAuthentication |
| 96 | + * |
| 97 | + * @dataProvider hostArrayProvider |
| 98 | + * |
| 99 | + * @covers \ElasticCloudHelperTest::getHost |
| 100 | + */ |
| 101 | + public function testGetHostWithBasicAuthentication($expected) |
| 102 | + { |
| 103 | + $cloudId = sprintf('name:%s', base64_encode(sprintf('d.co$es'))); |
| 104 | + |
| 105 | + $cloud = new ElasticCloudHelper($cloudId); |
| 106 | + |
| 107 | + // Hook Basic Auth Credentials in the Host config |
| 108 | + $expected['user'] = 'username'; |
| 109 | + $expected['pass'] = 'super-secret-password'; |
| 110 | + $cloud->setBasicAuthentication($expected['user'], $expected['pass']); |
| 111 | + |
| 112 | + $host = $cloud->getHost(); |
| 113 | + |
| 114 | + $this->assertArrayHasKey(0, $host); |
| 115 | + $this->assertEquals(1, count($host)); |
| 116 | + $this->assertEquals($expected, $host[0]); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @covers \ElasticCloudHelperTest::parse |
| 121 | + */ |
| 122 | + public function testCloudIdParseExceptionWithMissingCloudId() |
| 123 | + { |
| 124 | + $this->expectException(ElasticCloudIdParseException::class); |
| 125 | + $cloud = new ElasticCloudHelper(''); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @covers \ElasticCloudHelperTest::parse |
| 130 | + */ |
| 131 | + public function testCloudIdParseExceptionWithMissingHostPart() |
| 132 | + { |
| 133 | + $this->expectException(ElasticCloudIdParseException::class); |
| 134 | + $cloud = new ElasticCloudHelper('foo:'); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @covers \ElasticCloudHelperTest::parse |
| 139 | + */ |
| 140 | + public function testCloudIdParseExceptionWithMissingClusterName() |
| 141 | + { |
| 142 | + $this->expectException(ElasticCloudIdParseException::class); |
| 143 | + $cloud = new ElasticCloudHelper(':bar'); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @covers \ElasticCloudHelperTest::parse |
| 148 | + */ |
| 149 | + public function testCloudIdParseExceptionWithInvalidHostPart() |
| 150 | + { |
| 151 | + $cloudId = sprintf('name:%s', base64_encode('invalid-format')); |
| 152 | + |
| 153 | + $this->expectException(ElasticCloudIdParseException::class); |
| 154 | + $cloud = new ElasticCloudHelper($cloudId); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments