Skip to content

Commit

Permalink
Merge pull request #29 from razo7/dummy-E2E
Browse files Browse the repository at this point in the history
Dummy e2e Test
  • Loading branch information
openshift-merge-robot authored Mar 27, 2023
2 parents d1bffd4 + 706366f commit a8fdbb6
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and

.PHONY: fmt
fmt: goimports ## Run go goimports against code - goimports = go fmt + fixing imports.
$(GOIMPORTS) -w ./main.go ./pkg ./api ./controllers
$(GOIMPORTS) -w ./main.go ./pkg ./api ./controllers ./test

.PHONY: vet
vet: ## Run go vet against code - report likely mistakes in packages.
Expand Down Expand Up @@ -178,7 +178,7 @@ test: test-no-verify verify-unchanged ## Generate and format code, run tests, ge

.PHONY: test-no-verify
test-no-verify: manifests generate go-verify fmt vet envtest ginkgo # Generate and format code, and run tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(ENVTEST_DIR)/$(ENVTEST_VERSION) -p path)" $(GINKGO) -v -r --keepGoing -requireSuite -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(ENVTEST_DIR)/$(ENVTEST_VERSION) -p path)" $(GINKGO) --v -r --keepGoing -requireSuite -coverprofile cover.out ./controllers

##@ Build

Expand Down Expand Up @@ -372,3 +372,8 @@ container-push: docker-push bundle-push catalog-build catalog-push ## Push conta

.PHONY: container-build-and-push
container-build-and-push: container-build container-push ## Build and push all the four images to quay (docker, bundle, and catalog).

.PHONY: test-e2e
test-e2e: ginkgo ## Run end to end (E2E) tests
@test -n "${KUBECONFIG}" -o -r ${HOME}/.kube/config || (echo "Failed to find kubeconfig in ~/.kube/config or no KUBECONFIG set"; exit 1)
$(GINKGO) -r --keepGoing --requireSuite --v ./test/e2e -coverprofile cover.out
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-logr/logr v1.2.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
go.uber.org/zap v1.19.1
k8s.io/api v0.23.5
k8s.io/apimachinery v0.23.5
k8s.io/client-go v0.23.5
Expand Down Expand Up @@ -49,7 +50,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
Expand Down
67 changes: 67 additions & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package e2e

import (
"fmt"
"testing"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/zap/zapcore"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/medik8s/fence-agents-remediation/api/v1alpha1"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var (
log logr.Logger
clientSet *kubernetes.Clientset
k8sClient ctrl.Client
)

func TestE2e(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "FAR e2e suite")
}

var _ = BeforeSuite(func() {
opts := zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339NanoTimeEncoder,
}
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseFlagOptions(&opts)))
log = logf.Log

// +kubebuilder:scaffold:scheme

// get the k8sClient or die
config, err := config.GetConfig()
if err != nil {
Fail(fmt.Sprintf("Couldn't get kubeconfig %v", err))
}
clientSet, err = kubernetes.NewForConfig(config)
Expect(err).NotTo(HaveOccurred())
Expect(clientSet).NotTo(BeNil())

scheme.AddToScheme(scheme.Scheme)
err = v1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

k8sClient, err = ctrl.New(config, ctrl.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())

debug()
})

func debug() {
version, _ := clientSet.ServerVersion()
fmt.Fprint(GinkgoWriter, version)
}
64 changes: 64 additions & 0 deletions test/e2e/far_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package e2e

import (
"context"
"time"

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

apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/medik8s/fence-agents-remediation/api/v1alpha1"
)

const fenceAgentDummyName = "echo"

var _ = Describe("FAR E2e", func() {
Context("fence agent - dummy", func() {
var far *v1alpha1.FenceAgentsRemediation
testNodeName := "dummy-node"

BeforeEach(func() {
testShareParam := map[v1alpha1.ParameterName]string{}
testNodeParam := map[v1alpha1.ParameterName]map[v1alpha1.NodeName]string{}
far = createFAR(testNodeName, fenceAgentDummyName, testShareParam, testNodeParam)
})

AfterEach(func() {
deleteFAR(far)
})

It("should check whether the CR has been created", func() {
testFarCR := &v1alpha1.FenceAgentsRemediation{}
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(far), testFarCR)).To(Succeed(), "failed to get FAR CR")
})
})
})

// createFAR assigns the input to FenceAgentsRemediation object, creates CR with offset, and returns the CR object
func createFAR(nodeName string, agent string, sharedParameters map[v1alpha1.ParameterName]string, nodeParameters map[v1alpha1.ParameterName]map[v1alpha1.NodeName]string) *v1alpha1.FenceAgentsRemediation {
far := &v1alpha1.FenceAgentsRemediation{
ObjectMeta: metav1.ObjectMeta{Name: nodeName},
Spec: v1alpha1.FenceAgentsRemediationSpec{
Agent: agent,
SharedParameters: sharedParameters,
NodeParameters: nodeParameters,
},
}
ExpectWithOffset(1, k8sClient.Create(context.Background(), far)).ToNot(HaveOccurred())
return far
}

// deleteFAR deletes the CR with offset
func deleteFAR(far *v1alpha1.FenceAgentsRemediation) {
EventuallyWithOffset(1, func() error {
err := k8sClient.Delete(context.Background(), far)
if apiErrors.IsNotFound(err) {
return nil
}
return err
}, 2*time.Minute, 10*time.Second).ShouldNot(HaveOccurred(), "failed to delete far")
}

0 comments on commit a8fdbb6

Please sign in to comment.