瀏覽代碼

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
Guillaume Tardif 5 年之前
父節點
當前提交
77f8718f03
共有 2 個文件被更改,包括 24 次插入24 次删除
  1. 13 13
      aci/convert/convert.go
  2. 11 11
      aci/convert/convert_test.go

+ 13 - 13
aci/convert/convert.go

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

+ 11 - 11
aci/convert/convert_test.go

@@ -21,7 +21,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"os"
-	"path/filepath"
+	"path"
 	"testing"
 
 	"github.com/stretchr/testify/mock"
@@ -724,7 +724,7 @@ func TestConvertSecrets(t *testing.T) {
 	_, err = tmpFile.Write([]byte("test content"))
 	assert.NilError(t, err)
 	t.Cleanup(func() {
-		assert.NilError(t, os.Remove(tmpFile.Name()))
+		_ = os.Remove(tmpFile.Name())
 	})
 
 	t.Run("mix default and absolute", func(t *testing.T) {
@@ -742,15 +742,15 @@ func TestConvertSecrets(t *testing.T) {
 						},
 						{
 							Source: secretName,
-							Target: filepath.Join(defaultSecretsPath, "some_target2"),
+							Target: path.Join(defaultSecretsPath, "some_target2"),
 						},
 						{
 							Source: secretName,
-							Target: filepath.Join(absBasePath, "some_target3"),
+							Target: path.Join(absBasePath, "some_target3"),
 						},
 						{
 							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) {
-		targetName1 := filepath.Join(defaultSecretsPath, "target1")
-		targetName2 := filepath.Join(defaultSecretsPath, "sub/folder/target2")
+		targetName1 := path.Join(defaultSecretsPath, "target1")
+		targetName2 := path.Join(defaultSecretsPath, "sub/folder/target2")
 
 		service := serviceConfigAciHelper{
 			Name: serviceName,
@@ -832,12 +832,12 @@ func TestConvertSecrets(t *testing.T) {
 		_, err := service.getAciSecretsVolumeMounts()
 		assert.Equal(t, err.Error(),
 			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) {
-		targetName1 := filepath.Join(absBasePath, "target1")
-		targetName2 := filepath.Join(absBasePath, "sub/folder/target2")
+		targetName1 := path.Join(absBasePath, "target1")
+		targetName2 := path.Join(absBasePath, "sub/folder/target2")
 
 		service := serviceConfigAciHelper{
 			Name: serviceName,
@@ -856,7 +856,7 @@ func TestConvertSecrets(t *testing.T) {
 		_, err := service.getAciSecretsVolumeMounts()
 		assert.Equal(t, err.Error(),
 			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)))
 	})
 }