Testing a Mixin

We are working on filling this doc out more, until then this is more of a FAQ than a proscriptive guide. If you have a tip, please submit a PR and help us fill this out!

How do I unit test that my mixin is executing the right commands?

Here is a full working example of a unit test that validates the commands executed by a mixin.

Make sure that your package has a TestMain that calls github.com/deislabs/porter/pkg/test.TestMainWithMockedCommandHandlers

import "github.com/deislabs/porter/pkg/test"

func TestMain(m *testing.M) {
	test.TestMainWithMockedCommandHandlers(m)
}

Then you instantiate your mixin in test mode (the skeletor template generates this method for you):

m := NewTestMixin(t)

This sets up your test binary to assert that expected command(s) were called. You tell it what command to expect with

os.Setenv(test.ExpectedCommandEnv, "helm install")
defer os.Unsetenv(test.ExpectedCommandEnv)

If your mixin action executes multiple commands, separate them with a newline \n like so

os.Setenv(test.ExpectedCommandEnv, "helm install\nhelm upgrade")

Now execute your mixin action:

err = m.Execute()

Instead of os calls to the real commands, the test mixin mode calls back into your test binary. The TestMain handles asserting that the expected commands were made and fails the test if they weren’t.