kubernetes-sigs / kubebuilder

Kubebuilder - SDK for building Kubernetes APIs using CRDs
http://book.kubebuilder.io
Apache License 2.0
7.8k stars 1.44k forks source link

Improved test support for 3rd party apps #4006

Closed beatrausch closed 2 months ago

beatrausch commented 3 months ago

What do you want to happen?

Hi,

to provide better testing support for apps that use kubebuilder as a library, I propose the following small changes:

  1. Set the file system as CLI option

    func WithFilesystem(fs machinery.Filesystem) Option {
    return func(cli *CLI) error {
        cli.fs = fs
        return nil
    }
    }
  2. Allow access to root command

    func (c CLI) Cmd() *cobra.Command {
    return c.cmd
    }

This changes would help to build integration tests like:

func TestInitIntegration(t *testing.T)  {
    actual := afero.NewMemMapFs()
    expected := afero.NewOsFs()

    root, err := myapp.BuildRootCli(actual)
    if err != nil {
        t.Error(err)
    }
    root.Cmd().SetArgs([]string{
        "init", "--domain", "example.org", "--module-name", "example.org/example/operator", "--project-name", "example-operator",
    })

    err := root.Run()
    if err != nil {
        t.Error(err)
    }

    err := compare(actual, expected, "./testdata/init-int-integration")
    if err != nil {
        t.Error(err)
    }
}

What do you think?

Regards

Extra Labels

No response

camilamacedo86 commented 2 months ago

Hi @beatrausch

Please, feel free to push any PR that you seem fit to help you out use Kubebuilder as a lib. Unless we have a real/good reason for not accept that, we will more than happy to have those merged. If they are helpful to you probably will be helpful to others.

Your collaboration is very appreciated and welcome !!!

beatrausch commented 2 months ago

Great! I will prepare the code and create the PR.