From 1f703d33f5f0fddf2ea58da1f8efecb1bebfec0f Mon Sep 17 00:00:00 2001 From: David Eads Date: Mon, 30 Jul 2018 10:39:14 -0400 Subject: [PATCH] add methods to apimachinery to easy unit testing --- .../apimachinery/pkg/api/testing/codec.go | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/api/testing/codec.go b/staging/src/k8s.io/apimachinery/pkg/api/testing/codec.go index 8a13d1ff481..542b0aa275d 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/testing/codec.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/testing/codec.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package testing +package apitesting import ( "fmt" @@ -84,3 +84,33 @@ func init() { } } } + +// InstallOrDieFunc mirrors install functions that require success +type InstallOrDieFunc func(scheme *runtime.Scheme) + +// SchemeForInstallOrDie builds a simple test scheme and codecfactory pair for easy unit testing from higher level install methods +func SchemeForInstallOrDie(installFns ...InstallOrDieFunc) (*runtime.Scheme, runtimeserializer.CodecFactory) { + scheme := runtime.NewScheme() + codecFactory := runtimeserializer.NewCodecFactory(scheme) + for _, installFn := range installFns { + installFn(scheme) + } + + return scheme, codecFactory +} + +// InstallFunc mirrors install functions that can return an error +type InstallFunc func(scheme *runtime.Scheme) error + +// SchemeForOrDie builds a simple test scheme and codecfactory pair for easy unit testing from the bare registration methods. +func SchemeForOrDie(installFns ...InstallFunc) (*runtime.Scheme, runtimeserializer.CodecFactory) { + scheme := runtime.NewScheme() + codecFactory := runtimeserializer.NewCodecFactory(scheme) + for _, installFn := range installFns { + if err := installFn(scheme); err != nil { + panic(err) + } + } + + return scheme, codecFactory +}