diff --git a/test_framework/Dockerfile.setup b/test_framework/Dockerfile.setup index d4c035e2f1..e74a7af503 100644 --- a/test_framework/Dockerfile.setup +++ b/test_framework/Dockerfile.setup @@ -1,4 +1,4 @@ -From alpine:latest +FROM golang:1.21-alpine3.19 ARG KUBECTL_VERSION=v1.20.2 diff --git a/test_framework/Jenkinsfile b/test_framework/Jenkinsfile index 56b1a081bf..5343c389da 100644 --- a/test_framework/Jenkinsfile +++ b/test_framework/Jenkinsfile @@ -189,6 +189,73 @@ node { } } + stage ('longhorn coverage') { + if(!params.LONGHORN_UPGRADE_TEST && !params.AIR_GAP_INSTALLATION) { + sh "docker exec ${JOB_BASE_NAME}-${BUILD_NUMBER} test_framework/scripts/longhorn-coverage.sh" + + sh "docker cp ${JOB_BASE_NAME}-${BUILD_NUMBER}:/src/longhorn-tests/longhorn-manager-profile.html ." + sh "docker cp ${JOB_BASE_NAME}-${BUILD_NUMBER}:/src/longhorn-tests/longhorn-engine-profile.html ." + // sh "docker cp ${JOB_BASE_NAME}-${BUILD_NUMBER}:/src/longhorn-tests/longhorn-spdk-engine-profile.html ." + sh "docker cp ${JOB_BASE_NAME}-${BUILD_NUMBER}:/src/longhorn-tests/longhorn-instance-manager-profile.html ." + sh "docker cp ${JOB_BASE_NAME}-${BUILD_NUMBER}:/src/longhorn-tests/longhorn-share-manager-profile.html ." + sh "docker cp ${JOB_BASE_NAME}-${BUILD_NUMBER}:/src/longhorn-tests/backing-image-manager-profile.html ." + + publishHTML (target: [ + allowMissing: false, + alwaysLinkToLastBuild: false, + keepAll: true, + reportDir: '.', + reportFiles: 'longhorn-manager-profile.html', + reportName: "longhorn-manager coverage profile" + ]) + + publishHTML (target: [ + allowMissing: false, + alwaysLinkToLastBuild: false, + keepAll: true, + reportDir: '.', + reportFiles: 'longhorn-engine-profile.html', + reportName: "longhorn-engine coverage profile" + ]) + + // publishHTML (target: [ + // allowMissing: false, + // alwaysLinkToLastBuild: false, + // keepAll: true, + // reportDir: '.', + // reportFiles: 'longhorn-spdk-engine-profile.html', + // reportName: "longhorn-spdk-engine coverage profile" + // ]) + + publishHTML (target: [ + allowMissing: false, + alwaysLinkToLastBuild: false, + keepAll: true, + reportDir: '.', + reportFiles: 'longhorn-instance-manager-profile.html', + reportName: "longhorn-instance-manager coverage profile" + ]) + + + publishHTML (target: [ + allowMissing: false, + alwaysLinkToLastBuild: false, + keepAll: true, + reportDir: '.', + reportFiles: 'longhorn-share-manager-profile.html', + reportName: "longhorn-share-manager coverage profile" + ]) + + publishHTML (target: [ + allowMissing: false, + alwaysLinkToLastBuild: false, + keepAll: true, + reportDir: '.', + reportFiles: 'backing-image-manager-profile.html', + reportName: "backing-image-manager coverage profile" + ]) + } + } } catch (e) { currentBuild.result = "FAILED" throw e diff --git a/test_framework/scripts/longhorn-coverage.sh b/test_framework/scripts/longhorn-coverage.sh new file mode 100755 index 0000000000..0b45f5185b --- /dev/null +++ b/test_framework/scripts/longhorn-coverage.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +set -x + +source pipelines/utilities/kubeconfig.sh +source test_framework/scripts/longhorn-setup.sh + +copy_go-cover-dir_from_all_longhorn-manager_pods() { + local ITER=0 + local LONGHORN_MANAGER_PODS=`kubectl get pods -n longhorn-system -l app=longhorn-manager --no-headers -o custom-columns=":metadata.name"` + for pod in ${LONGHORN_MANAGER_PODS}; do + kubectl cp -n longhorn-system ${pod}:/go-cover-dir go-cover-dir-${ITER} -c longhorn-manager + ITER=$(expr $ITER + 1) + done + + mkdir -p go-cover-dir-merged + go tool covdata merge -i=$(ls | grep go-cover-dir- | xargs echo | sed 's/ /,/g') -o go-cover-dir-merged +} + +generate_text_profile_for_each_repo() { + go tool covdata textfmt -i=go-cover-dir-merged -o longhorn-manager-profile.txt -pkg=github.com/longhorn/longhorn-manager/... + go tool covdata textfmt -i=go-cover-dir-merged -o longhorn-engine-profile.txt -pkg=github.com/longhorn/longhorn-engine/... + # go tool covdata textfmt -i=go-cover-dir-merged -o longhorn-spdk-engine-profile.txt -pkg=github.com/longhorn/longhorn-spdk-engine/... + go tool covdata textfmt -i=go-cover-dir-merged -o longhorn-instance-manager-profile.txt -pkg=github.com/longhorn/longhorn-instance-manager/... + go tool covdata textfmt -i=go-cover-dir-merged -o longhorn-share-manager-profile.txt -pkg=github.com/longhorn/longhorn-share-manager/... + go tool covdata textfmt -i=go-cover-dir-merged -o backing-image-manager-profile.txt -pkg=github.com/longhorn/backing-image-manager/... +} + +generate_html_profiles_for_each_repo() { + CUSTOM_LONGHORN_MANAGER_BRANCH=${CUSTOM_LONGHORN_MANAGER_BRANCH:-"master"} + CUSTOM_LONGHORN_ENGINE_BRANCH=${CUSTOM_LONGHORN_ENGINE_BRANCH:-"master"} + # CUSTOM_LONGHORN_SPDK_ENGINE_BRANCH=${CUSTOM_LONGHORN_SPDK_ENGINE_BRANCH:-"master"} + CUSTOM_LONGHORN_INSTANCE_MANAGER_BRANCH=${CUSTOM_LONGHORN_INSTANCE_MANAGER_BRANCH:-"master"} + CUSTOM_LONGHORN_SHARE_MANAGER_BRANCH=${CUSTOM_LONGHORN_SHARE_MANAGER_BRANCH:-"master"} + CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_BRANCH=${CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_BRANCH:-"master"} + + git clone https://github.com/longhorn/longhorn-manager -b ${CUSTOM_LONGHORN_MANAGER_BRANCH} + git clone https://github.com/longhorn/longhorn-engine -b ${CUSTOM_LONGHORN_ENGINE_BRANCH} + # git clone https://github.com/longhorn/longhorn-spdk-engine -b ${CUSTOM_LONGHORN_SPDK_ENGINE_BRANCH} + git clone https://github.com/longhorn/longhorn-instance-manager -b ${CUSTOM_LONGHORN_INSTANCE_MANAGER_BRANCH} + git clone https://github.com/longhorn/longhorn-share-manager -b ${CUSTOM_LONGHORN_SHARE_MANAGER_BRANCH} + git clone https://github.com/longhorn/backing-image-manager -b ${CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_BRANCH} + + cp longhorn-manager-profile.txt longhorn-manager/profile.txt + cp longhorn-engine-profile.txt longhorn-engine/profile.txt + # cp longhorn-spdk-engine-profile.txt longhorn-spdk-engine/profile.txt + cp longhorn-instance-manager-profile.txt longhorn-instance-manager/profile.txt + cp longhorn-share-manager-profile.txt longhorn-share-manager/profile.txt + cp backing-image-manager-profile.txt backing-image-manager/profile.txt + + cd longhorn-manager && go tool cover -html=profile.txt -o ../longhorn-manager-profile.html && cd .. + cd longhorn-engine && go tool cover -html=profile.txt -o ../longhorn-engine-profile.html && cd .. + # cd longhorn-spdk-engine && go tool cover -html=profile.txt -o ../longhorn-spdk-engine-profile.html && cd .. + cd longhorn-instance-manager && go tool cover -html=profile.txt -o ../longhorn-instance-manager-profile.html && cd .. + cd longhorn-share-manager && go tool cover -html=profile.txt -o ../longhorn-share-manager-profile.html && cd .. + cd backing-image-manager && go tool cover -html=profile.txt -o ../backing-image-manager-profile.html && cd .. +} + +main() { + set_kubeconfig + + # uninstall longhorn to write coverage data + # the coverage data will be written when the program invokes os.Exit() or returns normally from main.main + # ref: https://go.dev/doc/build-cover#panicprof + uninstall_longhorn_by_chart + + # install longhorn again to copy coverage data from pods, so we don't need scp files from host + install_longhorn_by_chart + + copy_go-cover-dir_from_all_longhorn-manager_pods + generate_text_profile_for_each_repo + generate_html_profiles_for_each_repo +} + +main diff --git a/test_framework/scripts/longhorn-setup.sh b/test_framework/scripts/longhorn-setup.sh index ab3149d6c3..9bc10571cc 100755 --- a/test_framework/scripts/longhorn-setup.sh +++ b/test_framework/scripts/longhorn-setup.sh @@ -3,6 +3,7 @@ set -x source test_framework/scripts/kubeconfig.sh +source pipelines/utilities/longhorn_manifest.sh # create and clean tmpdir TMPDIR="/tmp/longhorn" @@ -237,10 +238,65 @@ install_longhorn_by_manifest(){ install_longhorn_by_chart(){ - helm install longhorn "${LONGHORN_REPO_DIR}/chart/" --namespace longhorn-system + CUSTOM_LONGHORN_MANAGER_IMAGE=${CUSTOM_LONGHORN_MANAGER_IMAGE:-"longhornio/longhorn-manager:master-head"} + CUSTOM_LONGHORN_ENGINE_IMAGE=${CUSTOM_LONGHORN_ENGINE_IMAGE:-"longhornio/longhorn-engine:master-head"} + + CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE=${CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE:-""} + CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE=${CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE:-""} + CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE=${CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE:-""} + + HELM_VARIABLES=("--set enableGoCoverDir=true") + CUSTOM_LONGHORN_MANAGER_IMAGE_REPO=$(echo $CUSTOM_LONGHORN_MANAGER_IMAGE | cut -d ":" -f 1) + CUSTOM_LONGHORN_MANAGER_IMAGE_TAG=$(echo $CUSTOM_LONGHORN_MANAGER_IMAGE | cut -d ":" -f 2) + HELM_VARIABLES+=("--set image.longhorn.manager.repository=${CUSTOM_LONGHORN_MANAGER_IMAGE_REPO}") + HELM_VARIABLES+=("--set image.longhorn.manager.tag=${CUSTOM_LONGHORN_MANAGER_IMAGE_TAG}") + + CUSTOM_LONGHORN_ENGINE_IMAGE_REPO=$(echo $CUSTOM_LONGHORN_ENGINE_IMAGE | cut -d ":" -f 1) + CUSTOM_LONGHORN_ENGINE_IMAGE_TAG=$(echo $CUSTOM_LONGHORN_ENGINE_IMAGE | cut -d ":" -f 2) + HELM_VARIABLES+=("--set image.longhorn.engine.repository=${CUSTOM_LONGHORN_ENGINE_IMAGE_REPO}") + HELM_VARIABLES+=("--set image.longhorn.engine.tag=${CUSTOM_LONGHORN_ENGINE_IMAGE_TAG}") + + # replace images if custom image is specified. + if [[ ! -z ${CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE} ]]; then + CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE_REPO=$(echo $CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE | cut -d ":" -f 1) + CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE_TAG=$(echo $CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE | cut -d ":" -f 2) + HELM_VARIABLES+=("--set image.longhorn.instanceManager.repository=${CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE_REPO}") + HELM_VARIABLES+=("--set image.longhorn.instanceManager.tag=${CUSTOM_LONGHORN_INSTANCE_MANAGER_IMAGE_TAG}") + fi + + if [[ ! -z ${CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE} ]]; then + CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE_REPO=$(echo $CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE | cut -d ":" -f 1) + CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE_TAG=$(echo $CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE | cut -d ":" -f 2) + HELM_VARIABLES+=("--set image.longhorn.shareManager.repository=${CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE_REPO}") + HELM_VARIABLES+=("--set image.longhorn.shareManager.tag=${CUSTOM_LONGHORN_SHARE_MANAGER_IMAGE_TAG}") + fi + + if [[ ! -z ${CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE} ]]; then + CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE_REPO=$(echo $CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE | cut -d ":" -f 1) + CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE_TAG=$(echo $CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE | cut -d ":" -f 2) + HELM_VARIABLES+=("--set image.longhorn.backingImageManager.repository=${CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE_REPO}") + HELM_VARIABLES+=("--set image.longhorn.backingImageManager.tag=${CUSTOM_LONGHORN_BACKING_IMAGE_MANAGER_IMAGE_TAG}") + fi + + HELM_VARIABLE_STR=$( printf " %s" "${HELM_VARIABLES[@]}" ) + echo ${HELM_VARIABLE_STR} + + helm install longhorn "${LONGHORN_REPO_DIR}/chart/" --namespace longhorn-system --create-namespace $HELM_VARIABLE_STR wait_longhorn_status_running } +uninstall_longhorn_by_chart() { + kubectl patch lhs -n longhorn-system deleting-confirmation-flag --type=json --patch-file=/dev/stdin <<-EOF +[ + { + "op": "replace", + "path": "/value", + "value": "true" + } +] +EOF + helm uninstall longhorn --namespace longhorn-system +} install_longhorn_stable(){ install_longhorn_by_manifest "${LONGHORN_STABLE_MANIFEST_URL}" @@ -467,8 +523,8 @@ main(){ run_longhorn_upgrade_test run_longhorn_tests else - generate_longhorn_yaml_manifest "${TF_VAR_tf_workspace}" - install_longhorn_by_manifest "${TF_VAR_tf_workspace}/longhorn.yaml" + get_longhorn_chart + install_longhorn_by_chart run_longhorn_tests fi }