Skip to content

Commit ff7e0c8

Browse files
authored
fix: update IMDS tests to avoid bugs when running on machines with EC2 metadata disabled (#858)
1 parent 4f00e77 commit ff7e0c8

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/auth/credentials/ImdsCredentialsProviderTest.kt

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ import kotlin.test.assertIs
4040
import kotlin.test.assertNotEquals
4141
import kotlin.time.Duration.Companion.minutes
4242

43+
private val ec2MetadataDisabledPlatform = TestPlatformProvider(
44+
env = mapOf(AwsSdkSetting.AwsEc2MetadataDisabled.environmentVariable to "true"),
45+
)
46+
private val ec2MetadataEnabledPlatform = TestPlatformProvider()
47+
4348
@OptIn(ExperimentalCoroutinesApi::class)
4449
class ImdsCredentialsProviderTest {
4550

4651
@Test
4752
fun testImdsDisabled() = runTest {
48-
val platform = TestPlatformProvider(
49-
env = mapOf(AwsSdkSetting.AwsEc2MetadataDisabled.environmentVariable to "true"),
50-
)
53+
val platform = ec2MetadataDisabledPlatform
5154
val provider = ImdsCredentialsProvider(platformProvider = platform)
5255
assertFailsWith<CredentialsNotLoadedException> {
5356
provider.getCredentials()
@@ -91,7 +94,11 @@ class ImdsCredentialsProviderTest {
9194
clock = testClock
9295
}
9396

94-
val provider = ImdsCredentialsProvider(client = lazyOf(client), clock = testClock)
97+
val provider = ImdsCredentialsProvider(
98+
client = lazyOf(client),
99+
clock = testClock,
100+
platformProvider = ec2MetadataEnabledPlatform,
101+
)
95102

96103
val actual = provider.getCredentials()
97104
val expected = Credentials(
@@ -138,7 +145,12 @@ class ImdsCredentialsProviderTest {
138145
clock = testClock
139146
}
140147

141-
val provider = ImdsCredentialsProvider(profileOverride = "imds-test-role", client = lazyOf(client), clock = testClock)
148+
val provider = ImdsCredentialsProvider(
149+
profileOverride = "imds-test-role",
150+
client = lazyOf(client),
151+
clock = testClock,
152+
platformProvider = ec2MetadataEnabledPlatform,
153+
)
142154

143155
val actual = provider.getCredentials()
144156
val expected = Credentials(
@@ -167,7 +179,11 @@ class ImdsCredentialsProviderTest {
167179
clock = testClock
168180
}
169181

170-
val provider = ImdsCredentialsProvider(client = lazyOf(client), clock = testClock)
182+
val provider = ImdsCredentialsProvider(
183+
client = lazyOf(client),
184+
clock = testClock,
185+
platformProvider = ec2MetadataEnabledPlatform,
186+
)
171187

172188
val ex = assertFailsWith<CredentialsProviderException> {
173189
provider.getCredentials()
@@ -213,7 +229,11 @@ class ImdsCredentialsProviderTest {
213229
clock = testClock
214230
}
215231

216-
val provider = ImdsCredentialsProvider(client = lazyOf(client), clock = testClock)
232+
val provider = ImdsCredentialsProvider(
233+
client = lazyOf(client),
234+
clock = testClock,
235+
platformProvider = ec2MetadataEnabledPlatform,
236+
)
217237

218238
assertFailsWith<CredentialsProviderException> {
219239
provider.getCredentials()
@@ -258,6 +278,7 @@ class ImdsCredentialsProviderTest {
258278
profileOverride = "imds-test-role",
259279
client = lazyOf(client),
260280
clock = testClock,
281+
platformProvider = ec2MetadataEnabledPlatform,
261282
)
262283

263284
val actual = provider.getCredentials()
@@ -312,6 +333,7 @@ class ImdsCredentialsProviderTest {
312333
profileOverride = "imds-test-role",
313334
client = lazyOf(client),
314335
clock = testClock,
336+
platformProvider = ec2MetadataEnabledPlatform,
315337
)
316338

317339
// call getCredentials 3 times
@@ -379,6 +401,7 @@ class ImdsCredentialsProviderTest {
379401
profileOverride = "imds-test-role",
380402
client = lazyOf(client),
381403
clock = testClock,
404+
platformProvider = ec2MetadataEnabledPlatform,
382405
)
383406

384407
val first = provider.getCredentials()
@@ -454,6 +477,7 @@ class ImdsCredentialsProviderTest {
454477
profileOverride = "imds-test-role",
455478
client = lazyOf(client),
456479
clock = testClock,
480+
platformProvider = ec2MetadataEnabledPlatform,
457481
)
458482

459483
// call the engine the first time to get a proper credentials response from IMDS
@@ -486,6 +510,7 @@ class ImdsCredentialsProviderTest {
486510
profileOverride = "imds-test-role",
487511
client = lazyOf(client),
488512
clock = testClock,
513+
platformProvider = ec2MetadataEnabledPlatform,
489514
)
490515

491516
// a read timeout should cause an exception to be thrown, because we have no previous credentials to re-serve
@@ -559,6 +584,7 @@ class ImdsCredentialsProviderTest {
559584
profileOverride = "imds-test-role",
560585
client = lazyOf(client),
561586
clock = testClock,
587+
platformProvider = ec2MetadataEnabledPlatform,
562588
)
563589

564590
// call the engine the first time to get a proper credentials response from IMDS
@@ -597,6 +623,7 @@ class ImdsCredentialsProviderTest {
597623
profileOverride = "imds-test-role",
598624
client = lazyOf(client),
599625
clock = testClock,
626+
platformProvider = ec2MetadataEnabledPlatform,
600627
)
601628

602629
assertFailsWith<CredentialsProviderException> {

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/region/ImdsRegionProviderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ImdsRegionProviderTest {
4949
clock = testClock
5050
}
5151

52-
val provider = ImdsRegionProvider(client = lazyOf(client))
52+
val provider = ImdsRegionProvider(client = lazyOf(client), platformProvider = TestPlatformProvider())
5353
assertEquals("us-east-2", provider.getRegion())
5454
connection.assertRequests()
5555

0 commit comments

Comments
 (0)