Repository: kubernetes-sigs/security-profiles-operator
Pull request #380 (2021-03-19 12:14:29)

Add unit test infrastructure for recording webhook
#### What type of PR is this?

/kind cleanup


#### What this PR does / why we need it:
This adds the necessary bits to be able to run encapsulated unit tests
for the recording webhook. The coverage is still low right now but
follow-up patches will increase the overall coverage.
#### Which issue(s) this PR fixes:

<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.

Fixes #

or

None
-->
None
#### Does this PR have test?
Yes
<!--
If tests aren't applicable just write N/A.
-->

#### Special notes for your reviewer:
This is already a huge PR, so I decided to add only 3 basic tests and follow-up with increasing coverage.
#### Does this PR introduce a user-facing change?

<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

For more information on release notes see: https://git.k8s.io/community/contributors/guide/release-notes.md
-->

```release-note
None
```


k8s-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/security-profiles-operator/pull/380#" title="Author self-approved">saschagrunert</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fsecurity-profiles-operator).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubernetes-sigs/security-profiles-operator/blob/master/OWNERS)~~ [saschagrunert]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
k8s-ci-robot: @saschagrunert: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-security-profiles-operator-verify | fa58c3c3c247db7f5351e72299f7b57eadd9a223 | [link](https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_security-profiles-operator/380/pull-security-profiles-operator-verify/1372884183612919808) | `/test pull-security-profiles-operator-verify`

