Unverified Commit 14356213 authored by Aman Sharma's avatar Aman Sharma Committed by GitHub
Browse files

changes in e2e test for diagnostic plugin (#4106)


- In place of kind cluster we have used unmanaged cluster
- test in written in GO to make it platform independent in future
Signed-off-by: default avatarAman Sharma <amansh@vmware.com>
parent 8a8b6815
No related merge requests found
Showing with 210 additions and 172 deletions
+210 -172
......@@ -83,10 +83,15 @@ jobs:
- build-release
runs-on: ${{ needs.setup-runner.outputs.ec2-instance-id }} # run the job on the newly created runner
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v3
with:
go-version: "1.17"
id: go
- name: Tests Diagnostics Plugin
run: |
cd cli/cmd/plugin/diagnostics/
make test-pr-check
make diagnostic-e2e-test
teardown-runner:
name: Stop self-hosted EC2 runner
needs:
......
......@@ -15,11 +15,27 @@ on:
- "**"
jobs:
setup-runner:
name: Start self-hosted EC2 runner
runs-on: ubuntu-latest
outputs:
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps:
- name: Start EC2 runner
id: start-ec2-runner
shell: bash
run: |
echo "GITHUB_RUN_ID: ${GITHUB_RUN_ID}"
echo "GITHUB_RUN_NUMBER: ${GITHUB_RUN_NUMBER}"
echo "GITHUB_RUN_ATTEMPT: ${GITHUB_RUN_ATTEMPT}"
INSTANCE_NAME="id-${GITHUB_RUN_ID}-${GITHUB_RUN_NUMBER}-${GITHUB_RUN_ATTEMPT}"
echo "INSTANCE_NAME: ${INSTANCE_NAME}"
echo ::set-output name=ec2-instance-id::${INSTANCE_NAME}
e2e-test-diagnostics-plugin:
# Only run this job if we're in the main repo, not a fork.
if: github.repository == 'vmware-tanzu/community-edition'
name: E2E Test - Diagnostics Plugin
runs-on: ubuntu-latest
needs: setup-runner # required to start the main job when the runner is ready
runs-on: ${{ needs.setup-runner.outputs.ec2-instance-id }} # run the job on the newly created runner
steps:
- name: Check out code
uses: actions/checkout@v1
......@@ -32,5 +48,21 @@ jobs:
- name: Run E2E Tests - Diagnostics Plugin
run: |
cd cli/cmd/plugin/diagnostics/
make e2e-test
make diagnostic-e2e-test
teardown-runner:
name: Stop self-hosted EC2 runner
needs:
- setup-runner # required to get output from the setup-runner job
- e2e-test-diagnostics-plugin # required to wait when the main job is done
runs-on: ubuntu-latest
steps:
- name: Stop EC2 runner
id: stop-ec2-runner
shell: bash
run: |
echo "GITHUB_RUN_ID: ${GITHUB_RUN_ID}"
echo "GITHUB_RUN_NUMBER: ${GITHUB_RUN_NUMBER}"
echo "GITHUB_RUN_ATTEMPT: ${GITHUB_RUN_ATTEMPT}"
INSTANCE_NAME="id-${GITHUB_RUN_ID}-${GITHUB_RUN_NUMBER}-${GITHUB_RUN_ATTEMPT}"
echo "INSTANCE_NAME: ${INSTANCE_NAME}"
......@@ -517,4 +517,7 @@ vsphere-management-and-workload-cluster-e2e-test:
unmanaged-cluster-e2e-test:
cd cli/cmd/plugin/unmanaged-cluster && BUILD_VERSION=$(BUILD_VERSION) make e2e-test
diagnostic-e2e-test:
cd cli/cmd/plugin/diagnostics && BUILD_VERSION=$(BUILD_VERSION) make e2e-test
##### E2E TESTS
# Copyright 2021 VMware Tanzu Community Edition contributors. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
ROOT_DIR := $(shell git rev-parse --show-toplevel)
.DEFAULT_GOAL:=help
help: ## Display this help message
......@@ -20,14 +22,9 @@ endif
test: ## Run unit testing suite
go test ./... -coverprofile=cover.out
.PHONY: test-pr-check
test-pr-check: ## Run e2e testing suite for PR check
./e2e-test/e2e-test.sh
.PHONY: e2e-test
e2e-test: ## Run e2e testing suite
./e2e-test/setup-e2e-test.sh
./e2e-test/e2e-test.sh
cd e2e-test && BUILD_VERSION=$(BUILD_VERSION) ROOT_DIR=$(ROOT_DIR) go test -tags=e2e -timeout 180m
build: ## Build the executable
echo "N/A: Implement building"
tanzu-diagnostics-e2e-bin
tce-installation
......@@ -2,24 +2,14 @@
## How to run E2E test
First go to diagnostics plugin directory
```bash
# if you are in TCE repo root, then
$ cd cli/cmd/plugin/diagnostics
```
Then simply run
```bash
make e2e-test
make diagnostic-e2e-test
```
Or you can directly invoke the E2E test script `e2e-test.sh`, present in this directory, from anywhere
## Internals
For testing `tanzu diagnostics collect` command, we use a `kind` cluster to quickly spin up a cluster and check if `tanzu diagnostics collect` can collect logs from the cluster. We use a single `kind` cluster and treat it as a bootstrap cluster, management cluster and a workload cluster. This is done just to keep the test simple and light weight and fast
For testing `tanzu diagnostics collect` command, we use an `Unmanaged Cluster` cluster to quickly spin up a cluster and check if `tanzu diagnostics collect` can collect logs from the cluster. We use an `Unmanaged cluster` and treat it as a bootstrap cluster, management cluster, unmanaged cluster and a workload cluster.
What we test as part of the E2E test currently - Does running the `tanzu diagnostics collect` command collect the diagnostics data from all the different kinds of clusters and create tar balls in the appropriate directory? We do this check at a very high level - check if the tar balls exist
......
#!/bin/bash
# Copyright 2021 VMware Tanzu Community Edition contributors. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
set -e
MY_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TANZU_DIAGNOSTICS_BIN=${MY_DIR}/tanzu-diagnostics-e2e-bin
CLUSTER_NAME_SUFFIX=${RANDOM}
CLUSTER_NAME="e2e-diagnostics-${CLUSTER_NAME_SUFFIX}"
CLUSTER_KUBE_CONTEXT="kind-${CLUSTER_NAME}"
OUTPUT_DIR=$(mktemp -d)
echo "Creating a kind cluster for the E2E test"
kind create cluster --name ${CLUSTER_NAME} || {
echo "Error creating kind cluster!"
exit 1
}
echo "Running tanzu diagnostics collect command"
if [[ ! -f "${TANZU_DIAGNOSTICS_BIN}" ]]; then
# we are running on a typical install of TCE
tanzu diagnostics collect --bootstrap-cluster-name ${CLUSTER_NAME} \
--management-cluster-kubeconfig "${HOME}/.kube/config" \
--management-cluster-context ${CLUSTER_KUBE_CONTEXT} \
--management-cluster-name ${CLUSTER_NAME} \
--workload-cluster-infra docker \
--workload-cluster-kubeconfig "${HOME}/.kube/config" \
--workload-cluster-context ${CLUSTER_KUBE_CONTEXT} \
--workload-cluster-name ${CLUSTER_NAME} \
--unmanaged-cluster-kubeconfig "${HOME}/.kube/config" \
--unmanaged-cluster-context ${CLUSTER_KUBE_CONTEXT} \
--unmanaged-cluster-name ${CLUSTER_NAME} \
--output-dir "${OUTPUT_DIR}" || {
echo "Error running tanzu diagnostics collect command!"
exit 1
}
else
"${TANZU_DIAGNOSTICS_BIN}" collect --bootstrap-cluster-name ${CLUSTER_NAME} \
--management-cluster-kubeconfig "${HOME}/.kube/config" \
--management-cluster-context ${CLUSTER_KUBE_CONTEXT} \
--management-cluster-name ${CLUSTER_NAME} \
--workload-cluster-infra docker \
--workload-cluster-kubeconfig "${HOME}/.kube/config" \
--workload-cluster-context ${CLUSTER_KUBE_CONTEXT} \
--workload-cluster-name ${CLUSTER_NAME} \
--unmanaged-cluster-kubeconfig "${HOME}/.kube/config" \
--unmanaged-cluster-context ${CLUSTER_KUBE_CONTEXT} \
--unmanaged-cluster-name ${CLUSTER_NAME} \
--output-dir "${OUTPUT_DIR}" || {
echo "Error running tanzu diagnostics collect command!"
exit 1
}
fi
echo "Checking if the diagnostics tar balls for the different clusters have been created"
EXPECTED_BOOTSTRAP_CLUSTER_DIAGNOSTICS="${OUTPUT_DIR}/bootstrap.${CLUSTER_NAME}.diagnostics.tar.gz"
EXPECTED_MANAGEMENT_CLUSTER_DIAGNOSTICS="${OUTPUT_DIR}/management-cluster.${CLUSTER_NAME}.diagnostics.tar.gz"
EXPECTED_WORKLOAD_CLUSTER_DIAGNOSTICS="${OUTPUT_DIR}/workload-cluster.${CLUSTER_NAME}.diagnostics.tar.gz"
EXPECTED_UNMANAGED_CLUSTER_DIAGNOSTICS="${OUTPUT_DIR}/unmanaged-cluster.${CLUSTER_NAME}.diagnostics.tar.gz"
errors=0
if [ ! -f "$EXPECTED_BOOTSTRAP_CLUSTER_DIAGNOSTICS" ]; then
echo "$EXPECTED_BOOTSTRAP_CLUSTER_DIAGNOSTICS does not exist. Expected bootstrap cluster diagnostics tar ball to be present"
((errors=errors+1))
fi
if [ ! -f "$EXPECTED_MANAGEMENT_CLUSTER_DIAGNOSTICS" ]; then
echo "$EXPECTED_MANAGEMENT_CLUSTER_DIAGNOSTICS does not exist. Expected management cluster diagnostics tar ball to be present"
((errors=errors+1))
fi
if [ ! -f "$EXPECTED_WORKLOAD_CLUSTER_DIAGNOSTICS" ]; then
echo "$EXPECTED_WORKLOAD_CLUSTER_DIAGNOSTICS does not exist. Expected workload cluster diagnostics tar ball to be present"
((errors=errors+1))
fi
if [ ! -f "$EXPECTED_UNMANAGED_CLUSTER_DIAGNOSTICS" ]; then
echo "$EXPECTED_UNMANAGED_CLUSTER_DIAGNOSTICS does not exist. Expected unmanaged cluster diagnostics tar ball to be present"
((errors=errors+1))
fi
if [[ ${errors} -gt 0 ]]; then
echo "Total E2E errors in tanzu diagnostics plugin: ${errors}"
fi
echo "Cleaning up"
kubectl config delete-context ${CLUSTER_KUBE_CONTEXT} || {
echo "Failed deleting kind cluster kube context. Please delete it manually"
}
kind delete cluster --name ${CLUSTER_NAME} || {
echo "Failed deleting kind cluster. Please delete it manually"
}
rm -rfv "${OUTPUT_DIR}"
if [[ ${errors} -gt 0 ]]; then
exit 1
fi
//go:build e2e
// Copyright 2022 VMware Tanzu Community Edition contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package e2e_test
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"log"
"math/big"
"os"
"os/exec"
"testing"
)
var myDir string
var tceRepoPath string
var tanzuDiagnosticPluginDir string
var tanzuDiagnosticBin string
var clusterName string
var clusterKubeContext string
var home string
func initializeVariable() {
// variables used across functions
myDir, _ = os.Getwd()
tceRepoPath = os.Getenv("ROOT_DIR")
tanzuDiagnosticPluginDir = myDir + "/.."
tanzuDiagnosticBin = myDir + "/tanzu-diagnostics-e2e-bin"
home = os.Getenv("HOME")
rand, _ := rand.Int(rand.Reader, big.NewInt(1000))
clusterName = "uc" + rand.String() + "test"
clusterKubeContext = "kind-" + clusterName
}
func TestSetup(t *testing.T) {
initializeVariable()
// Installing TCE
_, err := exec.LookPath("tanzu")
if err != nil {
err := os.Chdir(tceRepoPath)
if err != nil {
t.Errorf("error while changing directory %v", err)
}
err = runDeployScript("test/download-or-build-tce.sh")
if err != nil {
t.Errorf("Error while installing TCE: %v", err)
}
}
err = os.Chdir(tceRepoPath + "/cli/cmd/plugin/unmanaged-cluster")
if err != nil {
t.Errorf("error while changing directory to unmanged-cluster: %v", err)
}
// creating Unmanaged cluster
// Future Changes https://github.com/vmware-tanzu/community-edition/pull/4106#discussion_r860183492
err = cliRunner("go", nil, "run", ".", "create", clusterName)
if err != nil {
t.Errorf("Error while Unmanaged Cluster creation: %v", err)
}
}
func TestDiagnostic(t *testing.T) {
tempDir, err := os.MkdirTemp("", "diagnosticE2Etest")
if err != nil {
t.Errorf("error while creating temp directory: %v", err)
}
err = os.Chdir(tanzuDiagnosticPluginDir)
if err != nil {
t.Errorf("error while changing directory to Diagnostic plugin: %v", err)
}
diagnostic := []string{"run", ".", "collect",
"--bootstrap-cluster-name", clusterName,
"--management-cluster-kubeconfig", home + "/.kube/config",
"--management-cluster-context", clusterKubeContext,
"--management-cluster-name", clusterName,
"--workload-cluster-infra", "docker",
"--workload-cluster-kubeconfig", home + "/.kube/config",
"--workload-cluster-context", clusterKubeContext,
"--workload-cluster-name", clusterName,
"--unmanaged-cluster-kubeconfig", home + "/.kube/config",
"--unmanaged-cluster-context", clusterKubeContext,
"--unmanaged-cluster-name", clusterName,
"--output-dir", tempDir}
// Collecting logs for all the cluster
err = cliRunner("go", nil, diagnostic[:]...)
if err != nil {
t.Errorf("error while collectiong diagnostic %v", err)
}
// Checking all the log files
diagnosticFiles := [4]string{"/bootstrap." + clusterName + ".diagnostics.tar.gz",
"/management-cluster." + clusterName + ".diagnostics.tar.gz",
"/workload-cluster." + clusterName + ".diagnostics.tar.gz",
"/unmanaged-cluster." + clusterName + ".diagnostics.tar.gz"}
for _, element := range diagnosticFiles {
_, err = os.Stat(tempDir + element)
if err != nil {
t.Errorf("error while looking for %v %v", element, err)
}
}
// cleaning th temp directory
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
// deleting Unmanaged cluster
// Future Changes https://github.com/vmware-tanzu/community-edition/pull/4106#discussion_r860183492
err = os.Chdir(tceRepoPath + "/cli/cmd/plugin/unmanaged-cluster")
if err != nil {
t.Errorf("error while changing directory to unmanged-cluster: %v", err)
}
err = cliRunner("go", nil, "run", ".", "delete", clusterName)
if err != nil {
t.Errorf("Error while Unmanaged Cluster deletion: %v", err)
}
}
func runDeployScript(filename string, args ...string) error {
mwriter := io.MultiWriter(os.Stdout)
cmd := exec.Command(filename, args...)
cmd.Stderr = mwriter
cmd.Stdout = mwriter
err := cmd.Run() // blocks until sub process is complete
if err != nil {
log.Fatal(err)
}
return err
}
func cliRunner(name string, input io.Reader, args ...string) error {
var stdOut bytes.Buffer
mwriter := io.MultiWriter(&stdOut, os.Stderr)
cmd := exec.Command(name, args...)
cmd.Stdin = input
cmd.Stdout = mwriter
cmd.Stderr = mwriter
err := cmd.Run()
if err != nil {
rc := -1
if ee, ok := err.(*exec.ExitError); ok {
rc = ee.ExitCode()
}
return fmt.Errorf("%s\nexit status: %d", err.Error(), rc)
}
return err
}
#!/bin/bash
# Copyright 2021 VMware Tanzu Community Edition contributors. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
set -e
MY_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TCE_REPO_PATH="$(git rev-parse --show-toplevel)"
TCE_VERSION="v0.10.0"
echo "Installing TCE ${TCE_VERSION}"
BUILD_OS=$(uname -s | tr '[:upper:]' '[:lower:]')
TCE_RELEASE_TAR_BALL="tce-${BUILD_OS}-amd64-${TCE_VERSION}.tar.gz"
TCE_RELEASE_DIR="tce-${BUILD_OS}-amd64-${TCE_VERSION}"
INSTALLATION_DIR="${MY_DIR}/tce-installation"
"${TCE_REPO_PATH}"/hack/get-tce-release.sh ${TCE_VERSION} "${BUILD_OS}"-amd64
mkdir -p "${INSTALLATION_DIR}"
tar xzvf "${TCE_RELEASE_TAR_BALL}" --directory="${INSTALLATION_DIR}"
"${INSTALLATION_DIR}"/"${TCE_RELEASE_DIR}"/install.sh || { error "Unexpected failure during TCE installation"; exit 1; }
echo "TCE version: "
tanzu unmanaged-cluster version || { error "Unexpected failure during TCE installation"; exit 1; }
TANZU_DIAGNOSTICS_PLUGIN_DIR=${MY_DIR}/..
TANZU_DIAGNOSTICS_BIN=${MY_DIR}/tanzu-diagnostics-e2e-bin
echo "Entering ${TANZU_DIAGNOSTICS_PLUGIN_DIR} directory to build tanzu diagnostics plugin"
pushd "${TANZU_DIAGNOSTICS_PLUGIN_DIR}"
go build -o "${TANZU_DIAGNOSTICS_BIN}" -v
echo "Finished building tanzu diagnostics plugin. Leaving ${TANZU_DIAGNOSTICS_PLUGIN_DIR}"
popd
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment