Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System tests should cleanup after themselves #2216

Merged
merged 77 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
d993866
keeping track of resources created during tests and adding cleanup
ATorrise Aug 6, 2024
3b01d28
tests that ive been preoccupied with
ATorrise Aug 14, 2024
fb156de
fixing these was hardgit add .
ATorrise Aug 15, 2024
960777b
ensuring more tests delete jobs from mf, also fixing delete operations
ATorrise Aug 15, 2024
68a7fdd
Merge remote-tracking branch 'origin/next' into system-test-cleanup
ATorrise Aug 15, 2024
4f059e6
finished zosjobs sdk tests and starting zosfiles sdk tests
ATorrise Aug 16, 2024
81048a7
modifying upload.system.test took the longest... removing some test f…
ATorrise Aug 21, 2024
a505c0d
took awhile to see the difference in regex between v1 and v2 that i w…
ATorrise Aug 22, 2024
56f0e9a
providing more clarity in a comment about calculating previous jobid
ATorrise Aug 22, 2024
c458433
fixing cli.zos-jobs.download and its accompanying scripts
ATorrise Aug 22, 2024
e356ac8
more cli sytem tests and ammended shell scripts
ATorrise Aug 22, 2024
123c7f9
finishin clizosjobslistspoolfiles
ATorrise Aug 22, 2024
d7b2840
fixing submit with a different, less intensive cleanup approach
ATorrise Aug 22, 2024
e1f2ca2
realizing need to keep createZosmfSession for uss files..
ATorrise Aug 23, 2024
4ccaad5
finishing cliZosjobsSubmit
ATorrise Aug 23, 2024
c5995dd
fixing error in variable name across files and finishing view spool t…
ATorrise Aug 23, 2024
a0fa936
unable to modify file like others
ATorrise Aug 26, 2024
f746a0b
deletes remaining ds from mf
ATorrise Aug 26, 2024
885a28a
Merge branch 'next' into system-test-cleanup
ATorrise Aug 26, 2024
f334c76
Update cli.zos-jobs.submit.stdin.system.test.ts to remove semicolon
ATorrise Aug 26, 2024
e11726f
Update cli.zos-jobs.submit.stdin.system.test.ts removing accidental line
ATorrise Aug 26, 2024
70cb522
whoops
ATorrise Aug 26, 2024
d87f789
Merge branch 'next' into system-test-cleanup
ATorrise Aug 26, 2024
0b475ac
changelog
ATorrise Aug 27, 2024
3853c8a
linting
ATorrise Aug 27, 2024
0159d2c
Merge branch 'next' into system-test-cleanup
ATorrise Aug 28, 2024
c7de80c
no circ dependency error?
ATorrise Aug 28, 2024
54baa2d
updating changelog
ATorrise Aug 28, 2024
ebe3570
moving things around with andrew's help
ATorrise Aug 28, 2024
663340f
moving around things in testEnv and ITestEnv
ATorrise Aug 28, 2024
9d59e1a
about to make a really big change
ATorrise Aug 29, 2024
fe2c72b
restoring clitestutils cleanup()
ATorrise Aug 29, 2024
46677f7
Merge branch 'next' into system-test-cleanup
ATorrise Aug 29, 2024
21bf9aa
fixing other testEnv
ATorrise Aug 29, 2024
a9d2865
updating imports
ATorrise Aug 29, 2024
52361c2
fixing imports
ATorrise Aug 29, 2024
309be62
hopefully not breaking
ATorrise Aug 29, 2024
585bb75
accidentally left 1 thing out of if statement
ATorrise Aug 29, 2024
a83444a
whoops
ATorrise Aug 29, 2024
bc1ffa2
making a wait util method and implementing it everywhere
ATorrise Aug 29, 2024
8ccb8f4
addressing one of fernandos comments
ATorrise Aug 30, 2024
842ee6b
timeout corrections
ATorrise Aug 30, 2024
9e8f22e
removing change to a unit test
ATorrise Aug 30, 2024
1c7a5b2
changing system test to work with 'any' job name
ATorrise Aug 30, 2024
83faac9
wiping testEnvironment.resources in cleanup
ATorrise Sep 4, 2024
6c7064b
changing the way the test works but need testing mini to be active an…
ATorrise Sep 4, 2024
deb5cfa
Merge branch 'next' into system-test-cleanup
ATorrise Sep 4, 2024
cefbff1
hopefully good to merge now
ATorrise Sep 4, 2024
aaa43b8
Merge branch 'system-test-cleanup' of https://github.com/zowe/zowe-cl…
ATorrise Sep 4, 2024
201ecc1
fixin - initializing resources
ATorrise Sep 4, 2024
4a37668
addressing timothy's requests
ATorrise Sep 19, 2024
36a19ca
Merge remote-tracking branch 'origin/next' into system-test-cleanup
ATorrise Sep 19, 2024
5e40b67
Merge branch 'master' of https://github.com/zowe/zowe-cli into system…
ATorrise Sep 19, 2024
2d1c9be
fixing runCliScrip import and TestEnvironment.cleanup
ATorrise Sep 19, 2024
d89d3b1
createSession() to createZosmfSession()
ATorrise Sep 19, 2024
ae20141
createsession to createzosmfsession
ATorrise Sep 19, 2024
54e8d99
adding import back into test
ATorrise Sep 20, 2024
9de05a2
getting things deleted
ATorrise Sep 20, 2024
f9f3b16
Merge branch 'master' of https://github.com/zowe/zowe-cli into system…
zFernand0 Sep 26, 2024
3b008cb
review: update a few files based on comments :yum:
zFernand0 Sep 27, 2024
4a96709
chore: address Zos limitation on dataset name length
zFernand0 Sep 27, 2024
04b92c8
midway through editing file to utilize testResources
ATorrise Sep 30, 2024
5f1d974
midway changes.. tests failing
ATorrise Oct 1, 2024
6a1dc9f
test fixin
ATorrise Oct 2, 2024
a628144
1 error left in download test
ATorrise Oct 3, 2024
7f50352
Fix remaining uss download system tests
t1m0thyj Oct 3, 2024
66f5e11
Clean up resources in download system tests
t1m0thyj Oct 7, 2024
73e9201
Clean up remaining resources for zosfiles
t1m0thyj Oct 8, 2024
0a32123
Revert changes to cli-test-utils package
t1m0thyj Oct 8, 2024
e3805fd
Clean up local files from vsam tests
t1m0thyj Oct 8, 2024
b2a3bac
Fix failing zosjobs system tests
t1m0thyj Oct 11, 2024
336d3e4
Fix remaining errors in system tests
t1m0thyj Oct 14, 2024
7068b6c
Address unused variables found by CodeQL
t1m0thyj Oct 14, 2024
a2cb59e
Merge branch 'master' into system-test-cleanup
t1m0thyj Oct 14, 2024
f7f6696
Fix system tests for cancel and search jobs
t1m0thyj Oct 14, 2024
8229c76
Merge branch 'master' into system-test-cleanup
t1m0thyj Oct 18, 2024
cacdd3a
Fix failing TSO system tests
t1m0thyj Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 84 additions & 8 deletions __tests__/__src__/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,83 @@
*/

