Test Your Plugins
You can test your plugin in two dimension:
- Validate your plugin behavior through E2E tests
- Generate sample projects based on your plugin that can be placed in
./testdata/
Write E2E Tests
You can check Kubebuilder/v3/test/e2e/utils package that offers TestContext
of rich methods:
- NewTestContext helps define:
- Temporary folder for testing projects
- Temporary controller-manager image
- Kubectl execution method
- The cli executable (
kubebuilder
,operator-sdk
, OR your extended-cli)
Once defined, you can use TestContext
to:
- Setup testing environment, e.g:
- Clean up the environment, create temp dir. See Prepare
- Install prerequisites CRDs: See InstallCertManager, InstallPrometheusManager
- Validate the plugin behavior, e.g:
- Trigger the plugin’s bound subcommands. See Init, CreateAPI
- Use PluginUtil to verify the scaffolded outputs. See InsertCode, ReplaceInFile, UncommendCode
- Further make sure the scaffolded output works, e.g:
- Execute commands in your
Makefile
. See Make - Temporary load image of the testing controller. See LoadImageToKindCluster
- Call Kubectl to validate running resources. See utils.Kubectl
- Execute commands in your
- Delete temporary resources after testing exited, e.g:
- Uninstall prerequisites CRDs: See UninstallPrometheusOperManager
- Delete temp dir. See Destroy
References: operator-sdk e2e tests, kubebuiler e2e tests
Generate Test Samples
It can be straightforward to view content of sample projects generated by your plugin.
For example, Kubebuilder generate sample projects based on different plugins to validate the layouts.
Simply, you can also use TextContext
to generate folders of scaffolded projects from your plugin.
The commands are very similar as mentioned in creating-plugins.
Following is a general workflow to create a sample by the plugin go/v4
: (kbc
is an instance of TextContext
)
- To initialized a project:
By("initializing a project") err = kbc.Init( "--plugins", "go/v4", "--project-version", "3", "--domain", kbc.Domain, "--fetch-deps=false", ) ExpectWithOffset(1, err).NotTo(HaveOccurred())
- To define API:
By("creating API definition") err = kbc.CreateAPI( "--group", kbc.Group, "--version", kbc.Version, "--kind", kbc.Kind, "--namespaced", "--resource", "--controller", "--make=false", ) ExpectWithOffset(1, err).NotTo(HaveOccurred())
- To scaffold webhook configurations:
By("scaffolding mutating and validating webhooks") err = kbc.CreateWebhook( "--group", kbc.Group, "--version", kbc.Version, "--kind", kbc.Kind, "--defaulting", "--programmatic-validation", ) ExpectWithOffset(1, err).NotTo(HaveOccurred())