From 611d73a2ee40311719f9802f90648cf124202c2d Mon Sep 17 00:00:00 2001 From: Stanislav Egorov Date: Mon, 20 Apr 2020 20:38:19 -0700 Subject: [PATCH] Added error handler for empty runtime entry If there is no container runtime defined in the airshipctl config then the error message is not clear. New handler shows specific error description Change-Id: Ib5b735ddaa1556a8a88fdb15c4b3942cd010db9b --- pkg/container/container.go | 2 ++ pkg/container/container_test.go | 23 ++++++++++++++--------- pkg/container/errors.go | 8 ++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/container/container.go b/pkg/container/container.go index ab534f5e9..af136ce47 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -37,6 +37,8 @@ type Container interface { // * docker func NewContainer(ctx *context.Context, driver string, url string) (Container, error) { switch driver { + case "": + return nil, ErrNoContainerDriver{} case "docker": cli, err := NewDockerClient(ctx) if err != nil { diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index ed4a51a5d..3026e7549 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -16,20 +16,25 @@ package container import ( "context" - "fmt" "testing" "github.com/stretchr/testify/assert" ) func TestNewContainer(t *testing.T) { + assert := assert.New(t) + ctx := context.Background() - _, actualErr := NewContainer(&ctx, "test_drv", "") - expectedErr := ErrContainerDrvNotSupported{Driver: "test_drv"} - errS := fmt.Sprintf( - "Call NewContainer should have returned error %s, got %s", - expectedErr, - actualErr, - ) - assert.Equal(t, actualErr, expectedErr, errS) + + t.Run("not-supported-container", func(t *testing.T) { + cnt, err := NewContainer(&ctx, "test_drv", "") + assert.Equal(nil, cnt) + assert.Equal(ErrContainerDrvNotSupported{Driver: "test_drv"}, err) + }) + + t.Run("empty-container", func(t *testing.T) { + cnt, err := NewContainer(&ctx, "", "") + assert.Equal(nil, cnt) + assert.Equal(ErrNoContainerDriver{}, err) + }) } diff --git a/pkg/container/errors.go b/pkg/container/errors.go index 13703db2c..a465560be 100644 --- a/pkg/container/errors.go +++ b/pkg/container/errors.go @@ -44,3 +44,11 @@ type ErrContainerDrvNotSupported struct { func (e ErrContainerDrvNotSupported) Error() string { return fmt.Sprintf("Driver %s is not supported", e.Driver) } + +// ErrNoContainerDriver returned if no runtime defined in config +type ErrNoContainerDriver struct { +} + +func (e ErrNoContainerDriver) Error() string { + return fmt.Sprintf("container runtime is not defined in airshipctl config") +}