Skip to content

Add configurable interceptor for fake Client

Hossein Pursultani requested to merge hp-fake-client-interceptor-callback into main

What does this MR do?

Add configurable interceptor for fake client

The new test utility provides a rule-based mechanism to intercept objects in fake client. The objects are intercepted based on their type, i.e. kind, and a set of selectors such as object name and labels.

Each interceptor can be configured to respond to specific actions at desired stage, for example Before Get or After Create. A wide range of actions are supported. However the stage is limited to Before and After the specified actions.

Usage example

g := &v2alpha1.GitLab{
	// ...
}

// Create an interceptor for a specific scenario.
// Load varios callbacks if needs be.

workingUpgrade := testutils.InterceptorCallbacks{
	&testutils.InterceptorCallback[*batchv1.Job]{
		Stage:    testutils.After,  // Mandatory. Empty does no trigger the callback.
		Action:   testutils.Apply, // Mandatory. Empty does no trigger the callback.
		Selector: testutils.NamePatternSelector("migrations-pre"), // Optional.
		Reactor:  func(obj *batchv1.Job, err error) error { 
			if err != nil {
				return err
			}

			obj.Status.Succeeded = 1
			obj.Status.Conditions = nil

			return nil
		},
	},
	&testutils.InterceptorCallback[*corev1.ConfigMap]{
		// ...
	},

}


// Use workingUpgrade.Funcs to setup fake.Client
rtCtx := testutils.MockContext(
	testutils.WithObjects(g),
	testutils.WithFakeOption(func(cb *fake.ClientBuilder) {
		cb.WithStatusSubresource(g)
		cb.WithInterceptorFuncs(workingUpgrade.Funcs())
}))

Author's Checklist

For anything in this list which will not be completed, please provide a reason in the MR discussion.

Required

  • Ensure a release milestone is set.
  • MR title and description are up to date, accurate, and descriptive.
  • MR targeting the appropriate branch.
  • MR has a green pipeline on GitLab.com.
  • When ready for review, MR is labeled workflowready for review per the MR workflow.

Expected

  • Test plan indicating conditions for success has been posted and passes.
  • Documentation is created or updated.
  • Tests are added.

Related issues

Edited by Clemens Beck

Merge request reports