<p>I have a custom kubernetes operator written in Go. When writing unittests I came across the need to have a fake manager initialised. The manager is created with NewManager() function of the &quot;sigs.k8s.io/controller-runtime&quot; package. When run locally that is not a problem, but it is when running in a pipeline.</p>
<pre><code>import (
    ctrl &quot;sigs.k8s.io/controller-runtime&quot;
    // other imports
)

var _ = Describe(&quot;ProvideNewController&quot;, func() {
    var (
        reconciler  reconcile.Reconciler
        customUtils controllers.CustomUtils // local utils package
        mockCC      *controllers.MockCommandClient
        errResult   error
    )
    When(&quot;ProvideNewController is called&quot;, func() {
        BeforeEach(func() {
            logger := logr.Discard()
            recorder := record.NewFakeRecorder(100)
            mockCC = &amp;controllers.MockCommandClient{}
            customUtils = controllers.CustomUtils{
                CommandClient: mockCC,
            }
            scheme := scheme.Scheme
            scheme.AddKnownTypes(operatorV1alpha1.GroupVersion, &amp;operatorV1alpha1.Custom{})
            managerOptions := ctrl.Options{
                Scheme:           scheme,
                Logger:           ctrl.Log.WithName(&quot;controllers&quot;).WithName(&quot;custom&quot;),
                WebhookServer:    webhook.NewServer(webhook.Options{Port: 9443}),
                LeaderElectionID: &quot;someValue&quot;,
            }
            fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects().Build()
            reconciler = NewReconciler(fakeClient, customUtils, logger, scheme, recorder)
            mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), managerOptions)
            if err != nil {
                common.FailTestSetup(err)
            }
            errResult = ProvideNewController(mgr, reconciler)
        })
        It(&quot;should set up a controller with the specified resource types&quot;, func() {
            Expect(errResult).ToNot(HaveOccurred())
        })
    })
})
</code></pre>
<p>Now the line <code>mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), managerOptions)</code> crashes the tests completely and ungracefully because ctrl.GetConfigOrDie() fails to read a kubeconfig file, which is expected because it really isn't present on the pod of the CI/CD runner.</p>
<p>I'm new to Go, so I'm not aware of all the options there are. From what I've seen, if I was to mock the whole struct and provide it mocked like that as an argument for the NewManager, I would've had to mock all the methods that struct implements and I don't see that as a good approach.</p>
<p>Is there a way for me to mock the config file or provide it programmatically, so I don't have to edit the pipeline and provide it from there?</p>

Богдан Божић
<p>You could use the <a href="https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest" rel="nofollow noreferrer">envtest</a> package of the sigs.k8s.io controller-runtime repo.
See the <a href="https://book.kubebuilder.io/reference/envtest.html#writing-tests" rel="nofollow noreferrer">writing test</a> section under the <code>Configuring envtest for integration tests</code> doc page:</p>
<pre><code>import sigs.k8s.io/controller-runtime/pkg/envtest

//specify testEnv configuration
testEnv = &amp;envtest.Environment{
    CRDDirectoryPaths: []string{filepath.Join(&quot;..&quot;, &quot;config&quot;, &quot;crd&quot;, &quot;bases&quot;)},
}

//start testEnv
cfg, err = testEnv.Start()

//write test logic

//stop testEnv
err = testEnv.Stop()

</code></pre>
