GetMountRefs shouldn't error when file doesn'g exist in Windows and nsenter. Add unit test

pull/8/head
David Zhu 2018-09-17 17:50:27 -07:00
parent 9d207b3e3c
commit 704573d304
4 changed files with 18 additions and 0 deletions

View File

@ -1007,6 +1007,8 @@ func (mounter *Mounter) SafeMakeDir(subdir string, base string, perm os.FileMode
func (mounter *Mounter) GetMountRefs(pathname string) ([]string, error) { func (mounter *Mounter) GetMountRefs(pathname string) ([]string, error) {
if _, err := os.Stat(pathname); os.IsNotExist(err) { if _, err := os.Stat(pathname); os.IsNotExist(err) {
return []string{}, nil return []string{}, nil
} else if err != nil {
return nil, err
} }
realpath, err := filepath.EvalSymlinks(pathname) realpath, err := filepath.EvalSymlinks(pathname)
if err != nil { if err != nil {

View File

@ -110,6 +110,10 @@ func TestGetMountRefs(t *testing.T) {
"/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2", "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2",
}, },
}, },
{
"/var/fake/directory/that/doesnt/exist",
[]string{},
},
} }
for i, test := range tests { for i, test := range tests {

View File

@ -459,6 +459,11 @@ func getAllParentLinks(path string) ([]string, error) {
} }
func (mounter *Mounter) GetMountRefs(pathname string) ([]string, error) { func (mounter *Mounter) GetMountRefs(pathname string) ([]string, error) {
if _, err := os.Stat(normalizeWindowsPath(pathname)); os.IsNotExist(err) {
return []string{}, nil
} else if err != nil {
return nil, err
}
refs, err := getAllParentLinks(normalizeWindowsPath(pathname)) refs, err := getAllParentLinks(normalizeWindowsPath(pathname))
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -337,6 +337,13 @@ func (mounter *NsenterMounter) SafeMakeDir(subdir string, base string, perm os.F
} }
func (mounter *NsenterMounter) GetMountRefs(pathname string) ([]string, error) { func (mounter *NsenterMounter) GetMountRefs(pathname string) ([]string, error) {
exists, err := mounter.ExistsPath(pathname)
if err != nil {
return nil, err
}
if !exists {
return []string{}, nil
}
hostpath, err := mounter.ne.EvalSymlinks(pathname, true /* mustExist */) hostpath, err := mounter.ne.EvalSymlinks(pathname, true /* mustExist */)
if err != nil { if err != nil {
return nil, err return nil, err