github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/agent/mongo/mongo_test.go (about)

     1  package mongo
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	jc "launchpad.net/juju-core/testing/checkers"
    13  	"launchpad.net/juju-core/testing/testbase"
    14  	"launchpad.net/juju-core/upstart"
    15  )
    16  
    17  func Test(t *testing.T) { gc.TestingT(t) }
    18  
    19  type MongoSuite struct {
    20  	testbase.LoggingSuite
    21  }
    22  
    23  var _ = gc.Suite(&MongoSuite{})
    24  
    25  func (s *MongoSuite) SetUpSuite(c *gc.C) {
    26  	testpath := c.MkDir()
    27  	s.PatchEnvPathPrepend(testpath)
    28  	// mock out the start method so we can fake install services without sudo
    29  	start := filepath.Join(testpath, "start")
    30  	err := ioutil.WriteFile(start, []byte("#!/bin/bash --norc\nexit 0"), 0755)
    31  	c.Assert(err, gc.IsNil)
    32  
    33  	s.PatchValue(&upstart.InitDir, c.MkDir())
    34  }
    35  
    36  func (s *MongoSuite) TestJujuMongodPath(c *gc.C) {
    37  	d := c.MkDir()
    38  	defer os.RemoveAll(d)
    39  	mongoPath := filepath.Join(d, "mongod")
    40  	s.PatchValue(&JujuMongodPath, mongoPath)
    41  
    42  	err := ioutil.WriteFile(mongoPath, nil, 0777)
    43  	c.Assert(err, gc.IsNil)
    44  
    45  	obtained, err := MongodPath()
    46  	c.Check(err, gc.IsNil)
    47  	c.Check(obtained, gc.Equals, mongoPath)
    48  }
    49  
    50  func (s *MongoSuite) TestDefaultMongodPath(c *gc.C) {
    51  	s.PatchValue(&JujuMongodPath, "/not/going/to/exist/mongod")
    52  
    53  	dir := c.MkDir()
    54  	s.PatchEnvPathPrepend(dir)
    55  	filename := filepath.Join(dir, "mongod")
    56  	err := ioutil.WriteFile(filename, nil, 0777)
    57  	c.Assert(err, gc.IsNil)
    58  
    59  	obtained, err := MongodPath()
    60  	c.Check(err, gc.IsNil)
    61  	c.Check(obtained, gc.Equals, filename)
    62  }
    63  
    64  func (s *MongoSuite) TestRemoveOldMongoServices(c *gc.C) {
    65  	s.PatchValue(&oldMongoServiceName, "someNameThatShouldntExist")
    66  
    67  	// Make fake old services.
    68  	// We defer the removes manually just in case the test fails, we don't leave
    69  	// junk behind.
    70  	conf := makeService(oldMongoServiceName, c)
    71  	defer conf.Remove()
    72  	conf2 := makeService(makeServiceName(2), c)
    73  	defer conf2.Remove()
    74  	conf3 := makeService(makeServiceName(3), c)
    75  	defer conf3.Remove()
    76  
    77  	// Remove with current version = 4, which should remove all previous
    78  	// versions plus the old service name.
    79  	err := removeOldMongoServices(4)
    80  	c.Assert(err, gc.IsNil)
    81  
    82  	c.Assert(conf.Installed(), jc.IsFalse)
    83  	c.Assert(conf2.Installed(), jc.IsFalse)
    84  	c.Assert(conf3.Installed(), jc.IsFalse)
    85  }
    86  
    87  func (s *MongoSuite) TestMakeJournalDirs(c *gc.C) {
    88  	dir := c.MkDir()
    89  	err := makeJournalDirs(dir)
    90  	c.Assert(err, gc.IsNil)
    91  
    92  	testJournalDirs(dir, c)
    93  }
    94  
    95  func testJournalDirs(dir string, c *gc.C) {
    96  	journalDir := path.Join(dir, "journal")
    97  
    98  	c.Check(journalDir, jc.IsDirectory)
    99  	info, err := os.Stat(filepath.Join(journalDir, "prealloc.0"))
   100  	c.Check(err, gc.IsNil)
   101  
   102  	size := int64(1024 * 1024)
   103  
   104  	c.Check(info.Size(), gc.Equals, size)
   105  	info, err = os.Stat(filepath.Join(journalDir, "prealloc.1"))
   106  	c.Check(err, gc.IsNil)
   107  	c.Check(info.Size(), gc.Equals, size)
   108  	info, err = os.Stat(filepath.Join(journalDir, "prealloc.2"))
   109  	c.Check(err, gc.IsNil)
   110  	c.Check(info.Size(), gc.Equals, size)
   111  
   112  }
   113  
   114  func (s *MongoSuite) TestEnsureMongoServer(c *gc.C) {
   115  	dir := c.MkDir()
   116  	port := 25252
   117  
   118  	oldsvc := makeService(oldMongoServiceName, c)
   119  	defer oldsvc.Remove()
   120  
   121  	err := EnsureMongoServer(dir, port)
   122  	c.Assert(err, gc.IsNil)
   123  	svc, err := MongoUpstartService(makeServiceName(mongoScriptVersion), dir, port)
   124  	c.Assert(err, gc.IsNil)
   125  	defer svc.Remove()
   126  
   127  	testJournalDirs(dir, c)
   128  	c.Check(oldsvc.Installed(), jc.IsFalse)
   129  	c.Check(svc.Installed(), jc.IsTrue)
   130  
   131  	// now check we can call it multiple times without error
   132  	err = EnsureMongoServer(dir, port)
   133  	c.Assert(err, gc.IsNil)
   134  
   135  }
   136  
   137  func makeService(name string, c *gc.C) *upstart.Conf {
   138  	conf := &upstart.Conf{
   139  		Desc:    "foo",
   140  		Service: *upstart.NewService(name),
   141  		Cmd:     "echo hi",
   142  	}
   143  	err := conf.Install()
   144  	c.Assert(err, gc.IsNil)
   145  	return conf
   146  }