k8s.io/kubernetes@v1.29.3/pkg/util/removeall/removeall_test.go (about)

     1  /*
     2  Copyright 2017 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 removeall
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"path"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	utiltesting "k8s.io/client-go/util/testing"
    28  	"k8s.io/mount-utils"
    29  )
    30  
    31  type fakeMounter struct {
    32  	mount.FakeMounter
    33  }
    34  
    35  // IsLikelyNotMountPoint overrides mount.FakeMounter.IsLikelyNotMountPoint for our use.
    36  func (f *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
    37  	name := filepath.Base(file)
    38  	if strings.HasPrefix(name, "mount") {
    39  		return false, nil
    40  	}
    41  	if strings.HasPrefix(name, "err") {
    42  		return false, errors.New("mock error")
    43  	}
    44  	return true, nil
    45  }
    46  
    47  func TestRemoveAllOneFilesystem(t *testing.T) {
    48  	tests := []struct {
    49  		name string
    50  		// Items of the test directory. Directories end with "/".
    51  		// Directories starting with "mount" are considered to be mount points.
    52  		// Directories starting with "err" will cause an error in
    53  		// IsLikelyNotMountPoint.
    54  		items       []string
    55  		expectError bool
    56  	}{
    57  		{
    58  			"empty dir",
    59  			[]string{},
    60  			false,
    61  		},
    62  		{
    63  			"non-mount",
    64  			[]string{
    65  				"dir/",
    66  				"dir/file",
    67  				"dir2/",
    68  				"file2",
    69  			},
    70  			false,
    71  		},
    72  		{
    73  			"mount",
    74  			[]string{
    75  				"dir/",
    76  				"dir/file",
    77  				"dir2/",
    78  				"file2",
    79  				"mount/",
    80  				"mount/file3",
    81  			},
    82  			true,
    83  		},
    84  		{
    85  			"innermount",
    86  			[]string{
    87  				"dir/",
    88  				"dir/file",
    89  				"dir/dir2/",
    90  				"dir/dir2/file2",
    91  				"dir/dir2/mount/",
    92  				"dir/dir2/mount/file3",
    93  			},
    94  			true,
    95  		},
    96  		{
    97  			"error",
    98  			[]string{
    99  				"dir/",
   100  				"dir/file",
   101  				"dir2/",
   102  				"file2",
   103  				"err/",
   104  				"err/file3",
   105  			},
   106  			true,
   107  		},
   108  	}
   109  
   110  	for _, test := range tests {
   111  		tmpDir, err := utiltesting.MkTmpdir("removeall-" + test.name + "-")
   112  		if err != nil {
   113  			t.Fatalf("Can't make a tmp dir: %v", err)
   114  		}
   115  		defer os.RemoveAll(tmpDir)
   116  		// Create the directory structure
   117  		for _, item := range test.items {
   118  			if strings.HasSuffix(item, "/") {
   119  				item = strings.TrimRight(item, "/")
   120  				if err = os.Mkdir(path.Join(tmpDir, item), 0777); err != nil {
   121  					t.Fatalf("error creating %s: %v", item, err)
   122  				}
   123  			} else {
   124  				f, err := os.Create(path.Join(tmpDir, item))
   125  				if err != nil {
   126  					t.Fatalf("error creating %s: %v", item, err)
   127  				}
   128  				f.Close()
   129  			}
   130  		}
   131  
   132  		mounter := &fakeMounter{}
   133  		err = RemoveAllOneFilesystem(mounter, tmpDir)
   134  		if err == nil && test.expectError {
   135  			t.Errorf("test %q failed: expected error and got none", test.name)
   136  		}
   137  		if err != nil && !test.expectError {
   138  			t.Errorf("test %q failed: unexpected error: %v", test.name, err)
   139  		}
   140  	}
   141  }
   142  
   143  func TestRemoveDirsOneFilesystem(t *testing.T) {
   144  	tests := []struct {
   145  		name string
   146  		// Items of the test directory. Directories end with "/".
   147  		// Directories starting with "mount" are considered to be mount points.
   148  		// Directories starting with "err" will cause an error in
   149  		// IsLikelyNotMountPoint.
   150  		items       []string
   151  		expectError bool
   152  	}{
   153  		{
   154  			"empty dir",
   155  			[]string{},
   156  			false,
   157  		},
   158  		{
   159  			"non-mount-no-files",
   160  			[]string{
   161  				"dir/",
   162  				"dir/subdir1/",
   163  				"dir2/",
   164  				"dir2/subdir2/",
   165  				"dir2/subdir2/subdir3/",
   166  				"dir3/",
   167  			},
   168  			false,
   169  		},
   170  		{
   171  			"non-mount-with-files",
   172  			[]string{
   173  				"dir/",
   174  				"dir/file",
   175  				"dir2/",
   176  				"file2",
   177  			},
   178  			true,
   179  		},
   180  		{
   181  			"mount-no-files",
   182  			[]string{
   183  				"dir/",
   184  				"dir/subdir1/",
   185  				"dir2/",
   186  				"dir2/subdir2/",
   187  				"dir2/subdir2/subdir3/",
   188  				"mount/",
   189  				"mount/dir3/",
   190  			},
   191  			true,
   192  		},
   193  		{
   194  			"mount-with-files",
   195  			[]string{
   196  				"dir/",
   197  				"dir/file",
   198  				"dir2/",
   199  				"file2",
   200  				"mount/",
   201  				"mount/file3",
   202  			},
   203  			true,
   204  		},
   205  		{
   206  			"innermount",
   207  			[]string{
   208  				"dir/",
   209  				"dir/subdir1/",
   210  				"dir/dir2/",
   211  				"dir/dir2/subdir2/",
   212  				"dir/dir2/mount/",
   213  				"dir/dir2/mount/subdir3/",
   214  			},
   215  			true,
   216  		},
   217  		{
   218  			"error",
   219  			[]string{
   220  				"dir/",
   221  				"dir/subdir1/",
   222  				"dir2/",
   223  				"err/",
   224  				"err/subdir3/",
   225  			},
   226  			true,
   227  		},
   228  	}
   229  
   230  	for _, test := range tests {
   231  		tmpDir, err := utiltesting.MkTmpdir("removeall-" + test.name + "-")
   232  		if err != nil {
   233  			t.Fatalf("Can't make a tmp dir: %v", err)
   234  		}
   235  		defer os.RemoveAll(tmpDir)
   236  		// Create the directory structure
   237  		for _, item := range test.items {
   238  			if strings.HasSuffix(item, "/") {
   239  				item = strings.TrimRight(item, "/")
   240  				if err = os.Mkdir(path.Join(tmpDir, item), 0777); err != nil {
   241  					t.Fatalf("error creating %s: %v", item, err)
   242  				}
   243  			} else {
   244  				f, err := os.Create(path.Join(tmpDir, item))
   245  				if err != nil {
   246  					t.Fatalf("error creating %s: %v", item, err)
   247  				}
   248  				f.Close()
   249  			}
   250  		}
   251  
   252  		mounter := &fakeMounter{}
   253  		err = RemoveDirsOneFilesystem(mounter, tmpDir)
   254  		if err == nil && test.expectError {
   255  			t.Errorf("test %q failed: expected error and got none", test.name)
   256  		}
   257  		if err != nil && !test.expectError {
   258  			t.Errorf("test %q failed: unexpected error: %v", test.name, err)
   259  		}
   260  	}
   261  }