Skip to content

Commit

Permalink
Merge branch 'main' into fix-make-test-lints
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinkumarsingh092 authored Mar 15, 2022
2 parents e6b8b80 + cc9ae9b commit 8796492
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: turn off swap
run: sudo swapoff -a

- name: Set netfilter conntrack max
run: sudo sysctl -w net.netfilter.nf_conntrack_max=131072

- name: Set up Go
uses: actions/setup-go@v2
with:
Expand Down
31 changes: 16 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,6 @@ all: build

HOST_AGENT_DIR ?= agent

# Run tests
test: generate fmt vet manifests test-coverage

test-coverage:
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo --randomizeAllSpecs -r --cover --coverprofile=cover.out --outputdir=. --skipPackage=test .

agent-test:
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo --randomizeAllSpecs -r $(HOST_AGENT_DIR) -coverprofile cover.out

controller-test:
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo --randomizeAllSpecs controllers/infrastructure -coverprofile cover.out

webhook-test:
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo apis/infrastructure/v1beta1 -coverprofile cover.out

##@ General

# The help target prints out all targets with their descriptions organized
Expand Down Expand Up @@ -127,6 +112,22 @@ prepare-byoh-docker-host-image:
prepare-byoh-docker-host-image-dev:
docker build test/e2e -f docs/BYOHDockerFileDev -t ${BYOH_BASE_IMG_DEV}


# Run tests
test: generate fmt vet manifests test-coverage

test-coverage: prepare-byoh-docker-host-image
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo --randomizeAllSpecs -r --cover --coverprofile=cover.out --outputdir=. --skipPackage=test .

agent-test: prepare-byoh-docker-host-image
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo --randomizeAllSpecs -r $(HOST_AGENT_DIR) -coverprofile cover.out

controller-test:
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo --randomizeAllSpecs controllers/infrastructure -coverprofile cover.out

webhook-test:
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; ginkgo apis/infrastructure/v1beta1 -coverprofile cover.out

test-e2e: take-user-input docker-build prepare-byoh-docker-host-image $(GINKGO) cluster-templates-e2e ## Run the end-to-end tests
$(GINKGO) -v -trace -tags=e2e -focus="$(GINKGO_FOCUS)" $(_SKIP_ARGS) -nodes=$(GINKGO_NODES) --noColor=$(GINKGO_NOCOLOR) $(GINKGO_ARGS) test/e2e -- \
-e2e.artifacts-folder="$(ARTIFACTS)" \
Expand Down
63 changes: 62 additions & 1 deletion agent/host_agent_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
package main

import (
"context"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
dClient "github.com/docker/docker/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
infrastructurev1beta1 "github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/apis/infrastructure/v1beta1"
"github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/test/e2e"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand All @@ -30,7 +35,19 @@ var (
kubeconfigFile *os.File
k8sClient client.Client
tmpFilePrefix = "kubeconfigFile-"
defaultByoMachineName = "my-byomachine"
agentLogFile = "/tmp/agent-integration.log"
fakeKubeConfig = "fake-kubeconfig-path"
fakeDownloadPath = "fake-download-path"
fakeBootstrapSecret = "fake-bootstrap-secret"
testEnv *envtest.Environment
dockerClient *dClient.Client
)

const (
bundleLookupBaseRegistry = "projects.registry.vmware.com/cluster_api_provider_bringyourownhost"
BundleLookupTag = "v0.1.0_alpha.2"
K8sVersion = "v1.22.3"
)

func TestHostAgent(t *testing.T) {
Expand All @@ -52,6 +69,7 @@ var _ = BeforeSuite(func() {
CRDDirectoryPaths: []string{
filepath.Join("..", "config", "crd", "bases"),
filepath.Join(build.Default.GOPATH, "pkg", "mod", "sigs.k8s.io", "[email protected]", "config", "crd", "bases"),
filepath.Join(build.Default.GOPATH, "pkg", "mod", "sigs.k8s.io", "[email protected]", "bootstrap", "kubeadm", "config", "crd", "bases"),
},

ErrorIfCRDPathMissing: true,
Expand All @@ -74,10 +92,14 @@ var _ = BeforeSuite(func() {

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
writeKubeConfig()

dockerClient, err = dClient.NewClientWithOpts(dClient.FromEnv)
Expect(err).NotTo(HaveOccurred())

pathToHostAgentBinary, err = gexec.Build("github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent")
Expect(err).NotTo(HaveOccurred())

writeKubeConfig()
})

var _ = AfterSuite(func() {
Expand Down Expand Up @@ -110,3 +132,42 @@ func writeKubeConfig() {
_, err = getKubeConfig().Write(kubeConfigData)
Expect(err).NotTo(HaveOccurred())
}

func setupTestInfra(ctx context.Context, hostname, kubeconfig string, namespace *corev1.Namespace) *e2e.ByoHostRunner {
byohostRunner := e2e.ByoHostRunner{
Context: ctx,
Namespace: namespace.Name,
PathToHostAgentBinary: pathToHostAgentBinary,
DockerClient: dockerClient,
NetworkInterface: "host",
ByoHostName: hostname,
Port: testEnv.ControlPlane.APIServer.Port,
CommandArgs: map[string]string{
"--kubeconfig": "/mgmt.conf",
"--namespace": namespace.Name,
"-v": "1",
},
KubeconfigFile: kubeconfig,
}

return &byohostRunner
}

func cleanup(ctx context.Context, byoHostContainer *container.ContainerCreateCreatedBody, namespace *corev1.Namespace, agentLogFile string) {
err := dockerClient.ContainerStop(ctx, byoHostContainer.ID, nil)
Expect(err).NotTo(HaveOccurred())

err = dockerClient.ContainerRemove(ctx, byoHostContainer.ID, dockertypes.ContainerRemoveOptions{})
Expect(err).NotTo(HaveOccurred())

err = k8sClient.Delete(ctx, namespace)
Expect(err).NotTo(HaveOccurred(), "failed to delete test namespace")

_, err = os.Stat(agentLogFile)
if err == nil {
err = os.Remove(agentLogFile)
if err != nil {
e2e.Showf("error removing log file %s: %v", agentLogFile, err)
}
}
}
Loading

0 comments on commit 8796492

Please sign in to comment.