Test Your Plugins

You can test your plugin in two dimension:

  1. Validate your plugin behavior through E2E tests
  2. 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:

  1. Setup testing environment, e.g:
  2. Validate the plugin behavior, e.g:
  3. Further make sure the scaffolded output works, e.g:
  4. Delete temporary resources after testing exited, e.g:

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/v3: (kbc is an instance of TextContext)

  • To initialized a project:
    By("initializing a project")
    err = kbc.Init(
    	"--plugins", "go/v3",
    	"--project-version", "3",
    	"--domain", kbc.Domain,
    	"--fetch-deps=false",
    	"--component-config=true",
    )
    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())