Skip to content

Commit

Permalink
test: add integration test for cli
Browse files Browse the repository at this point in the history
ci: add step for integration test cli

Signed-off-by: Zvi Grinberg <zgrinber@redhat.com>
  • Loading branch information
zvigrinberg committed Jan 18, 2024
1 parent e4a2932 commit 5e500e6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 46 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ jobs:
- name: Run integration tests
run: npm run integration-tests

- name: Run integration tests cli
working-directory: integration
run: bash ./run_its.sh

- name: Upload coverage reports
if: ${{ matrix.node == env.MAIN_NODE_VER }}
uses: actions/upload-artifact@v3
Expand Down
79 changes: 37 additions & 42 deletions integration/run_its.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ match() {
cleanup 1
fi
echo "- PASSED"
echo
}

matchConstant() {
if [[ "$1" != "$2" ]]; then
TEST_MESSAGE="$3"
sleep 1
echo $TEST_MESSAGE
if [[ "$1" != "$2" ]]; then
echo "- FAILED"
echo "expected = $1, actual= $2"
cleanup 1
echo "expected = $1, actual= $2"
cleanup 1
fi
echo "- PASSED"
echo
}

##########################################
Expand Down Expand Up @@ -66,40 +71,23 @@ if ! mvn --version > /dev/null 2>&1; then
fi
echo "- SUCCESSFUL"

##########################################
###### Mock Server Conditional Start ######
##########################################
# unless required to use real backend, set custom url (from config) and start mock server
# note that based on the config file, the server will be automatically stopped after 5 minutes
if [ "$EXHORT_ITS_USE_REAL_API" != "true" ]; then
echo "STARTING Mock HTTP Server"
export EXHORT_BACKEND_URL=http://localhost:9432
eval "node server/mock_server.js server/mock_server_config.json &"
PID="$!"
# shellcheck disable=SC2181
if [ "$?" -ne 0 ]; then
echo "- FAILED Starting up the Mock Server"
cleanup 1
fi
echo "- SUCCESSFUL"
fi

##########################################
###### JavaScript Integration Tests ######
###### Preparing CLI Tests ######
##########################################
echo "PREPARING JavaScript integration/component tests environment"
rm -rf testers/javascript/node_modules
rm -f testers/javascript/package-lock.json
if ! npm --prefix ./testers/javascript install --silent; then
echo "- FAILED Installing modules for JS environment"
echo "PREPARING JavaScript CLI tests environment"
rm -rf testers/cli/node_modules
rm -f testers/cli/package-lock.json
if ! npm --prefix testers/cli install --silent ; then
echo "- FAILED Installing exhort-javascript-api environment for testing"
cleanup $?
fi
echo "- SUCCESSFUL"
mkdir -p ./responses
#### JAVA MAVEN
echo "RUNNING JavaScript integration test for Stack Analysis report in Html for Java Maven"
echo "RUNNING JavaScript CLI integration test for Stack Analysis report in Html for Java Maven"

node --no-warnings testers/javascript/index.js stack scenarios/maven/pom.xml true &> ./responses/stack.html
testers/cli/node_modules/.bin/exhort-javascript-api stack scenarios/maven/pom.xml --html &> ./responses/stack.html

if [ "$?" -ne 0 ]; then
echo "- FAILED , return $RC from invocation"
Expand All @@ -111,9 +99,10 @@ if [[ -z "${RESPONSE_CONTENT}" ]]; then
cleanup 1
fi
echo "- PASSED"
echo

echo "RUNNING JavaScript integration test for Stack Analysis report in Json for Java Maven"
node --no-warnings testers/javascript/index.js stack scenarios/maven/pom.xml false &> ./responses/stack.json
echo "RUNNING JavaScript CLI integration test for Stack Analysis report in Json for Java Maven"
testers/cli/node_modules/.bin/exhort-javascript-api stack scenarios/maven/pom.xml > ./responses/stack.json

if [ "$?" -ne 0 ]; then
echo "- FAILED , return $RC from invocation"
Expand All @@ -124,12 +113,14 @@ if [ "$?" -ne 0 ]; then
echo "- FAILED , response is not a valid json"
cleanup $RC
fi
StatusCodeTC=$(jq '.providers["trusted-content"].status.code' ./responses/stack.json)
matchConstant "200" "$StatusCodeTC" "Check that Response code from Trusted Content is OK ( Http Status = 200)..."

StatusCode=$(jq '.summary.providerStatuses[] | select(.provider== "snyk") ' ./responses/stack.json | jq .status)
matchConstant "200" "$StatusCode"
StatusCodeSnyk=$(jq '.providers.snyk.status.code' ./responses/stack.json)
matchConstant "200" "$StatusCodeSnyk" "Check that Response code from Snyk Provider is OK ( Http Status = 200)..."

echo "RUNNING JavaScript integration test for Component Analysis report for Java Maven"
eval "node --no-warnings testers/javascript/index.js component pom.xml '$(<scenarios/maven/pom.xml)'" &> ./responses/component.json
echo "RUNNING JavaScript CLI integration test for Component Analysis report for Java Maven"
eval "testers/cli/node_modules/.bin/exhort-javascript-api component pom.xml '$(<scenarios/maven/pom.xml)'" > ./responses/component.json

