Commit 83323c12 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Merge branch 'extraact-unsupportedOSErr' into 'master'

Extract unsupportedOSTypeError to errors pkg

See merge request gitlab-org/gitlab-runner!1305
parents 7549fc08 6ce02c07
package errors
import (
"fmt"
)
// ErrOSNotSupported is used when docker does not support the detected OSType.
// NewErrOSNotSupported is used to initialize this type.
type ErrOSNotSupported struct {
detectedOSType string
}
func (e *ErrOSNotSupported) Error() string {
return fmt.Sprintf("unsupported OSType %q", e.detectedOSType)
}
// NewErrOSNotSupported creates a ErrOSNotSupported for the specified OSType.
func NewErrOSNotSupported(osType string) *ErrOSNotSupported {
return &ErrOSNotSupported{
detectedOSType: osType,
}
}
package helperimage package helperimage
import ( import (
"fmt"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/errors"
) )
const ( const (
...@@ -19,20 +19,6 @@ type Info interface { ...@@ -19,20 +19,6 @@ type Info interface {
IsSupportingLocalImport() bool IsSupportingLocalImport() bool
} }
type unsupportedOSTypeError struct {
detectedOSType string
}
func (e *unsupportedOSTypeError) Error() string {
return fmt.Sprintf("unsupported OSType %q", e.detectedOSType)
}
func newUnsupportedOSTypeError(osType string) *unsupportedOSTypeError {
return &unsupportedOSTypeError{
detectedOSType: osType,
}
}
type infoFactory func(info types.Info) Info type infoFactory func(info types.Info) Info
var supportedOsTypesFactories = map[string]infoFactory{ var supportedOsTypesFactories = map[string]infoFactory{
...@@ -43,7 +29,7 @@ var supportedOsTypesFactories = map[string]infoFactory{ ...@@ -43,7 +29,7 @@ var supportedOsTypesFactories = map[string]infoFactory{
func GetInfo(info types.Info) (Info, error) { func GetInfo(info types.Info) (Info, error) {
factory, ok := supportedOsTypesFactories[info.OSType] factory, ok := supportedOsTypesFactories[info.OSType]
if !ok { if !ok {
return nil, newUnsupportedOSTypeError(info.OSType) return nil, errors.NewErrOSNotSupported(info.OSType)
} }
return factory(info), nil return factory(info), nil
......
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/errors"
) )
func TestGetInfo(t *testing.T) { func TestGetInfo(t *testing.T) {
...@@ -15,7 +17,7 @@ func TestGetInfo(t *testing.T) { ...@@ -15,7 +17,7 @@ func TestGetInfo(t *testing.T) {
}{ }{
{osType: OSTypeLinux, expectedHelperImageType: &linuxInfo{}, expectedError: nil}, {osType: OSTypeLinux, expectedHelperImageType: &linuxInfo{}, expectedError: nil},
{osType: OSTypeWindows, expectedHelperImageType: &windowsInfo{}, expectedError: nil}, {osType: OSTypeWindows, expectedHelperImageType: &windowsInfo{}, expectedError: nil},
{osType: "unsupported", expectedHelperImageType: nil, expectedError: newUnsupportedOSTypeError("unsupported")}, {osType: "unsupported", expectedHelperImageType: nil, expectedError: errors.NewErrOSNotSupported("unsupported")},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment