diff --git a/internal/handlers/lokistack_create.go b/internal/handlers/lokistack_create.go index 21b73aca82..1b50510b2c 100644 --- a/internal/handlers/lokistack_create.go +++ b/internal/handlers/lokistack_create.go @@ -3,6 +3,7 @@ package handlers import ( "context" + "github.com/ViaQ/logerr/kverrors" "github.com/ViaQ/logerr/log" lokiv1beta1 "github.com/ViaQ/loki-operator/api/v1beta1" "github.com/ViaQ/loki-operator/internal/external/k8s" @@ -25,6 +26,7 @@ func CreateLokiStack(ctx context.Context, req ctrl.Request, k k8s.Client) error ll.Error(err, "could not find the requested loki stack", "name", req.NamespacedName) return nil } + return kverrors.Wrap(err, "failed to lookup lokistack", "name", req.NamespacedName) } // Here we will translate the lokiv1beta1.LokiStack options into manifest options diff --git a/internal/handlers/lokistack_create_test.go b/internal/handlers/lokistack_create_test.go index 917b8510fd..c02489a49f 100644 --- a/internal/handlers/lokistack_create_test.go +++ b/internal/handlers/lokistack_create_test.go @@ -2,6 +2,7 @@ package handlers_test import ( "context" + "errors" "flag" "io/ioutil" "os" @@ -60,6 +61,30 @@ func TestCreateLokiStack_WhenGetReturnsNotFound_DoesNotError(t *testing.T) { require.Zero(t, k.CreateCallCount()) } + +func TestCreateLokiStack_WhenGetReturnsAnErrorOtherThanNotFound_ReturnsTheError(t *testing.T) { + k := &k8sfakes.FakeClient{} + r := ctrl.Request{ + NamespacedName: types.NamespacedName{ + Name: "my-stack", + Namespace: "some-ns", + }, + } + + badRequestErr := apierrors.NewBadRequest("you do not belong here") + k.GetStub = func(ctx context.Context, name types.NamespacedName, object client.Object) error { + return badRequestErr + } + + err := handlers.CreateLokiStack(context.TODO(), r, k) + + require.Equal(t, badRequestErr, errors.Unwrap(err)) + + // make sure create was NOT called because the Get failed + require.Zero(t, k.CreateCallCount()) +} + + func TestCreateLokiStack_SetsNamespaceOnAllObjects(t *testing.T) { k := &k8sfakes.FakeClient{} r := ctrl.Request{