import { randomBytes } from "crypto";
import { ZosFilesConstants } from "../../packages/zosfiles/src";
import * as fs from "fs";
import { Imperative, Headers, AbstractSession } from "@zowe/imperative";
import { ZosmfRestClient } from "../../packages/core/src";
import { ZosFilesConstants, Delete } from "../../packages/zosfiles/src";
import { DeleteJobs, GetJobs, ICommonJobParms, IDeleteJobParms, IJob } from "../../packages/zosjobs/src";
import { promisify } from "util";

/**
* Delete a local testing file after use
* @param {string} filePath - File path of temporary file
*/
export function deleteLocalFile(filePath: string): void {
try {
fs.unlinkSync(filePath);
} catch {
throw new Error(`Error deleting local file: ${filePath}`);
}
}

/**
* Delete local directories after use
* @param {string[]} directories - Array of directories to delete
*/
export function deleteLocalDirectories(directories: string[]): void {
directories.forEach((dir) => {
try {
if (fs.existsSync(dir)) {
fs.rmdirSync(dir, { recursive: true });
}
} catch {
throw new Error(`Error deleting directory: ${dir}`);
}
});
}

/**
* Deletes a USS file from the mainframe
* @param {AbstractSession} session - The session object
* @param {string} fileName - The name of the file to delete
* @returns {Promise<void>} A promise that resolves when the file is deleted
*/
export async function deleteFiles(session: AbstractSession, fileName: string): Promise<void> {
await Delete.ussFile(session, fileName, true); //recursive = true
}

