Skip to content

Commit

Permalink
Merge branch 'master' into fix/convert-v1-profiles
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <[email protected]>
  • Loading branch information
t1m0thyj committed Nov 5, 2024
2 parents b141946 + 440f8e3 commit 17f7435
Show file tree
Hide file tree
Showing 308 changed files with 7,360 additions and 4,332 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
],
"rules": {
"@typescript-eslint/no-magic-numbers": "off",
"@typescript-eslint/no-restricted-imports": "off",
"jest/expect-expect": ["warn", {
"assertFunctionNames": ["expect*", "**.*expect*"]
}],
Expand Down Expand Up @@ -64,6 +65,11 @@ module.exports = {
"ignoreEnums": true,
"ignoreReadonlyClassProperties": true
}],
"@typescript-eslint/no-restricted-imports": ["error", {
"patterns": [{
"group": ["**/../lib", "**/../src"]
}]
}],
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/semi": "warn",
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:
- name: Configure Sonar Scan
uses: zowe-actions/octorelease/script@v1
env:
COVERAGE_ARTIFACT: "ubuntu-latest-18.x-results:__tests__/__results__"
COVERAGE_ARTIFACT: "ubuntu-22.04-20.x-results:__tests__/__results__"
# COVERAGE_ARTIFACT: "ubuntu-latest-20.x-results:__tests__/__results__"
with:
script: sonarConfig

Expand Down
4 changes: 4 additions & 0 deletions __tests__/__packages__/cli-test-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the Zowe CLI test utils package will be documented in this file.

## Recent Changes

- BugFix: Improved the error message shown on MacOS when `runCliScript` method fails to run script that is missing shebang line. [#2314](https://github.com/zowe/zowe-cli/pull/2314)

## `8.1.1`

- BugFix: Updated peer dependencies to `^8.0.0`, dropping support for versions tagged `next`. [#2287](https://github.com/zowe/zowe-cli/pull/2287)
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__packages__/cli-test-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zowe/cli-test-utils",
"version": "8.2.0",
"version": "8.7.0",
"description": "Test utilities package for Zowe CLI plug-ins",
"author": "Zowe",
"license": "EPL-2.0",
Expand Down Expand Up @@ -43,7 +43,7 @@
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"@types/uuid": "^10.0.0",
"@zowe/imperative": "8.2.0"
"@zowe/imperative": "8.7.0"
},
"peerDependencies": {
"@zowe/imperative": "^8.0.0"
Expand Down
11 changes: 9 additions & 2 deletions __tests__/__packages__/cli-test-utils/src/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

import * as fs from "fs";
import * as path from "path";
import { spawnSync, SpawnSyncReturns, ExecFileException } from "child_process";
import { ITestEnvironment } from "./environment/doc/response/ITestEnvironment";
import { ICommandDefinition, IHandlerParameters } from "@zowe/imperative";
Expand Down Expand Up @@ -55,12 +56,16 @@ export function runCliScript(scriptPath: string, testEnvironment: ITestEnvironme
} catch {
fs.chmodSync(scriptPath, "755");
}
return spawnSync(scriptPath, args, {
const response = spawnSync(scriptPath, args, {
cwd: testEnvironment.workingDir,
env: childEnv,
encoding: "buffer"
});
if (process.platform === "darwin" && (response.error as ExecFileException)?.errno === -8) {
throw new Error(`The script file ${path.basename(scriptPath)} failed to execute. Check that it starts with a shebang line.`);
}

return response;
} else {
throw new Error(`The script file ${scriptPath} doesn't exist`);

Expand Down Expand Up @@ -121,7 +126,9 @@ export function mockHandlerParameters(params: PartialHandlerParameters): IHandle
},
progress: {
startBar: jest.fn((parms) => undefined),
endBar: jest.fn(() => undefined)
endBar: jest.fn(() => undefined),
startSpinner: jest.fn(() => undefined),
endSpinner: jest.fn(() => undefined)
},
format: {
output: jest.fn((parms) => {
Expand Down
2 changes: 2 additions & 0 deletions __tests__/__resources__/properties/default_properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ zosjobs:
modifiedJobclass: B
# System affinity
sysaff: zosjobs-sysaff
# CIM Support
skipCIM: false
#-----------------------------------------------------------------------------#
# Set of properties for testing provisioning #
#-----------------------------------------------------------------------------#
Expand Down
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

0 comments on commit 17f7435

Please sign in to comment.