Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(toolkit): Add region provider remote test #6425

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions packages/core/src/shared/regions/regionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,30 @@ export class RegionProvider {
remote: () => Endpoints | Promise<Endpoints>
}): RegionProvider {
const instance = new this()
void instance.init(endpointsProvider)
return instance
}

async function load() {
getLogger().info('endpoints: retrieving AWS endpoints data')
instance.loadFromEndpoints(await endpointsProvider.local())

try {
instance.loadFromEndpoints(await endpointsProvider.remote())
} catch (err) {
getLogger().warn(
`endpoints: failed to load from remote source, region data may appear outdated: %s`,
err
)
}
async init(endpointsProvider: {
Copy link
Contributor

@nkomonen-amazon nkomonen-amazon Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we switch this to a try/catch at this point, dropping the const load

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just played around with this. let me know what you think

local: () => Endpoints | Promise<Endpoints>
remote: () => Endpoints | Promise<Endpoints>
}) {
getLogger().info('endpoints: retrieving AWS endpoints data')

try {
this.loadFromEndpoints(await endpointsProvider.local())
} catch (err) {
getLogger().warn(`endpoints: failed to load from local source: %s`, err)
}

load().catch((err) => {
getLogger().error('Failure while loading Endpoints Manifest: %s', err)
try {
this.loadFromEndpoints(await endpointsProvider.remote())
} catch (err) {
getLogger().warn(`endpoints: failed to load from remote source, region data may appear outdated: %s`, err)
}

return vscode.window.showErrorMessage(
if (this.getRegions().length === 0) {
void vscode.window.showErrorMessage(
`${localize(
'AWS.error.endpoint.load.failure',
'The {0} Toolkit was unable to load endpoints data.',
Expand All @@ -190,9 +195,7 @@ export class RegionProvider {
'Toolkit functionality may be impacted until VS Code is restarted.'
)}`
)
})

return instance
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/testInteg/regionProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import assert from 'assert'
import { makeEndpointsProvider } from '../extension'
import { RegionProvider } from '../shared/regions/regionProvider'
import globals from '../shared/extensionGlobals'
import path from 'path'
import { TestFolder } from '../test/testUtil'

describe('Region Provider', async function () {
let tempFolder: TestFolder

before(async () => {
tempFolder = await TestFolder.create()
})

it('resolves from remote', async function () {
Copy link
Contributor

@nkomonen-amazon nkomonen-amazon Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice and simple test 🎉

/**
* Make sure the local file doesn't resolve to any endpoints.
* That way we can make sure remote contents are fetched
*/
await tempFolder.write('foo.json', '{}')
globals.manifestPaths.endpoints = path.join(tempFolder.path, 'foo.json')

await assert.doesNotReject(async () => {
const endpointProvider = makeEndpointsProvider()
const regionProvider = new RegionProvider()
await regionProvider.init(endpointProvider)

// regions loaded from the remote
assert.ok(regionProvider.getRegions().length > 0)
})
})
})
Loading