if [ "$?" -ne 0 ]; then
echo "- FAILED , return $RC from invocation"
Expand All @@ -140,15 +131,19 @@ if [ "$?" -ne 0 ]; then
echo "- FAILED , response is not a valid json, got $RC from parsing the file"
cleanup $RC
fi
StatusCode=$(jq '.summary.providerStatuses[] | select(.provider== "snyk") ' ./responses/component.json | jq .status)
matchConstant "200" "$StatusCode"

echo "RUNNING JavaScript integration test for Validate Token Function With wrong token, expecting getting 401 http status code "
answerAboutToken=$(node --no-warnings testers/javascript/index.js validateToken veryBadToken)
matchConstant "401" "$answerAboutToken"
StatusCodeTC=$(jq '.providers["trusted-content"].status.code' ./responses/stack.json)
matchConstant "200" "$StatusCodeTC" "Check that Response code from Trusted Content is OK ( Http Status = 200)..."
StatusCodeSnyk=$(jq '.providers.snyk.status.code' ./responses/stack.json)
matchConstant "200" "$StatusCodeSnyk" "Check that Response code from Snyk Provider is OK ( Http Status = 200)..."

echo "RUNNING JavaScript CLI integration test for Validate Token Function With wrong token, expecting getting 401 http status code "
answerAboutToken=$(testers/cli/node_modules/.bin/exhort-javascript-api validate-token snyk --value=veryBadTokenValue)
matchConstant "401" "$answerAboutToken" "Checking That dummy Token is Invalid , Expecting Response Status of Authentication Failure( Http Status = 401)..."

echo "RUNNING JavaScript integration test for Validate Token Function With no token at all, Expecting getting 400 http status code"
answerAboutToken=$(node --no-warnings testers/javascript/index.js validateToken )
matchConstant "400" "$answerAboutToken"
echo "RUNNING JavaScript CLI integration test for Validate Token Function With no token at all, Expecting getting 400 http status code"
answerAboutToken=$(testers/cli/node_modules/.bin/exhort-javascript-api validate-token snyk )
matchConstant "400" "$answerAboutToken" "Checking That Token is missing , Expecting Response Status of Bad Request( Http Status = 400)..."
echo "==>SUCCESS!!"

cleanup 0
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@babel/core": "^7.23.2",
"@cyclonedx/cyclonedx-library": "^4.0.0",
"fast-xml-parser": "^4.2.4",
"help": "^3.0.2",
"packageurl-js": "^1.0.2",
"yargs": "^17.7.2"
},
Expand Down
32 changes: 31 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const component = {
desc: 'manifest name and type',
type: 'string',
choices: ['pom.xml','package.json', 'go.mo', 'requirements.txt']

}
).positional(
'manifest-content',
Expand All @@ -29,6 +30,34 @@ const component = {
console.log(JSON.stringify(res, null, 2))
}
}
const validateToken = {
command: 'validate-token <token-provider> [--token-value thevalue]',
desc: 'Validates input token if authentic and authorized',
builder: yargs => yargs.positional(
'token-provider',
{
desc: 'the token provider',
type: 'string',
choices: ['snyk','oss-index'],
}
).options({
tokenValue: {
alias: 'value',
desc: 'the actual token value to be checked',
type: 'string',
}
}),
handler: async args => {
let tokenProvider = args['token-provider'].toUpperCase()
let opts={}
if(args['tokenValue'] !== undefined && args['tokenValue'].trim() !=="" ) {
let tokenValue = args['tokenValue'].trim()
opts[`EXHORT_${tokenProvider}_TOKEN`] = tokenValue
}
let res = await exhort.validateToken(opts)
console.log(res)
}
}

// command for stack analysis takes a manifest path
const stack = {
Expand Down Expand Up @@ -70,9 +99,10 @@ const stack = {

// parse and invoke the command
yargs(hideBin(process.argv))
.usage('Usage: $0 {component|stack}')
.usage('Usage: $0 {component|stack|validate-token}')
.command(stack)
.command(component)
.command(validateToken)
.version(false)
.demandCommand(1)
.parse()
7 changes: 4 additions & 3 deletions test/it/end-to-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ suite('Integration Tests', () => {
// // process.env["EXHORT_PYTHON_VIRTUAL_ENV"] = "true"
// // process.env["EXHORT_PYTHON_INSTALL_BEST_EFFORTS"] = "true"
// // process.env["MATCH_MANIFEST_VERSIONS"] = "false"
// let pomPath = `/tmp/070124/requirements.txt`
// let pomPath = `./test/providers/tst_manifests/maven/pom_with_multiple_modules/module3/pom.xml`
// // let pomPath = `/home/zgrinber/git/tracing-demos-and-examples/tracing-parent/pom.xml`
// let providedDataForStack;
// providedDataForStack = await index.stackAnalysis(pomPath, false,opts);
// // providedDataForStack = await index.stackAnalysis(pomPath, false,opts);
// // console.log(JSON.stringify(providedDataForStack,null , 4))
// // fs.writeFileSync(`/tmp/301123/report.html`,providedDataForStack)
// // providedDataForStack = await index.componentAnalysis("pom.xml",fs.readFileSync(pomPath).toString())
// providedDataForStack = await index.componentAnalysis("pom.xml",undefined,opts,pomPath)
// console.log(JSON.stringify(providedDataForStack,null , 4))
// // expect(providedDataForStack.summary.dependencies.scanned).greaterThan(0)
// }).timeout(15000);
Expand Down

0 comments on commit 5e500e6

Please sign in to comment.