Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Integration Tests to Open Source #58

Open
tunahanertekin opened this issue Jan 9, 2023 · 1 comment
Open

Migrate Integration Tests to Open Source #58

tunahanertekin opened this issue Jan 9, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request operator Robot specifications Kubernetes domain

Comments

@tunahanertekin
Copy link
Member

What would you like to be added?

Kubebuilder uses EnvTest for integration tests. robolaunch Test Suite should be open source to make contributors able to test their code.

Why is this needed?

It's needed to migrate CI/CT/CD operations to open source.

@tunahanertekin tunahanertekin added enhancement New feature or request operator Robot specifications Kubernetes domain labels Jan 9, 2023
@tunahanertekin tunahanertekin self-assigned this Jan 9, 2023
@tunahanertekin
Copy link
Member Author

/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package relay_server

import (
	"context"
	"path/filepath"
	"testing"
	"time"

	. "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/apimachinery/pkg/types"
	"k8s.io/client-go/kubernetes/scheme"
	"k8s.io/client-go/rest"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/envtest"
	logf "sigs.k8s.io/controller-runtime/pkg/log"
	"sigs.k8s.io/controller-runtime/pkg/log/zap"

	robotv1alpha1 "github.com/robolaunch/robot-operator/pkg/api/roboscale.io/v1alpha1"
	//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc

func TestAPIs(t *testing.T) {
	RegisterFailHandler(Fail)

	RunSpecs(t, "Controller Suite")
}

var _ = BeforeSuite(func() {
	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

	ctx, cancel = context.WithCancel(context.TODO())

	By("bootstrapping test environment")
	testEnv = &envtest.Environment{
		CRDDirectoryPaths:     []string{filepath.Join("..", "..", "..", "..", "config", "crd", "bases")},
		ErrorIfCRDPathMissing: true,
	}

	var err error
	// cfg is defined in this file globally.
	cfg, err = testEnv.Start()
	Expect(err).NotTo(HaveOccurred())
	Expect(cfg).NotTo(BeNil())

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

	//+kubebuilder:scaffold:scheme

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

	// start a manager
	k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
		Scheme: scheme.Scheme,
	})
	Expect(err).ToNot(HaveOccurred())

	err = (&RelayServerReconciler{
		Client: k8sManager.GetClient(),
		Scheme: k8sManager.GetScheme(),
	}).SetupWithManager(k8sManager)
	Expect(err).ToNot(HaveOccurred())

	go func() {
		defer GinkgoRecover()
		err = k8sManager.Start(ctx)
		Expect(err).ToNot(HaveOccurred(), "failed to run manager")
	}()

})

var _ = AfterSuite(func() {
	cancel()
	By("tearing down the test environment")
	err := testEnv.Stop()
	Expect(err).NotTo(HaveOccurred())
})

var _ = Describe("RelayServer controller", func() {
	const (
		resourceName = "test"
		namespace    = "default"

		timeout  = time.Second * 60
		duration = time.Second * 60
		interval = time.Millisecond * 250
	)

	Context("desc1", func() {
		It("desc2", func() {
			By("Creating a new RelayServer")
			relayServer := robotv1alpha1.RelayServer{
				ObjectMeta: metav1.ObjectMeta{
					Name:      resourceName,
					Namespace: namespace,
				},
				Spec: robotv1alpha1.RelayServerSpec{
					Hostname:        "cloudy-dev-ide",
					Subdomain:       "cloudy-dev-ide-cloudy-mini-agv",
					InstanceName:    "cloudy-mini-agv",
					RemoteNamespace: namespace,
					RemotePort:      9000,
					RootDNSConfig: robotv1alpha1.RootDNSConfig{
						Host: "tuna.robolaunch.cloud",
					},
					TLSSecretReference: robotv1alpha1.TLSSecretReference{
						Name:      "webhook-server-cert",
						Namespace: "robot-system",
					},
				},
			}
			Expect(k8sClient.Create(ctx, &relayServer)).Should(Succeed())
			By("By checking that RelayServer is available")
			Eventually(func() error {
				rsInstance := robotv1alpha1.RelayServer{}
				err := k8sClient.Get(ctx, types.NamespacedName{
					Namespace: namespace,
					Name:      resourceName,
				}, &rsInstance)
				if err != nil {
					return err
				}
				return nil
			}, timeout, interval).Should(BeNil())
			By("By checking that RelayServer's phase is CreatingPod")
			Eventually(func() bool {
				rsInstance := robotv1alpha1.RelayServer{}
				err := k8sClient.Get(ctx, types.NamespacedName{
					Namespace: namespace,
					Name:      resourceName,
				}, &rsInstance)
				if err != nil {
					return false
				}
				return rsInstance.Status.Phase == robotv1alpha1.RelayServerPhaseCreatingPod && rsInstance.Status.PodStatus.Phase == string(corev1.PodRunning)
			}, timeout, interval).Should(BeTrue())

			By("By checking that RelayServer's phase is CreatingService")
			Eventually(func() bool {
				rsInstance := robotv1alpha1.RelayServer{}
				err := k8sClient.Get(ctx, types.NamespacedName{
					Namespace: namespace,
					Name:      resourceName,
				}, &rsInstance)
				if err != nil {
					return false
				}
				return rsInstance.Status.Phase == robotv1alpha1.RelayServerPhaseCreatingService
			}, timeout, interval).Should(BeTrue())
		})
	})

})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request operator Robot specifications Kubernetes domain
Projects
Development

No branches or pull requests

1 participant