Skip to content

Commit b88816a

Browse files
Merge master into feature/ui-e2e-tests
2 parents 54a8b98 + 2f39da8 commit b88816a

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

packages/core/src/shared/extensionUtilities.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,17 @@ export function isSageMaker(appName: 'SMAI' | 'SMUS' = 'SMAI'): boolean {
194194
}
195195

196196
export function isCn(): boolean {
197-
return getComputeRegion()?.startsWith('cn') ?? false
197+
try {
198+
const region = getComputeRegion()
199+
if (!region || region === 'notInitialized') {
200+
getLogger().debug('isCn called before compute region initialized, defaulting to false')
201+
return false
202+
}
203+
return region.startsWith('cn')
204+
} catch (err) {
205+
getLogger().error(`Error in isCn method: ${err}`)
206+
return false
207+
}
198208
}
199209

200210
/**

packages/core/src/test/shared/extensionUtilities.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AWSError } from 'aws-sdk'
99
import * as sinon from 'sinon'
1010
import { DefaultEc2MetadataClient } from '../../shared/clients/ec2MetadataClient'
1111
import * as vscode from 'vscode'
12-
import { UserActivity, getComputeRegion, initializeComputeRegion } from '../../shared/extensionUtilities'
12+
import { UserActivity, getComputeRegion, initializeComputeRegion, isCn } from '../../shared/extensionUtilities'
1313
import { isDifferentVersion, setMostRecentVersion } from '../../shared/extensionUtilities'
1414
import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
1515
import { extensionVersion } from '../../shared/vscode/env'
@@ -135,6 +135,73 @@ describe('initializeComputeRegion, getComputeRegion', async function () {
135135
})
136136
})
137137

138+
describe('isCn', function () {
139+
let sandbox: sinon.SinonSandbox
140+
const metadataService = new DefaultEc2MetadataClient()
141+
142+
beforeEach(function () {
143+
sandbox = sinon.createSandbox()
144+
})
145+
146+
afterEach(function () {
147+
sandbox.restore()
148+
})
149+
150+
it('returns false when compute region is not defined', async function () {
151+
// Reset the compute region to undefined first
152+
const utils = require('../../shared/extensionUtilities')
153+
Object.defineProperty(utils, 'computeRegion', {
154+
value: undefined,
155+
configurable: true,
156+
})
157+
158+
const result = isCn()
159+
160+
assert.strictEqual(result, false, 'isCn() should return false when compute region is undefined')
161+
})
162+
163+
it('returns false when compute region is not initialized', async function () {
164+
// Set the compute region to "notInitialized"
165+
const utils = require('../../shared/extensionUtilities')
166+
Object.defineProperty(utils, 'computeRegion', {
167+
value: 'notInitialized',
168+
configurable: true,
169+
})
170+
171+
const result = isCn()
172+
173+
assert.strictEqual(result, false, 'isCn() should return false when compute region is notInitialized')
174+
})
175+
176+
it('returns true for CN regions', async function () {
177+
sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'cn-north-1' })
178+
await initializeComputeRegion(metadataService, false, true)
179+
180+
const result = isCn()
181+
182+
assert.strictEqual(result, true, 'isCn() should return true for China regions')
183+
})
184+
185+
it('returns false for non-CN regions', async function () {
186+
sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'us-east-1' })
187+
await initializeComputeRegion(metadataService, false, true)
188+
189+
const result = isCn()
190+
191+
assert.strictEqual(result, false, 'isCn() should return false for non-China regions')
192+
})
193+
194+
it('returns false when an error occurs', async function () {
195+
const utils = require('../../shared/extensionUtilities')
196+
197+
sandbox.stub(utils, 'getComputeRegion').throws(new Error('Test error'))
198+
199+
const result = isCn()
200+
201+
assert.strictEqual(result, false, 'isCn() should return false when an error occurs')
202+
})
203+
})
204+
138205
describe('UserActivity', function () {
139206
let count: number
140207
let sandbox: sinon.SinonSandbox

0 commit comments

Comments
 (0)