tests: create temporary dir/files than /usr directory. (#4820)

Fixes #4816
master
Bala FA 7 years ago committed by Dee Koder
parent 9dca0c1889
commit 7505bac037
  1. 123
      cmd/posix_test.go

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -47,6 +48,59 @@ func newPosixTestSetup() (StorageAPI, string, error) {
return posixStorage, diskPath, nil return posixStorage, diskPath, nil
} }
// createPermDeniedFile - creates temporary directory and file with path '/mybucket/myobject'
func createPermDeniedFile(t *testing.T) (permDeniedDir string) {
var errMsg string
defer func() {
if errMsg == "" {
return
}
if permDeniedDir != "" {
os.RemoveAll(permDeniedDir)
}
t.Fatalf(errMsg)
}()
var err error
if permDeniedDir, err = ioutil.TempDir(globalTestTmpDir, "minio-"); err != nil {
errMsg = fmt.Sprintf("Unable to create temporary directory. %v", err)
return permDeniedDir
}
if err = os.Mkdir(slashpath.Join(permDeniedDir, "mybucket"), 0775); err != nil {
errMsg = fmt.Sprintf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
return permDeniedDir
}
if err = ioutil.WriteFile(slashpath.Join(permDeniedDir, "mybucket", "myobject"), []byte(""), 0400); err != nil {
errMsg = fmt.Sprintf("Unable to create file %v. %v", slashpath.Join(permDeniedDir, "mybucket", "myobject"), err)
return permDeniedDir
}
if err = os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0400); err != nil {
errMsg = fmt.Sprintf("Unable to change permission to temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
return permDeniedDir
}
if err = os.Chmod(permDeniedDir, 0400); err != nil {
errMsg = fmt.Sprintf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
}
return permDeniedDir
}
// removePermDeniedFile - removes temporary directory and file with path '/mybucket/myobject'
func removePermDeniedFile(permDeniedDir string) {
if err := os.Chmod(permDeniedDir, 0775); err == nil {
if err = os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0775); err == nil {
os.RemoveAll(permDeniedDir)
}
}
}
// TestPosixs posix.getDiskInfo() // TestPosixs posix.getDiskInfo()
func TestPosixGetDiskInfo(t *testing.T) { func TestPosixGetDiskInfo(t *testing.T) {
path, err := ioutil.TempDir(globalTestTmpDir, "minio-") path, err := ioutil.TempDir(globalTestTmpDir, "minio-")
@ -327,13 +381,21 @@ func TestPosixMakeVol(t *testing.T) {
// TestPosix for permission denied. // TestPosix for permission denied.
if runtime.GOOS != globalWindowsOSName { if runtime.GOOS != globalWindowsOSName {
permDeniedDir, err := ioutil.TempDir(globalTestTmpDir, "minio-")
if err != nil {
t.Fatalf("Unable to create temporary directory. %v", err)
}
defer os.RemoveAll(permDeniedDir)
if err = os.Chmod(permDeniedDir, 0400); err != nil {
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
}
// Initialize posix storage layer for permission denied error. // Initialize posix storage layer for permission denied error.
posix, err := newPosix("/usr") posixStorage, err = newPosix(permDeniedDir)
if err != nil { if err != nil {
t.Fatalf("Unable to initialize posix, %s", err) t.Fatalf("Unable to initialize posix, %s", err)
} }
if err := posix.MakeVol("test-vol"); err != errDiskAccessDenied { if err := posixStorage.MakeVol("test-vol"); err != errDiskAccessDenied {
t.Fatalf("expected: %s, got: %s", errDiskAccessDenied, err) t.Fatalf("expected: %s, got: %s", errDiskAccessDenied, err)
} }
} }
@ -418,8 +480,19 @@ func TestPosixDeleteVol(t *testing.T) {
// TestPosix for permission denied. // TestPosix for permission denied.
if runtime.GOOS != globalWindowsOSName { if runtime.GOOS != globalWindowsOSName {
var permDeniedDir string
if permDeniedDir, err = ioutil.TempDir(globalTestTmpDir, "minio-"); err != nil {
t.Fatalf("Unable to create temporary directory. %v", err)
}
defer removePermDeniedFile(permDeniedDir)
if err = os.Mkdir(slashpath.Join(permDeniedDir, "mybucket"), 0400); err != nil {
t.Fatalf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
}
if err = os.Chmod(permDeniedDir, 0400); err != nil {
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
}
// Initialize posix storage layer for permission denied error. // Initialize posix storage layer for permission denied error.
posixStorage, err = newPosix("/usr") posixStorage, err = newPosix(permDeniedDir)
if err != nil { if err != nil {
t.Fatalf("Unable to initialize posix, %s", err) t.Fatalf("Unable to initialize posix, %s", err)
} }
@ -697,14 +770,18 @@ func TestPosixPosixListDir(t *testing.T) {
// TestPosix for permission denied. // TestPosix for permission denied.
if runtime.GOOS != globalWindowsOSName { if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
// Initialize posix storage layer for permission denied error. // Initialize posix storage layer for permission denied error.
posixStorage, err = newPosix("/usr") posixStorage, err = newPosix(permDeniedDir)
if err != nil { if err != nil {
t.Errorf("Unable to initialize posix, %s", err) t.Fatalf("Unable to initialize posix, %s", err)
} }
if err = posixStorage.DeleteFile("bin", "yes"); err != errFileAccessDenied { errMsg := fmt.Sprintf("stat %v: permission denied", slashpath.Join(permDeniedDir, "mybucket"))
t.Errorf("expected: %s, got: %s", errFileAccessDenied, err) if err = posixStorage.DeleteFile("mybucket", "myobject"); err.Error() != errMsg {
t.Errorf("expected: %s, got: %s", errMsg, err)
} }
} }
@ -831,14 +908,18 @@ func TestPosixDeleteFile(t *testing.T) {
// TestPosix for permission denied. // TestPosix for permission denied.
if runtime.GOOS != globalWindowsOSName { if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
// Initialize posix storage layer for permission denied error. // Initialize posix storage layer for permission denied error.
posixStorage, err = newPosix("/usr") posixStorage, err = newPosix(permDeniedDir)
if err != nil { if err != nil {
t.Errorf("Unable to initialize posix, %s", err) t.Fatalf("Unable to initialize posix, %s", err)
} }
if err = posixStorage.DeleteFile("bin", "yes"); err != errFileAccessDenied { errMsg := fmt.Sprintf("stat %v: permission denied", slashpath.Join(permDeniedDir, "mybucket"))
t.Errorf("expected: %s, got: %s", errFileAccessDenied, err) if err = posixStorage.DeleteFile("mybucket", "myobject"); err.Error() != errMsg {
t.Errorf("expected: %s, got: %s", errMsg, err)
} }
} }
@ -1211,14 +1292,18 @@ func TestPosixAppendFile(t *testing.T) {
// TestPosix for permission denied. // TestPosix for permission denied.
if runtime.GOOS != globalWindowsOSName { if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
var posixPermStorage StorageAPI
// Initialize posix storage layer for permission denied error. // Initialize posix storage layer for permission denied error.
posixStorage, err = newPosix("/usr") posixPermStorage, err = newPosix(permDeniedDir)
if err != nil { if err != nil {
t.Fatalf("Unable to initialize posix, %s", err) t.Fatalf("Unable to initialize posix, %s", err)
} }
if err = posixStorage.AppendFile("bin", "yes", []byte("hello, world")); !os.IsPermission(err) { if err = posixPermStorage.AppendFile("mybucket", "myobject", []byte("hello, world")); !os.IsPermission(err) {
t.Errorf("expected: Permission error, got: %s", err) t.Fatalf("expected: Permission error, got: %s", err)
} }
} }
// TestPosix case with invalid volume name. // TestPosix case with invalid volume name.
@ -1298,14 +1383,18 @@ func TestPosixPrepareFile(t *testing.T) {
// TestPosix for permission denied. // TestPosix for permission denied.
if runtime.GOOS != globalWindowsOSName { if runtime.GOOS != globalWindowsOSName {
permDeniedDir := createPermDeniedFile(t)
defer removePermDeniedFile(permDeniedDir)
var posixPermStorage StorageAPI
// Initialize posix storage layer for permission denied error. // Initialize posix storage layer for permission denied error.
posixStorage, err = newPosix("/usr") posixPermStorage, err = newPosix(permDeniedDir)
if err != nil { if err != nil {
t.Fatalf("Unable to initialize posix, %s", err) t.Fatalf("Unable to initialize posix, %s", err)
} }
if err = posixStorage.PrepareFile("bin", "yes", 16); !os.IsPermission(err) { if err = posixPermStorage.PrepareFile("mybucket", "myobject", 16); !os.IsPermission(err) {
t.Errorf("expected: Permission error, got: %s", err) t.Fatalf("expected: Permission error, got: %s", err)
} }
} }

Loading…
Cancel
Save