/**
* Deletes a data set from the mainframe
* @param {AbstractSession} session - The session object
* @param {string} dataSetName - The name of the data set to delete.
* @returns {Promise<void>} A promise that resolves when the data set is deleted
*/
export async function deleteDataset(session: AbstractSession, dataSetName: string): Promise<void> {
await Delete.dataSet(session, dataSetName);
}

/**
* Delete a job from the mainframe using Zowe SDKs - IJob
* @param {AbstractSession} session - z/OSMF connection info
* @param {IJob | string} job - the job or job ID that you want to delete
* @returns {Promise<void>} A promise that resolves when the job is deleted.
*/
export async function deleteJob(session: AbstractSession, job: IJob | string): Promise<void> {
if (typeof job === "string") {
job = await GetJobs.getJob(session, job);
}
await DeleteJobs.deleteJobForJob(session, job);
}

/**
* Delete a job from the mainframe using Zowe SDKs - jobid, jobname
* @param {AbstractSession} session - z/OSMF connection info
* @param {params} ICommonJobParms - constains jobname and jobid for job to delete
* @returns {Promise<void>} A promise that resolves when the job is deleted.
*/
export async function deleteJobCommon(session: AbstractSession, params: ICommonJobParms): Promise<void> {
await DeleteJobs.deleteJobCommon(session, params as IDeleteJobParms);
}

/**
* This function strips any new lines out of the string passed.
Expand All @@ -32,14 +106,14 @@ export function stripNewLines(str: string): string {
* @param {string} hlq User specified high level qualify
* @returns {string} A generated data set name
*/
export function getUniqueDatasetName(hlq: string, encoded = false): string {
export function getUniqueDatasetName(hlq: string, encoded = false, maxNodes = 2): string {
let generatedName: string = "";
const randomNumber = Math.random();
const timestampInMsNum = Date.now();
let timestampInMs = Math.floor(randomNumber * timestampInMsNum).toString();
let tempStr: string;
const MAX_NODE_LENGTH = 7;
let MAX_NODES = 2;
let MAX_NODES = maxNodes;
let currNodes = 0;

if (encoded) {MAX_NODES = 1;}
Expand Down Expand Up @@ -100,11 +174,13 @@ export async function getTag(session: AbstractSession, ussPath: string) {
return response.stdout[0];
}

export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms) );
}

export const delTime = 500;
/**
* Pauses execution for a given number of milliseconds.
* @param {number} ms - Number of milliseconds to wait
* @returns {Promise<void>} - Resolves after the specified time has passed
*/
export const wait = promisify(setTimeout);
export const waitTime = 2000; //wait 2 seconds

/**
* Use instead of `util.inspect` to get consistently formatted output that can be used in snapshots.
Expand Down
27 changes: 27 additions & 0 deletions __tests__/__src__/environment/ITestEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import { ITestEnvironment as IBaseTestEnvironment } from "../../__packages__/cli-test-utils/src/environment/doc/response/ITestEnvironment";
import { ITestEnvironmentResources } from "./ITestEnvironmentResources";

/**
* The test environment for your test.
* @export
* @interface ITestEnvironment
*/
export interface ITestEnvironment<TestPropertiesSchema> extends IBaseTestEnvironment<TestPropertiesSchema>{
/**
* A collection of resources used within the test environment that need to be cleaned up once test finishes.
* @type {ITestEnvironmentResources}
* @memberof ITestEnvironment
*/
resources?: ITestEnvironmentResources;
}
50 changes: 50 additions & 0 deletions __tests__/__src__/environment/ITestEnvironmentResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import { AbstractSession } from "@zowe/imperative";
import { IJob } from "../../../packages/zosjobs";

/**
* Represents the resources used within the test environment.
* @export
* @interface ITestEnvironmentResources
*/
export interface ITestEnvironmentResources {
/**
* Array of local file paths used within the test environment.
* @type {string[]}
*/
localFiles: string[];

/**
* Array of mainframe uss files used within the test environment.
* @type {string[]}
*/
files: string[];

/**
* Array of job objects or job IDs representing jobs submitted to the mainframe during the test.
* @type {IJob[]}
*/
jobs: (IJob | string)[];

/**
* Array of dataset names used within the test environment.
* @type {string[]}
*/
datasets: string[];

/**
* The session used for interacting with z/OS systems during the test, if applicable.
* @type {AbstractSession}
*/
session?: AbstractSession;
}
57 changes: 52 additions & 5 deletions __tests__/__src__/environment/TestEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

import * as nodePath from "path";

import { AbstractSession, Session } from "@zowe/imperative";
import { AbstractSession, Imperative, ImperativeError, Session } from "@zowe/imperative";

import { ITestPropertiesSchema } from "../properties/ITestPropertiesSchema";
import { ISetupEnvironmentParms, ITestEnvironment, TestEnvironment as BaseTestEnvironment } from "../../__packages__/cli-test-utils";
import { ISetupEnvironmentParms, TestEnvironment as BaseTestEnvironment } from "../../__packages__/cli-test-utils";
import { ITestEnvironment } from "./ITestEnvironment";
import { SshSession } from "../../../packages/zosuss/src/SshSession";
import { deleteLocalFile, deleteFiles, deleteJob, deleteJobCommon, deleteDataset } from "../TestUtils";

/**
* Use the utility methods here to setup the test environment for running APIs
Expand All @@ -36,7 +38,7 @@ export class TestEnvironment extends BaseTestEnvironment {
* @memberof TestEnvironment
*/
public static async setUp(params: ISetupEnvironmentParms): Promise<ITestEnvironment<ITestPropertiesSchema>> {
const result = await super.setUp(params);
const result: ITestEnvironment<ITestPropertiesSchema> = await super.setUp(params);

// Ensure correct path separator for windows or linux like systems.
const separator = process.platform === "win32" ? ";" : ":";
Expand All @@ -45,11 +47,56 @@ export class TestEnvironment extends BaseTestEnvironment {
nodePath.resolve(__dirname, "../../__resources__/application_instances"),
process.env.PATH
].join(separator);

result.resources = {
localFiles: [],
datasets: [],
files: [],
jobs: [],
session: params.skipProperties ? null : TestEnvironment.createZosmfSession(result)
};
// Return the test environment including working directory that the tests should be using
return result;
}

/**
* Clean up your test environment.
* Deletes any temporary profiles that have been created
* @params {ITestEnvironment} testEnvironment - the test environment returned by createTestEnv
*
* @returns {Promise<void>} - promise fulfilled when cleanup is complete
* @throws errors if profiles fail to delete
* @memberof TestEnvironment
*/
public static async cleanUp(testEnvironment: ITestEnvironment<ITestPropertiesSchema>) {
// Delete profiles and plugindir
await super.cleanUp(testEnvironment);

// Deleting resources (if they exist)
if (testEnvironment?.resources?.session) {
const session = testEnvironment.resources.session;
for (const file of testEnvironment.resources.localFiles) {
deleteLocalFile(file);
}
for (const dataset of testEnvironment.resources.datasets) {
await deleteDataset(session, dataset);
}
for (const file of testEnvironment.resources.files) {
await deleteFiles(session, file);
}
for (const job of testEnvironment.resources.jobs) {
await deleteJob(session, job);
}

testEnvironment.resources = {
localFiles: [],
datasets: [],
files: [],
jobs: [],
session: testEnvironment.resources.session
};
}
}

/**
* Create a ZOSMF session from properties present in your test environment
* @param testEnvironment - your test environment with system test properties populated
Expand Down Expand Up @@ -100,4 +147,4 @@ export class TestEnvironment extends BaseTestEnvironment {
handshakeTimeout: defaultSystem.ssh.handshakeTimeout
});
}
}
}
Loading