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

Testing quickJS via configmgr #489

Open
wants to merge 11 commits into
base: v3.x/staging
Choose a base branch
from
6 changes: 6 additions & 0 deletions build/build_cmgr_xlclang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ xlclang \

rm -rf "${TMP_DIR}"

if [ "${1}" = "--test" ]; then
if [ -f "${COMMON}/bin/configmgr" ]; then
"${COMMON}/bin/configmgr" -script "${COMMON}/tests/quickJS/quickJS.js"
echo
fi
fi

# This program and the accompanying materials are
# made available under the terms of the Eclipse Public License v2.0 which accompanies
Expand Down
31 changes: 31 additions & 0 deletions tests/quickJS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# QuickJS tests

## Simple unit tests

This framework is as unit testing for individual javascript functions.

## Adding new test

Follow the existing structure, always returning the number of errors and tests.

Keep in mind, test the simple cases like:
* Working cases
* No parameters when exepecting some
* Incorrect data types
* Empty, null, undefined values...

```javascript
export function test_someFunction() {
const TEST = [ -1, 0, 1, 42 ]
let errs = 0;
for (let t in TEST) {
const result = someFunction(TEST[t]);
print.clog(result == TEST[t], `someFunction(${TEST[i]})=${result}`);
if (result != TEST[t]) {
errs++;
}
}
return { errors: errs, total: TEST.lenght }
}

```
51 changes: 51 additions & 0 deletions tests/quickJS/lib/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
// 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.
*/

export const RED = "\u001b[31m";
export const GREEN = "\u001b[32m";
export const YELLOW = "\u001b[33m";
export const PURPLE = "\u001b[35m";
export const CYAN = "\u001b[36m";
export const RESET = "\u001b[0m";

export function color(color, msg) {
console.log(`${color}${msg}${RESET}`);
}

export function red(msg) {
color(RED, msg);
}

export function green(msg) {
color(GREEN, msg);
}

export function yellow(msg) {
color(YELLOW, msg);
}

export function purple(msg) {
color(PURPLE, msg);
}

export function cyan(msg) {
color(CYAN, msg);
}

export function conditionally(condition, ...msg) {
console.log(`${condition ? GREEN : RED}` + msg + RESET);
}

export function lines(clr, msg) {
color(clr, `\n${'-'.repeat(msg.length)}`);
color(clr, `${msg}`);
color(clr, `${'-'.repeat(msg.length)}\n`);
}
48 changes: 48 additions & 0 deletions tests/quickJS/quickJS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
// 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 * as std from 'cm_std';
import * as print from './lib/print';
import * as testZos from './testLib/testZos';

const TEST_ZOS = [
testZos.test_changeTag,
testZos.test_changeExtAttr,
testZos.test_changeStreamCCSID,
testZos.test_zstat,
testZos.test_getZosVersion,
testZos.test_getEsm,
testZos.test_dslist,
testZos.test_resolveSymbol,
testZos.test_getStatvfs,
]

const TESTS = [
...TEST_ZOS
]

let result = {};
let errors = 0;
let total = 0;

for (let testFunction in TESTS) {
result = TESTS[testFunction]();
errors += result.errors;
total += result.total;
}

if (errors) {
print.lines(print.RED, `${errors} error${errors == 1 ? '' : 's'} detected in ${total} tests, review the test output.`);
std.exit(8);
} else {
print.lines(print.GREEN, `${total} tests succesfull.`);
std.exit(0);
}
37 changes: 37 additions & 0 deletions tests/quickJS/run_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

#######################################################################
# 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.
#######################################################################

if [ `uname` != "OS/390" ]; then
echo "This test must run on a z/OS system."
exit 1
fi

if [ "${1}" = "--help" ]; then
echo "Run the Quick JS tests"
echo " no parm: tries to run configmgr from current 'zowe-common-c/bin'"
echo " path: path to configmgr"
echo " --help: this help"
exit 0
fi

configmgr_path="${1}"
if [ -z "${configmgr_path}" ]; then
configmgr_path="../../bin/configmgr"
fi

if [ -f "${configmgr_path}" ]; then
"${configmgr_path}" -script ./quickJS.js
else
echo "configmgr not found in '${configmgr_path}'"
exit 4
fi
123 changes: 123 additions & 0 deletions tests/quickJS/testLib/testZos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
// 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 * as zos from 'zos';
import * as print from '../lib/print';


