@@ -12,6 +12,7 @@ import os from 'os'
1212import fs from '../../../shared/fs/fs'
1313import vscode from 'vscode'
1414import { getComputeEnvType } from '../../../shared/telemetry/util'
15+ import * as globals from '../../../shared/extensionGlobals'
1516
1617describe ( 'env' , function ( ) {
1718 // create a sinon sandbox instance and instantiate in a beforeEach
@@ -97,18 +98,183 @@ describe('env', function () {
9798 assert . strictEqual ( isBeta ( ) , expected )
9899 } )
99100
100- it ( 'isAmazonLinux2' , function ( ) {
101- sandbox . stub ( process , 'platform' ) . value ( 'linux' )
102- const versionStub = stubOsVersion ( '5.10.220-188.869.amzn2int.x86_64' )
101+ describe ( 'isAmazonLinux2' , function ( ) {
102+ let fsExistsStub : sinon . SinonStub
103+ let fsReadFileStub : sinon . SinonStub
104+ let isWebStub : sinon . SinonStub
105+ let platformStub : sinon . SinonStub
106+ let osReleaseStub : sinon . SinonStub
107+ let requireStub : sinon . SinonStub
108+
109+ beforeEach ( function ( ) {
110+ // Default stubs
111+ platformStub = sandbox . stub ( process , 'platform' ) . value ( 'linux' )
112+ osReleaseStub = stubOsVersion ( '5.10.220-188.869.amzn2int.x86_64' )
113+ isWebStub = sandbox . stub ( globals , 'isWeb' ) . returns ( false )
114+
115+ // Mock fs module
116+ const fsMock = {
117+ existsSync : sandbox . stub ( ) . returns ( false ) ,
118+ readFileSync : sandbox . stub ( ) . returns ( '' ) ,
119+ }
120+ fsExistsStub = fsMock . existsSync
121+ fsReadFileStub = fsMock . readFileSync
122+
123+ // Stub the global require function to return our mocked fs
124+ requireStub = sandbox . stub ( global , 'require' ) . callThrough ( )
125+ requireStub . withArgs ( 'fs' ) . returns ( fsMock )
126+ } )
103127
104- // Test with actual AL2 kernel
105- assert . strictEqual ( isAmazonLinux2 ( ) , true )
128+ it ( 'returns false in web environment' , function ( ) {
129+ isWebStub . returns ( true )
130+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
131+ } )
132+
133+ it ( 'returns false in SageMaker environment with SAGEMAKER_APP_TYPE' , function ( ) {
134+ sandbox . stub ( process . env , 'SAGEMAKER_APP_TYPE' ) . value ( 'JupyterLab' )
135+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
136+ } )
106137
107- versionStub . returns ( '5.10.236-227.928.amzn2.x86_64' )
108- assert . strictEqual ( isAmazonLinux2 ( ) , true )
138+ it ( 'returns false in SageMaker environment with SM_APP_TYPE' , function ( ) {
139+ sandbox . stub ( process . env , 'SM_APP_TYPE' ) . value ( 'JupyterLab' )
140+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
141+ } )
142+
143+ it ( 'returns false in SageMaker environment with SERVICE_NAME' , function ( ) {
144+ sandbox . stub ( process . env , 'SERVICE_NAME' ) . value ( 'SageMakerUnifiedStudio' )
145+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
146+ } )
109147
110- versionStub . returns ( '5.10.220-188.869.NOT_INTERNAL.x86_64' )
111- assert . strictEqual ( isAmazonLinux2 ( ) , false )
148+ it ( 'returns false when /etc/os-release indicates Ubuntu in container' , function ( ) {
149+ fsExistsStub . returns ( true )
150+ fsReadFileStub . returns ( `
151+ NAME="Ubuntu"
152+ VERSION="20.04.6 LTS (Focal Fossa)"
153+ ID=ubuntu
154+ ID_LIKE=debian
155+ PRETTY_NAME="Ubuntu 20.04.6 LTS"
156+ VERSION_ID="20.04"
157+ ` )
158+
159+ // Even with AL2 kernel (host is AL2), should return false (container is Ubuntu)
160+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
161+ } )
162+
163+ it ( 'returns false when /etc/os-release indicates Amazon Linux 2023' , function ( ) {
164+ fsExistsStub . returns ( true )
165+ fsReadFileStub . returns ( `
166+ NAME="Amazon Linux"
167+ VERSION="2023"
168+ ID="amzn"
169+ ID_LIKE="fedora"
170+ VERSION_ID="2023"
171+ PLATFORM_ID="platform:al2023"
172+ PRETTY_NAME="Amazon Linux 2023"
173+ ` )
174+
175+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
176+ } )
177+
178+ it ( 'returns true when /etc/os-release indicates Amazon Linux 2' , function ( ) {
179+ fsExistsStub . returns ( true )
180+ fsReadFileStub . returns ( `
181+ NAME="Amazon Linux 2"
182+ VERSION="2"
183+ ID="amzn"
184+ ID_LIKE="centos rhel fedora"
185+ VERSION_ID="2"
186+ PRETTY_NAME="Amazon Linux 2"
187+ ` )
188+
189+ assert . strictEqual ( isAmazonLinux2 ( ) , true )
190+ } )
191+
192+ it ( 'returns true when /etc/os-release has ID="amzn" and VERSION_ID="2"' , function ( ) {
193+ fsExistsStub . returns ( true )
194+ fsReadFileStub . returns ( `
195+ NAME="Amazon Linux"
196+ VERSION="2"
197+ ID="amzn"
198+ VERSION_ID="2"
199+ ` )
200+
201+ assert . strictEqual ( isAmazonLinux2 ( ) , true )
202+ } )
203+
204+ it ( 'returns false when /etc/os-release indicates CentOS' , function ( ) {
205+ fsExistsStub . returns ( true )
206+ fsReadFileStub . returns ( `
207+ NAME="CentOS Linux"
208+ VERSION="7 (Core)"
209+ ID="centos"
210+ ID_LIKE="rhel fedora"
211+ VERSION_ID="7"
212+ ` )
213+
214+ // Even with AL2 kernel
215+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
216+ } )
217+
218+ it ( 'falls back to kernel check when /etc/os-release does not exist' , function ( ) {
219+ fsExistsStub . returns ( false )
220+
221+ // Test with AL2 kernel
222+ assert . strictEqual ( isAmazonLinux2 ( ) , true )
223+
224+ // Test with non-AL2 kernel
225+ osReleaseStub . returns ( '5.10.220-188.869.NOT_INTERNAL.x86_64' )
226+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
227+ } )
228+
229+ it ( 'falls back to kernel check when /etc/os-release read fails' , function ( ) {
230+ fsExistsStub . returns ( true )
231+ fsReadFileStub . throws ( new Error ( 'Permission denied' ) )
232+
233+ // Should fall back to kernel check
234+ assert . strictEqual ( isAmazonLinux2 ( ) , true )
235+ } )
236+
237+ it ( 'returns true with .amzn2. kernel pattern' , function ( ) {
238+ fsExistsStub . returns ( false )
239+ osReleaseStub . returns ( '5.10.236-227.928.amzn2.x86_64' )
240+ assert . strictEqual ( isAmazonLinux2 ( ) , true )
241+ } )
242+
243+ it ( 'returns true with .amzn2int. kernel pattern' , function ( ) {
244+ fsExistsStub . returns ( false )
245+ osReleaseStub . returns ( '5.10.220-188.869.amzn2int.x86_64' )
246+ assert . strictEqual ( isAmazonLinux2 ( ) , true )
247+ } )
248+
249+ it ( 'returns false with non-AL2 kernel' , function ( ) {
250+ fsExistsStub . returns ( false )
251+ osReleaseStub . returns ( '5.15.0-91-generic' )
252+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
253+ } )
254+
255+ it ( 'returns false on non-Linux platforms' , function ( ) {
256+ platformStub . value ( 'darwin' )
257+ fsExistsStub . returns ( false )
258+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
259+
260+ platformStub . value ( 'win32' )
261+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
262+ } )
263+
264+ it ( 'returns false when container OS is different from host OS' , function ( ) {
265+ // Scenario: Host is AL2 (kernel shows AL2) but container is Ubuntu
266+ fsExistsStub . returns ( true )
267+ fsReadFileStub . returns ( `
268+ NAME="Ubuntu"
269+ VERSION="22.04"
270+ ID=ubuntu
271+ VERSION_ID="22.04"
272+ ` )
273+ osReleaseStub . returns ( '5.10.220-188.869.amzn2int.x86_64' ) // AL2 kernel from host
274+
275+ // Should trust container OS over kernel
276+ assert . strictEqual ( isAmazonLinux2 ( ) , false )
277+ } )
112278 } )
113279
114280 it ( 'isCloudDesktop' , async function ( ) {
0 commit comments