Merge pull request #55095 from smarterclayton/fix_mac

Automatic merge from submit-queue (batch tested with PRs 55092, 55348, 55095, 55277, 55352). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Fix unit tests on darwin / non-linux platforms
pull/6/head
Kubernetes Submit Queue 2017-11-08 21:18:23 -08:00 committed by GitHub
commit 73d53678c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 40 deletions

View File

@ -301,10 +301,12 @@ func recursiveTar(srcBase, srcFile, destBase, destFile string, tw *tar.Writer) e
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
_, err = io.Copy(tw, f)
return err if _, err := io.Copy(tw, f); err != nil {
return err
}
return f.Close()
} }
return nil return nil
} }
@ -330,7 +332,6 @@ func untarAll(reader io.Reader, destFile, prefix string) error {
return err return err
} }
if header.FileInfo().IsDir() { if header.FileInfo().IsDir() {
if err := os.MkdirAll(outFileName, 0755); err != nil { if err := os.MkdirAll(outFileName, 0755); err != nil {
return err return err
} }
@ -359,7 +360,12 @@ func untarAll(reader io.Reader, destFile, prefix string) error {
return err return err
} }
defer outFile.Close() defer outFile.Close()
io.Copy(outFile, tarReader) if _, err := io.Copy(outFile, tarReader); err != nil {
return err
}
if err := outFile.Close(); err != nil {
return err
}
} }
} }

View File

@ -109,8 +109,8 @@ func TestGetPrefix(t *testing.T) {
} }
func TestTarUntar(t *testing.T) { func TestTarUntar(t *testing.T) {
dir, err := ioutil.TempDir(os.TempDir(), "input") dir, err := ioutil.TempDir("", "input")
dir2, err2 := ioutil.TempDir(os.TempDir(), "output") dir2, err2 := ioutil.TempDir("", "output")
if err != nil || err2 != nil { if err != nil || err2 != nil {
t.Errorf("unexpected error: %v | %v", err, err2) t.Errorf("unexpected error: %v | %v", err, err2)
t.FailNow() t.FailNow()
@ -160,74 +160,74 @@ func TestTarUntar(t *testing.T) {
for _, file := range files { for _, file := range files {
filepath := path.Join(dir, file.name) filepath := path.Join(dir, file.name)
if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil { if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
t.FailNow()
} }
if file.fileType == RegularFile { if file.fileType == RegularFile {
f, err := os.Create(filepath) f, err := os.Create(filepath)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
t.FailNow()
} }
defer f.Close() defer f.Close()
if _, err := io.Copy(f, bytes.NewBuffer([]byte(file.data))); err != nil { if _, err := io.Copy(f, bytes.NewBuffer([]byte(file.data))); err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
t.FailNow() }
if err := f.Close(); err != nil {
t.Fatal(err)
} }
} else if file.fileType == SymLink { } else if file.fileType == SymLink {
err := os.Symlink(file.data, filepath) err := os.Symlink(file.data, filepath)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
t.FailNow()
} }
} else { } else {
t.Errorf("unexpected file type: %v", file) t.Fatalf("unexpected file type: %v", file)
t.FailNow()
} }
} }
writer := &bytes.Buffer{} writer := &bytes.Buffer{}
if err := makeTar(dir, dir, writer); err != nil { if err := makeTar(dir, dir, writer); err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
reader := bytes.NewBuffer(writer.Bytes()) reader := bytes.NewBuffer(writer.Bytes())
if err := untarAll(reader, dir2, ""); err != nil { if err := untarAll(reader, dir2, ""); err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
t.FailNow()
} }
for _, file := range files { for _, file := range files {
absPath := dir2 + strings.TrimPrefix(dir, os.TempDir()) absPath := filepath.Join(dir2, strings.TrimPrefix(dir, os.TempDir()))
filepath := path.Join(absPath, file.name) filePath := filepath.Join(absPath, file.name)
if file.fileType == RegularFile { if file.fileType == RegularFile {
f, err := os.Open(filepath) f, err := os.Open(filePath)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
defer f.Close() defer f.Close()
buff := &bytes.Buffer{} buff := &bytes.Buffer{}
io.Copy(buff, f) if _, err := io.Copy(buff, f); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
if file.data != string(buff.Bytes()) { if file.data != string(buff.Bytes()) {
t.Errorf("expected: %s, saw: %s", file.data, string(buff.Bytes())) t.Fatalf("expected: %s, saw: %s", file.data, string(buff.Bytes()))
} }
} else if file.fileType == SymLink { } else if file.fileType == SymLink {
dest, err := os.Readlink(filepath) dest, err := os.Readlink(filePath)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if file.data != dest { if file.data != dest {
t.Errorf("expected: %s, saw: %s", file.data, dest) t.Fatalf("expected: %s, saw: %s", file.data, dest)
} }
} else { } else {
t.Errorf("unexpected file type: %v", file) t.Fatalf("unexpected file type: %v", file)
t.FailNow()
} }
} }
} }

