Skip to content

Commit

Permalink
Adding more test cases (#54)
Browse files Browse the repository at this point in the history
* added logs volume in the faas image to store the logs in the app.log file

* Added nodejs integration test

* Dependency deploy added in the CI

* Refactored test.sh file

* Fixed ci

* workflow event added

* Revert "workflow event added"

This reverts commit 7677101.

* ci fixing

* ci fixing

* Added env development and deployment

* Node environment corrected

* Added node_evironment in the first run

* Remove delete on development env

* Add Depedency deploy in the command

* Issue resolved
  • Loading branch information
HeeManSu authored Jul 22, 2024
1 parent f5f2bfe commit abc3616
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 32 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- master
tags:
- 'v*.*.*'
workflow_dispatch:

jobs:
ci:
Expand Down Expand Up @@ -50,8 +51,8 @@ jobs:
shell: bash
run: |
docker compose build
docker compose up --exit-code-from test
TEST_FAAS_STARTUP_DEPLOY=true docker compose up --exit-code-from test
NODE_ENVIRONMENT=deployment TEST_FAAS_DEPENDENCY_DEPLOY=true docker compose up --exit-code-from test
TEST_FAAS_STARTUP_DEPLOY=true TEST_FAAS_DEPENDENCY_DEPLOY=true NODE_ENVIRONMENT=deployment docker compose up --exit-code-from test
docker compose down
- name: Publish
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ services:
target: faas
ports:
- "9000:9000"
volumes:
- ./logs:/metacall/logs

test:
image: metacall/faas:test
Expand All @@ -39,7 +41,8 @@ services:
target: test
environment:
TEST_FAAS_STARTUP_DEPLOY: ${TEST_FAAS_STARTUP_DEPLOY:-false}
TEST_FAAS_DEPENDENCY_DEPLOY: ${TEST_FAAS_DEPENDENCY_DEPLOY:-true}
TEST_FAAS_DEPENDENCY_DEPLOY: ${TEST_FAAS_DEPENDENCY_DEPLOY:-false}
NODE_ENVIRONMENT: ${NODE_ENVIRONMENT:-development}
network_mode: host
depends_on:
- faas
Expand Down
19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,24 @@
"caughtErrorsIgnorePattern": "^_"
}
]
}
},
"overrides": [
{
"files": [
"*.js"
],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unsafe-return": "off"

}
}
]
},
"dependencies": {
"@metacall/protocol": "^0.1.26",
Expand Down
2 changes: 1 addition & 1 deletion src/controller/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const inspect = (_req: Request, res: Response): Response => {
if (!application.deployment.packages) {
throw new Error('Packages is undefined or null');
}
deployments.push(application.deployment);
deployments.unshift(application.deployment);
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/data/nodejs-base-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

function isPalindrome(str) {
const cleanedStr = String(str)
.replace(/[^a-zA-Z0-9]/g, '')
.toLowerCase();
return cleanedStr === cleanedStr.split('').reverse().join('');
}

module.exports = { isPalindrome };
5 changes: 5 additions & 0 deletions test/data/nodejs-base-app/metacall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"language_id": "node",
"path": ".",
"scripts": ["index.js"]
}
5 changes: 5 additions & 0 deletions test/data/nodejs-dependency-app/metacall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"language_id": "node",
"path": ".",
"scripts": ["middleware.js"]
}
45 changes: 45 additions & 0 deletions test/data/nodejs-dependency-app/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node

const jwt = require('jsonwebtoken');
const secret = 'secret';

// Mock user validation (DB)
function validUser(user, password) {
return user === 'viferga' && password === '123';
}

// Sign in function that returns a token if login is valid
function signin(user, password) {
if (validUser(user, password)) return jwt.sign({ user, password }, secret);

return false;
}

// Reverse string function
function reverse({ str }) {
return str.split('').reverse().join('');
}

// Sum function
function sum({ a, b }) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
return a + b;
}

// Middleware for verifying the token
function middleware(f) {
return function (token, args) {
try {
return jwt.verify(token, secret) ? f(args) : 'Invalid token';
} catch (e) {
return e.message;
}
};
}

// Export sign in without middleware and reverse and sum with it
module.exports = {
signin,
reverse: middleware(reverse),
sum: middleware(sum)
};
87 changes: 87 additions & 0 deletions test/data/nodejs-dependency-app/package-lock.json

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

7 changes: 7 additions & 0 deletions test/data/nodejs-dependency-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "auth-middleware",
"version": "0.1.0",
"dependencies": {
"jsonwebtoken": "^9.0.0"
}
}
116 changes: 91 additions & 25 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -exuo pipefail
# You can enable this for debugging in development by changing NODE_ENVIRONMENT to development.
if [[ "${NODE_ENVIRONMENT}" == "deployment" ]]; then
set -exuo pipefail
fi

