Skip to content

Commit

Permalink
test: [OSM-2368] more robust test case for python acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gemaxim committed Jan 9, 2025
1 parent fb1f7ee commit 21b802f
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .circleci/chocolatey.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<package id="maven" version="3.8.2" />
<package id="make" version="4.4.1" />
<package id="gradle" version="8.1.1" />
<package id="python3" version="3.11.4" />
<package id="python3" version="3.12.8" />
<package id="dotnet-sdk" version="8.0.100" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pandas @ https://files.pythonhosted.org/packages/c6/2a/4bba3f03f7d07207481fed47f5b35f556c7441acddc368ec43d6643c5777/pandas-2.2.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl ; python_version=="3.12"
pandas @ https://files.pythonhosted.org/packages/f5/94/6c79b07f0e5aab1dcfa35a75f4817f5c4f677931d4234afcd75f0e6a66ca/pandas-2.2.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl ; python_version=="3.13"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r ./module/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pandas @ https://files.pythonhosted.org/packages/e1/0c/ad295fd74bfac85358fd579e271cded3ac969de81f62dd0142c426b9da91/pandas-2.2.3-cp312-cp312-macosx_11_0_arm64.whl ; python_version=="3.12"
pandas @ https://files.pythonhosted.org/packages/e4/93/b3f5d1838500e22c8d793625da672f3eec046b1a99257666c94446969282/pandas-2.2.3-cp313-cp313-macosx_11_0_arm64.whl ; python_version=="3.13"
1 change: 1 addition & 0 deletions test/acceptance/workspaces/pip-app-macos/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r ./module/requirements.txt
1 change: 1 addition & 0 deletions test/acceptance/workspaces/pip-app-robust/constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.31.0
1 change: 1 addition & 0 deletions test/acceptance/workspaces/pip-app-robust/req.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oauth2==1.1.3
11 changes: 11 additions & 0 deletions test/acceptance/workspaces/pip-app-robust/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--trusted-host pypi.fury.io

Jinja2==2.7.2
git+https://github.com/snyk-fixtures/[email protected]#egg=simple==v1.0.0
-e git+https://github.com/snyk-fixtures/python-pypi-package-sample-subdir#egg=sample&subdirectory=subdir
https://files.pythonhosted.org/packages/d0/d7/4806fd165c27716f02a3a9c23d207854b8a3ed884db53c2781b92bd8d4f4/whl.setup-0.2-py2.py3-none-any.whl
requests; python_version >= "3.8.dev0"
pydantic[email]==1.9.0
djangorestframework @ git+https://github.com/encode/django-rest-framework.git@1db19f4b2d1500894448634852e02f73043382e4
-r req.txt
-c constraints.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pandas @ https://files.pythonhosted.org/packages/52/11/9eac327a38834f162b8250aab32a6781339c69afe7574368fffe46387edf/pandas-2.2.3-cp311-cp311-macosx_11_0_arm64.whl ; python_version=="3.12"
pandas @ https://files.pythonhosted.org/packages/3b/bc/4b18e2b8c002572c5a441a64826252ce5da2aa738855747247a971988043/pandas-2.2.3-cp313-cp313-win_amd64.whl ; python_version=="3.13"

git commit -m "test: [OSM-2368] python acceptance tests specific to windows"
2 changes: 2 additions & 0 deletions test/acceptance/workspaces/pip-app-windows/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pywin32; sys_platform == 'win32'
-r module\requirements.txt
11 changes: 9 additions & 2 deletions test/jest/acceptance/snyk-test/basic-test-all-languages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,15 @@ describe('`snyk test` of basic projects for each language/ecosystem', () => {
expect(code).toEqual(0);
});

test('run `snyk test` on a python project', async () => {
const project = await createProjectFromWorkspace('pip-app');
test.each([
{
fixture: 'pip-app',
},
{
fixture: 'pip-app-robust',
},
])('run `snyk test` on a python project $fixture', async ({ fixture }) => {
const project = await createProjectFromWorkspace(fixture);
let pythonCommand = 'python';

await runCommand(pythonCommand, ['--version']).catch(function () {
Expand Down
107 changes: 107 additions & 0 deletions test/jest/acceptance/snyk-test/test-python-os-specific.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { createProjectFromWorkspace } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';
import { fakeServer } from '../../../acceptance/fake-server';
import { runCommand } from '../../util/runCommand';
import { isDontSkipTestsEnabled } from '../../util/isDontSkipTestsEnabled';
import { getServerPort } from '../../util/getServerPort';
import * as os from 'os';

jest.setTimeout(1000 * 60);

const PLATFORM_FIXTURES_MAPS = {
win32_amd64: 'pip-app-windows',
darwin_arm64: 'pip-app-macos',
linux_arm64: 'pip-app-linux-arm64',
// linux_amd64: 'pip-app-linux-amd64',
// alpine_amd64: 'pip-app-alpine-amd64',
// alpine_arm64: 'pip-app-alpine-arm64',
};

describe('`snyk test` of python projects with OS specific dependencies', () => {
let server;
let env: Record<string, string>;
let dontSkip: boolean;

beforeAll((done) => {
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
SNYK_API: 'http://localhost:' + port + baseApi,
SNYK_HOST: 'http://localhost:' + port,
SNYK_TOKEN: '123456789',
SNYK_DISABLE_ANALYTICS: '1',
};
server = fakeServer(baseApi, env.SNYK_TOKEN);
server.listen(port, () => {
done();
});
dontSkip = isDontSkipTestsEnabled();
console.debug("Don't skip tests: " + dontSkip);
});

afterEach(() => {
jest.resetAllMocks();
server.restore();
});

afterAll((done) => {
server.close(() => {
done();
});
});

test('run `snyk test` on python project $fixture on the corresponding platform', async () => {
const currentPlatform = os.platform() + '_' + os.arch();

if (!(currentPlatform in PLATFORM_FIXTURES_MAPS)) {
return;
}

const fixture = PLATFORM_FIXTURES_MAPS[currentPlatform];

const project = await createProjectFromWorkspace(fixture);
let pythonCommand = 'python';

await runCommand(pythonCommand, ['--version']).catch(function () {
pythonCommand = 'python3';
});

console.debug('Using: ' + pythonCommand);
let pipResult = await runCommand(
pythonCommand,
[
'-m',
'pip',
'install',
'-r',
'requirements.txt',
'--break-system-packages',
],
{
shell: true,
cwd: project.path(),
},
);

if (pipResult && pipResult.code != 0) {
pipResult = await runCommand(
pythonCommand,
['-m', 'pip', 'install', '-r', 'requirements.txt'],
{
shell: true,
cwd: project.path(),
},
);
}

expect(pipResult.code).toEqual(0);

const { code } = await runSnykCLI('test -d --command=' + pythonCommand, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);
});
});

0 comments on commit 21b802f

Please sign in to comment.