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 1 commit
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
17 changes: 11 additions & 6 deletions packages/core/src/shared/regions/regionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,20 @@ export class RegionProvider {
remote: () => Endpoints | Promise<Endpoints>
}): RegionProvider {
const instance = new this()
void instance.init(endpointsProvider)
return instance
}

async function load() {
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>
}) {
const load = async () => {
getLogger().info('endpoints: retrieving AWS endpoints data')
instance.loadFromEndpoints(await endpointsProvider.local())
this.loadFromEndpoints(await endpointsProvider.local())

try {
instance.loadFromEndpoints(await endpointsProvider.remote())
this.loadFromEndpoints(await endpointsProvider.remote())
} catch (err) {
getLogger().warn(
`endpoints: failed to load from remote source, region data may appear outdated: %s`,
Expand All @@ -177,7 +184,7 @@ export class RegionProvider {
}
}

load().catch((err) => {
return load().catch((err) => {
getLogger().error('Failure while loading Endpoints Manifest: %s', err)

return vscode.window.showErrorMessage(
Expand All @@ -191,8 +198,6 @@ export class RegionProvider {
)}`
)
})

return instance
}
}

Expand Down
46 changes: 46 additions & 0 deletions packages/core/src/testInteg/regionProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*!
* 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 { makeTemporaryToolkitFolder } from '../shared/filesystemUtilities'
import { fs } from '../shared/fs/fs'
import path from 'path'

describe('Region Provider', async function () {
let tempDir: string

before(async () => {
tempDir = await makeTemporaryToolkitFolder()
jpinkney-aws marked this conversation as resolved.
Show resolved Hide resolved
})

after(async () => {
await fs.delete(tempDir, {
recursive: true,
force: true,
})
})

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
*/
const filePath = path.join(tempDir, 'foo.json')
await fs.writeFile(filePath, '{}')
globals.manifestPaths.endpoints = filePath

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)
})
})
})