k8s.io/kubernetes@v1.29.3/pkg/kubelet/container/testing/os.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testing
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"sync"
    23  	"time"
    24  )
    25  
    26  // FakeOS mocks out certain OS calls to avoid perturbing the filesystem
    27  // If a member of the form `*Fn` is set, that function will be called in place
    28  // of the real call.
    29  type FakeOS struct {
    30  	StatFn     func(string) (os.FileInfo, error)
    31  	ReadDirFn  func(string) ([]os.DirEntry, error)
    32  	MkdirAllFn func(string, os.FileMode) error
    33  	SymlinkFn  func(string, string) error
    34  	GlobFn     func(string, string) bool
    35  	HostName   string
    36  	Removes    []string
    37  	Files      map[string][]*os.FileInfo
    38  	FilesLock  sync.RWMutex
    39  }
    40  
    41  // Mkdir is a fake call that just returns nil.
    42  func (f *FakeOS) MkdirAll(path string, perm os.FileMode) error {
    43  	if f.MkdirAllFn != nil {
    44  		return f.MkdirAllFn(path, perm)
    45  	}
    46  	return nil
    47  }
    48  
    49  // Symlink is a fake call that just returns nil.
    50  func (f *FakeOS) Symlink(oldname string, newname string) error {
    51  	if f.SymlinkFn != nil {
    52  		return f.SymlinkFn(oldname, newname)
    53  	}
    54  	return nil
    55  }
    56  
    57  // Stat is a fake that returns an error
    58  func (f *FakeOS) Stat(path string) (os.FileInfo, error) {
    59  	if f.StatFn != nil {
    60  		return f.StatFn(path)
    61  	}
    62  	return nil, errors.New("unimplemented testing mock")
    63  }
    64  
    65  // Remove is a fake call that returns nil.
    66  func (f *FakeOS) Remove(path string) error {
    67  	f.Removes = append(f.Removes, path)
    68  	return nil
    69  }
    70  
    71  // RemoveAll is a fake call that just returns nil.
    72  func (f *FakeOS) RemoveAll(path string) error {
    73  	f.Removes = append(f.Removes, path)
    74  	return nil
    75  }
    76  
    77  // Create is a fake call that creates a virtual file and returns nil.
    78  func (f *FakeOS) Create(path string) (*os.File, error) {
    79  	f.FilesLock.Lock()
    80  	defer f.FilesLock.Unlock()
    81  	if f.Files == nil {
    82  		f.Files = make(map[string][]*os.FileInfo)
    83  	}
    84  	f.Files[path] = []*os.FileInfo{}
    85  	return nil, nil
    86  }
    87  
    88  // Chmod is a fake call that returns nil.
    89  func (*FakeOS) Chmod(path string, perm os.FileMode) error {
    90  	return nil
    91  }
    92  
    93  // Hostname is a fake call that returns nil.
    94  func (f *FakeOS) Hostname() (name string, err error) {
    95  	return f.HostName, nil
    96  }
    97  
    98  // Chtimes is a fake call that returns nil.
    99  func (*FakeOS) Chtimes(path string, atime time.Time, mtime time.Time) error {
   100  	return nil
   101  }
   102  
   103  // Pipe is a fake call that returns nil.
   104  func (*FakeOS) Pipe() (r *os.File, w *os.File, err error) {
   105  	return nil, nil, nil
   106  }
   107  
   108  // ReadDir is a fake call that returns the files under the directory.
   109  func (f *FakeOS) ReadDir(dirname string) ([]os.DirEntry, error) {
   110  	if f.ReadDirFn != nil {
   111  		return f.ReadDirFn(dirname)
   112  	}
   113  	return nil, nil
   114  }
   115  
   116  // Glob is a fake call that returns list of virtual files matching a pattern.
   117  func (f *FakeOS) Glob(pattern string) ([]string, error) {
   118  	if f.GlobFn != nil {
   119  		var res []string
   120  		f.FilesLock.RLock()
   121  		defer f.FilesLock.RUnlock()
   122  		for k := range f.Files {
   123  			if f.GlobFn(pattern, k) {
   124  				res = append(res, k)
   125  			}
   126  		}
   127  		return res, nil
   128  	}
   129  	return nil, nil
   130  }
   131  
   132  // Open is a fake call that returns nil.
   133  func (*FakeOS) Open(name string) (*os.File, error) {
   134  	return nil, nil
   135  }
   136  
   137  // OpenFile is a fake call that return nil.
   138  func (*FakeOS) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
   139  	return nil, nil
   140  }
   141  
   142  // Rename is a fake call that return nil.
   143  func (*FakeOS) Rename(oldpath, newpath string) error {
   144  	return nil
   145  }