View File

@ -40,17 +40,20 @@ func (mi *fakeMountInterface) List() ([]mount.MountPoint, error) {
return mi.mountPoints, nil return mi.mountPoints, nil
} }
func (f *fakeMountInterface) IsMountPointMatch(mp mount.MountPoint, dir string) bool { func (mi *fakeMountInterface) IsMountPointMatch(mp mount.MountPoint, dir string) bool {
return (mp.Path == dir) return (mp.Path == dir)
} }
func (f *fakeMountInterface) IsNotMountPoint(dir string) (bool, error) { func (mi *fakeMountInterface) IsNotMountPoint(dir string) (bool, error) {
return false, fmt.Errorf("unsupported") return false, fmt.Errorf("unsupported")
} }
func (mi *fakeMountInterface) IsLikelyNotMountPoint(file string) (bool, error) { func (mi *fakeMountInterface) IsLikelyNotMountPoint(file string) (bool, error) {
return false, fmt.Errorf("unsupported") return false, fmt.Errorf("unsupported")
} }
func (mi *fakeMountInterface) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
return "", nil
}
func (mi *fakeMountInterface) DeviceOpened(pathname string) (bool, error) { func (mi *fakeMountInterface) DeviceOpened(pathname string) (bool, error) {
for _, mp := range mi.mountPoints { for _, mp := range mi.mountPoints {
@ -65,14 +68,26 @@ func (mi *fakeMountInterface) PathIsDevice(pathname string) (bool, error) {
return true, nil return true, nil
} }
func (mi *fakeMountInterface) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
return "", nil
}
func (mi *fakeMountInterface) MakeRShared(path string) error { func (mi *fakeMountInterface) MakeRShared(path string) error {
return nil return nil
} }
func (mi *fakeMountInterface) GetFileType(pathname string) (mount.FileType, error) {
return mount.FileType("fake"), nil
}
func (mi *fakeMountInterface) MakeDir(pathname string) error {
return nil
}
func (mi *fakeMountInterface) MakeFile(pathname string) error {
return nil
}
func (mi *fakeMountInterface) ExistsPath(pathname string) bool {
return true
}
func fakeContainerMgrMountInt() mount.Interface { func fakeContainerMgrMountInt() mount.Interface {
return &fakeMountInterface{ return &fakeMountInterface{
[]mount.MountPoint{ []mount.MountPoint{

View File

@ -86,7 +86,7 @@ func (mounter *Mounter) MakeRShared(path string) error {
} }
func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, fstype string, options []string) error { func (mounter *SafeFormatAndMount) formatAndMount(source string, target string, fstype string, options []string) error {
return nil return mounter.Interface.Mount(source, target, fstype, options)
} }
func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) { func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) {

View File

@ -19,6 +19,7 @@ package rbd
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
@ -149,7 +150,7 @@ func checkMounterLog(t *testing.T, fakeMounter *mount.FakeMounter, expected int,
lastIndex := len(fakeMounter.Log) - 1 lastIndex := len(fakeMounter.Log) - 1
lastAction := fakeMounter.Log[lastIndex] lastAction := fakeMounter.Log[lastIndex]
if !reflect.DeepEqual(expectedAction, lastAction) { if !reflect.DeepEqual(expectedAction, lastAction) {
t.Fatalf("fakeMounter.Log[%d] should be %v, not: %v", lastIndex, expectedAction, lastAction) t.Fatalf("fakeMounter.Log[%d] should be %#v, not: %#v", lastIndex, expectedAction, lastAction)
} }
} }
@ -276,6 +277,10 @@ func TestPlugin(t *testing.T) {
t.Fatalf("error creating temp dir: %v", err) t.Fatalf("error creating temp dir: %v", err)
} }
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
podUID := uuid.NewUUID() podUID := uuid.NewUUID()
var cases []*testcase var cases []*testcase