Skip to content

Commit

Permalink
test: refactor to introduce helper to determine port (#5137)
Browse files Browse the repository at this point in the history
* refactor: mark less recommended path as deprecated

* chore: introduce test helper to isolate port selection

* refactor: switch to using helper to determine port

We need to know about the port ahead of time so that we can
use it in our configuration. This supports iterating on fixed
ports to random ports so we can support running in parallel and
ensure no collision between our tests.

* chore: apply formatting
  • Loading branch information
thisislawatts authored Mar 26, 2024
1 parent a4b4dbb commit 3607eb3
Show file tree
Hide file tree
Showing 35 changed files with 72 additions and 33 deletions.
3 changes: 2 additions & 1 deletion test/jest/acceptance/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
createProjectFromWorkspace,
} from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 30);

describe('analytics module', () => {
let server;
let env: Record<string, string>;
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);

beforeAll((done) => {
const baseApi = '/api/v1';
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { fakeServer, getFirstIPv4Address } from '../../acceptance/fake-server';
import { runSnykCLI } from '../util/runSnykCLI';
import { getCliConfig, restoreCliConfig } from '../../acceptance/config-helper';
import { ciEnvs } from '../../../src/lib/is-ci';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -13,7 +14,7 @@ describe('Auth', () => {

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://' + getFirstIPv4Address() + ':' + apiPort + apiPath,
Expand Down
4 changes: 3 additions & 1 deletion test/jest/acceptance/cli-args.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { UnsupportedOptionCombinationError } from '../../../src/lib/errors/unsup
import { runSnykCLI } from '../util/runSnykCLI';
import { fakeServer } from '../../acceptance/fake-server';
import { createProject } from '../util/createProject';
import { getServerPort } from '../util/getServerPort';

import * as os from 'os';

const isWindows = os.platform().indexOf('win') === 0;
Expand All @@ -14,7 +16,7 @@ describe('cli args', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/cli-json-file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs';
import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { humanFileSize } from '../../utils';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -12,7 +13,7 @@ describe('test --json-file-output', () => {

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/cli-json-output.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fakeServer } from '../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../util/createProject';
import { getServerPort } from '../util/getServerPort';
import { runSnykCLI } from '../util/runSnykCLI';
import * as Parser from 'jsonparse';

Expand All @@ -11,7 +12,7 @@ describe('test --json', () => {

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/cli-token-precedence.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { runSnykCLI } from '../util/runSnykCLI';
import { fakeServer } from '../../acceptance/fake-server';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 30); // 30 seconds

Expand All @@ -8,7 +9,7 @@ describe('cli token precedence', () => {
let env: Record<string, string>;
let initialConfig: Record<string, string> = {};

const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
const initialEnvVars = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { createProjectFromWorkspace } from '../util/createProject';
import { getFixturePath } from '../util/getFixturePath';
import { runSnykCLI } from '../util/runSnykCLI';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 30);

Expand All @@ -18,7 +19,7 @@ describe('https', () => {
const ipaddress = getFirstIPv4Address();
console.log('Using ip: ' + ipaddress);

const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/oauth-token.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fakeServer } from '../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -10,7 +11,7 @@ describe('OAuth Token', () => {

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/print-graph.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fakeServer } from '../../acceptance/fake-server';
import { createProjectFromFixture } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { getServerPort } from '../util/getServerPort';
import * as path from 'path';

jest.setTimeout(1000 * 30);
Expand All @@ -10,7 +11,7 @@ describe('`test` command with `--print-graph` option', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/proxy-behavior.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TestCLI,
startSnykCLI,
} from '../../../test/jest/util/startSnykCLI';
import { getServerPort } from '../util/getServerPort';
import { unlink } from 'fs';
import { execSync } from 'child_process';
import * as os from 'os';
Expand Down Expand Up @@ -48,7 +49,7 @@ const containerName = 'proxy_authentication_container';
const hostnameFakeServer = 'host.docker.internal';
const hostnameProxy = 'proxy.snyk.local';
const proxyPort = '3128';
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
const SNYK_API = 'http://' + hostnameFakeServer + ':' + port + baseApi;
const HTTP_PROXY_WITH_USER = 'http://patch:dogsrule@localhost:' + proxyPort;
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-apps/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { fakeServer } from '../../../acceptance/fake-server';
import { runSnykCLI } from '../../util/runSnykCLI';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 30);

describe('config', () => {
let server: ReturnType<typeof fakeServer>;
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseURL = '/realbase';
const orgId = '4e0828f9-d92a-4f54-b005-6b9d8150b75f';
const testData = {
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-apps/create-app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { fakeServer, FakeServer } from '../../../acceptance/fake-server';
import { startSnykCLI, TestCLI } from '../../util/startSnykCLI';
import { getServerPort } from '../../util/getServerPort';

describe('snyk-apps: create app', () => {
let server: FakeServer;
let cli: TestCLI | null = null;
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/rest';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-config/snyk-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { runSnykCLI } from '../../util/runSnykCLI';
import { FakeServer, fakeServer } from '../../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../../util/createProject';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -19,7 +20,7 @@ test('returns value in one line', async () => {

describe('snyk config set endpoint', () => {
let server: FakeServer;
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api';
const token = '123456789';

Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-fix/fix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../util/createProject';
import { runCommand } from '../../util/runCommand';
import { runSnykCLI } from '../../util/runSnykCLI';
import { getServerPort } from '../../util/getServerPort';

// Check for existence of pipenv in the environment
const hasPipEnv = spawnSync('pipenv', ['--version']).status === 0;
Expand All @@ -18,7 +19,7 @@ describe('snyk fix', () => {

beforeAll(async () => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-ignore/snyk-ignore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fakeServer } from '../../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../../util/createProject';
import { requireSnykToken } from '../../util/requireSnykToken';
import { runSnykCLI, runSnykCLIWithArray } from '../../util/runSnykCLI';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -12,7 +13,7 @@ describe('snyk ignore', () => {

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-monitor/target-reference.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { runSnykCLI } from '../../util/runSnykCLI';
import { fakeServer } from '../../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../../util/createProject';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -9,7 +10,7 @@ describe('--target-reference', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fakeServer } from '../../acceptance/fake-server';
import { createProject } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { getServerPort } from '../util/getServerPort';

jest.setTimeout(1000 * 60 * 5);

Expand All @@ -9,7 +10,7 @@ describe('snyk test --all-projects with one project that has errors', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-test/all-projects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createProjectFromWorkspace } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';
import { fakeServer } from '../../../acceptance/fake-server';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -9,7 +10,7 @@ describe('snyk test --all-projects (mocked server only)', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import { fakeServer } from '../../../acceptance/fake-server';
import { runSnykCLI } from '../../util/runSnykCLI';
import { getServerPort } from '../../util/getServerPort';

describe('container test projects behavior with --app-vulns, --file and --exclude-base-image-vulns flags', () => {
it('should find nothing when only vulns are in base image', async () => {
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('container test projects behavior with --json flag', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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';

jest.setTimeout(1000 * 60);

Expand All @@ -22,7 +23,7 @@ describe('`snyk test` of basic projects for each language/ecosystem', () => {
let dontSkip: boolean;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-test/fail-on.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createProjectFromWorkspace } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';
import { fakeServer } from '../../../acceptance/fake-server';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -10,7 +11,7 @@ describe('snyk test --fail-on', () => {

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
const apiPort = getServerPort(process);
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-test/missing-node-modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getWorkspaceJSON } from '../../../acceptance/workspace-helper';
import { createProject } from '../../util/createProject';
import { requireSnykToken } from '../../util/requireSnykToken';
import { runSnykCLI } from '../../util/runSnykCLI';
import { getServerPort } from '../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -17,7 +18,7 @@ describe('snyk test with missing node_modules', () => {
'no-vulns',
'vulns-result.json',
);
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const BASE_API = '/api/v1';
env = {
...process.env,
Expand Down
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-test/output-formats/sarif.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createProjectFromFixture } from '../../../util/createProject';
import { runSnykCLI } from '../../../util/runSnykCLI';
import { fakeServer } from '../../../../acceptance/fake-server';
import { getServerPort } from '../../../util/getServerPort';

jest.setTimeout(1000 * 60);

Expand All @@ -9,7 +10,7 @@ describe('snyk test --sarif', () => {
let env: Record<string, string>;

beforeAll((done) => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const port = getServerPort(process);
const baseApi = '/api/v1';
env = {
...process.env,
Expand Down
Loading

0 comments on commit 3607eb3

Please sign in to comment.