Skip to content

Commit

Permalink
Adding test for installing and deleting virtual cluster from kubectl
Browse files Browse the repository at this point in the history
  • Loading branch information
Sowmya viswam authored and Sowmya viswam committed Aug 2, 2024
1 parent 04edf4b commit 35ac965
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 13 deletions.
47 changes: 34 additions & 13 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,30 @@ jobs:

# - name: Setup upterm session for debugging
# uses: lhotari/action-upterm@v1

- name: Setup Environment
run: |
sudo apt-get install -y sed
sed -i "s|REPLACE_REPOSITORY_NAME|${{ env.REPOSITORY_NAME }}|g" ${{ matrix.test-suite-path }}/../commonValues.yaml
sed -i "s|REPLACE_TAG_NAME|${{ env.TAG_NAME }}|g" ${{ matrix.test-suite-path }}/../commonValues.yaml
kind load image-archive vcluster_syncer
chmod +x vcluster && sudo mv vcluster /usr/bin
- name: Check if values.yaml exists
id: check-values
run: |
if [[ -f "${{ matrix.test-suite-path }}/values.yaml" ]]; then
echo "values_yaml_exists=true" >> $GITHUB_ENV
else
echo "values_yaml_exists=false" >> $GITHUB_ENV
fi
- name: Create vcluster
id: create-vcluster
if: env.values_yaml_exists == 'true'
run: |
set -x
Expand All @@ -346,15 +367,6 @@ jobs:
kubectl apply -f ${{ matrix.test-suite-path }}/role.yaml
fi
sudo apt-get install -y sed
sed -i "s|REPLACE_REPOSITORY_NAME|${{ env.REPOSITORY_NAME }}|g" ${{ matrix.test-suite-path }}/../commonValues.yaml
sed -i "s|REPLACE_TAG_NAME|${{ env.TAG_NAME }}|g" ${{ matrix.test-suite-path }}/../commonValues.yaml
kind load image-archive vcluster_syncer
chmod +x vcluster && sudo mv vcluster /usr/bin
vcluster create ${{ env.VCLUSTER_SUFFIX }} -n ${{ env.VCLUSTER_NAMESPACE }} \
--create-namespace \
--debug \
Expand All @@ -369,7 +381,7 @@ jobs:

- name: Wait until vcluster is ready
id: wait-until-vcluster-is-ready
if: steps.create-vcluster.outcome == 'success'
if: env.values_yaml_exists == 'true' && steps.create-vcluster.outcome == 'success'
run: |
set -x
Expand All @@ -378,7 +390,7 @@ jobs:
continue-on-error: true

- name: Collect deployment information in case vcluster fails to start
if: steps.wait-until-vcluster-is-ready.outcome != 'success'
if: env.values_yaml_exists == 'true' && steps.wait-until-vcluster-is-ready.outcome != 'success'
run: |
set -x
kubectl get pods -o yaml -n ${{ env.VCLUSTER_NAMESPACE }}
Expand All @@ -404,6 +416,15 @@ jobs:
sudo chmod +x $(echo "${{ matrix.test-suite-path }}" | sed "s#./test/##g").test
VCLUSTER_SUFFIX=${{ env.VCLUSTER_SUFFIX }} VCLUSTER_NAME=${{ env.VCLUSTER_NAME }} VCLUSTER_NAMESPACE=${{ env.VCLUSTER_NAMESPACE }} MULTINAMESPACE_MODE=${{ matrix.multinamespace-mode }} ./$(echo "${{ matrix.test-suite-path }}" | sed "s#./test/##g").test -test.v --ginkgo.v --ginkgo.skip='.*NetworkPolicy.*' --ginkgo.fail-fast
continue-on-error: true

- name: check vCluster logs
id: check-vcluster-logs
if: env.values_yaml_exists == 'true'
run: |
set -x
if kubectl logs -l app=${{ env.VCLUSTER_SUFFIX }} -n ${{ env.VCLUSTER_NAMESPACE }} -c syncer --tail=-1 -p >/dev/null 2>/dev/null; then
echo "vCluster has restarted during testing, failing..."
exit 1
Expand All @@ -412,7 +433,7 @@ jobs:
continue-on-error: true

- name: Print logs if e2e tests fail
if: steps.execute-e2e-tests.outcome == 'failure'
if: env.values_yaml_exists == 'true' && steps.execute-e2e-tests.outcome == 'failure'
run: |
set -x
kubectl get pods -o yaml -n ${{ env.VCLUSTER_NAMESPACE }}
Expand All @@ -422,4 +443,4 @@ jobs:
kubectl logs -l app=${{ env.VCLUSTER_SUFFIX }} -n ${{ env.VCLUSTER_NAMESPACE }} -c syncer --tail=-1 -p || kubectl logs -l app=${{ env.VCLUSTER_SUFFIX }} -n ${{ env.VCLUSTER_NAMESPACE }} -c syncer --tail=-1
echo "======================================================================================================================"
kubectl describe pods -n ${{ env.VCLUSTER_NAMESPACE }}
exit 1
exit 1
65 changes: 65 additions & 0 deletions test/e2e_vcluster_install/deploy_and_delete_vcluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package e2evclusterinstall

import (
"os"
"os/exec"
"strings"
"time"

"github.com/loft-sh/vcluster/pkg/platform/random"
"github.com/loft-sh/vcluster/test/framework"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

const (
pollingInterval = time.Second * 2
pollingDurationLong = time.Minute * 2
vclusterRepo = "https://charts.loft.sh"
filePath = "../commonValues.yaml"
outputFile = "output.yaml"
)

var _ = ginkgo.Describe("Deploy and Delete vCluster", func() {
ginkgo.It("should deploy a vCluster using kubectl and delete it using kubectl", func() {
vClusterName := "t-cluster-" + random.String(6)
vClusterNamespace := "t-ns-" + random.String(6)
file, err := os.Create(outputFile)
framework.ExpectNoError(err)
defer file.Close()

createNamespaceCmd := exec.Command("kubectl", "create", "namespace", vClusterNamespace)
err = createNamespaceCmd.Run()
framework.ExpectNoError(err)

helmCmd := exec.Command("helm", "template", vClusterName, "vcluster", "--repo", vclusterRepo, "-n", vClusterNamespace, "-f", filePath)
helmCmd.Stdout = file
err = helmCmd.Run()
framework.ExpectNoError(err)

kubectlCmd := exec.Command("kubectl", "apply", "-f", outputFile)
err = kubectlCmd.Run()
framework.ExpectNoError(err)

gomega.Eventually(func() bool {

checkCmd := exec.Command("vcluster", "list")
output, err := checkCmd.CombinedOutput()
framework.ExpectNoError(err)
return err == nil && strings.Contains(string(output), vClusterName) && strings.Contains(string(output), "Running")
}).WithPolling(pollingInterval).WithTimeout(pollingDurationLong).Should(gomega.BeTrue())

deleteCmd := exec.Command("kubectl", "delete", "-f", outputFile)
err = deleteCmd.Run()
framework.ExpectNoError(err)

gomega.Eventually(func() bool {

checkCmd := exec.Command("vcluster", "list")
output, err := checkCmd.CombinedOutput()
framework.ExpectNoError(err)
return err == nil && !strings.Contains(string(output), vClusterName)
}).WithPolling(pollingInterval).WithTimeout(pollingDurationLong).Should(gomega.BeTrue())

})
})
13 changes: 13 additions & 0 deletions test/e2e_vcluster_install/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package e2evclusterinstall

import (
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

func TestRunE2ETests(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Vcluster Install and Delete Suite")
}

0 comments on commit 35ac965

Please sign in to comment.