Merge "Added error handler for empty runtime entry"

This commit is contained in:
Zuul 2020-04-27 17:48:42 +00:00 committed by Gerrit Code Review
commit cbc2fad7f7
3 changed files with 24 additions and 9 deletions

View File

@ -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 {

View File

@ -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)
})
}

View File

@ -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")
}