|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Detectors\Aws\Ec2Detector; |
| 6 | +use GuzzleHttp\Client; |
| 7 | +use GuzzleHttp\Handler\MockHandler; |
| 8 | +use GuzzleHttp\HandlerStack; |
| 9 | +use GuzzleHttp\Psr7\Response; |
| 10 | +use OpenTelemetry\Sdk\Resource\ResourceConstants; |
| 11 | +use OpenTelemetry\Sdk\Resource\ResourceInfo; |
| 12 | +use OpenTelemetry\Sdk\Trace\Attributes; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class Ec2DetectorTest extends TestCase |
| 16 | +{ |
| 17 | + private const MOCK_TOKEN_RESPONSE = 'my-token'; |
| 18 | + private const MOCK_HOSTNAME = 'my-hostname'; |
| 19 | + private const MOCK_IDENTITY = '{ |
| 20 | + "instanceId": "my-instance-id", |
| 21 | + "instanceType": "my-instance-type", |
| 22 | + "accountId": "my-account-id", |
| 23 | + "region": "my-region", |
| 24 | + "availabilityZone": "my-zone", |
| 25 | + "imageId": "image-id" |
| 26 | + }'; |
| 27 | + |
| 28 | + private const MOCK_IDENTITY_INCOMPLETE = '{ |
| 29 | + "instanceId": "my-instance-id", |
| 30 | + "instanceType": "my-instance-type", |
| 31 | + "availabilityZone": "my-zone", |
| 32 | + "imageId": "image-id" |
| 33 | + }'; |
| 34 | + |
| 35 | + private const HOST_ID = 'my-instance-id'; |
| 36 | + private const CLOUD_ZONE = 'my-zone'; |
| 37 | + private const HOST_TYPE = 'my-instance-type'; |
| 38 | + private const HOST_IMAGE_ID = 'image-id'; |
| 39 | + private const CLOUD_ACCOUNT_ID = 'my-account-id'; |
| 40 | + private const CLOUD_REGION = 'my-region'; |
| 41 | + private const CLOUD_PROVIDER = 'aws'; |
| 42 | + |
| 43 | + /** |
| 44 | + * @test |
| 45 | + */ |
| 46 | + public function TestValidEc2() |
| 47 | + { |
| 48 | + $mockGuzzle = new MockHandler([ |
| 49 | + //Fetch token response |
| 50 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_TOKEN_RESPONSE), |
| 51 | + // Fetch hostName response |
| 52 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_HOSTNAME), |
| 53 | + // Fetch identities reponse |
| 54 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_IDENTITY), |
| 55 | + ]); |
| 56 | + |
| 57 | + $handlerStack = HandlerStack::create($mockGuzzle); |
| 58 | + $client = new Client(['handler' => $handlerStack]); |
| 59 | + |
| 60 | + $detector = new Ec2Detector($client); |
| 61 | + |
| 62 | + $this->assertEquals(ResourceInfo::create( |
| 63 | + new Attributes( |
| 64 | + [ |
| 65 | + ResourceConstants::HOST_ID => self::HOST_ID, |
| 66 | + ResourceConstants::CLOUD_ZONE => self::CLOUD_ZONE, |
| 67 | + ResourceConstants::HOST_TYPE => self::HOST_TYPE, |
| 68 | + ResourceConstants::HOST_IMAGE_ID => self::HOST_IMAGE_ID, |
| 69 | + ResourceConstants::CLOUD_ACCOUNT_ID => self::CLOUD_ACCOUNT_ID, |
| 70 | + ResourceConstants::CLOUD_REGION => self::CLOUD_REGION, |
| 71 | + ResourceConstants::HOST_HOSTNAME => self::MOCK_HOSTNAME, |
| 72 | + ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER, |
| 73 | + ] |
| 74 | + ) |
| 75 | + ), $detector->detect()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @test |
| 80 | + */ |
| 81 | + public function TestInvalidTokenBody() |
| 82 | + { |
| 83 | + $mockGuzzle = new MockHandler([ |
| 84 | + //Fetch token response |
| 85 | + new Response(200, ['Foo' => 'Bar']), |
| 86 | + // Fetch hostName response |
| 87 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_HOSTNAME), |
| 88 | + // Fetch identities reponse |
| 89 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_IDENTITY), |
| 90 | + ]); |
| 91 | + |
| 92 | + $handlerStack = HandlerStack::create($mockGuzzle); |
| 93 | + $client = new Client(['handler' => $handlerStack]); |
| 94 | + |
| 95 | + $detector = new Ec2Detector($client); |
| 96 | + |
| 97 | + $this->assertEquals(ResourceInfo::emptyResource(), $detector->detect()); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @test |
| 102 | + */ |
| 103 | + public function TestInvalidTokenResponseCode() |
| 104 | + { |
| 105 | + $mockGuzzle = new MockHandler([ |
| 106 | + //Fetch token response |
| 107 | + new Response(404, ['Foo' => 'Bar'], self::MOCK_TOKEN_RESPONSE), |
| 108 | + // Fetch hostName response |
| 109 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_HOSTNAME), |
| 110 | + // Fetch identities reponse |
| 111 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_IDENTITY), |
| 112 | + ]); |
| 113 | + |
| 114 | + $handlerStack = HandlerStack::create($mockGuzzle); |
| 115 | + $client = new Client(['handler' => $handlerStack]); |
| 116 | + |
| 117 | + $detector = new Ec2Detector($client); |
| 118 | + |
| 119 | + $this->assertEquals(ResourceInfo::emptyResource(), $detector->detect()); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @test |
| 124 | + */ |
| 125 | + public function TestInvalidHostName() |
| 126 | + { |
| 127 | + $mockGuzzle = new MockHandler([ |
| 128 | + //Fetch token response |
| 129 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_TOKEN_RESPONSE), |
| 130 | + // Fetch hostName response |
| 131 | + new Response(200, ['Foo' => 'Bar']), |
| 132 | + // Fetch identities reponse |
| 133 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_IDENTITY), |
| 134 | + ]); |
| 135 | + |
| 136 | + $handlerStack = HandlerStack::create($mockGuzzle); |
| 137 | + $client = new Client(['handler' => $handlerStack]); |
| 138 | + |
| 139 | + $detector = new Ec2Detector($client); |
| 140 | + |
| 141 | + $this->assertEquals(ResourceInfo::create( |
| 142 | + new Attributes( |
| 143 | + [ |
| 144 | + ResourceConstants::HOST_ID => self::HOST_ID, |
| 145 | + ResourceConstants::CLOUD_ZONE => self::CLOUD_ZONE, |
| 146 | + ResourceConstants::HOST_TYPE => self::HOST_TYPE, |
| 147 | + ResourceConstants::HOST_IMAGE_ID => self::HOST_IMAGE_ID, |
| 148 | + ResourceConstants::CLOUD_ACCOUNT_ID => self::CLOUD_ACCOUNT_ID, |
| 149 | + ResourceConstants::CLOUD_REGION => self::CLOUD_REGION, |
| 150 | + ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER, |
| 151 | + ] |
| 152 | + ) |
| 153 | + ), $detector->detect()); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @test |
| 158 | + */ |
| 159 | + public function TestInvalidIdentities() |
| 160 | + { |
| 161 | + $mockGuzzle = new MockHandler([ |
| 162 | + //Fetch token response |
| 163 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_TOKEN_RESPONSE), |
| 164 | + // Fetch hostName response |
| 165 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_HOSTNAME), |
| 166 | + // Fetch identities reponse |
| 167 | + new Response(200, ['Foo' => 'Bar']), |
| 168 | + ]); |
| 169 | + |
| 170 | + $handlerStack = HandlerStack::create($mockGuzzle); |
| 171 | + $client = new Client(['handler' => $handlerStack]); |
| 172 | + |
| 173 | + $detector = new Ec2Detector($client); |
| 174 | + |
| 175 | + $this->assertEquals(ResourceInfo::emptyResource(), $detector->detect()); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @test |
| 180 | + */ |
| 181 | + public function TestInvalidIncompleteIdentities() |
| 182 | + { |
| 183 | + $mockGuzzle = new MockHandler([ |
| 184 | + //Fetch token response |
| 185 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_TOKEN_RESPONSE), |
| 186 | + // Fetch hostName response |
| 187 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_HOSTNAME), |
| 188 | + // Fetch identities reponse |
| 189 | + new Response(200, ['Foo' => 'Bar'], self::MOCK_IDENTITY_INCOMPLETE), |
| 190 | + ]); |
| 191 | + |
| 192 | + $handlerStack = HandlerStack::create($mockGuzzle); |
| 193 | + $client = new Client(['handler' => $handlerStack]); |
| 194 | + |
| 195 | + $detector = new Ec2Detector($client); |
| 196 | + |
| 197 | + $this->assertEquals(ResourceInfo::create( |
| 198 | + new Attributes( |
| 199 | + [ |
| 200 | + ResourceConstants::HOST_ID => self::HOST_ID, |
| 201 | + ResourceConstants::CLOUD_ZONE => self::CLOUD_ZONE, |
| 202 | + ResourceConstants::HOST_TYPE => self::HOST_TYPE, |
| 203 | + ResourceConstants::HOST_IMAGE_ID => self::HOST_IMAGE_ID, |
| 204 | + ResourceConstants::HOST_HOSTNAME => self::MOCK_HOSTNAME, |
| 205 | + ResourceConstants::CLOUD_PROVIDER => self::CLOUD_PROVIDER, |
| 206 | + ] |
| 207 | + ) |
| 208 | + ), $detector->detect()); |
| 209 | + } |
| 210 | +} |
0 commit comments