// int status = tagFile(pathname, ccsid);
export function test_changeTag() {
const result = zos.changeTag('./');
print.purple(`DUMMY TEST: zos.changeTag(./)=${result}`);
return { errors: 0, total: 0 }
}


// int status = changeExtendedAttributes(pathname, extattr, onOffInt ? true : false);
export function test_changeExtAttr() {
const result = zos.changeExtAttr('./');
print.purple(`DUMMY TEST: zos.changeExtAttr(./)=${result}`);
return { errors: 0, total: 0 }
}


// int status = convertOpenStream(fd, ccsid);
export function test_changeStreamCCSID() {
const result = zos.changeStreamCCSID('./');
print.purple(`DUMMY TEST: zos.changeStreamCCSID(./)=${result}`);
return { errors: 0, total: 0 }
}


// res = stat(pathNative, &st);
export function test_zstat() {
const result = zos.zstat('./testLib/testZos.js');
print.purple(`DUMMY TEST: zos.zstat(./testLib/testZos.js)=${JSON.stringify(result)}`);
return { errors: 0, total: 0 }
}


export function test_getZosVersion() {
const result = zos.getZosVersion();
print.conditionally(true, `zos.getZosVersion()=${result}${result > 0 ? `=hex(0x${result.toString(16)}` : ``})`);
return { errors: result ? 0 : 1, total : 1 };
}


export function test_getEsm() {
const EXPECTED = [ 'ACF2', 'RACF', 'TSS', 'NONE' ];
const result = zos.getEsm();

if (result == null || !EXPECTED.includes(result)) {
print.conditionally(false, `zos.getEsms()=${result}`);
return { errors: 1, total: 1 };
}
print.conditionally(true, `zos.getEsms()=${result}`);
return { errors: 0, total: 1 };
}



export function test_dslist() {
const result = zos.dslist('SYS1.MACLIB');
print.purple(`DUMMY TEST: zos.zstat(SYS1.MACLIB)=${JSON.stringify(result)}`);
return { errors: 0, total: 0 }
}


export function test_resolveSymbol() {
const result = zos.resolveSymbol('&YYMMDD');
const date = new Date();
const yymmdd = (date.getFullYear() - 2000) * 10000 + (date.getMonth() + 1) * 100 + date.getDate() + '';
let errs = 0;

print.conditionally(result == yymmdd, `zos.resolveSymbol('&YYMMDD')=${result} -> ${yymmdd}`);
if (result != yymmdd) {
errs ++
}

const SYMBOLS_ERR = [ undefined, null, '', 'YYMMDD', ' &', ['a', 'b'], '& UNDEFINED SYMBOL !@#$%^&*()' ];
for (let s in SYMBOLS_ERR) {
const result = zos.resolveSymbol(SYMBOLS_ERR[s]);
print.conditionally(result.length == 0, `zos.resolveSymbol(${SYMBOLS_ERR[s]})=${result}`);
if (result.length) {
errs++
}
}
return { errors: errs, total : SYMBOLS_ERR.length + 1 };;
}


export function test_getStatvfs() {
const PATHS_OK = [ './', '/', '/bin/' ];
const PATHS_ERR = [ '', '/aaaaaaaaaaaaaaaaaaaaaaaaaaaaa', [ 'a', 3.14 ], { path: '/dev/null' }, null, true, false, undefined, 0, -1, 800 ];
let errs = 0;

for (let p in PATHS_OK) {
const result = zos.getStatvfs(PATHS_OK[p]);
print.conditionally(result[1] == 0, `zos.getStatvfs(${PATHS_OK[p]})=${result[1]}`);
if (result[1] != 0) {
errs++;
}
if (result[0]) {
console.log(`Stats=${JSON.stringify(result[0])}`);
}
}

for (let p in PATHS_ERR) {
const result = zos.getStatvfs(PATHS_ERR[p]);
print.conditionally(result[1] != 0, `zos.getStatvfs(${PATHS_ERR[p]})=${result[1]}`);
if (result[1] == 0) {
errs++;
}
}
return { errors: errs, total: PATHS_ERR.length + PATHS_OK.length };
}
Loading