Skip to content

Commit

Permalink
Enable integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
electrocucaracha committed Apr 12, 2024
1 parent 98a8c5a commit 107d938
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/on-demand_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2024
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

name: Check End-to-End (On Demand)
# yamllint disable-line rule:truthy
on:
push:
paths:
- '**.go'
- '**.sh'
- '!.github/*'
pull_request_review:
types:
- submitted
jobs:
check-scripts-format:
if: >-
(
github.event_name == 'pull_request_review' &&
github.event.review.state == 'approved'
) || github.event_name != 'pull_request_review'
name: Check scripts format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.1
- name: Run the sh-checker
uses: luizm/action-sh-checker@v0.8.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHFMT_OPTS: -i 4 -s
with:
sh_checker_shellcheck_disable: true
functional-test:
name: Check functional tests
needs: check-scripts-format
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4.1.1
- name: Deploy Nephio services
working-directory: ./scripts
env:
DEBUG: true
ENABLE_FUNC_TEST: true
run: ./main.sh
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Vagrant.configure('2') do |config|
config.vm.provision 'shell', privileged: false do |sh|
sh.env = {
DEBUG: ENV.fetch('DEBUG', true),
ENABLE_FUNC_TEST: ENV.fetch('ENABLE_FUNC_TEST', false),
ENABLE_CLUSTER_API: 'true',
ENABLE_NETWORK_CONFIG: 'true'
}
Expand Down
47 changes: 47 additions & 0 deletions scripts/_assertions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2023
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

set -o errexit
set -o nounset
set -o pipefail
DEBUG="${DEBUG:-false}"
[[ ${DEBUG} != "true" ]] || set -o xtrace

# shellcheck source=scripts/_utils.sh
source _utils.sh

# assert_non_empty() - This assertion checks if the expected value is not empty
function assert_non_empty {
local input=$1
local error_msg=$2

[[ ${DEBUG} != "true" ]] || debug "NonEmpty Assertion - value: $1"
[[ -n $input ]] || error "$error_msg"
}

# assert_are_equal() - This assertion checks if the inputs are equal
function assert_are_equal {
local input=$1
local expected=$2
local error_msg=${3:-"got $input, want $expected"}

[[ ${DEBUG} != "true" ]] || debug "Are equal Assertion - value: $1 expected: $2"
[[ $input == "$expected" ]] || error "$error_msg"
}

# assert_contains() - This assertion checks if the input contains another value
function assert_contains {
local input=$1
local expected=$2
local error_msg=${3:-"$input doesn't contains $expected"}

[[ ${DEBUG} != "true" ]] || debug "Contains Assertion - value: $1 expected: $2"
[[ $input == *"$expected"* ]] || error "$error_msg"
}
69 changes: 69 additions & 0 deletions scripts/configure_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2024
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

set -o pipefail
set -o errexit
set -o nounset
[[ ${DEBUG:-false} != "true" ]] || set -o xtrace

# shellcheck source=./scripts/_assertions.sh
source _assertions.sh

gitea_cache_tokens_base_dir=/tmp
gitea_admin_account=nephio
nephio_repos=(mgmt mgmt-staging)

function _assert_inotify_maxs {
local var="$1"
local val="$2"

assert_contains "$(sudo sysctl sysctl --all)" "fs.inotify.max_user_$var"
assert_are_equal "$(sudo sysctl sysctl --values "fs.inotify.max_user_$var" 2>/dev/null)" "$val"
}

function exec_gitea {
gitea_cmd="/app/gitea/gitea $*"
kubectl exec -n gitea -c gitea --context kind-gitea "$(kubectl get pods -n gitea -l app=gitea --context kind-gitea -o jsonpath='{.items[*].metadata.name}')" -- su git -c "$gitea_cmd"
}

function _get_admin_token {
if [ ! -f "$gitea_cache_tokens_base_dir/$gitea_admin_account" ]; then
mkdir -p "$gitea_cache_tokens_base_dir"
token="$(exec_gitea admin user generate-access-token --username "$gitea_admin_account" --scopes write:org,admin:org | grep 'Access token' | awk -F ':' '{ print $2}')"
echo "$token" | xargs | tee "$gitea_cache_tokens_base_dir/$gitea_admin_account"
else
cat "$gitea_cache_tokens_base_dir/$gitea_admin_account"
fi
}

function curl_gitea_api {
curl_cmd="curl -s -H 'Authorization: token $(_get_admin_token)' -H 'content-type: application/json' http://$(kubectl get service gitea -n gitea --context kind-gitea -o jsonpath='{.status.loadBalancer.ingress[0].ip}'):3000/api/v1/$1"
[[ -z ${2-} ]] || curl_cmd+=" -k --data '$2'"
eval "$curl_cmd"
}

# shellcheck disable=SC1091
[ -f /etc/profile.d/path.sh ] && source /etc/profile.d/path.sh

info "Assert inotify user max values"
_assert_inotify_maxs "watches" "524288"
_assert_inotify_maxs "instances" "512"

info "Assert KinD clusters creation"
assert_are_equal "$(sudo docker ps --filter label=io.x-k8s.kind.role=control-plane --quiet | wc -l)" "2" "There are two KinD clusters running"

info "Assert gitea users creation"
assert_contains "$(exec_gitea admin user list --admin)" "$gitea_admin_account"

info "Assert repositories creation"
nephio_gitea_repos="$(curl_gitea_api "users/nephio/repos")"
for repo in "${nephio_repos[@]}"; do
assert_contains "$nephio_gitea_repos" "$repo"
done
30 changes: 30 additions & 0 deletions scripts/install_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
# SPDX-license-identifier: Apache-2.0
##############################################################################
# Copyright (c) 2024
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

set -o pipefail
set -o errexit
set -o nounset
[[ ${DEBUG:-false} != "true" ]] || set -o xtrace

# shellcheck source=./scripts/_utils.sh
source _utils.sh

function _assert_cmd_exists {
local cmd="$1"
local error_msg="${2:-"$cmd command doesn't exist"}"

[[ ${DEBUG:-false} != "true" ]] || debug "Command $cmd assertion validation"
command -v "$cmd" >/dev/null || error "$error_msg"
}

info "Assert command requirements"
for cmd in docker kubectl kpt kind; do
_assert_cmd_exists "$cmd"
done
1 change: 1 addition & 0 deletions scripts/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ sudo rm -r /tmp/*
for step in install configure; do
info "Running $step process"
bash "./$step.sh"
[[ ${ENABLE_FUNC_TEST:-false} != "true" && -f "./${step}_test.sh" ]] || bash "./${step}_test.sh"
done

0 comments on commit 107d938

Please sign in to comment.