github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/util/fileutil_test.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package util 15 16 import ( 17 "io/ioutil" 18 "os" 19 "os/user" 20 "path/filepath" 21 "runtime" 22 23 "github.com/pingcap/check" 24 "github.com/pingcap/failpoint" 25 "github.com/pingcap/ticdc/pkg/config" 26 "github.com/pingcap/ticdc/pkg/util/testleak" 27 ) 28 29 type fileUtilSuite struct{} 30 31 var _ = check.Suite(&fileUtilSuite{}) 32 33 func (s *fileUtilSuite) TestIsDirWritable(c *check.C) { 34 defer testleak.AfterTest(c)() 35 dir := c.MkDir() 36 err := IsDirWritable(dir) 37 c.Assert(err, check.IsNil) 38 39 err = os.Chmod(dir, 0o400) 40 c.Assert(err, check.IsNil) 41 me, err := user.Current() 42 c.Assert(err, check.IsNil) 43 if me.Name == "root" || runtime.GOOS == "windows" { 44 // chmod is not supported under windows. 45 c.Skip("test case is running as a superuser or in windows") 46 } 47 err = IsDirWritable(dir) 48 c.Assert(err, check.ErrorMatches, ".*permission denied") 49 } 50 51 func (s *fileUtilSuite) TestIsDirAndWritable(c *check.C) { 52 defer testleak.AfterTest(c)() 53 dir := c.MkDir() 54 path := filepath.Join(dir, "file.test") 55 56 err := IsDirAndWritable(path) 57 c.Assert(err, check.ErrorMatches, ".*no such file or directory") 58 59 err = ioutil.WriteFile(path, nil, 0o600) 60 c.Assert(err, check.IsNil) 61 err = IsDirAndWritable(path) 62 c.Assert(err, check.ErrorMatches, ".*is not a directory") 63 64 err = IsDirAndWritable(dir) 65 c.Assert(err, check.IsNil) 66 } 67 68 func (s *fileUtilSuite) TestIsDirReadWritable(c *check.C) { 69 defer testleak.AfterTest(c)() 70 71 dir := c.MkDir() 72 err := IsDirReadWritable(dir) 73 c.Assert(err, check.IsNil) 74 75 path := filepath.Join(dir, "/foo") 76 err = IsDirReadWritable(path) 77 c.Assert(err, check.ErrorMatches, ".*no such file or directory") 78 } 79 80 func (s *fileUtilSuite) TestGetDiskInfo(c *check.C) { 81 defer testleak.AfterTest(c)() 82 83 dir := c.MkDir() 84 info, err := GetDiskInfo(dir) 85 c.Assert(err, check.IsNil) 86 c.Assert(info, check.NotNil) 87 88 dir = filepath.Join(dir, "/tmp/sorter") 89 info, err = GetDiskInfo(dir) 90 c.Assert(info, check.IsNil) 91 c.Assert(err, check.ErrorMatches, ".*no such file or directory") 92 } 93 94 func (s *fileUtilSuite) TestCheckDataDirSatisfied(c *check.C) { 95 defer testleak.AfterTest(c)() 96 dir := c.MkDir() 97 conf := config.GetGlobalServerConfig() 98 conf.DataDir = dir 99 config.StoreGlobalServerConfig(conf) 100 101 c.Assert(failpoint.Enable("github.com/pingcap/ticdc/pkg/util/InjectCheckDataDirSatisfied", ""), check.IsNil) 102 err := CheckDataDirSatisfied() 103 c.Assert(err, check.IsNil) 104 c.Assert(failpoint.Disable("github.com/pingcap/ticdc/pkg/util/InjectCheckDataDirSatisfied"), check.IsNil) 105 }