From 107d938b92cab16e09424e6ed5136e6d5d63e153 Mon Sep 17 00:00:00 2001 From: Victor Morales Date: Fri, 12 Apr 2024 10:51:49 -0700 Subject: [PATCH] Enable integration tests --- .github/workflows/on-demand_ci.yml | 51 ++++++++++++++++++++++ Vagrantfile | 1 + scripts/_assertions.sh | 47 ++++++++++++++++++++ scripts/configure_test.sh | 69 ++++++++++++++++++++++++++++++ scripts/install_test.sh | 30 +++++++++++++ scripts/main.sh | 1 + 6 files changed, 199 insertions(+) create mode 100644 .github/workflows/on-demand_ci.yml create mode 100755 scripts/_assertions.sh create mode 100755 scripts/configure_test.sh create mode 100755 scripts/install_test.sh diff --git a/.github/workflows/on-demand_ci.yml b/.github/workflows/on-demand_ci.yml new file mode 100644 index 0000000..760ecfb --- /dev/null +++ b/.github/workflows/on-demand_ci.yml @@ -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 diff --git a/Vagrantfile b/Vagrantfile index 33b03a9..3122d3d 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -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' } diff --git a/scripts/_assertions.sh b/scripts/_assertions.sh new file mode 100755 index 0000000..5898b07 --- /dev/null +++ b/scripts/_assertions.sh @@ -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" +} diff --git a/scripts/configure_test.sh b/scripts/configure_test.sh new file mode 100755 index 0000000..ef8ac15 --- /dev/null +++ b/scripts/configure_test.sh @@ -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 diff --git a/scripts/install_test.sh b/scripts/install_test.sh new file mode 100755 index 0000000..ccb48dd --- /dev/null +++ b/scripts/install_test.sh @@ -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 diff --git a/scripts/main.sh b/scripts/main.sh index 0655dd6..c69c869 100755 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -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