in internal/testintegration/setup.go [84:145]
func NewTestHarness() (*EnvTestHarness, error) {
var err error
ctrl.SetLogger(Log)
// Initialize the test environment
// if the KUBEBUILDER_ASSETS env var is not set, then run setup-envtest
// and set it according.
kubebuilderAssets, exists := os.LookupEnv("KUBEBUILDER_ASSETS")
if !exists {
kubebuilderAssets, err = runSetupEnvtest()
if err != nil {
return nil, fmt.Errorf("unable to run setup-envtest %v", err)
}
}
Log.Info("Starting up kubebuilder EnvTest")
ctx, cancel := context.WithCancel(logr.NewContext(context.Background(), Log))
testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,
BinaryAssetsDirectory: kubebuilderAssets,
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
},
}
// Start the testenv
cfg, err := testEnv.Start()
th := &EnvTestHarness{
testEnvCtx: ctx,
testEnv: testEnv,
testEnvCancel: cancel,
cfg: cfg,
cancel: func() {},
}
if err != nil {
th.Teardown()
return nil, fmt.Errorf("unable to start kuberenetes envtest %v", err)
}
// Initialize rest client configuration
th.s = scheme.Scheme
controller.InitScheme(th.s)
cl, err := client.New(cfg, client.Options{Scheme: th.s})
if err != nil {
th.Teardown()
return nil, fmt.Errorf("unable to to create client %v", err)
}
th.Client = cl
// Start the controller-runtime manager
err = th.StartManager(workload.DefaultProxyImage)
if err != nil {
th.Teardown()
return nil, fmt.Errorf("unable to start kuberenetes envtest %v", err)
}
return th, nil
}