-
Notifications
You must be signed in to change notification settings - Fork 16
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
Refactor Controller Unit Tests #118
Changes from 3 commits
8c20657
e4527ee
c068b59
199b494
b3ced39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,17 +21,18 @@ import ( | |
"path/filepath" | ||
"testing" | ||
|
||
"github.com/medik8s/common/pkg/lease" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/record" | ||
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" | ||
metricsServer "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
|
||
nodemaintenancev1beta1 "github.com/medik8s/node-maintenance-operator/api/v1beta1" | ||
//+kubebuilder:scaffold:imports | ||
|
@@ -40,10 +41,15 @@ import ( | |
// 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 ctxFromSignalHandler context.Context | ||
var ( | ||
cfg *rest.Config | ||
k8sClient client.Client | ||
testEnv *envtest.Environment | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
fakeRecorder *record.FakeRecorder | ||
r *NodeMaintenanceReconciler | ||
) | ||
|
||
func TestControllers(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
@@ -55,9 +61,6 @@ var _ = BeforeSuite(func() { | |
|
||
// call start or refactor when moving to "normal" testEnv test | ||
|
||
}) | ||
|
||
func startTestEnv() { | ||
By("bootstrapping test environment") | ||
testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, | ||
|
@@ -74,40 +77,38 @@ func startTestEnv() { | |
Expect(err).NotTo(HaveOccurred()) | ||
|
||
//+kubebuilder:scaffold:scheme | ||
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{Scheme: scheme.Scheme}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(k8sManager).NotTo(BeNil()) | ||
|
||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(k8sClient).NotTo(BeNil()) | ||
|
||
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ | ||
Scheme: scheme.Scheme, | ||
Metrics: metricsServer.Options{BindAddress: "0"}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
mockManager, _ := lease.NewManager(k8sClient, "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: there are a lot of managers involved, maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Create a ReconcileNodeMaintenance object with the scheme and fake client | ||
r = &NodeMaintenanceReconciler{ | ||
Client: k8sClient, | ||
Scheme: scheme.Scheme, | ||
LeaseManager: &mockLeaseManager{mockManager}, | ||
logger: ctrl.Log.WithName("unit test"), | ||
} | ||
Expect(initDrainer(r, cfg)).To(Succeed()) | ||
// in test pods are not evicted, so don't wait forever for them | ||
r.drainer.SkipWaitForDeleteTimeoutSeconds = 0 | ||
|
||
// comment in when moving to "normal" testEnv test | ||
// this isn't needed atm, because the controller tests call relevant funcs of the controller themself | ||
//err = (&NodeMaintenanceReconciler{ | ||
// Client: k8sManager.GetClient(), | ||
// Scheme: k8sManager.GetScheme(), | ||
//}).SetupWithManager(k8sManager) | ||
//Expect(err).ToNot(HaveOccurred()) | ||
err = (r).SetupWithManager(k8sManager) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
go func() { | ||
if ctxFromSignalHandler == nil { | ||
ctxFromSignalHandler = ctrl.SetupSignalHandler() | ||
} | ||
err = k8sManager.Start(ctxFromSignalHandler) | ||
Expect(err).ToNot(HaveOccurred()) | ||
// https://github.com/kubernetes-sigs/controller-runtime/issues/1571 | ||
ctx, cancel = context.WithCancel(ctrl.SetupSignalHandler()) | ||
Expect(k8sManager.Start(ctx)).To(Succeed()) | ||
}() | ||
} | ||
|
||
var _ = AfterSuite(func() { | ||
// call stop or refactor when moving to "normal" testEnv test | ||
}) | ||
|
||
func stopTestEnv() { | ||
var _ = AfterSuite(func() { | ||
By("tearing down the test environment") | ||
err := testEnv.Stop() | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
cancel() | ||
Expect(testEnv.Stop()).To(Succeed()) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
about the file name: eiher just keep suite_test.go (good enough IMHO), or use <package_name>_suite_test.go please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to go with
<package_name>_suite_test.go
, and I forgot the suffixs
👍🏻There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW should
nodemaintenance_controller.go
andnodemaintenance_controller_test
follow the same pattern? Since they both use "controller" and not "controllers"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, each file is about a single controller, not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the package can contain multiple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, as SNR has multiple controllers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filename still is wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done