implement proposal 34058: hostPath volume type

This commit is contained in:
Di Xu
2017-06-07 13:10:09 +08:00
parent 625eb9ab7a
commit 5c45db564f
14 changed files with 753 additions and 22 deletions

View File

@@ -34,6 +34,21 @@ import (
volumetest "k8s.io/kubernetes/pkg/volume/testing"
)
func newHostPathType(pathType string) *v1.HostPathType {
hostPathType := new(v1.HostPathType)
*hostPathType = v1.HostPathType(pathType)
return hostPathType
}
func newHostPathTypeList(pathType ...string) []*v1.HostPathType {
typeList := []*v1.HostPathType{}
for _, ele := range pathType {
typeList = append(typeList, newHostPathType(ele))
}
return typeList
}
func TestCanSupport(t *testing.T) {
plugMgr := volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil))
@@ -108,7 +123,7 @@ func TestDeleter(t *testing.T) {
if err := deleter.Delete(); err != nil {
t.Errorf("Mock Recycler expected to return nil but got %s", err)
}
if exists, _ := utilfile.FileExists("foo"); exists {
if exists, _ := utilfile.FileExists(tempPath); exists {
t.Errorf("Temp path expected to be deleted, but was found at %s", tempPath)
}
}
@@ -215,11 +230,14 @@ func TestPlugin(t *testing.T) {
if err != nil {
t.Errorf("Can't find the plugin by name")
}
volPath := "/tmp/vol1"
spec := &v1.Volume{
Name: "vol1",
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/vol1"}},
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: volPath, Type: newHostPathType(string(v1.HostPathDirectoryOrCreate))}},
}
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
defer os.RemoveAll(volPath)
mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err)
@@ -229,7 +247,7 @@ func TestPlugin(t *testing.T) {
}
path := mounter.GetPath()
if path != "/vol1" {
if path != volPath {
t.Errorf("Got unexpected path: %s", path)
}
@@ -257,13 +275,14 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) {
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
HostPath: &v1.HostPathVolumeSource{Path: "foo"},
HostPath: &v1.HostPathVolumeSource{Path: "foo", Type: newHostPathType(string(v1.HostPathDirectoryOrCreate))},
},
ClaimRef: &v1.ObjectReference{
Name: "claimA",
},
},
}
defer os.RemoveAll("foo")
claim := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
@@ -293,3 +312,297 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) {
t.Errorf("Expected true for mounter.IsReadOnly")
}
}
type fakeFileTypeChecker struct {
desiredType string
}
func (fftc *fakeFileTypeChecker) getFileType(_ os.FileInfo) (v1.HostPathType, error) {
return *newHostPathType(fftc.desiredType), nil
}
func setUp() error {
err := os.MkdirAll("/tmp/ExistingFolder", os.FileMode(0755))
if err != nil {
return err
}
f, err := os.OpenFile("/tmp/ExistingFolder/foo", os.O_CREATE, os.FileMode(0644))
defer f.Close()
if err != nil {
return err
}
return nil
}
func tearDown() {
os.RemoveAll("/tmp/ExistingFolder")
}
func TestOSFileTypeChecker(t *testing.T) {
err := setUp()
if err != nil {
t.Error(err)
}
defer tearDown()
testCases := []struct {
name string
path string
desiredType string
isDir bool
isFile bool
isSocket bool
isBlock bool
isChar bool
}{
{
name: "Existing Folder",
path: "/tmp/ExistingFolder",
isDir: true,
},
{
name: "Existing File",
path: "/tmp/ExistingFolder/foo",
isFile: true,
},
{
name: "Existing Socket File",
path: "/tmp/ExistingFolder/foo",
desiredType: string(v1.HostPathSocket),
isSocket: true,
},
{
name: "Existing Character Device",
path: "/tmp/ExistingFolder/foo",
desiredType: string(v1.HostPathCharDev),
isChar: true,
},
{
name: "Existing Block Device",
path: "/tmp/ExistingFolder/foo",
desiredType: string(v1.HostPathBlockDev),
isBlock: true,
},
}
for i, tc := range testCases {
oftc, err := newOSFileTypeChecker(tc.path,
&fakeFileTypeChecker{desiredType: tc.desiredType})
if err != nil {
t.Errorf("[%d: %q] expect nil, but got %v", i, tc.name, err)
}
path := oftc.GetPath()
if path != tc.path {
t.Errorf("[%d: %q] got unexpected path: %s", i, tc.name, path)
}
exist := oftc.Exists()
if !exist {
t.Errorf("[%d: %q] path: %s does not exist", i, tc.name, path)
}
if tc.isDir {
if !oftc.IsDir() {
t.Errorf("[%d: %q] expected folder, got unexpected: %s", i, tc.name, path)
}
if oftc.IsFile() {
t.Errorf("[%d: %q] expected folder, got unexpected file: %s", i, tc.name, path)
}
if oftc.IsSocket() {
t.Errorf("[%d: %q] expected folder, got unexpected socket file: %s", i, tc.name, path)
}
if oftc.IsBlock() {
t.Errorf("[%d: %q] expected folder, got unexpected block device: %s", i, tc.name, path)
}
if oftc.IsChar() {
t.Errorf("[%d: %q] expected folder, got unexpected character device: %s", i, tc.name, path)
}
}
if tc.isFile {
if !oftc.IsFile() {
t.Errorf("[%d: %q] expected file, got unexpected: %s", i, tc.name, path)
}
if oftc.IsDir() {
t.Errorf("[%d: %q] expected file, got unexpected folder: %s", i, tc.name, path)
}
if oftc.IsSocket() {
t.Errorf("[%d: %q] expected file, got unexpected socket file: %s", i, tc.name, path)
}
if oftc.IsBlock() {
t.Errorf("[%d: %q] expected file, got unexpected block device: %s", i, tc.name, path)
}
if oftc.IsChar() {
t.Errorf("[%d: %q] expected file, got unexpected character device: %s", i, tc.name, path)
}
}
if tc.isSocket {
if !oftc.IsSocket() {
t.Errorf("[%d: %q] expected socket file, got unexpected: %s", i, tc.name, path)
}
if oftc.IsDir() {
t.Errorf("[%d: %q] expected socket file, got unexpected folder: %s", i, tc.name, path)
}
if !oftc.IsFile() {
t.Errorf("[%d: %q] expected socket file, got unexpected file: %s", i, tc.name, path)
}
if oftc.IsBlock() {
t.Errorf("[%d: %q] expected socket file, got unexpected block device: %s", i, tc.name, path)
}
if oftc.IsChar() {
t.Errorf("[%d: %q] expected socket file, got unexpected character device: %s", i, tc.name, path)
}
}
if tc.isChar {
if !oftc.IsChar() {
t.Errorf("[%d: %q] expected character device, got unexpected: %s", i, tc.name, path)
}
if oftc.IsDir() {
t.Errorf("[%d: %q] expected character device, got unexpected folder: %s", i, tc.name, path)
}
if !oftc.IsFile() {
t.Errorf("[%d: %q] expected character device, got unexpected file: %s", i, tc.name, path)
}
if oftc.IsSocket() {
t.Errorf("[%d: %q] expected character device, got unexpected socket file: %s", i, tc.name, path)
}
if oftc.IsBlock() {
t.Errorf("[%d: %q] expected character device, got unexpected block device: %s", i, tc.name, path)
}
}
if tc.isBlock {
if !oftc.IsBlock() {
t.Errorf("[%d: %q] expected block device, got unexpected: %s", i, tc.name, path)
}
if oftc.IsDir() {
t.Errorf("[%d: %q] expected block device, got unexpected folder: %s", i, tc.name, path)
}
if !oftc.IsFile() {
t.Errorf("[%d: %q] expected block device, got unexpected file: %s", i, tc.name, path)
}
if oftc.IsSocket() {
t.Errorf("[%d: %q] expected block device, got unexpected socket file: %s", i, tc.name, path)
}
if oftc.IsChar() {
t.Errorf("[%d: %q] expected block device, got unexpected character device: %s", i, tc.name, path)
}
}
}
}
type fakeHostPathTypeChecker struct {
name string
path string
exists bool
isDir bool
isFile bool
isSocket bool
isBlock bool
isChar bool
validpathType []*v1.HostPathType
invalidpathType []*v1.HostPathType
}
func (ftc *fakeHostPathTypeChecker) MakeFile() error { return nil }
func (ftc *fakeHostPathTypeChecker) MakeDir() error { return nil }
func (ftc *fakeHostPathTypeChecker) Exists() bool { return ftc.exists }
func (ftc *fakeHostPathTypeChecker) IsFile() bool { return ftc.isFile }
func (ftc *fakeHostPathTypeChecker) IsDir() bool { return ftc.isDir }
func (ftc *fakeHostPathTypeChecker) IsBlock() bool { return ftc.isBlock }
func (ftc *fakeHostPathTypeChecker) IsChar() bool { return ftc.isChar }
func (ftc *fakeHostPathTypeChecker) IsSocket() bool { return ftc.isSocket }
func (ftc *fakeHostPathTypeChecker) GetPath() string { return ftc.path }
func TestHostPathTypeCheckerInternal(t *testing.T) {
testCases := []fakeHostPathTypeChecker{
{
name: "Existing Folder",
path: "/existingFolder",
isDir: true,
exists: true,
validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory)),
invalidpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile),
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
},
{
name: "New Folder",
path: "/newFolder",
isDir: false,
exists: false,
validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate)),
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory), string(v1.HostPathFile),
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
},
{
name: "Existing File",
path: "/existingFile",
isFile: true,
exists: true,
validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
},
{
name: "New File",
path: "/newFile",
isFile: false,
exists: false,
validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate)),
invalidpathType: newHostPathTypeList(string(v1.HostPathFile), string(v1.HostPathDirectory),
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
},
{
name: "Existing Socket",
path: "/existing.socket",
isSocket: true,
isFile: true,
exists: true,
validpathType: newHostPathTypeList(string(v1.HostPathSocket), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
},
{
name: "Existing Character Device",
path: "/existing.char",
isChar: true,
isFile: true,
exists: true,
validpathType: newHostPathTypeList(string(v1.HostPathCharDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
string(v1.HostPathSocket), string(v1.HostPathBlockDev)),
},
{
name: "Existing Block Device",
path: "/existing.block",
isBlock: true,
isFile: true,
exists: true,
validpathType: newHostPathTypeList(string(v1.HostPathBlockDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
string(v1.HostPathSocket), string(v1.HostPathCharDev)),
},
}
for i, tc := range testCases {
for _, pathType := range tc.validpathType {
err := checkTypeInternal(&tc, pathType)
if err != nil {
t.Errorf("[%d: %q] [%q] expected nil, got %v", i, tc.name, string(*pathType), err)
}
}
for _, pathType := range tc.invalidpathType {
checkResult := checkTypeInternal(&tc, pathType)
if checkResult == nil {
t.Errorf("[%d: %q] [%q] expected error, got nil", i, tc.name, string(*pathType))
}
}
}
}