diff --git a/go.sum b/go.sum index 625ff033e..271420f85 100644 --- a/go.sum +++ b/go.sum @@ -530,6 +530,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= github.com/pquerna/ffjson v0.0.0-20180717144149-af8b230fcd20/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M= @@ -617,6 +618,7 @@ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoH github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/syndtr/gocapability v0.0.0-20160928074757-e7cb7fa329f4/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/thecodeteam/goscaleio v0.1.0/go.mod h1:68sdkZAsK8bvEwBlbQnlLS+xU+hvLYM/iQ8KXej1AwM= diff --git a/pkg/controller/humiocluster/humiocluster_controller_test.go b/pkg/controller/humiocluster/humiocluster_controller_test.go new file mode 100644 index 000000000..d834bc395 --- /dev/null +++ b/pkg/controller/humiocluster/humiocluster_controller_test.go @@ -0,0 +1,86 @@ +package humiocluster + +import ( + "context" + "fmt" + "testing" + + humioClusterv1alpha1 "github.com/humio/humio-operator/pkg/apis/core/v1alpha1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" + + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +var ( + humioCluster1 = &humioClusterv1alpha1.HumioCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "humiocluster", + Namespace: "logging", + }, + Spec: humioClusterv1alpha1.HumioClusterSpec{ + Image: "humio/humio-core", + Version: "1.9.0", + TargetReplicationFactor: 1, + }, + } +) + +func TestReconcileHumioCluster_Reconcile(t *testing.T) { + // Set the logger to development mode for verbose logs. + logf.SetLogger(logf.ZapLogger(true)) + + // Objects to track in the fake client. + objs := []runtime.Object{ + humioCluster1, + } + + // Register operator types with the runtime scheme. + s := scheme.Scheme + s.AddKnownTypes(humioClusterv1alpha1.SchemeGroupVersion, humioCluster1) + + // Create a fake client to mock API calls. + cl := fake.NewFakeClient(objs...) + // Create a ReconcileHumioCluster object with the scheme and fake client. + r := &ReconcileHumioCluster{client: cl, scheme: s} + + // Mock request to simulate Reconcile() being called on an event for a + // watched resource . + req := reconcile.Request{ + NamespacedName: types.NamespacedName{ + Name: humioCluster1.ObjectMeta.Name, + Namespace: humioCluster1.ObjectMeta.Namespace, + }, + } + res, err := r.Reconcile(req) + if err != nil { + t.Fatalf("reconcile: (%v)", err) + } + + pod := &corev1.Pod{} + err = cl.Get(context.TODO(), types.NamespacedName{Name: fmt.Sprintf("%s-pod", humioCluster1.ObjectMeta.Name), Namespace: humioCluster1.ObjectMeta.Namespace}, pod) + if err != nil { + t.Fatalf("get pod: (%v). %+v", err, pod) + } + + // Reconcile again so Reconcile() checks pods and updates the HumioCluster resources' Status. + res, err = r.Reconcile(req) + if err != nil { + t.Fatalf("reconcile: (%v)", err) + } + if res != (reconcile.Result{}) { + t.Error("reconcile did not return an empty Result") + } + + // Get the updated HumioCluster object. + humioCluster := &humioClusterv1alpha1.HumioCluster{} + err = r.client.Get(context.TODO(), req.NamespacedName, humioCluster) + if err != nil { + t.Errorf("get HumioCluster: (%v)", err) + } +}