[Full PR test history](https://prow.k8s.io/pr-history?org=kubernetes-sigs&repo=security-profiles-operator&pr=380). [Your PR dashboard](https://prow.k8s.io/pr?query=is%3Apr%20state%3Aopen%20author%3Asaschagrunert). Please help us cut down on flakes by [linking to](https://git.k8s.io/community/contributors/devel/sig-testing/flaky-tests.md#filing-issues-for-flaky-tests) an [open issue](https://github.com/kubernetes-sigs/security-profiles-operator/issues?q=is:issue+is:open) when you hit one in your PR.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
k8s-ci-robot: @saschagrunert: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-security-profiles-operator-test-e2e | d3e5f79a30fadd3aff907a501ce9fd5f10c69711 | [link](https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_security-profiles-operator/380/pull-security-profiles-operator-test-e2e/1372885234357374976) | `/test pull-security-profiles-operator-test-e2e`

[Full PR test history](https://prow.k8s.io/pr-history?org=kubernetes-sigs&repo=security-profiles-operator&pr=380). [Your PR dashboard](https://prow.k8s.io/pr?query=is%3Apr%20state%3Aopen%20author%3Asaschagrunert). Please help us cut down on flakes by [linking to](https://git.k8s.io/community/contributors/devel/sig-testing/flaky-tests.md#filing-issues-for-flaky-tests) an [open issue](https://github.com/kubernetes-sigs/security-profiles-operator/issues?q=is:issue+is:open) when you hit one in your PR.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->
saschagrunert: /retest
JAORMX: /lgtm

Patch:
diff --git a/internal/pkg/webhooks/recording/impl.go b/internal/pkg/webhooks/recording/impl.go
new file mode 100644
index 000000000..8e921ce3d
--- /dev/null
+++ b/internal/pkg/webhooks/recording/impl.go
@@ -0,0 +1,72 @@
+/*
+Copyright 2021 The Kubernetes Authors.
+
+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 recording
+
+import (
+	"context"
+
+	"github.com/go-logr/logr"
+	"github.com/pkg/errors"
+	corev1 "k8s.io/api/core/v1"
+	"sigs.k8s.io/controller-runtime/pkg/client"
+	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
+
+	"sigs.k8s.io/security-profiles-operator/api/profilerecording/v1alpha1"
+	"sigs.k8s.io/security-profiles-operator/internal/pkg/webhooks/utils"
+)
+
+type defaultImpl struct {
+	client  client.Client
+	decoder *admission.Decoder
+}
+
+//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
+//counterfeiter:generate . impl
+type impl interface {
+	ListProfileRecordings(context.Context, ...client.ListOption) (*v1alpha1.ProfileRecordingList, error)
+	UpdateResource(context.Context, logr.Logger, client.Object, string) error
+	SetDecoder(*admission.Decoder)
+	DecodePod(admission.Request) (*corev1.Pod, error)
+}
+
+func (d *defaultImpl) ListProfileRecordings(
+	ctx context.Context, opts ...client.ListOption,
+) (*v1alpha1.ProfileRecordingList, error) {
+	profileRecordings := &v1alpha1.ProfileRecordingList{}
+	err := d.client.List(ctx, profileRecordings, opts...)
+	return profileRecordings, errors.Wrap(err, "list profile recordings")
+}
+
+func (d *defaultImpl) UpdateResource(
+	ctx context.Context,
+	logger logr.Logger,
+	object client.Object,
+	name string,
+) error {
+	return utils.UpdateResource(ctx, logger, d.client.Status(), object, name)
+}
+
+func (d *defaultImpl) SetDecoder(decoder *admission.Decoder) {
+	d.decoder = decoder
+}
+
+// nolint: gocritic
+func (d *defaultImpl) DecodePod(req admission.Request) (*corev1.Pod, error) {
+	pod := &corev1.Pod{}
+	err := d.decoder.Decode(req, pod)
+	return pod, errors.Wrap(err, "decode pod")
+}
diff --git a/internal/pkg/webhooks/recording/recording.go b/internal/pkg/webhooks/recording/recording.go
index de9edde61..003f6d896 100644
--- a/internal/pkg/webhooks/recording/recording.go
+++ b/internal/pkg/webhooks/recording/recording.go
@@ -45,9 +45,8 @@ import (
 const finalizer = "active-seccomp-profile-recording-lock"
 
 type podSeccompRecorder struct {
-	client   client.Client
+	impl
 	log      logr.Logger
-	decoder  *admission.Decoder
 	replicas sync.Map
 }
 
@@ -56,8 +55,8 @@ func RegisterWebhook(server *webhook.Server, c client.Client) {
 		"/mutate-v1-pod-recording",
 		&webhook.Admission{
 			Handler: &podSeccompRecorder{
-				client: c,
-				log:    logf.Log.WithName("recording"),
+				impl: &defaultImpl{client: c},
+				log:  logf.Log.WithName("recording"),
 			},
 		},
 	)
@@ -82,10 +81,10 @@ func (p *podSeccompRecorder) Handle(
 	ctx context.Context,
 	req admission.Request,
 ) admission.Response {
-	profileRecordings := &profilerecordingv1alpha1.ProfileRecordingList{}
-	if err := p.client.List(
-		ctx, profileRecordings, client.InNamespace(req.Namespace),
-	); err != nil {
+	profileRecordings, err := p.impl.ListProfileRecordings(
+		ctx, client.InNamespace(req.Namespace),
+	)
+	if err != nil {
 		p.log.Error(err, "Could not list profile recordings")
 		return admission.Errored(http.StatusInternalServerError, err)
 	}
@@ -93,7 +92,7 @@ func (p *podSeccompRecorder) Handle(
 	podChanged := false
 	pod := &corev1.Pod{}
 	if req.Operation != admissionv1.Delete {
-		err := p.decoder.Decode(req, pod)
+		pod, err = p.impl.DecodePod(req)
 		if err != nil {
 			p.log.Error(err, "Failed to decode pod")
 			return admission.Errored(http.StatusBadRequest, err)
@@ -241,8 +240,8 @@ func (p *podSeccompRecorder) addPod(
 		profileRecording.Status.ActiveWorkloads, podName,
 	)
 
-	if err := utils.UpdateResource(
-		ctx, p.log, p.client.Status(), profileRecording, "profilerecording status",
+	if err := p.impl.UpdateResource(
+		ctx, p.log, profileRecording, "profilerecording status",
 	); err != nil {
 		return errors.Wrap(err, "update resource on adding pod")
 	}
@@ -251,7 +250,7 @@ func (p *podSeccompRecorder) addPod(
 		controllerutil.AddFinalizer(profileRecording, finalizer)
 	}
 
-	return utils.UpdateResource(ctx, p.log, p.client, profileRecording, "profilerecording")
+	return p.impl.UpdateResource(ctx, p.log, profileRecording, "profilerecording")
 }
 
 func (p *podSeccompRecorder) removePod(
@@ -267,8 +266,8 @@ func (p *podSeccompRecorder) removePod(
 	}
 	profileRecording.Status.ActiveWorkloads = newActiveWorkloads
 
-	if err := utils.UpdateResource(
-		ctx, p.log, p.client.Status(), profileRecording, "profilerecording status",
+	if err := p.impl.UpdateResource(
+		ctx, p.log, profileRecording, "profilerecording status",
 	); err != nil {
 		return errors.Wrap(err, "update resource on removing pod")
 	}
@@ -278,10 +277,10 @@ func (p *podSeccompRecorder) removePod(
 		controllerutil.RemoveFinalizer(profileRecording, finalizer)
 	}
 
-	return utils.UpdateResource(ctx, p.log, p.client, profileRecording, "profilerecording")
+	return p.impl.UpdateResource(ctx, p.log, profileRecording, "profilerecording")
 }
 
-func (p *podSeccompRecorder) InjectDecoder(d *admission.Decoder) error {
-	p.decoder = d
+func (p *podSeccompRecorder) InjectDecoder(decoder *admission.Decoder) error {
+	p.impl.SetDecoder(decoder)
 	return nil
 }
diff --git a/internal/pkg/webhooks/recording/recordingfakes/fake_impl.go b/internal/pkg/webhooks/recording/recordingfakes/fake_impl.go
new file mode 100644
index 000000000..c70c68066
--- /dev/null
+++ b/internal/pkg/webhooks/recording/recordingfakes/fake_impl.go
@@ -0,0 +1,319 @@
+// Code generated by counterfeiter. DO NOT EDIT.
+package recordingfakes
+
+import (
+	"context"
+	"sync"
+
+	"github.com/go-logr/logr"
+	v1 "k8s.io/api/core/v1"
+	"sigs.k8s.io/controller-runtime/pkg/client"
+	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
+	"sigs.k8s.io/security-profiles-operator/api/profilerecording/v1alpha1"
+)
+
+type FakeImpl struct {
+	DecodePodStub        func(admission.Request) (*v1.Pod, error)
+	decodePodMutex       sync.RWMutex
+	decodePodArgsForCall []struct {
+		arg1 admission.Request
+	}
+	decodePodReturns struct {
+		result1 *v1.Pod
+		result2 error
+	}
+	decodePodReturnsOnCall map[int]struct {
+		result1 *v1.Pod
+		result2 error
+	}
+	ListProfileRecordingsStub        func(context.Context, ...client.ListOption) (*v1alpha1.ProfileRecordingList, error)
+	listProfileRecordingsMutex       sync.RWMutex
+	listProfileRecordingsArgsForCall []struct {
+		arg1 context.Context
+		arg2 []client.ListOption
+	}
+	listProfileRecordingsReturns struct {
+		result1 *v1alpha1.ProfileRecordingList
+		result2 error
+	}
+	listProfileRecordingsReturnsOnCall map[int]struct {
+		result1 *v1alpha1.ProfileRecordingList
+		result2 error
+	}
+	SetDecoderStub        func(*admission.Decoder)
+	setDecoderMutex       sync.RWMutex
+	setDecoderArgsForCall []struct {
+		arg1 *admission.Decoder
+	}
+	UpdateResourceStub        func(context.Context, logr.Logger, client.Object, string) error
+	updateResourceMutex       sync.RWMutex
+	updateResourceArgsForCall []struct {
+		arg1 context.Context
+		arg2 logr.Logger
+		arg3 client.Object
+		arg4 string
+	}
+	updateResourceReturns struct {
+		result1 error
+	}
+	updateResourceReturnsOnCall map[int]struct {
+		result1 error
+	}
+	invocations      map[string][][]interface{}
+	invocationsMutex sync.RWMutex
+}
+
+func (fake *FakeImpl) DecodePod(arg1 admission.Request) (*v1.Pod, error) {
+	fake.decodePodMutex.Lock()
+	ret, specificReturn := fake.decodePodReturnsOnCall[len(fake.decodePodArgsForCall)]
+	fake.decodePodArgsForCall = append(fake.decodePodArgsForCall, struct {
+		arg1 admission.Request
+	}{arg1})
+	stub := fake.DecodePodStub
+	fakeReturns := fake.decodePodReturns
+	fake.recordInvocation("DecodePod", []interface{}{arg1})
+	fake.decodePodMutex.Unlock()
+	if stub != nil {
+		return stub(arg1)
+	}
+	if specificReturn {
+		return ret.result1, ret.result2
+	}
+	return fakeReturns.result1, fakeReturns.result2
+}
+
+func (fake *FakeImpl) DecodePodCallCount() int {
+	fake.decodePodMutex.RLock()
+	defer fake.decodePodMutex.RUnlock()
+	return len(fake.decodePodArgsForCall)
+}
+
+func (fake *FakeImpl) DecodePodCalls(stub func(admission.Request) (*v1.Pod, error)) {
+	fake.decodePodMutex.Lock()
+	defer fake.decodePodMutex.Unlock()
+	fake.DecodePodStub = stub
+}
+
+func (fake *FakeImpl) DecodePodArgsForCall(i int) admission.Request {
+	fake.decodePodMutex.RLock()
+	defer fake.decodePodMutex.RUnlock()
+	argsForCall := fake.decodePodArgsForCall[i]
+	return argsForCall.arg1
+}
+
+func (fake *FakeImpl) DecodePodReturns(result1 *v1.Pod, result2 error) {
+	fake.decodePodMutex.Lock()
+	defer fake.decodePodMutex.Unlock()
+	fake.DecodePodStub = nil
+	fake.decodePodReturns = struct {
+		result1 *v1.Pod
+		result2 error
+	}{result1, result2}
+}
+
+func (fake *FakeImpl) DecodePodReturnsOnCall(i int, result1 *v1.Pod, result2 error) {
+	fake.decodePodMutex.Lock()
+	defer fake.decodePodMutex.Unlock()
+	fake.DecodePodStub = nil
+	if fake.decodePodReturnsOnCall == nil {
+		fake.decodePodReturnsOnCall = make(map[int]struct {
+			result1 *v1.Pod
+			result2 error
+		})
+	}
+	fake.decodePodReturnsOnCall[i] = struct {
+		result1 *v1.Pod
+		result2 error
+	}{result1, result2}
+}
+
+func (fake *FakeImpl) ListProfileRecordings(arg1 context.Context, arg2 ...client.ListOption) (*v1alpha1.ProfileRecordingList, error) {
+	fake.listProfileRecordingsMutex.Lock()
+	ret, specificReturn := fake.listProfileRecordingsReturnsOnCall[len(fake.listProfileRecordingsArgsForCall)]
+	fake.listProfileRecordingsArgsForCall = append(fake.listProfileRecordingsArgsForCall, struct {
+		arg1 context.Context
+		arg2 []client.ListOption
+	}{arg1, arg2})
+	stub := fake.ListProfileRecordingsStub
+	fakeReturns := fake.listProfileRecordingsReturns
+	fake.recordInvocation("ListProfileRecordings", []interface{}{arg1, arg2})
+	fake.listProfileRecordingsMutex.Unlock()
+	if stub != nil {
+		return stub(arg1, arg2...)
+	}
+	if specificReturn {
+		return ret.result1, ret.result2
+	}
+	return fakeReturns.result1, fakeReturns.result2
+}
+
+func (fake *FakeImpl) ListProfileRecordingsCallCount() int {
+	fake.listProfileRecordingsMutex.RLock()
+	defer fake.listProfileRecordingsMutex.RUnlock()
+	return len(fake.listProfileRecordingsArgsForCall)
+}
+
+func (fake *FakeImpl) ListProfileRecordingsCalls(stub func(context.Context, ...client.ListOption) (*v1alpha1.ProfileRecordingList, error)) {
+	fake.listProfileRecordingsMutex.Lock()
+	defer fake.listProfileRecordingsMutex.Unlock()
+	fake.ListProfileRecordingsStub = stub
+}
+
+func (fake *FakeImpl) ListProfileRecordingsArgsForCall(i int) (context.Context, []client.ListOption) {
+	fake.listProfileRecordingsMutex.RLock()
+	defer fake.listProfileRecordingsMutex.RUnlock()
+	argsForCall := fake.listProfileRecordingsArgsForCall[i]
+	return argsForCall.arg1, argsForCall.arg2
+}
+
+func (fake *FakeImpl) ListProfileRecordingsReturns(result1 *v1alpha1.ProfileRecordingList, result2 error) {
+	fake.listProfileRecordingsMutex.Lock()
+	defer fake.listProfileRecordingsMutex.Unlock()
+	fake.ListProfileRecordingsStub = nil
+	fake.listProfileRecordingsReturns = struct {
+		result1 *v1alpha1.ProfileRecordingList
+		result2 error
+	}{result1, result2}
+}
+
+func (fake *FakeImpl) ListProfileRecordingsReturnsOnCall(i int, result1 *v1alpha1.ProfileRecordingList, result2 error) {
+	fake.listProfileRecordingsMutex.Lock()
+	defer fake.listProfileRecordingsMutex.Unlock()
+	fake.ListProfileRecordingsStub = nil
+	if fake.listProfileRecordingsReturnsOnCall == nil {
+		fake.listProfileRecordingsReturnsOnCall = make(map[int]struct {
+			result1 *v1alpha1.ProfileRecordingList
+			result2 error
+		})
+	}
+	fake.listProfileRecordingsReturnsOnCall[i] = struct {
+		result1 *v1alpha1.ProfileRecordingList
+		result2 error
+	}{result1, result2}
+}
+
+func (fake *FakeImpl) SetDecoder(arg1 *admission.Decoder) {
+	fake.setDecoderMutex.Lock()
+	fake.setDecoderArgsForCall = append(fake.setDecoderArgsForCall, struct {
+		arg1 *admission.Decoder
+	}{arg1})
+	stub := fake.SetDecoderStub
+	fake.recordInvocation("SetDecoder", []interface{}{arg1})
+	fake.setDecoderMutex.Unlock()
+	if stub != nil {
+		fake.SetDecoderStub(arg1)
+	}
+}
+
+func (fake *FakeImpl) SetDecoderCallCount() int {
+	fake.setDecoderMutex.RLock()
+	defer fake.setDecoderMutex.RUnlock()
+	return len(fake.setDecoderArgsForCall)
+}
+
+func (fake *FakeImpl) SetDecoderCalls(stub func(*admission.Decoder)) {
+	fake.setDecoderMutex.Lock()
+	defer fake.setDecoderMutex.Unlock()
+	fake.SetDecoderStub = stub
+}
+
+func (fake *FakeImpl) SetDecoderArgsForCall(i int) *admission.Decoder {
+	fake.setDecoderMutex.RLock()
+	defer fake.setDecoderMutex.RUnlock()
+	argsForCall := fake.setDecoderArgsForCall[i]
+	return argsForCall.arg1
+}
+
+func (fake *FakeImpl) UpdateResource(arg1 context.Context, arg2 logr.Logger, arg3 client.Object, arg4 string) error {
+	fake.updateResourceMutex.Lock()
+	ret, specificReturn := fake.updateResourceReturnsOnCall[len(fake.updateResourceArgsForCall)]
+	fake.updateResourceArgsForCall = append(fake.updateResourceArgsForCall, struct {
+		arg1 context.Context
+		arg2 logr.Logger
+		arg3 client.Object
+		arg4 string
+	}{arg1, arg2, arg3, arg4})
+	stub := fake.UpdateResourceStub
+	fakeReturns := fake.updateResourceReturns
+	fake.recordInvocation("UpdateResource", []interface{}{arg1, arg2, arg3, arg4})
+	fake.updateResourceMutex.Unlock()
+	if stub != nil {
+		return stub(arg1, arg2, arg3, arg4)
+	}
+	if specificReturn {
+		return ret.result1
+	}
+	return fakeReturns.result1
+}
+
+func (fake *FakeImpl) UpdateResourceCallCount() int {
+	fake.updateResourceMutex.RLock()
+	defer fake.updateResourceMutex.RUnlock()
+	return len(fake.updateResourceArgsForCall)
+}
+
+func (fake *FakeImpl) UpdateResourceCalls(stub func(context.Context, logr.Logger, client.Object, string) error) {
+	fake.updateResourceMutex.Lock()
+	defer fake.updateResourceMutex.Unlock()
+	fake.UpdateResourceStub = stub
+}
+
+func (fake *FakeImpl) UpdateResourceArgsForCall(i int) (context.Context, logr.Logger, client.Object, string) {
+	fake.updateResourceMutex.RLock()
+	defer fake.updateResourceMutex.RUnlock()
+	argsForCall := fake.updateResourceArgsForCall[i]
+	return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
+}
+
+func (fake *FakeImpl) UpdateResourceReturns(result1 error) {
+	fake.updateResourceMutex.Lock()
+	defer fake.updateResourceMutex.Unlock()
+	fake.UpdateResourceStub = nil
+	fake.updateResourceReturns = struct {
+		result1 error
+	}{result1}
+}
+
+func (fake *FakeImpl) UpdateResourceReturnsOnCall(i int, result1 error) {
+	fake.updateResourceMutex.Lock()
+	defer fake.updateResourceMutex.Unlock()
+	fake.UpdateResourceStub = nil
+	if fake.updateResourceReturnsOnCall == nil {
+		fake.updateResourceReturnsOnCall = make(map[int]struct {
+			result1 error
+		})
+	}
+	fake.updateResourceReturnsOnCall[i] = struct {
+		result1 error
+	}{result1}
+}
+
+func (fake *FakeImpl) Invocations() map[string][][]interface{} {
+	fake.invocationsMutex.RLock()
+	defer fake.invocationsMutex.RUnlock()
+	fake.decodePodMutex.RLock()
+	defer fake.decodePodMutex.RUnlock()
+	fake.listProfileRecordingsMutex.RLock()
+	defer fake.listProfileRecordingsMutex.RUnlock()
+	fake.setDecoderMutex.RLock()
+	defer fake.setDecoderMutex.RUnlock()
+	fake.updateResourceMutex.RLock()
+	defer fake.updateResourceMutex.RUnlock()
+	copiedInvocations := map[string][][]interface{}{}
+	for key, value := range fake.invocations {
+		copiedInvocations[key] = value
+	}
+	return copiedInvocations
+}
+
+func (fake *FakeImpl) recordInvocation(key string, args []interface{}) {
+	fake.invocationsMutex.Lock()
+	defer fake.invocationsMutex.Unlock()
+	if fake.invocations == nil {
+		fake.invocations = map[string][][]interface{}{}
+	}
+	if fake.invocations[key] == nil {
+		fake.invocations[key] = [][]interface{}{}
+	}
+	fake.invocations[key] = append(fake.invocations[key], args)
+}


Test patch:
diff --git a/internal/pkg/webhooks/recording/recording_test.go b/internal/pkg/webhooks/recording/recording_test.go
new file mode 100644
index 000000000..1a35d845f
--- /dev/null
+++ b/internal/pkg/webhooks/recording/recording_test.go
@@ -0,0 +1,81 @@
+/*
+Copyright 2021 The Kubernetes Authors.
+
+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 recording
+
+import (
+	"context"
+	"errors"
+	"net/http"
+	"testing"
+
+	"github.com/go-logr/logr"
+	"github.com/stretchr/testify/require"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
+
+	"sigs.k8s.io/security-profiles-operator/api/profilerecording/v1alpha1"
+	"sigs.k8s.io/security-profiles-operator/internal/pkg/webhooks/recording/recordingfakes"
+)
+
+var errTest = errors.New("error")
+
+func TestHandle(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range []struct {
+		prepare func(*recordingfakes.FakeImpl)
+		request admission.Request
+		assert  func(admission.Response)
+	}{
+		{ // success pod unchanged
+			prepare: func(mock *recordingfakes.FakeImpl) {
+				mock.ListProfileRecordingsReturns(&v1alpha1.ProfileRecordingList{}, nil)
+				mock.DecodePodReturns(&corev1.Pod{}, nil)
+			},
+			assert: func(resp admission.Response) {
+				require.True(t, resp.Allowed)
+				require.Equal(t, http.StatusOK, int(resp.Result.Code))
+				require.Equal(t, metav1.StatusReason("pod unchanged"), resp.Result.Reason)
+			},
+		},
+		{ // error could not list profile recordings
+			prepare: func(mock *recordingfakes.FakeImpl) {
+				mock.ListProfileRecordingsReturns(nil, errTest)
+			},
+			assert: func(resp admission.Response) {
+				require.Equal(t, http.StatusInternalServerError, int(resp.Result.Code))
+			},
+		},
+		{ // error failed to decode pod
+			prepare: func(mock *recordingfakes.FakeImpl) {
+				mock.ListProfileRecordingsReturns(&v1alpha1.ProfileRecordingList{}, nil)
+				mock.DecodePodReturns(nil, errTest)
+			},
+			assert: func(resp admission.Response) {
+				require.Equal(t, http.StatusBadRequest, int(resp.Result.Code))
+			},
+		},
+	} {
+		mock := &recordingfakes.FakeImpl{}
+		tc.prepare(mock)
+
+		recorder := podSeccompRecorder{impl: mock, log: logr.Discard()}
+		resp := recorder.Handle(context.Background(), admission.Request{})
+		tc.assert(resp)
+	}
+}