Skip to content

Commit f88f231

Browse files
committed
E2E bug fixes
1 parent fe163c9 commit f88f231

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/ResourceDetectors/DigitalOcean/src/DigitalOceanDetector.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getResource(): ResourceInfo
3434
{
3535
$client = Discovery::find();
3636
$requestFactory = MessageFactoryResolver::create()->resolveRequestFactory();
37-
$token = $_ENV[self::ENV_DO_API_TOKEN] ?? '';
37+
$token = $_SERVER[self::ENV_DO_API_TOKEN] ?? '';
3838

3939
/** Bail early if wrong environment */
4040
if (!self::isDigitalOcean()) {
@@ -57,6 +57,9 @@ public function getResource(): ResourceInfo
5757
/** Attributes available without authentication via the link-local IP API */
5858
$metadataRequest = $requestFactory->createRequest('GET', self::DO_METADATA_ENDPOINT_URL);
5959
$metadataResponse = $client->sendRequest($metadataRequest);
60+
if ($metadataResponse->getStatusCode() !== 200) {
61+
throw new UnexpectedValueException('Failed to read the DigitalOcean metadata API.');
62+
}
6063
$metadata = json_decode($metadataResponse->getBody()->getContents(), flags: JSON_THROW_ON_ERROR);
6164

6265
$attributes[ResourceAttributes::CLOUD_REGION] = $metadata->region;
@@ -78,14 +81,15 @@ public function getResource(): ResourceInfo
7881
)
7982
->withHeader('Authorization', sprintf('Bearer %s', $token));
8083
$accountResponse = $client->sendRequest($accountRequest);
84+
if ($accountResponse->getStatusCode() !== 200) {
85+
throw new UnexpectedValueException('Failed to read the account endpoint on the DigitalOcean API.');
86+
}
8187
$account = json_decode($accountResponse->getBody()->getContents(), flags: JSON_THROW_ON_ERROR)->account;
8288

8389
$attributes[ResourceAttributes::CLOUD_ACCOUNT_ID] = $account->team->uuid;
84-
} catch (Throwable $t) {
85-
self::logNotice(
86-
'DigitalOcean Access Token found, but unable to get account info',
87-
['exception' => $t]
88-
);
90+
self::logInfo('DigitalOcean Access Token found; setting DigitalOcean account ID in resource attributes');
91+
} catch (Throwable) {
92+
// The token being available and scoped is the abnormal state, so logging the reverse of this catch
8993
}
9094

9195
try {
@@ -96,17 +100,18 @@ public function getResource(): ResourceInfo
96100
)
97101
->withHeader('Authorization', sprintf('Bearer %s', $token));
98102
$dropletResponse = $client->sendRequest($dropletRequest);
103+
if ($dropletResponse->getStatusCode() !== 200) {
104+
throw new UnexpectedValueException('Failed to read the droplet endpoint on the DigitalOcean API.');
105+
}
99106
$droplet = json_decode($dropletResponse->getBody()->getContents(), flags: JSON_THROW_ON_ERROR)->droplet;
100107

101108
$attributes[ResourceAttributes::HOST_IMAGE_ID] = (string) $droplet->image->id;
102109
$attributes[ResourceAttributes::HOST_IMAGE_NAME] = $droplet->image->name;
103110
$attributes[ResourceAttributes::HOST_TYPE] = $droplet->size_slug;
104111
$attributes[ResourceAttributes::OS_NAME] = $droplet->image->distribution;
105-
} catch (Throwable $t) {
106-
self::logNotice(
107-
'DigitalOcean Access Token found, but unable to get Droplet info',
108-
['exception' => $t]
109-
);
112+
self::logInfo('DigitalOcean Access Token found; setting additional Droplet info in resource attributes');
113+
} catch (Throwable) {
114+
// The token being available and scoped is the abnormal state, so logging the reverse of this catch
110115
}
111116
}
112117

@@ -134,6 +139,6 @@ private static function readSMBIOS(string $dmiKeyword): string
134139
throw new UnexpectedValueException('Failed to read SMBIOS value from sysfs.');
135140
}
136141

137-
return strtolower($dmiValue);
142+
return strtolower(trim($dmiValue));
138143
}
139144
}

