From ffb2aee6cf030b9edd3d47dbce812f17987aba53 Mon Sep 17 00:00:00 2001 From: Wil Simpson Date: Thu, 21 Mar 2024 10:19:29 -0400 Subject: [PATCH] Adding e2e testing --- Makefile | 6 ++- test/e2e/character_test.go | 12 +++++ test/e2e/chat_test.go | 12 +++++ test/e2e/e2e_suite_test.go | 102 +++++++++++++++++++++++++++++++++++ test/e2e/gamebackend_test.go | 12 +++++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 test/e2e/character_test.go create mode 100644 test/e2e/chat_test.go create mode 100644 test/e2e/e2e_suite_test.go create mode 100644 test/e2e/gamebackend_test.go diff --git a/Makefile b/Makefile index 6b33168..65c0011 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,11 @@ CMDS = $(shell find $(ROOT_DIR)/cmd -maxdepth 1 -mindepth 1 -type d | sed "s/^.* .PHONY: test report mocks clean-mocks report-watch test: - ginkgo --race -p --cover -covermode atomic -coverprofile=coverage.out --output-dir $(ROOT_DIR)/ $(ROOT_DIR)/... + ginkgo --race -p --cover -covermode atomic -coverprofile=coverage.out --output-dir $(ROOT_DIR)/ $(ROOT_DIR)/pkg/... + +test-e2e: + @echo "Starting e2e integration tests" + ginkgo --race -p $(ROOT_DIR)/test/e2e/... test-watch: ginkgo watch --race -p --cover -covermode atomic -output-dir=$(ROOT_DIR) $(ROOT_DIR)/... diff --git a/test/e2e/character_test.go b/test/e2e/character_test.go new file mode 100644 index 0000000..3ef2dd5 --- /dev/null +++ b/test/e2e/character_test.go @@ -0,0 +1,12 @@ +package e2e_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Character e2e", func() { + It("should work", func() { + Expect(true).To(BeTrue()) + }) +}) diff --git a/test/e2e/chat_test.go b/test/e2e/chat_test.go new file mode 100644 index 0000000..c0e08ce --- /dev/null +++ b/test/e2e/chat_test.go @@ -0,0 +1,12 @@ +package e2e_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Chat e2e", func() { + It("should work", func() { + Expect(true).To(BeTrue()) + }) +}) diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go new file mode 100644 index 0000000..0844500 --- /dev/null +++ b/test/e2e/e2e_suite_test.go @@ -0,0 +1,102 @@ +package e2e_test + +import ( + "bytes" + "context" + "encoding/gob" + "os" + "path/filepath" + "testing" + + "github.com/bxcodec/faker/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" +) + +var ( + clientset *kubernetes.Clientset + testData *setupData +) + +func TestE2e(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "E2E Suite") +} + +type setupData struct { + // Namespace used for SRO + Namespace string + + // Namespace used for Agones + AgonesNamespace string +} + +var _ = SynchronizedBeforeSuite(func(ctx context.Context) []byte { + testData := setupData{ + Namespace: "sro-testing-" + faker.Username(), + AgonesNamespace: "agones-system-testing-" + faker.Username(), + } + setupClientSet() + + Expect(clientset.CoreV1().Namespaces().Create( + ctx, + &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: testData.Namespace, + }, + }, + metav1.CreateOptions{}, + )).To(Succeed()) + Expect(clientset.CoreV1().Namespaces().Create( + ctx, + &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: testData.AgonesNamespace, + }, + }, + metav1.CreateOptions{}, + )).To(Succeed()) + + return testData.encode() +}, func(data []byte) { + testData = decodeSetupData(data) + setupClientSet() +}) + +var _ = SynchronizedAfterSuite(func() {}, func(ctx context.Context) { + Expect(clientset.CoreV1().Namespaces().Delete(ctx, testData.Namespace, metav1.DeleteOptions{})). + NotTo(HaveOccurred()) + Expect(clientset.CoreV1().Namespaces().Delete(ctx, testData.AgonesNamespace, metav1.DeleteOptions{})). + NotTo(HaveOccurred()) +}) + +func setupClientSet() { + homeDir, err := os.UserHomeDir() + Expect(err).NotTo(HaveOccurred(), "unable to get home directory") + Expect(homeDir).NotTo(BeEmpty()) + + config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homeDir, ".kube", "config")) + Expect(err).NotTo(HaveOccurred(), "unable to get kubernetes config") + + clientset, err = kubernetes.NewForConfig(config) + Expect(err).NotTo(HaveOccurred(), "kubernetes config invalid") +} + +func (s *setupData) encode() []byte { + var buf *bytes.Buffer + enc := gob.NewEncoder(buf) + Expect(enc.Encode(s)).NotTo(HaveOccurred()) + return buf.Bytes() +} + +func decodeSetupData(in []byte) *setupData { + buf := bytes.NewBuffer(in) + out := &setupData{} + dec := gob.NewDecoder(buf) + Expect(dec.Decode(out)).NotTo(HaveOccurred()) + return out +} diff --git a/test/e2e/gamebackend_test.go b/test/e2e/gamebackend_test.go new file mode 100644 index 0000000..25c1e30 --- /dev/null +++ b/test/e2e/gamebackend_test.go @@ -0,0 +1,12 @@ +package e2e_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Gamebackend e2e", func() { + It("should work", func() { + Expect(true).To(BeTrue()) + }) +})