github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/mongo/mongodfinder_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package mongo_test 5 6 import ( 7 "os" 8 "path/filepath" 9 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 "go.uber.org/mock/gomock" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/mongo" 16 ) 17 18 type MongodFinderSuite struct { 19 testing.IsolationSuite 20 21 ctrl *gomock.Controller 22 finder *mongo.MongodFinder 23 search *mongo.MockSearchTools 24 } 25 26 var _ = gc.Suite(&MongodFinderSuite{}) 27 28 // setUpMock must be called at the start of each test, and then s.ctrl.Finish() called (you can use defer()). 29 // this cannot be done in SetUpTest() and TearDownTest() because gomock.NewController assumes the TestReporter is valid 30 // for the entire lifetime of the Controller, and gocheck passes a different C object to SetUpTest vs the Test itself 31 // vs TearDownTest. And calling c.Fatalf() on the original C object doesn't actually fail the test suite in TearDown. 32 func (s *MongodFinderSuite) setUpMock(c *gc.C) { 33 s.ctrl = gomock.NewController(c) 34 s.finder, s.search = mongo.NewMongodFinderWithMockSearch(s.ctrl) 35 } 36 37 func (s *MongodFinderSuite) TestFindJujuMongodb(c *gc.C) { 38 s.setUpMock(c) 39 defer s.ctrl.Finish() 40 exp := s.search.EXPECT() 41 gomock.InOrder( 42 exp.Exists("/snap/bin/juju-db.mongod").Return(true), 43 ) 44 path, err := s.finder.InstalledAt() 45 c.Assert(err, jc.ErrorIsNil) 46 c.Check(path, gc.Equals, "/snap/bin/juju-db.mongod") 47 } 48 49 func (s *MongodFinderSuite) TestFindJujuMongodbNone(c *gc.C) { 50 s.setUpMock(c) 51 defer s.ctrl.Finish() 52 exp := s.search.EXPECT() 53 gomock.InOrder( 54 exp.Exists("/snap/bin/juju-db.mongod").Return(false), 55 ) 56 _, err := s.finder.InstalledAt() 57 c.Assert(err, gc.ErrorMatches, "juju-db snap not installed, no mongo at /snap/bin/juju-db.mongod") 58 } 59 60 type OSSearchToolsSuite struct { 61 testing.IsolationSuite 62 } 63 64 var _ = gc.Suite(&OSSearchToolsSuite{}) 65 66 func (s *OSSearchToolsSuite) TestExists(c *gc.C) { 67 dir := c.MkDir() 68 path := filepath.Join(dir, "filename") 69 f, err := os.Create(path) 70 c.Assert(err, jc.ErrorIsNil) 71 f.Close() 72 tools := mongo.OSSearchTools{} 73 c.Check(tools.Exists(path), jc.IsTrue) 74 c.Check(tools.Exists(path+"-not-there"), jc.IsFalse) 75 } 76 77 func (s *OSSearchToolsSuite) TestGetCommandOutputValid(c *gc.C) { 78 tools := mongo.OSSearchTools{} 79 out, err := tools.GetCommandOutput("/bin/echo", "argument") 80 c.Assert(err, jc.ErrorIsNil) 81 c.Check(out, gc.Equals, "argument\n") 82 } 83 84 func (s *OSSearchToolsSuite) TestGetCommandOutputExitNonzero(c *gc.C) { 85 dir := c.MkDir() 86 path := filepath.Join(dir, "failing") 87 err := os.WriteFile(path, []byte(`#!/bin/bash --norc 88 echo "hello $1" 89 exit 1 90 `), 0755) 91 c.Assert(err, jc.ErrorIsNil) 92 tools := mongo.OSSearchTools{} 93 out, err := tools.GetCommandOutput(path, "argument") 94 c.Assert(err, gc.NotNil) 95 c.Check(out, gc.Equals, "hello argument\n") 96 } 97 98 func (s *OSSearchToolsSuite) TestGetCommandOutputNonExecutable(c *gc.C) { 99 dir := c.MkDir() 100 path := filepath.Join(dir, "failing") 101 err := os.WriteFile(path, []byte(`#!/bin/bash --norc 102 echo "shouldn't happen $1" 103 `), 0644) 104 c.Assert(err, jc.ErrorIsNil) 105 tools := mongo.OSSearchTools{} 106 out, err := tools.GetCommandOutput(path, "argument") 107 c.Assert(err, gc.NotNil) 108 c.Check(out, gc.Equals, "") 109 }