pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/pid/pid_test.go (about)

     1  package pid
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"testing"
    15  
    16  	. "pkg.re/essentialkaos/check.v1"
    17  
    18  	"pkg.re/essentialkaos/ek.v12/fsutil"
    19  )
    20  
    21  // ////////////////////////////////////////////////////////////////////////////////// //
    22  
    23  type PidSuite struct {
    24  	Dir string
    25  }
    26  
    27  // ////////////////////////////////////////////////////////////////////////////////// //
    28  
    29  func Test(t *testing.T) { TestingT(t) }
    30  
    31  // ////////////////////////////////////////////////////////////////////////////////// //
    32  
    33  var _ = Suite(&PidSuite{})
    34  
    35  // ////////////////////////////////////////////////////////////////////////////////// //
    36  
    37  func (s *PidSuite) SetUpSuite(c *C) {
    38  	s.Dir = c.MkDir()
    39  }
    40  
    41  func (s *PidSuite) TestErrors(c *C) {
    42  	Dir = "/_NOT_EXIST"
    43  
    44  	err := Create("test")
    45  
    46  	c.Assert(err, NotNil)
    47  	c.Assert(err.Error(), Equals, "Directory /_NOT_EXIST does not exist")
    48  
    49  	// //////////////////////////////////////////////////////////////////////////////// //
    50  
    51  	Dir = os.Args[0]
    52  
    53  	err = Create("test")
    54  
    55  	c.Assert(err, NotNil)
    56  	c.Assert(err.Error(), Equals, fmt.Sprintf("%s is not directory", os.Args[0]))
    57  
    58  	// //////////////////////////////////////////////////////////////////////////////// //
    59  
    60  	Dir = "/"
    61  
    62  	err = Create("test")
    63  
    64  	c.Assert(err, NotNil)
    65  	c.Assert(err.Error(), Equals, "Directory / is not writable")
    66  
    67  	// //////////////////////////////////////////////////////////////////////////////// //
    68  
    69  	err = Remove("test")
    70  
    71  	c.Assert(err, NotNil)
    72  	c.Assert(err.Error(), Equals, "Directory / is not writable")
    73  
    74  	// //////////////////////////////////////////////////////////////////////////////// //
    75  
    76  	pidNum := Get("test")
    77  
    78  	c.Assert(pidNum, Equals, -1)
    79  
    80  	// //////////////////////////////////////////////////////////////////////////////// //
    81  
    82  	Dir = s.Dir
    83  
    84  	err = Create("")
    85  
    86  	c.Assert(err, NotNil)
    87  	c.Assert(err.Error(), Equals, "PID file name can't be blank")
    88  
    89  	// //////////////////////////////////////////////////////////////////////////////// //
    90  
    91  	pidNum = Get("_test_")
    92  
    93  	c.Assert(pidNum, Equals, -1)
    94  
    95  	// //////////////////////////////////////////////////////////////////////////////// //
    96  
    97  	ioutil.WriteFile(s.Dir+"/bad.pid", []byte("ABCDE\n"), 0644)
    98  
    99  	pidNum = Get("bad.pid")
   100  
   101  	c.Assert(pidNum, Equals, -1)
   102  
   103  	// //////////////////////////////////////////////////////////////////////////////// //
   104  
   105  	nonReadableDir := s.Dir + "/non-readable"
   106  
   107  	os.Mkdir(nonReadableDir, 0200)
   108  
   109  	Dir = nonReadableDir
   110  
   111  	err = Create("test.pid")
   112  
   113  	c.Assert(err, NotNil)
   114  	c.Assert(err.Error(), Equals, fmt.Sprintf("Directory %s is not readable", nonReadableDir))
   115  	c.Assert(Get("test.pid"), Equals, -1)
   116  }
   117  
   118  func (s *PidSuite) TestPidFuncs(c *C) {
   119  	Dir = s.Dir
   120  
   121  	err := Create("test.pid")
   122  
   123  	c.Assert(err, IsNil)
   124  
   125  	c.Assert(fsutil.IsExist(Dir+"/test.pid"), Equals, true)
   126  	c.Assert(fsutil.IsReadable(Dir+"/test.pid"), Equals, true)
   127  	c.Assert(fsutil.IsReadable(Dir+"/test.pid"), Equals, true)
   128  	c.Assert(fsutil.IsNonEmpty(Dir+"/test.pid"), Equals, true)
   129  
   130  	err = Create("test")
   131  
   132  	c.Assert(err, IsNil)
   133  
   134  	pid := Get("test")
   135  
   136  	c.Assert(pid, Not(Equals), -1)
   137  	c.Assert(os.Getpid(), Equals, pid)
   138  
   139  	Remove("test")
   140  
   141  	c.Assert(fsutil.IsExist(Dir+"/test.pid"), Equals, false)
   142  	c.Assert(IsWorks("test"), Equals, false)
   143  }
   144  
   145  // ////////////////////////////////////////////////////////////////////////////////// //