Merge pull request #800 from docker/fix_windows_tests

Fix unit tests on windows: for secret target path, use path.Join etc. instead of filepath.Join
This commit is contained in:
Guillaume Tardif 2020-10-16 15:32:31 +02:00 committed by GitHub
commit 77f8718f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 24 deletions

View File

@ -23,7 +23,7 @@ import (
"io/ioutil" "io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path"
"strconv" "strconv"
"strings" "strings"
@ -212,18 +212,18 @@ func (p projectAciHelper) getAciSecretVolumes() ([]containerinstance.Volume, err
scr.Target = scr.Source scr.Target = scr.Source
} }
if !filepath.IsAbs(scr.Target) && strings.ContainsAny(scr.Target, "\\/") { if !path.IsAbs(scr.Target) && strings.ContainsAny(scr.Target, "\\/") {
return []containerinstance.Volume{}, return []containerinstance.Volume{},
errors.Errorf("in service %q, secret with source %q cannot have a relative path as target. "+ errors.Errorf("in service %q, secret with source %q cannot have a relative path as target. "+
"Only absolute paths are allowed. Found %q", "Only absolute paths are allowed. Found %q",
svc.Name, scr.Source, scr.Target) svc.Name, scr.Source, scr.Target)
} }
if !filepath.IsAbs(scr.Target) { if !path.IsAbs(scr.Target) {
scr.Target = filepath.Join(defaultSecretsPath, scr.Target) scr.Target = path.Join(defaultSecretsPath, scr.Target)
} }
targetDir := filepath.Dir(scr.Target) targetDir := path.Dir(scr.Target)
targetDirKey := getServiceSecretKey(svc.Name, targetDir) targetDirKey := getServiceSecretKey(svc.Name, targetDir)
if _, ok := squashedTargetVolumes[targetDir]; !ok { if _, ok := squashedTargetVolumes[targetDir]; !ok {
squashedTargetVolumes[targetDir] = containerinstance.Volume{ squashedTargetVolumes[targetDir] = containerinstance.Volume{
@ -232,7 +232,7 @@ func (p projectAciHelper) getAciSecretVolumes() ([]containerinstance.Volume, err
} }
} }
squashedTargetVolumes[targetDir].Secret[filepath.Base(scr.Target)] = &dataStr squashedTargetVolumes[targetDir].Secret[path.Base(scr.Target)] = &dataStr
} }
for _, v := range squashedTargetVolumes { for _, v := range squashedTargetVolumes {
secretVolumes = append(secretVolumes, v) secretVolumes = append(secretVolumes, v)
@ -354,15 +354,15 @@ func (s serviceConfigAciHelper) getAciSecretsVolumeMounts() ([]containerinstance
if scr.Target == "" { if scr.Target == "" {
scr.Target = scr.Source scr.Target = scr.Source
} }
if !filepath.IsAbs(scr.Target) { if !path.IsAbs(scr.Target) {
scr.Target = filepath.Join(defaultSecretsPath, scr.Target) scr.Target = path.Join(defaultSecretsPath, scr.Target)
} }
presenceKey := filepath.Dir(scr.Target) presenceKey := path.Dir(scr.Target)
if !presenceSet[presenceKey] { if !presenceSet[presenceKey] {
vms = append(vms, containerinstance.VolumeMount{ vms = append(vms, containerinstance.VolumeMount{
Name: to.StringPtr(getServiceSecretKey(s.Name, filepath.Dir(scr.Target))), Name: to.StringPtr(getServiceSecretKey(s.Name, path.Dir(scr.Target))),
MountPath: to.StringPtr(filepath.Dir(scr.Target)), MountPath: to.StringPtr(path.Dir(scr.Target)),
ReadOnly: to.BoolPtr(true), ReadOnly: to.BoolPtr(true),
}) })
presenceSet[presenceKey] = true presenceSet[presenceKey] = true
@ -382,8 +382,8 @@ func validateMountPathCollisions(vms []containerinstance.VolumeMount) error {
continue continue
} }
var ( var (
biggerVMPath = strings.Split(*vm1.MountPath, string(filepath.Separator)) biggerVMPath = strings.Split(*vm1.MountPath, "/")
smallerVMPath = strings.Split(*vm2.MountPath, string(filepath.Separator)) smallerVMPath = strings.Split(*vm2.MountPath, "/")
) )
if len(smallerVMPath) > len(biggerVMPath) { if len(smallerVMPath) > len(biggerVMPath) {
tmp := biggerVMPath tmp := biggerVMPath

View File

@ -21,7 +21,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path"
"testing" "testing"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
@ -724,7 +724,7 @@ func TestConvertSecrets(t *testing.T) {
_, err = tmpFile.Write([]byte("test content")) _, err = tmpFile.Write([]byte("test content"))
assert.NilError(t, err) assert.NilError(t, err)
t.Cleanup(func() { t.Cleanup(func() {
assert.NilError(t, os.Remove(tmpFile.Name())) _ = os.Remove(tmpFile.Name())
}) })
t.Run("mix default and absolute", func(t *testing.T) { t.Run("mix default and absolute", func(t *testing.T) {
@ -742,15 +742,15 @@ func TestConvertSecrets(t *testing.T) {
}, },
{ {
Source: secretName, Source: secretName,
Target: filepath.Join(defaultSecretsPath, "some_target2"), Target: path.Join(defaultSecretsPath, "some_target2"),
}, },
{ {
Source: secretName, Source: secretName,
Target: filepath.Join(absBasePath, "some_target3"), Target: path.Join(absBasePath, "some_target3"),
}, },
{ {
Source: secretName, Source: secretName,
Target: filepath.Join(absBasePath, "some_target4"), Target: path.Join(absBasePath, "some_target4"),
}, },
}, },
}, },
@ -812,8 +812,8 @@ func TestConvertSecrets(t *testing.T) {
}) })
t.Run("convert colliding default targets", func(t *testing.T) { t.Run("convert colliding default targets", func(t *testing.T) {
targetName1 := filepath.Join(defaultSecretsPath, "target1") targetName1 := path.Join(defaultSecretsPath, "target1")
targetName2 := filepath.Join(defaultSecretsPath, "sub/folder/target2") targetName2 := path.Join(defaultSecretsPath, "sub/folder/target2")
service := serviceConfigAciHelper{ service := serviceConfigAciHelper{
Name: serviceName, Name: serviceName,
@ -832,12 +832,12 @@ func TestConvertSecrets(t *testing.T) {
_, err := service.getAciSecretsVolumeMounts() _, err := service.getAciSecretsVolumeMounts()
assert.Equal(t, err.Error(), assert.Equal(t, err.Error(),
fmt.Sprintf(`mount paths %q and %q collide. A volume mount cannot include another one.`, fmt.Sprintf(`mount paths %q and %q collide. A volume mount cannot include another one.`,
filepath.Dir(targetName1), filepath.Dir(targetName2))) path.Dir(targetName1), path.Dir(targetName2)))
}) })
t.Run("convert colliding absolute targets", func(t *testing.T) { t.Run("convert colliding absolute targets", func(t *testing.T) {
targetName1 := filepath.Join(absBasePath, "target1") targetName1 := path.Join(absBasePath, "target1")
targetName2 := filepath.Join(absBasePath, "sub/folder/target2") targetName2 := path.Join(absBasePath, "sub/folder/target2")
service := serviceConfigAciHelper{ service := serviceConfigAciHelper{
Name: serviceName, Name: serviceName,
@ -856,7 +856,7 @@ func TestConvertSecrets(t *testing.T) {
_, err := service.getAciSecretsVolumeMounts() _, err := service.getAciSecretsVolumeMounts()
assert.Equal(t, err.Error(), assert.Equal(t, err.Error(),
fmt.Sprintf(`mount paths %q and %q collide. A volume mount cannot include another one.`, fmt.Sprintf(`mount paths %q and %q collide. A volume mount cannot include another one.`,
filepath.Dir(targetName1), filepath.Dir(targetName2))) path.Dir(targetName1), path.Dir(targetName2)))
}) })
} }