|
5 | 5 |
|
6 | 6 | import assert from 'assert' |
7 | 7 | import path from 'path' |
8 | | -import { isCloudDesktop, getEnvVars, getServiceEnvVarConfig, isAmazonLinux2, isBeta } from '../../../shared/vscode/env' |
| 8 | +import { |
| 9 | + isCloudDesktop, |
| 10 | + getEnvVars, |
| 11 | + getServiceEnvVarConfig, |
| 12 | + isAmazonLinux2, |
| 13 | + isBeta, |
| 14 | + hasSageMakerEnvVars, |
| 15 | +} from '../../../shared/vscode/env' |
9 | 16 | import { ChildProcess } from '../../../shared/utilities/processUtils' |
10 | 17 | import * as sinon from 'sinon' |
11 | 18 | import os from 'os' |
12 | 19 | import fs from '../../../shared/fs/fs' |
13 | 20 | import vscode from 'vscode' |
14 | 21 | import { getComputeEnvType } from '../../../shared/telemetry/util' |
| 22 | +import * as globals from '../../../shared/extensionGlobals' |
15 | 23 |
|
16 | 24 | describe('env', function () { |
17 | 25 | // create a sinon sandbox instance and instantiate in a beforeEach |
@@ -97,18 +105,225 @@ describe('env', function () { |
97 | 105 | assert.strictEqual(isBeta(), expected) |
98 | 106 | }) |
99 | 107 |
|
100 | | - it('isAmazonLinux2', function () { |
101 | | - sandbox.stub(process, 'platform').value('linux') |
102 | | - const versionStub = stubOsVersion('5.10.220-188.869.amzn2int.x86_64') |
| 108 | + describe('isAmazonLinux2', function () { |
| 109 | + let fsExistsStub: sinon.SinonStub |
| 110 | + let fsReadFileStub: sinon.SinonStub |
| 111 | + let isWebStub: sinon.SinonStub |
| 112 | + let platformStub: sinon.SinonStub |
| 113 | + let osReleaseStub: sinon.SinonStub |
| 114 | + let moduleLoadStub: sinon.SinonStub |
| 115 | + |
| 116 | + beforeEach(function () { |
| 117 | + // Default stubs |
| 118 | + platformStub = sandbox.stub(process, 'platform').value('linux') |
| 119 | + osReleaseStub = stubOsVersion('5.10.220-188.869.amzn2int.x86_64') |
| 120 | + isWebStub = sandbox.stub(globals, 'isWeb').returns(false) |
| 121 | + |
| 122 | + // Mock fs module |
| 123 | + const fsMock = { |
| 124 | + existsSync: sandbox.stub().returns(false), |
| 125 | + readFileSync: sandbox.stub().returns(''), |
| 126 | + } |
| 127 | + fsExistsStub = fsMock.existsSync |
| 128 | + fsReadFileStub = fsMock.readFileSync |
| 129 | + |
| 130 | + // Stub Module._load to intercept require calls |
| 131 | + const Module = require('module') |
| 132 | + moduleLoadStub = sandbox.stub(Module, '_load').callThrough() |
| 133 | + moduleLoadStub.withArgs('fs').returns(fsMock) |
| 134 | + }) |
| 135 | + |
| 136 | + it('returns false in web environment', function () { |
| 137 | + isWebStub.returns(true) |
| 138 | + assert.strictEqual(isAmazonLinux2(), false) |
| 139 | + }) |
| 140 | + |
| 141 | + it('returns false in SageMaker environment with SAGEMAKER_APP_TYPE', function () { |
| 142 | + sandbox.stub(process.env, 'SAGEMAKER_APP_TYPE').value('JupyterLab') |
| 143 | + assert.strictEqual(isAmazonLinux2(), false) |
| 144 | + }) |
| 145 | + |
| 146 | + it('returns false in SageMaker environment with SM_APP_TYPE', function () { |
| 147 | + sandbox.stub(process.env, 'SM_APP_TYPE').value('JupyterLab') |
| 148 | + assert.strictEqual(isAmazonLinux2(), false) |
| 149 | + }) |
| 150 | + |
| 151 | + it('returns false in SageMaker environment with SERVICE_NAME', function () { |
| 152 | + sandbox.stub(process.env, 'SERVICE_NAME').value('SageMakerUnifiedStudio') |
| 153 | + assert.strictEqual(isAmazonLinux2(), false) |
| 154 | + }) |
| 155 | + |
| 156 | + it('returns false when /etc/os-release indicates Ubuntu in container', function () { |
| 157 | + fsExistsStub.returns(true) |
| 158 | + fsReadFileStub.returns(` |
| 159 | +NAME="Ubuntu" |
| 160 | +VERSION="20.04.6 LTS (Focal Fossa)" |
| 161 | +ID=ubuntu |
| 162 | +ID_LIKE=debian |
| 163 | +PRETTY_NAME="Ubuntu 20.04.6 LTS" |
| 164 | +VERSION_ID="20.04" |
| 165 | + `) |
| 166 | + |
| 167 | + // Even with AL2 kernel (host is AL2), should return false (container is Ubuntu) |
| 168 | + assert.strictEqual(isAmazonLinux2(), false) |
| 169 | + }) |
| 170 | + |
| 171 | + it('returns false when /etc/os-release indicates Amazon Linux 2023', function () { |
| 172 | + fsExistsStub.returns(true) |
| 173 | + fsReadFileStub.returns(` |
| 174 | +NAME="Amazon Linux" |
| 175 | +VERSION="2023" |
| 176 | +ID="amzn" |
| 177 | +ID_LIKE="fedora" |
| 178 | +VERSION_ID="2023" |
| 179 | +PLATFORM_ID="platform:al2023" |
| 180 | +PRETTY_NAME="Amazon Linux 2023" |
| 181 | + `) |
| 182 | + |
| 183 | + assert.strictEqual(isAmazonLinux2(), false) |
| 184 | + }) |
| 185 | + |
| 186 | + it('returns true when /etc/os-release indicates Amazon Linux 2', function () { |
| 187 | + fsExistsStub.returns(true) |
| 188 | + fsReadFileStub.returns(` |
| 189 | +NAME="Amazon Linux 2" |
| 190 | +VERSION="2" |
| 191 | +ID="amzn" |
| 192 | +ID_LIKE="centos rhel fedora" |
| 193 | +VERSION_ID="2" |
| 194 | +PRETTY_NAME="Amazon Linux 2" |
| 195 | + `) |
| 196 | + |
| 197 | + assert.strictEqual(isAmazonLinux2(), true) |
| 198 | + }) |
| 199 | + |
| 200 | + it('returns true when /etc/os-release has ID="amzn" and VERSION_ID="2"', function () { |
| 201 | + fsExistsStub.returns(true) |
| 202 | + fsReadFileStub.returns(` |
| 203 | +NAME="Amazon Linux" |
| 204 | +VERSION="2" |
| 205 | +ID="amzn" |
| 206 | +VERSION_ID="2" |
| 207 | + `) |
| 208 | + |
| 209 | + assert.strictEqual(isAmazonLinux2(), true) |
| 210 | + }) |
| 211 | + |
| 212 | + it('returns false when /etc/os-release indicates CentOS', function () { |
| 213 | + fsExistsStub.returns(true) |
| 214 | + fsReadFileStub.returns(` |
| 215 | +NAME="CentOS Linux" |
| 216 | +VERSION="7 (Core)" |
| 217 | +ID="centos" |
| 218 | +ID_LIKE="rhel fedora" |
| 219 | +VERSION_ID="7" |
| 220 | + `) |
| 221 | + |
| 222 | + // Even with AL2 kernel |
| 223 | + assert.strictEqual(isAmazonLinux2(), false) |
| 224 | + }) |
| 225 | + |
| 226 | + it('falls back to kernel check when /etc/os-release does not exist', function () { |
| 227 | + fsExistsStub.returns(false) |
103 | 228 |
|
104 | | - // Test with actual AL2 kernel |
105 | | - assert.strictEqual(isAmazonLinux2(), true) |
| 229 | + // Test with AL2 kernel |
| 230 | + assert.strictEqual(isAmazonLinux2(), true) |
106 | 231 |
|
107 | | - versionStub.returns('5.10.236-227.928.amzn2.x86_64') |
108 | | - assert.strictEqual(isAmazonLinux2(), true) |
| 232 | + // Test with non-AL2 kernel |
| 233 | + osReleaseStub.returns('5.10.220-188.869.NOT_INTERNAL.x86_64') |
| 234 | + assert.strictEqual(isAmazonLinux2(), false) |
| 235 | + }) |
| 236 | + |
| 237 | + it('falls back to kernel check when /etc/os-release read fails', function () { |
| 238 | + fsExistsStub.returns(true) |
| 239 | + fsReadFileStub.throws(new Error('Permission denied')) |
| 240 | + |
| 241 | + // Should fall back to kernel check |
| 242 | + assert.strictEqual(isAmazonLinux2(), true) |
| 243 | + }) |
| 244 | + |
| 245 | + it('returns true with .amzn2. kernel pattern', function () { |
| 246 | + fsExistsStub.returns(false) |
| 247 | + osReleaseStub.returns('5.10.236-227.928.amzn2.x86_64') |
| 248 | + assert.strictEqual(isAmazonLinux2(), true) |
| 249 | + }) |
| 250 | + |
| 251 | + it('returns true with .amzn2int. kernel pattern', function () { |
| 252 | + fsExistsStub.returns(false) |
| 253 | + osReleaseStub.returns('5.10.220-188.869.amzn2int.x86_64') |
| 254 | + assert.strictEqual(isAmazonLinux2(), true) |
| 255 | + }) |
| 256 | + |
| 257 | + it('returns false with non-AL2 kernel', function () { |
| 258 | + fsExistsStub.returns(false) |
| 259 | + osReleaseStub.returns('5.15.0-91-generic') |
| 260 | + assert.strictEqual(isAmazonLinux2(), false) |
| 261 | + }) |
| 262 | + |
| 263 | + it('returns false on non-Linux platforms', function () { |
| 264 | + platformStub.value('darwin') |
| 265 | + fsExistsStub.returns(false) |
| 266 | + assert.strictEqual(isAmazonLinux2(), false) |
| 267 | + |
| 268 | + platformStub.value('win32') |
| 269 | + assert.strictEqual(isAmazonLinux2(), false) |
| 270 | + }) |
| 271 | + |
| 272 | + it('returns false when container OS is different from host OS', function () { |
| 273 | + // Scenario: Host is AL2 (kernel shows AL2) but container is Ubuntu |
| 274 | + fsExistsStub.returns(true) |
| 275 | + fsReadFileStub.returns(` |
| 276 | +NAME="Ubuntu" |
| 277 | +VERSION="22.04" |
| 278 | +ID=ubuntu |
| 279 | +VERSION_ID="22.04" |
| 280 | + `) |
| 281 | + osReleaseStub.returns('5.10.220-188.869.amzn2int.x86_64') // AL2 kernel from host |
| 282 | + |
| 283 | + // Should trust container OS over kernel |
| 284 | + assert.strictEqual(isAmazonLinux2(), false) |
| 285 | + }) |
| 286 | + }) |
| 287 | + |
| 288 | + describe('hasSageMakerEnvVars', function () { |
| 289 | + afterEach(function () { |
| 290 | + // Clean up environment variables |
| 291 | + delete process.env.SAGEMAKER_APP_TYPE |
| 292 | + delete process.env.SAGEMAKER_INTERNAL_IMAGE_URI |
| 293 | + delete process.env.STUDIO_LOGGING_DIR |
| 294 | + delete process.env.SM_APP_TYPE |
| 295 | + delete process.env.SM_INTERNAL_IMAGE_URI |
| 296 | + delete process.env.SERVICE_NAME |
| 297 | + }) |
| 298 | + |
| 299 | + it('returns true when SAGEMAKER_APP_TYPE is set', function () { |
| 300 | + process.env.SAGEMAKER_APP_TYPE = 'JupyterLab' |
| 301 | + assert.strictEqual(hasSageMakerEnvVars(), true) |
| 302 | + }) |
| 303 | + |
| 304 | + it('returns true when SM_APP_TYPE is set', function () { |
| 305 | + process.env.SM_APP_TYPE = 'JupyterLab' |
| 306 | + assert.strictEqual(hasSageMakerEnvVars(), true) |
| 307 | + }) |
109 | 308 |
|
110 | | - versionStub.returns('5.10.220-188.869.NOT_INTERNAL.x86_64') |
111 | | - assert.strictEqual(isAmazonLinux2(), false) |
| 309 | + it('returns true when SERVICE_NAME is SageMakerUnifiedStudio', function () { |
| 310 | + process.env.SERVICE_NAME = 'SageMakerUnifiedStudio' |
| 311 | + assert.strictEqual(hasSageMakerEnvVars(), true) |
| 312 | + }) |
| 313 | + |
| 314 | + it('returns true when STUDIO_LOGGING_DIR contains /var/log/studio', function () { |
| 315 | + process.env.STUDIO_LOGGING_DIR = '/var/log/studio/logs' |
| 316 | + assert.strictEqual(hasSageMakerEnvVars(), true) |
| 317 | + }) |
| 318 | + |
| 319 | + it('returns false when no SageMaker env vars are set', function () { |
| 320 | + assert.strictEqual(hasSageMakerEnvVars(), false) |
| 321 | + }) |
| 322 | + |
| 323 | + it('returns false when SERVICE_NAME is set but not SageMakerUnifiedStudio', function () { |
| 324 | + process.env.SERVICE_NAME = 'SomeOtherService' |
| 325 | + assert.strictEqual(hasSageMakerEnvVars(), false) |
| 326 | + }) |
112 | 327 | }) |
113 | 328 |
|
114 | 329 | it('isCloudDesktop', async function () { |
|
0 commit comments