cert-manager/pkg/controller/clusterissuers/util_test.go
2018-08-07 16:13:46 +01:00

72 lines
1.9 KiB
Go

package clusterissuers
import (
"testing"
"github.com/jetstack/cert-manager/test/util/generate"
"github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1"
"github.com/jetstack/cert-manager/pkg/controller/test"
)
type controllerFixture struct {
// The Solver under test
Controller *Controller
Builder *test.Builder
// Issuer to be passed to functions on the Solver (a default will be used if nil)
Issuer *v1alpha1.ClusterIssuer
// PreFn will run before the test is run, but after the fixture has been initialised.
// This is useful if you want to load the clientset with some resources *after* the
// fixture has been created.
PreFn func(*testing.T, *controllerFixture)
// CheckFn should performs checks to ensure the output of the test is as expected.
// Optional additional values may be provided, which represent the output of the
// function under test.
CheckFn func(*testing.T, *controllerFixture, ...interface{})
// Err should be true if an error is expected from the function under test
Err bool
// testResources is used to store references to resources used or created during
// the test.
testResources map[string]interface{}
}
const (
defaultTestIssuerName = "issuer"
defaultTestNamespace = "default"
)
func (s *controllerFixture) Setup(t *testing.T) {
if s.Issuer == nil {
s.Issuer = generate.ClusterIssuer(generate.ClusterIssuerConfig{
Name: defaultTestIssuerName,
Namespace: defaultTestNamespace,
})
}
if s.testResources == nil {
s.testResources = map[string]interface{}{}
}
if s.Builder == nil {
s.Builder = &test.Builder{}
}
s.Builder.Start()
s.Controller = New(s.Builder.Context)
s.Builder.Sync()
if s.PreFn != nil {
s.PreFn(t, s)
s.Builder.Sync()
}
}
func (s *controllerFixture) Finish(t *testing.T, args ...interface{}) {
defer s.Builder.Stop()
// resync listers before running checks
s.Builder.Sync()
// run custom checks
if s.CheckFn != nil {
s.CheckFn(t, s, args...)
}
}