Skip to content

Commit

Permalink
Fixed ci
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 12, 2024
1 parent 3727b66 commit 4eb12b3
Showing 1 changed file with 93 additions and 61 deletions.
154 changes: 93 additions & 61 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
# 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
# set -exuo pipefail

# Maximum number of retries
MAX_RETRIES=5
Expand All @@ -29,135 +29,167 @@ RETRY_COUNT=0
BASE_URL="http://localhost:9000"

# Function to check readiness
check_readiness() {
curl -s -o /dev/null -w "%{http_code}" $BASE_URL/readiness
function check_readiness() {
local status_code
status_code=$(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/readiness)
echo "$status_code"
}

# Get the prefix of a deployment
get_prefix() {
metacall-deploy --dev --inspect Raw | jq -r ".[] | select(.suffix == \"$1\") | .prefix"
function getPrefix() {
prefix=$(metacall-deploy --dev --inspect Raw | jq -r ".[] | select(.suffix == \"$1\") | .prefix")
echo $prefix
}

# Deploy only if we are not testing startup deployments, otherwise the deployments have been loaded already
deploy_if_not_testing() {
[[ "${TEST_FAAS_STARTUP_DEPLOY}" != "true" ]] && metacall-deploy --dev
function deploy() {
if [[ "${TEST_FAAS_STARTUP_DEPLOY}" != "true" ]]; then
metacall-deploy --dev
fi
}

# Wait for the FaaS to be ready
wait_for_readiness() {
while [[ $(check_readiness) != "200" ]]; do
if [[ $RETRY_COUNT -ge $MAX_RETRIES ]]; then
echo "Readiness check failed after $MAX_RETRIES retries."
exit 1
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 1
done
echo "FaaS ready, starting tests."
}
while [[ $(check_readiness) != "200" ]]; do
if [[ $RETRY_COUNT -ge $MAX_RETRIES ]]; then
echo "Readiness check failed after $MAX_RETRIES retries."
exit 1
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 1
done

echo "FaaS ready, starting tests."

# Run tests for an application
run_tests() {
# Function to run tests
function run_tests() {
local app=$1
local test_func=$2

pushd data/$app
deploy_if_not_testing
prefix=$(get_prefix $app)
deploy
prefix=$(getPrefix $app)
url=$BASE_URL/$prefix/$app/v1/call
$test_func $url
popd

# Test inspect functionality
# Test inspect
echo "Testing inspect functionality."

# Inspect the deployed projects
inspect_response=$(curl -s $BASE_URL/api/inspect)

# Verify inspection
if [[ $inspect_response != *"$prefix"* || $inspect_response != *"packages"* ]]; then
if [[ $inspect_response != *"$prefix"* ]]; then
echo "Inspection test failed."
exit 1
fi

# Verify packages are included in the response
if [[ $inspect_response != *"packages"* ]]; then
echo "packages not found in inspection response."
exit 1
fi

echo "Inspection test passed."

# Test delete functionality if testing startup deployments
if [[ "${TEST_FAAS_STARTUP_DEPLOY}" == "true" ]]; then
echo "Testing delete functionality."
delete_project $app $prefix
echo "Deletion test passed."
fi
# Call delete functionality
delete_functionality $app $prefix
}

# Delete a deployed project
delete_project() {
# 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
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
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
test_python_base_app() {
function test_python_base_app() {
local url=$1
[[ $(curl -s $url/number) = 100 ]] || exit 1
[[ $(curl -s $url/text) = '"asd"' ]] || exit 1
}

# Test function for python-dependency-app
test_python_dependency_app() {
function test_python_dependency_app() {
local url=$1
[[ $(curl -s $url/fetchJoke) == *"setup"* && $(curl -s $url/fetchJoke) == *"punchline"* ]] || exit 1
}

# Test function for nodejs-base-app
test_nodejs_app() {
function test_nodejs_app() {
local url=$1
[[ $(curl -s -X POST -H "Content-Type: application/json" -d '{"params":["madam"]}' $url/isPalindrome) == "true" ]] || exit 1
[[ $(curl -s -X POST -H "Content-Type: application/json" -d '{"params":["world"]}' $url/isPalindrome) == "false" ]] || exit 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
test_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/')
[[ -n "$token" ]] || {

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

[[ $(curl -s -X POST -H "Content-Type: application/json" -d '{"token":"'"$token"'","args":{"str":"hello"}}' $url/reverse) == '"olleh"' ]] || exit 1
[[ $(curl -s -X POST -H "Content-Type: application/json" -d '{"token":"'"$token"'","args":{"a":1,"b":2}}' $url/sum) == 3 ]] || exit 1
}
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

# Main execution starts here
# Wait for FaaS readiness
wait_for_readiness
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 for different applications
# Run tests
run_tests "nodejs-base-app" test_nodejs_app
run_tests "nodejs-dependency-app" test_nodejs_dependency_app
run_tests "python-base-app" test_python_base_app

# Run dependency app tests if specified
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."

0 comments on commit 4eb12b3

Please sign in to comment.