Verified Commit f4b72104 authored by Tomasz Maczukin's avatar Tomasz Maczukin 💬
Browse files

Move helperImageInfo to its own package

parent c97da184
Pipeline #52808616 passed with stages
in 66 minutes and 37 seconds
......@@ -28,6 +28,7 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/executors"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
docker_helpers "gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/helperimage"
)
const (
......@@ -281,12 +282,12 @@ func (e *executor) getPrebuiltImage() (*types.ImageInspect, error) {
revision = common.REVISION
}
helperImgInfo, err := getHelperImageInfo(e.info)
helperImageInfo, err := helperimage.GetInfo(e.info)
if err != nil {
return nil, err
}
tag, err := helperImgInfo.Tag(revision)
tag, err := helperImageInfo.Tag(revision)
if err != nil {
return nil, err
}
......@@ -300,7 +301,7 @@ func (e *executor) getPrebuiltImage() (*types.ImageInspect, error) {
}
// Try to load prebuilt image from local filesystem
loadedImage := e.getLocalDockerImage(helperImgInfo, tag)
loadedImage := e.getLocalDockerImage(helperImageInfo, tag)
if loadedImage != nil {
return loadedImage, nil
}
......@@ -310,12 +311,12 @@ func (e *executor) getPrebuiltImage() (*types.ImageInspect, error) {
return e.getDockerImage(imageName)
}
func (e *executor) getLocalDockerImage(helperImgInfo helperImageInfo, tag string) *types.ImageInspect {
if !helperImgInfo.IsSupportingLocalImport() {
func (e *executor) getLocalDockerImage(helperImageInfo helperimage.Info, tag string) *types.ImageInspect {
if !helperImageInfo.IsSupportingLocalImport() {
return nil
}
architecture := helperImgInfo.Architecture()
architecture := helperImageInfo.Architecture()
for _, dockerPrebuiltImagesPath := range DockerPrebuiltImagesPaths {
dockerPrebuiltImageFilePath := filepath.Join(dockerPrebuiltImagesPath, "prebuilt-"+architecture+prebuiltImageExtension)
image, err := e.loadPrebuiltImage(dockerPrebuiltImageFilePath, prebuiltImageName, tag)
......
......@@ -25,6 +25,7 @@ import (
"gitlab.com/gitlab-org/gitlab-runner/executors"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
"gitlab.com/gitlab-org/gitlab-runner/helpers/docker/helperimage"
)
func TestMain(m *testing.M) {
......@@ -1353,8 +1354,10 @@ func setUpExecutorForFeatureFlag(variables common.JobVariables, helperImage stri
},
Context: context.Background(),
},
client: client,
helperImageInfo: newLinuxHelperImageInfo(types.Info{Architecture: "x86_64"}),
client: client,
info: types.Info{
OSType: helperimage.OSTypeLinux,
},
}
}
......
package docker
package helperimage
import (
"fmt"
......@@ -11,9 +11,9 @@ const (
OSTypeWindows = "windows"
)
// helperImageInfo provides information about the helper image that can be used to
// Info provides information about the helper image that can be used to
// pull from Docker Hub.
type helperImageInfo interface {
type Info interface {
Architecture() string
Tag(revision string) (string, error)
IsSupportingLocalImport() bool
......@@ -27,26 +27,23 @@ func (e *unsupportedOSTypeError) Error() string {
return fmt.Sprintf("unsupported OSType %q", e.detectedOSType)
}
func (e *unsupportedOSTypeError) OSType(osType string) *unsupportedOSTypeError {
e.detectedOSType = osType
return e
func newUnsupportedOSTypeError(osType string) *unsupportedOSTypeError {
return &unsupportedOSTypeError{
detectedOSType: osType,
}
}
var errUnsupportedOSType = &unsupportedOSTypeError{}
type helperImageInfoFactory func(info types.Info) helperImageInfo
type infoFactory func(info types.Info) Info
var supportedOsTypesFactories = map[string]helperImageInfoFactory{
OSTypeWindows: newWindowsHelperImageInfo,
OSTypeLinux: newLinuxHelperImageInfo,
var supportedOsTypesFactories = map[string]infoFactory{
OSTypeWindows: newWindowsInfo,
OSTypeLinux: newLinuxInfo,
}
func getHelperImageInfo(info types.Info) (helperImageInfo, error) {
osType := info.OSType
factory, ok := supportedOsTypesFactories[osType]
func GetInfo(info types.Info) (Info, error) {
factory, ok := supportedOsTypesFactories[info.OSType]
if !ok {
return nil, errUnsupportedOSType.OSType(osType)
return nil, newUnsupportedOSTypeError(info.OSType)
}
return factory(info), nil
......
package docker
package helperimage
import (
"testing"
......@@ -7,20 +7,20 @@ import (
"github.com/stretchr/testify/assert"
)
func TestGetHelperImageInfo(t *testing.T) {
func TestGetInfo(t *testing.T) {
testCases := []struct {
osType string
expectedHelperImageType interface{}
expectedError interface{}
}{
{osType: OSTypeLinux, expectedHelperImageType: &linuxHelperImageInfo{}, expectedError: nil},
{osType: OSTypeWindows, expectedHelperImageType: &windowsHelperImageInfo{}, expectedError: nil},
{osType: "unsupported", expectedHelperImageType: nil, expectedError: errUnsupportedOSType},
{osType: OSTypeLinux, expectedHelperImageType: &linuxInfo{}, expectedError: nil},
{osType: OSTypeWindows, expectedHelperImageType: &windowsInfo{}, expectedError: nil},
{osType: "unsupported", expectedHelperImageType: nil, expectedError: newUnsupportedOSTypeError("unsupported")},
}
for _, testCase := range testCases {
t.Run(testCase.osType, func(t *testing.T) {
i, err := getHelperImageInfo(types.Info{OSType: testCase.osType})
i, err := GetInfo(types.Info{OSType: testCase.osType})
assert.IsType(t, testCase.expectedHelperImageType, i)
assert.Equal(t, testCase.expectedError, err)
......
package docker
package helperimage
import (
"fmt"
......@@ -7,11 +7,11 @@ import (
"github.com/docker/docker/api/types"
)
type linuxHelperImageInfo struct {
type linuxInfo struct {
dockerArch string
}
func (u *linuxHelperImageInfo) Architecture() string {
func (u *linuxInfo) Architecture() string {
switch u.dockerArch {
case "armv6l", "armv7l", "aarch64":
return "arm"
......@@ -31,16 +31,16 @@ func (u *linuxHelperImageInfo) Architecture() string {
}
}
func (u *linuxHelperImageInfo) Tag(revision string) (string, error) {
func (u *linuxInfo) Tag(revision string) (string, error) {
return fmt.Sprintf("%s-%s", u.Architecture(), revision), nil
}
func (u *linuxHelperImageInfo) IsSupportingLocalImport() bool {
func (u *linuxInfo) IsSupportingLocalImport() bool {
return true
}
func newLinuxHelperImageInfo(info types.Info) helperImageInfo {
return &linuxHelperImageInfo{
func newLinuxInfo(info types.Info) Info {
return &linuxInfo{
dockerArch: info.Architecture,
}
}
package docker
package helperimage
import (
"testing"
......@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_linuxHelperImageInfo_Tag(t *testing.T) {
func Test_linuxInfo_Tag(t *testing.T) {
cases := []struct {
name string
dockerArch string
......@@ -36,7 +36,7 @@ func Test_linuxHelperImageInfo_Tag(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
u := newLinuxHelperImageInfo(types.Info{Architecture: c.dockerArch})
u := newLinuxInfo(types.Info{Architecture: c.dockerArch})
tag, err := u.Tag(c.revision)
......@@ -46,7 +46,7 @@ func Test_linuxHelperImageInfo_Tag(t *testing.T) {
}
}
func Test_linuxHelperImageInfo_IsSupportingLocalImport(t *testing.T) {
u := newLinuxHelperImageInfo(types.Info{})
func Test_linuxInfo_IsSupportingLocalImport(t *testing.T) {
u := newLinuxInfo(types.Info{})
assert.True(t, u.IsSupportingLocalImport())
}
package docker
package helperimage
import (
"errors"
......@@ -25,15 +25,15 @@ var supportedOSVersions = map[string]string{
var ErrUnsupportedOSVersion = errors.New("could not determine windows version")
type windowsHelperImageInfo struct {
type windowsInfo struct {
operatingSystem string
}
func (*windowsHelperImageInfo) Architecture() string {
func (*windowsInfo) Architecture() string {
return windowsSupportedArchitecture
}
func (u *windowsHelperImageInfo) Tag(revision string) (string, error) {
func (u *windowsInfo) Tag(revision string) (string, error) {
osVersion, err := u.osVersion()
if err != nil {
return "", err
......@@ -42,7 +42,7 @@ func (u *windowsHelperImageInfo) Tag(revision string) (string, error) {
return fmt.Sprintf("%s-%s-%s", u.Architecture(), revision, osVersion), nil
}
func (u *windowsHelperImageInfo) osVersion() (string, error) {
func (u *windowsInfo) osVersion() (string, error) {
for operatingSystem, osVersion := range supportedOSVersions {
if strings.Contains(u.operatingSystem, operatingSystem) {
return osVersion, nil
......@@ -52,12 +52,12 @@ func (u *windowsHelperImageInfo) osVersion() (string, error) {
return "", ErrUnsupportedOSVersion
}
func (u *windowsHelperImageInfo) IsSupportingLocalImport() bool {
func (u *windowsInfo) IsSupportingLocalImport() bool {
return false
}
func newWindowsHelperImageInfo(info types.Info) helperImageInfo {
return &windowsHelperImageInfo{
func newWindowsInfo(info types.Info) Info {
return &windowsInfo{
operatingSystem: info.OperatingSystem,
}
}
package docker
package helperimage
import (
"fmt"
......@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_windowsHelperImage_Tag(t *testing.T) {
func Test_windowsInfo_Tag(t *testing.T) {
revision := "4011f186"
cases := []struct {
operatingSystem string
......@@ -33,7 +33,7 @@ func Test_windowsHelperImage_Tag(t *testing.T) {
for _, c := range cases {
t.Run(c.operatingSystem, func(t *testing.T) {
w := newWindowsHelperImageInfo(types.Info{OperatingSystem: c.operatingSystem})
w := newWindowsInfo(types.Info{OperatingSystem: c.operatingSystem})
tag, err := w.Tag(revision)
......@@ -43,7 +43,7 @@ func Test_windowsHelperImage_Tag(t *testing.T) {
}
}
func Test_windowsHelperImage_IsSupportingLocalImport(t *testing.T) {
u := newWindowsHelperImageInfo(types.Info{})
func Test_windowsInfo_IsSupportingLocalImport(t *testing.T) {
u := newWindowsInfo(types.Info{})
assert.False(t, u.IsSupportingLocalImport())
}
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