Verified Commit 6ce02c07 authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Extract unsupportedOSTypeError to errors pkg

This error is going to be used in multiple places since we only support
a specific set of OS for docker.
parent 7549fc08
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
import (
"fmt"
"github.com/docker/docker/api/types"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/errors"
)
const (
......@@ -19,20 +19,6 @@ type Info interface {
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
var supportedOsTypesFactories = map[string]infoFactory{
......@@ -43,7 +29,7 @@ var supportedOsTypesFactories = map[string]infoFactory{
func GetInfo(info types.Info) (Info, error) {
factory, ok := supportedOsTypesFactories[info.OSType]
if !ok {
return nil, newUnsupportedOSTypeError(info.OSType)
return nil, errors.NewErrOSNotSupported(info.OSType)
}
return factory(info), nil
......
......@@ -5,6 +5,8 @@ import (
"github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/errors"
)
func TestGetInfo(t *testing.T) {
......@@ -15,7 +17,7 @@ func TestGetInfo(t *testing.T) {
}{
{osType: OSTypeLinux, expectedHelperImageType: &linuxInfo{}, 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 {
......
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