Skip to content

Commit

Permalink
add reconciler function test using envtest
Browse files Browse the repository at this point in the history
Fix CRD and other issues for envtest

convert test function block into multiple blocks for separate dedicated operations
  • Loading branch information
abhijeet-dhumal committed Jan 2, 2024
1 parent 51363f6 commit 3f99ec7
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 119 deletions.
4 changes: 4 additions & 0 deletions controllers/appwrapper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ const (
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.Info(
"AppWrapper deleted, scaling down machineset;;;;;;;;exisecutng..............",
)

_ = log.FromContext(ctx)
// todo: Move the getOCMClusterID call out of reconcile loop.
// Only reason we are calling it here is that the client is not able to make
Expand Down
130 changes: 130 additions & 0 deletions controllers/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package controllers

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

. "github.com/onsi/gomega"
. "github.com/project-codeflare/codeflare-common/support"
mcadv1beta1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
mc "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/client/clientset/versioned"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

var (
cfg *rest.Config
k8sClient client.Client // You'll be using this client in your tests.
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
err error
)

func startEnvTest(t *testing.T) *envtest.Environment {
test := With(t)
//specify testEnv configuration
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "config", "crd", "bases"),
filepath.Join(build.Default.GOROOT, "pkg", "mod", "github.com", "project-codeflare", "[email protected]", "config", "crd", "bases"),
},
}
cfg, err = testEnv.Start()
test.Expect(err).NotTo(HaveOccurred())

return testEnv
}

func establishClient(t *testing.T) {
test := With(t)
err = mcadv1beta1.AddToScheme(scheme.Scheme)
test.Expect(err).NotTo(HaveOccurred())

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

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
test.Expect(err).ToNot(HaveOccurred())

instaScaleController := &AppWrapperReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
// Config: cfg.InstaScale.InstaScaleConfiguration,
}
err = instaScaleController.SetupWithManager(context.Background(), k8sManager)
test.Expect(err).ToNot(HaveOccurred())

go func() {

err = k8sManager.Start(ctrl.SetupSignalHandler())
test.Expect(err).ToNot(HaveOccurred())
}()

time.Sleep(5 * time.Second)
}

func teardownTestEnv(testEnv *envtest.Environment) {
if err := testEnv.Stop(); err != nil {
klog.Errorf("Error stopping test Environment : %v\n", err)
}
}

func TestReconciler(t *testing.T) {
testEnv = startEnvTest(t)
defer teardownTestEnv(testEnv)

establishClient(t)

app := &mcadv1beta1.AppWrapper{
ObjectMeta: metav1.ObjectMeta{
Name: "mnist",
Labels: map[string]string{
"orderedinstance": "test.instance1_test.instance2",
},
},
Spec: mcadv1beta1.AppWrapperSpec{
AggrResources: mcadv1beta1.AppWrapperResourceList{
GenericItems: []mcadv1beta1.AppWrapperGenericResource{
{
DesiredAvailable: 1,
CustomPodResources: []mcadv1beta1.CustomPodResourceTemplate{
{
Replicas: 1,
Requests: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("250m"),
apiv1.ResourceMemory: resource.MustParse("512Mi"),
},
Limits: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("1"),
apiv1.ResourceMemory: resource.MustParse("1G"),
},
},
},
},
},
},
},
}

mcadClient, err := mc.NewForConfig(cfg)
With(t).Expect(err).ToNot(HaveOccurred())

_, err = mcadClient.WorkloadV1beta1().AppWrappers("default").Create(With(t).Ctx(), app, metav1.CreateOptions{})
With(t).Expect(err).ToNot(HaveOccurred())

time.Sleep(3 * time.Second)
}
31 changes: 17 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ module github.com/project-codeflare/instascale
go 1.19

require (
github.com/onsi/gomega v1.27.4
github.com/openshift-online/ocm-sdk-go v0.1.327
github.com/openshift/api v0.0.0-20220411210816-c3bb724c282a
github.com/onsi/gomega v1.27.10
github.com/openshift-online/ocm-sdk-go v0.1.368
github.com/openshift/api v0.0.0-20230213134911-7ba313770556
github.com/project-codeflare/codeflare-common v0.0.0-20231213113014-78776a40b4a8
github.com/project-codeflare/multi-cluster-app-dispatcher v1.38.1
k8s.io/api v0.26.2
k8s.io/apimachinery v0.26.2
k8s.io/client-go v0.26.2
k8s.io/api v0.26.3
k8s.io/apimachinery v0.26.3
k8s.io/client-go v0.26.3
sigs.k8s.io/controller-runtime v0.14.6
)

Expand All @@ -24,7 +25,7 @@ require (
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
Expand All @@ -36,10 +37,10 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -48,20 +49,22 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openshift/client-go v0.0.0-20221019143426-16aed247da5c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/ray-project/kuberay/ray-operator v0.0.0-20231016183545-097828931d15 // indirect
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.24.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit 3f99ec7

Please sign in to comment.