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 {
return err
}
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
}
@ -330,7 +332,6 @@ func untarAll(reader io.Reader, destFile, prefix string) error {
return err
}
if header.FileInfo().IsDir() {
if err := os.MkdirAll(outFileName, 0755); err != nil {
return err
}
@ -359,7 +360,12 @@ func untarAll(reader io.Reader, destFile, prefix string) error {
return err
}
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) {
dir, err := ioutil.TempDir(os.TempDir(), "input")
dir2, err2 := ioutil.TempDir(os.TempDir(), "output")
dir, err := ioutil.TempDir("", "input")
dir2, err2 := ioutil.TempDir("", "output")
if err != nil || err2 != nil {
t.Errorf("unexpected error: %v | %v", err, err2)
t.FailNow()
@ -160,74 +160,74 @@ func TestTarUntar(t *testing.T) {
for _, file := range files {
filepath := path.Join(dir, file.name)
if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
t.Errorf("unexpected error: %v", err)
t.FailNow()
t.Fatalf("unexpected error: %v", err)
}
if file.fileType == RegularFile {
f, err := os.Create(filepath)
if err != nil {
t.Errorf("unexpected error: %v", err)
t.FailNow()
t.Fatalf("unexpected error: %v", err)
}
defer f.Close()
if _, err := io.Copy(f, bytes.NewBuffer([]byte(file.data))); err != nil {
t.Errorf("unexpected error: %v", err)
t.FailNow()
t.Fatalf("unexpected error: %v", err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
} else if file.fileType == SymLink {
err := os.Symlink(file.data, filepath)
if err != nil {
t.Errorf("unexpected error: %v", err)
t.FailNow()
t.Fatalf("unexpected error: %v", err)
}
} else {
t.Errorf("unexpected file type: %v", file)
t.FailNow()
t.Fatalf("unexpected file type: %v", file)
}
}
writer := &bytes.Buffer{}
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())
if err := untarAll(reader, dir2, ""); err != nil {
t.Errorf("unexpected error: %v", err)
t.FailNow()
t.Fatalf("unexpected error: %v", err)
}
for _, file := range files {
absPath := dir2 + strings.TrimPrefix(dir, os.TempDir())
filepath := path.Join(absPath, file.name)
absPath := filepath.Join(dir2, strings.TrimPrefix(dir, os.TempDir()))
filePath := filepath.Join(absPath, file.name)
if file.fileType == RegularFile {
f, err := os.Open(filepath)
f, err := os.Open(filePath)
if err != nil {
t.Errorf("unexpected error: %v", err)
t.Fatalf("unexpected error: %v", err)
}
defer f.Close()
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()) {
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 {
dest, err := os.Readlink(filepath)
dest, err := os.Readlink(filePath)
if err != nil {
t.Errorf("unexpected error: %v", err)
t.Fatalf("unexpected error: %v", err)
}
if file.data != dest {
t.Errorf("expected: %s, saw: %s", file.data, dest)
t.Fatalf("expected: %s, saw: %s", file.data, dest)
}
} else {
t.Errorf("unexpected file type: %v", file)
t.FailNow()
t.Fatalf("unexpected file type: %v", file)
}
}
}

View File

@ -40,17 +40,20 @@ func (mi *fakeMountInterface) List() ([]mount.MountPoint, error) {
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)
}
func (f *fakeMountInterface) IsNotMountPoint(dir string) (bool, error) {
func (mi *fakeMountInterface) IsNotMountPoint(dir string) (bool, error) {
return false, fmt.Errorf("unsupported")
}
func (mi *fakeMountInterface) IsLikelyNotMountPoint(file string) (bool, error) {
return false, fmt.Errorf("unsupported")
}
func (mi *fakeMountInterface) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
return "", nil
}
func (mi *fakeMountInterface) DeviceOpened(pathname string) (bool, error) {
for _, mp := range mi.mountPoints {
@ -65,14 +68,26 @@ func (mi *fakeMountInterface) PathIsDevice(pathname string) (bool, error) {
return true, nil
}
func (mi *fakeMountInterface) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) {
return "", nil
}
func (mi *fakeMountInterface) MakeRShared(path string) error {
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 {
return &fakeMountInterface{
[]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 {
return nil
return mounter.Interface.Mount(source, target, fstype, options)
}
func (mounter *SafeFormatAndMount) diskLooksUnformatted(disk string) (bool, error) {

View File

@ -19,6 +19,7 @@ package rbd
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
@ -149,7 +150,7 @@ func checkMounterLog(t *testing.T, fakeMounter *mount.FakeMounter, expected int,
lastIndex := len(fakeMounter.Log) - 1
lastAction := fakeMounter.Log[lastIndex]
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)
}
defer os.RemoveAll(tmpDir)
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
podUID := uuid.NewUUID()
var cases []*testcase