# Maximum number of retries
MAX_RETRIES=5
Expand All @@ -28,7 +30,7 @@ RETRY_COUNT=0
# FaaS base URL
BASE_URL="http://localhost:9000"

# #function to check readiness
# Function to check readiness
function check_readiness() {
local status_code
status_code=$(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/readiness)
Expand Down Expand Up @@ -92,28 +94,53 @@ function run_tests() {

echo "Inspection test passed."

# Test delete only if we are not testing startup deployments
if [[ "${TEST_FAAS_STARTUP_DEPLOY}" == "true" ]]; then
echo "Testing delete functionality."

# Delete the deployed project
curl -X POST -H "Content-Type: application/json" -d '{"suffix":"'"$app"'","prefix":"'"$prefix"'","version":"v1"}' $BASE_URL/api/deploy/delete

# Verify deletion
if [[ "$app" == "python-dependency-app" ]]; then
if [[ $(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/$prefix/$app/v1/call/fetchJoke) != "404" ]]; then
echo "Deletion test failed."
exit 1
fi
else
if [[ $(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/$prefix/$app/v1/call/number) != "404" ]]; then
echo "Deletion test failed."
exit 1
fi
fi

echo "Deletion test passed."
# Call delete functionality in local development
if [[ "${NODE_ENVIRONMENT}" == "development" && "${TEST_FAAS_STARTUP_DEPLOY}" == "false" ]]; then
delete_functionality $app $prefix
fi

# Call delete functionality
if [[ "${TEST_FAAS_STARTUP_DEPLOY}" == "true" && "${NODE_ENVIRONMENT}" == "deployment" ]]; then
delete_functionality $app $prefix
fi
}

# Function to test delete functionality
function delete_functionality() {
local app=$1
local prefix=$2

echo "Testing delete functionality."

# Delete the deployed project
curl -X POST -H "Content-Type: application/json" -d '{"suffix":"'"$app"'","prefix":"'"$prefix"'","version":"v1"}' $BASE_URL/api/deploy/delete

# Verify deletion
case "$app" in
"python-dependency-app")
endpoint="fetchJoke"
;;
"python-base-app")
endpoint="number"
;;
"nodejs-base-app")
endpoint="isPalindrome"
;;
"nodejs-dependency-app")
endpoint="signin"
;;
*)
echo "Unknown application: $app"
exit 1
;;
esac

if [[ $(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/$prefix/$app/v1/call/$endpoint) != "404" ]]; then
echo "Deletion test failed."
exit 1
fi

echo "Deletion test passed."
}

# Test function for python-base-app
Expand All @@ -129,10 +156,49 @@ function test_python_dependency_app() {
[[ $(curl -s $url/fetchJoke) == *"setup"* && $(curl -s $url/fetchJoke) == *"punchline"* ]] || exit 1
}

# Run tests without dependencies
# Test function for nodejs-base-app
function test_nodejs_app() {
local url=$1

local response1
response1=$(curl -s -X POST -H "Content-Type: application/json" -d '{"params":["madam"]}' $url/isPalindrome)
[[ $response1 == "true" ]] || exit 1

local response2
response2=$(curl -s -X POST -H "Content-Type: application/json" -d '{"params":["world"]}' $url/isPalindrome)
[[ $response2 == "false" ]] || exit 1
}

# Test function for nodejs-dependency-app
function test_nodejs_dependency_app() {
local url=$1

local signin_response
signin_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"user":"viferga","password":"123"}' $url/signin)

local token
token=$(echo $signin_response | sed 's/^"\(.*\)"$/\1/')

if [[ -z "$token" ]]; then
echo "Failed to extract token"
exit 1
fi

local reverse_response
reverse_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"token":"'"$token"'","args":{"str":"hello"}}' $url/reverse)
[[ $reverse_response = '"olleh"' ]] || exit 1

local sum_response
sum_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"token":"'"$token"'","args":{"a":1,"b":2}}' $url/sum)
[[ $sum_response = 3 ]] || exit 1
}

# Run tests
run_tests "nodejs-base-app" test_nodejs_app
run_tests "python-base-app" test_python_base_app
if [[ "${TEST_FAAS_DEPENDENCY_DEPLOY}" == "true" ]]; then
run_tests "python-dependency-app" test_python_dependency_app
run_tests "nodejs-dependency-app" test_nodejs_dependency_app
fi

echo "Integration tests passed without errors."
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"outDir": "dist",
"esModuleInterop": true
},
"include": ["types/*.ts", "src/*.ts", "src/**/*.ts"]
}
"include": [
"types/*.ts",
"src/**/*.ts",
"test/**/*.js"
]
}

0 comments on commit abc3616

Please sign in to comment.