|
| 1 | +<?php |
| 2 | +require __DIR__ . '/../../../vendor/autoload.php'; |
| 3 | + |
| 4 | +use Google\Auth\CredentialSource\AwsNativeSource; |
| 5 | +use GuzzleHttp\Client; |
| 6 | + |
| 7 | +echo "Starting thorough ECS mock testing suite...\n\n"; |
| 8 | + |
| 9 | +$client = new Client(['http_errors' => true]); |
| 10 | +$httpHandler = function ($request) use ($client) { |
| 11 | + return $client->send($request); |
| 12 | +}; |
| 13 | + |
| 14 | +$testsPassed = 0; |
| 15 | +$testsFailed = 0; |
| 16 | + |
| 17 | +function runTestCase($name, $setupEnv, $assertion) |
| 18 | +{ |
| 19 | + global $httpHandler, $testsPassed, $testsFailed; |
| 20 | + |
| 21 | + echo "Running Test: $name... "; |
| 22 | + |
| 23 | + // Clear potentially polluting env vars before setup |
| 24 | + putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI'); |
| 25 | + putenv('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'); |
| 26 | + putenv('AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE'); |
| 27 | + |
| 28 | + $setupEnv(); |
| 29 | + |
| 30 | + try { |
| 31 | + $result = AwsNativeSource::getSigningVarsFromEcs($httpHandler); |
| 32 | + $assertion($result, null); |
| 33 | + } catch (\Exception $e) { |
| 34 | + $assertion(null, $e); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// 1. Standard Success |
| 39 | +runTestCase('Success - Valid Credentials', function () { |
| 40 | + putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI=http://127.0.0.1:8080/?scenario=success'); |
| 41 | +}, function ($result, $exception) use (&$testsPassed, &$testsFailed) { |
| 42 | + if ($result && $result[0] === 'MOCK_ACCESS_KEY_123' && $result[1] === 'MOCK_SECRET_KEY_456') { |
| 43 | + echo "✅ PASS\n"; |
| 44 | + $testsPassed++; |
| 45 | + } else { |
| 46 | + echo "❌ FAIL (Expected valid credentials array)\n"; |
| 47 | + $testsFailed++; |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +// 2. Invalid Metadata Response (JSON Error) |
| 52 | +runTestCase('Error - Invalid JSON Response', function () { |
| 53 | + putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI=http://127.0.0.1:8080/?scenario=invalid_json'); |
| 54 | +}, function ($result, $exception) use (&$testsPassed, &$testsFailed) { |
| 55 | + if ($exception instanceof \UnexpectedValueException && strpos($exception->getMessage(), 'Invalid or missing ECS credentials') !== false) { |
| 56 | + echo "✅ PASS\n"; |
| 57 | + $testsPassed++; |
| 58 | + } else { |
| 59 | + echo "❌ FAIL (Expected UnexpectedValueException)\n"; |
| 60 | + $testsFailed++; |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +// 3. Missing Required Fields |
| 65 | +runTestCase('Error - Missing Fields in JSON', function () { |
| 66 | + putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI=http://127.0.0.1:8080/?scenario=missing_fields'); |
| 67 | +}, function ($result, $exception) use (&$testsPassed, &$testsFailed) { |
| 68 | + if ($exception instanceof \UnexpectedValueException && strpos($exception->getMessage(), 'Invalid or missing ECS credentials') !== false) { |
| 69 | + echo "✅ PASS\n"; |
| 70 | + $testsPassed++; |
| 71 | + } else { |
| 72 | + echo "❌ FAIL (Expected UnexpectedValueException)\n"; |
| 73 | + $testsFailed++; |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +// 4. Server Error (500) |
| 78 | +runTestCase('Error - HTTP 500 Server Error', function () { |
| 79 | + putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI=http://127.0.0.1:8080/?scenario=server_error'); |
| 80 | +}, function ($result, $exception) use (&$testsPassed, &$testsFailed) { |
| 81 | + if ($exception instanceof \GuzzleHttp\Exception\ServerException) { |
| 82 | + echo "✅ PASS\n"; |
| 83 | + $testsPassed++; |
| 84 | + } else { |
| 85 | + echo "❌ FAIL (Expected Guzzle ServerException)\n"; |
| 86 | + $testsFailed++; |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +// 5. Unreadable Token File |
| 91 | +runTestCase('Error - Unreadable Token File', function () { |
| 92 | + putenv('AWS_CONTAINER_CREDENTIALS_FULL_URI=http://127.0.0.1:8080/?scenario=success'); |
| 93 | + putenv('AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE=/path/to/nowhere/that/does/not/exist.txt'); |
| 94 | +}, function ($result, $exception) use (&$testsPassed, &$testsFailed) { |
| 95 | + if ($exception instanceof \RuntimeException && strpos($exception->getMessage(), 'is not readable') !== false) { |
| 96 | + echo "✅ PASS\n"; |
| 97 | + $testsPassed++; |
| 98 | + } else { |
| 99 | + echo "❌ FAIL (Expected RuntimeException for unreadable file)\n"; |
| 100 | + $testsFailed++; |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +// 6. Bailout (No Environment Variables) |
| 105 | +runTestCase('Success - Bailout (No URIs Set)', function () { |
| 106 | + // We already clear the env vars in runTestCase. |
| 107 | +}, function ($result, $exception) use (&$testsPassed, &$testsFailed) { |
| 108 | + if ($result === null && $exception === null) { |
| 109 | + echo "✅ PASS\n"; |
| 110 | + $testsPassed++; |
| 111 | + } else { |
| 112 | + echo "❌ FAIL (Expected null return)\n"; |
| 113 | + $testsFailed++; |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +echo "\nTest Suite Complete. Passed: $testsPassed, Failed: $testsFailed\n"; |
| 118 | +if ($testsFailed > 0) { |
| 119 | + exit(1); |
| 120 | +} |
0 commit comments