github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/backups/internal_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 //go:build !windows 5 6 package backups 7 8 import ( 9 "fmt" 10 "os" 11 "syscall" 12 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/testing" 17 ) 18 19 var _ = gc.Suite(&pathsSuite{}) 20 21 type pathsSuite struct { 22 testing.BaseSuite 23 } 24 25 func (s *pathsSuite) SetUpTest(c *gc.C) { 26 s.PatchValue(&getMongodPath, func() (string, error) { 27 return "path/to/mongod", nil 28 }) 29 } 30 31 func (s *pathsSuite) TestPathDefaultMongoExists(c *gc.C) { 32 calledWithPaths := []string{} 33 osStat := func(aPath string) (os.FileInfo, error) { 34 calledWithPaths = append(calledWithPaths, aPath) 35 return nil, nil 36 } 37 mongoPath, err := getMongoToolPath("tool", osStat, nil) 38 c.Assert(err, jc.ErrorIsNil) 39 c.Assert(mongoPath, gc.Equals, "path/to/juju-db.tool") 40 c.Assert(calledWithPaths, gc.DeepEquals, []string{"path/to/juju-db.tool"}) 41 } 42 43 func (s *pathsSuite) TestPathNoDefaultMongo(c *gc.C) { 44 calledWithPaths := []string{} 45 osStat := func(aPath string) (os.FileInfo, error) { 46 calledWithPaths = append(calledWithPaths, aPath) 47 return nil, fmt.Errorf("sorry no mongo") 48 } 49 50 calledWithLookup := []string{} 51 execLookPath := func(aLookup string) (string, error) { 52 calledWithLookup = append(calledWithLookup, aLookup) 53 return "/a/fake/mongo/path", nil 54 } 55 56 mongoPath, err := getMongoToolPath("tool", osStat, execLookPath) 57 c.Assert(err, jc.ErrorIsNil) 58 c.Assert(mongoPath, gc.Equals, "/a/fake/mongo/path") 59 c.Assert(calledWithPaths, gc.DeepEquals, []string{ 60 "path/to/juju-db.tool", 61 }) 62 c.Assert(calledWithLookup, gc.DeepEquals, []string{"tool"}) 63 } 64 65 func (s *pathsSuite) TestPathSnapMongo(c *gc.C) { 66 statPaths := []string{} 67 mockStat := func(path string) (os.FileInfo, error) { 68 statPaths = append(statPaths, path) 69 switch path { 70 case "path/to/juju-db.mongodump": 71 return nil, nil // nil FileInfo is okay; value isn't used 72 default: 73 return nil, &os.PathError{Op: "mockStat", Path: path, Err: syscall.ENOENT} 74 } 75 } 76 77 path, err := getMongoToolPath("mongodump", mockStat, nil) 78 c.Assert(err, jc.ErrorIsNil) 79 c.Assert(path, gc.Equals, "path/to/juju-db.mongodump") 80 c.Assert(statPaths, gc.DeepEquals, []string{ 81 "path/to/juju-db.mongodump", 82 }) 83 }