Skip to content

Commit

Permalink
Test for deploy using URL (fixes #63) (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu authored Aug 21, 2024
1 parent f86d757 commit 6553185
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ CMD ["node", "dist/index.js"]
FROM base AS test

RUN apt-get update \
&& apt-get install curl ca-certificates jq git -y --no-install-recommends \
&& apt-get install curl ca-certificates jq git expect -y --no-install-recommends \
&& npm install -g @metacall/deploy
33 changes: 30 additions & 3 deletions src/controller/repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextFunction, Request, Response } from 'express';
import { promises as fs } from 'fs';
import { join } from 'path';
import path, { join } from 'path';
import { Application, Applications, Resource } from '../app';
import AppError from '../utils/appError';
import { appsDirectory } from '../utils/config';
Expand Down Expand Up @@ -31,6 +31,25 @@ const deleteRepoFolderIfExist = async <Path extends string>(
await fs.rm(repoFilePath, { recursive: true, force: true });
};

const handleRunners = async (repoPath: string): Promise<string[]> => {
const runners: string[] = [];
const files = await fs.readdir(repoPath);

for (const file of files) {
const fullPath = path.join(repoPath, file);
const stat = await fs.stat(fullPath);

if (file === 'requirements.txt') runners.push('python');

if (file === 'package.json') runners.push('nodejs');

if (stat.isDirectory()) {
await handleRunners(fullPath);
}
}
return runners;
};

export const fetchFilesFromRepo = catchAsync(
async (
req: Omit<Request, 'body'> & { body: FetchFilesFromRepoBody },
Expand All @@ -41,13 +60,14 @@ export const fetchFilesFromRepo = catchAsync(
const resource: Resource = {
id: '',
path: '',
jsons: []
jsons: [],
runners: []
};

try {
await deleteRepoFolderIfExist(appsDirectory, url);
} catch (err) {
next(
return next(
new AppError(
'error occurred in deleting repository directory',
500
Expand All @@ -74,6 +94,13 @@ export const fetchFilesFromRepo = catchAsync(
resource.id = id;
resource.path = join(appsDirectory, id);

const detectedRunners = await handleRunners(resource.path);
if (detectedRunners.length > 0) {
resource.runners = detectedRunners;
} else {
console.warn('No runners detected');
}

// Create a new Application instance and assign the resource to it
const application = new Application();
application.resource = Promise.resolve(resource);
Expand Down
135 changes: 134 additions & 1 deletion test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,137 @@ run_tests "python-base-app" test_python_base_app
run_tests "python-dependency-app" test_python_dependency_app
run_tests "nodejs-dependency-app" test_nodejs_dependency_app

echo "Integration tests passed without errors."
echo "Integration tests for package deployment passed without errors."

echo "Integration tests for deploy with repo url starts"
function test_deploy_from_repo() {
local repo_url=$1
local app_name=$2
local test_func=$3

echo "Testing Deployment from repository: $repo_url"

# Deploy the repository using expect to handle the interactive prompts
expect <<EOF
spawn metacall-deploy --addrepo $repo_url --dev
expect "Select a container to get logs"
send "Deploy\r"
expect eof
EOF

# Get the prefix of the deployment
prefix=$(getPrefix $app_name)
url=$BASE_URL/$prefix/$app_name/v1/call

#Run the test function
$test_func $url

#Verify the deployment
inspect_response=$(curl -s $BASE_URL/api/inspect)
if [[$inspect_response != *"$prefix"*]]; then
echo "Inspection test failed for $app_name."
exit 1
fi
echo "Deployment test passed for $app_name."
}

function test_nodejs-parameter-example() {
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
echo "Node.js base app test passed."
}

function test_python_time_app() {
local url=$1
local response

echo "Testing Python Time App at $url"

# Test index endpoint
response=$(curl -s $url/index)
if [[ $response != *"<html"* ]] || [[ $response != *"Python Time App"* ]]; then
echo "Index test failed for Python Time App"
exit 1
fi

# Test time endpoint
response=$(curl -s $url/time)
if [[ ! $response =~ [0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2} ]]; then
echo "Time test failed for Python Time App"
exit 1
fi

echo "Python Time App tests passed"

}

# Test function for python-dependency-metacall
function test_python_dependency_metacall() {
local url=$1
local response=$(curl -s $url/fetch_joke)

if [[ $response == *"setup"* && $response == *"punchline"* ]]; then
echo "fetch_joke test passed: Joke structure is correct"
elif [[ $response == *"Error fetching joke"* ]]; then
echo "fetch_joke test passed: Error handling works"
else
echo "fetch_joke test failed: Unexpected response"
exit 1
fi
}

function test_nodejs_dependency_app() {
local url=$1

echo "Testing Node.js Dependency App at $url"

# Test signin function
local signin_response=$(curl -s -X POST "$url/signin" -H "Content-Type: application/json" -d '{"user":"viferga","password":"123"}')
local token=$(echo $signin_response | sed 's/^"\(.*\)"$/\1/')

if [ -z "$token" ]; then
echo "Signin test failed for Node.js Dependency App"
exit 1
fi

# Test reverse function with middleware
local reverse_response=$(curl -s -X POST "$url/reverse" -H "Content-Type: application/json" -d "{\"token\":\"$token\",\"args\":{\"str\":\"hello\"}}")
if [ "$reverse_response" != '"olleh"' ]; then
echo "Reverse function test failed for Node.js Dependency App"
exit 1
fi

# Test sum function with middleware
local sum_response=$(curl -s -X POST "$url/sum" -H "Content-Type: application/json" -d "{\"token\":\"$token\",\"args\":{\"a\":5,\"b\":3}}")
if [ "$sum_response" != "8" ]; then
echo "Sum function test failed for Node.js Dependency App"
exit 1
fi

echo "Node.js Dependency App tests passed"
}

echo "starting repository deployment tests"

#without Dependencies

#Test NodeJs app
test_deploy_from_repo "https://github.com/HeeManSu/nodejs-parameter-example" "nodejs-parameter-example" test_nodejs-parameter-example
# #Test Python app
test_deploy_from_repo "https://github.com/HeeManSu/metacall-python-example" "metacall-python-example" test_python_time_app

# #With Dependencies

# # Test Python app
test_deploy_from_repo "https://github.com/HeeManSu/python-dependency-metacall" "python-dependency-metacall" test_python_dependency_metacall
#Test NodeJs app
test_deploy_from_repo "https://github.com/HeeManSu/auth-middleware-metacall" "auth-middleware-metacall" test_nodejs_dependency_app

echo "Repository deployment tests completed."

0 comments on commit 6553185

Please sign in to comment.