src/ResourceDetectors/DigitalOcean/tests/Unit/DigitalOceanDetectorTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
use PHPUnit\Framework\Attributes\CoversClass;
1818
use PHPUnit\Framework\TestCase;
1919
use Psr\Http\Message\RequestInterface;
20-
use Zalas\PHPUnit\Globals\Attribute\Env;
20+
use Zalas\PHPUnit\Globals\Attribute\Server;
2121

2222
function file_get_contents(string $filename)
2323
{
24-
if (($_ENV['FAIL_IO'] ?? 'false') === 'true') {
24+
if (($_SERVER['FAIL_IO'] ?? 'false') === 'true') {
2525
return false;
2626
}
2727
if (str_starts_with($filename, '/sys/devices/virtual/dmi/id')) {
2828
return match (substr($filename, 28)) {
29-
'sys_vendor' => (($_ENV['WRONG_VENDOR'] ?? 'false') === 'true') ? 'AWS' : 'DigitalOcean',
29+
'sys_vendor' => (($_SERVER['WRONG_VENDOR'] ?? 'false') === 'true') ? 'AWS' : 'DigitalOcean',
3030
'product_family' => 'DigitalOcean_Droplet',
3131
'board_asset_tag' => '10000000',
3232
default => 'unknown'
@@ -56,8 +56,8 @@ public function setUp(): void
5656
$client = $this->createStub(Psr18Client::class);
5757
$client->method('sendRequest')->willReturnCallback(function (RequestInterface $request) use ($responseFactory) {
5858
if ((string) $request->getUri() === 'http://169.254.169.254/metadata/v1.json') {
59-
if (($_ENV['FAIL_METADATA'] ?? 'false') === 'true') {
60-
throw new Exception();
59+
if (($_SERVER['FAIL_METADATA'] ?? 'false') === 'true') {
60+
return $responseFactory->createResponse(500);
6161
}
6262

6363
/** @psalm-suppress PossiblyFalseArgument */
@@ -107,7 +107,7 @@ public function test_droplet_attributes_with_no_authz()
107107
);
108108
}
109109

110-
#[Env('DIGITALOCEAN_ACCESS_TOKEN', 'scoped-for-account')]
110+
#[Server('DIGITALOCEAN_ACCESS_TOKEN', 'scoped-for-account')]
111111
public function test_droplet_attributes_with_account_only_authz()
112112
{
113113
$this->assertEquals(
@@ -126,12 +126,12 @@ public function test_droplet_attributes_with_account_only_authz()
126126
);
127127
/** @psalm-suppress PossiblyFalseArgument */
128128
$this->assertStringContainsString(
129-
'DigitalOcean Access Token found, but unable to get Droplet info',
129+
'DigitalOcean Access Token found; setting DigitalOcean account ID in resource attributes',
130130
stream_get_contents($this->errorLog)
131131
);
132132
}
133133

134-
#[Env('DIGITALOCEAN_ACCESS_TOKEN', 'scoped-for-droplet')]
134+
#[Server('DIGITALOCEAN_ACCESS_TOKEN', 'scoped-for-droplet')]
135135
public function test_droplet_attributes_with_droplet_only_authz()
136136
{
137137
$this->assertEquals(
@@ -153,12 +153,12 @@ public function test_droplet_attributes_with_droplet_only_authz()
153153
);
154154
/** @psalm-suppress PossiblyFalseArgument */
155155
$this->assertStringContainsString(
156-
'DigitalOcean Access Token found, but unable to get account info',
156+
'DigitalOcean Access Token found; setting additional Droplet info in resource attributes',
157157
stream_get_contents($this->errorLog)
158158
);
159159
}
160160

161-
#[Env('FAIL_METADATA', 'true')]
161+
#[Server('FAIL_METADATA', 'true')]
162162
public function test_no_droplet_attributes()
163163
{
164164
$this->assertEquals(
@@ -172,7 +172,7 @@ public function test_no_droplet_attributes()
172172
);
173173
}
174174

175-
#[Env('WRONG_VENDOR', 'true')]
175+
#[Server('WRONG_VENDOR', 'true')]
176176
public function test_not_digitalocean_environment()
177177
{
178178
$this->assertEquals(
@@ -186,7 +186,7 @@ public function test_not_digitalocean_environment()
186186
);
187187
}
188188

189-
#[Env('FAIL_IO', 'true')]
189+
#[Server('FAIL_IO', 'true')]
190190
public function test_not_linux()
191191
{
192192
$this->assertEquals(

0 commit comments

Comments
 (0)