github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/backups/db_dump_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package backups_test 5 6 import ( 7 "os" 8 "path/filepath" 9 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/utils/set" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/state/backups" 15 "github.com/juju/juju/testing" 16 ) 17 18 type dumpSuite struct { 19 testing.BaseSuite 20 21 targets set.Strings 22 dbInfo *backups.DBInfo 23 dumpDir string 24 ranCommand bool 25 } 26 27 var _ = gc.Suite(&dumpSuite{}) // Register the suite. 28 29 func (s *dumpSuite) SetUpTest(c *gc.C) { 30 s.BaseSuite.SetUpTest(c) 31 32 targets := set.NewStrings("juju", "admin") 33 s.dbInfo = &backups.DBInfo{"a", "b", "c", targets} 34 s.targets = targets 35 s.dumpDir = c.MkDir() 36 } 37 38 func (s *dumpSuite) patch(c *gc.C) { 39 s.PatchValue(backups.GetMongodumpPath, func() (string, error) { 40 return "bogusmongodump", nil 41 }) 42 43 s.PatchValue(backups.RunCommand, func(cmd string, args ...string) error { 44 s.ranCommand = true 45 return nil 46 }) 47 } 48 49 func (s *dumpSuite) prepDB(c *gc.C, name string) string { 50 dirName := filepath.Join(s.dumpDir, name) 51 err := os.Mkdir(dirName, 0777) 52 c.Assert(err, jc.ErrorIsNil) 53 return dirName 54 } 55 56 func (s *dumpSuite) prep(c *gc.C, targetDBs ...string) backups.DBDumper { 57 dumper, err := backups.NewDBDumper(s.dbInfo) 58 c.Assert(err, jc.ErrorIsNil) 59 60 // Prep each of the target databases. 61 for _, dbName := range targetDBs { 62 s.prepDB(c, dbName) 63 } 64 65 return dumper 66 } 67 68 func (s *dumpSuite) checkDBs(c *gc.C, dbNames ...string) { 69 for _, dbName := range dbNames { 70 _, err := os.Stat(filepath.Join(s.dumpDir, dbName)) 71 c.Check(err, jc.ErrorIsNil) 72 } 73 } 74 75 func (s *dumpSuite) checkStripped(c *gc.C, dbName string) { 76 dirName := filepath.Join(s.dumpDir, dbName) 77 _, err := os.Stat(dirName) 78 c.Check(err, jc.Satisfies, os.IsNotExist) 79 } 80 81 func (s *dumpSuite) TestDumpRanCommand(c *gc.C) { 82 s.patch(c) 83 dumper := s.prep(c, "juju", "admin") 84 85 err := dumper.Dump(s.dumpDir) 86 c.Assert(err, jc.ErrorIsNil) 87 88 c.Check(s.ranCommand, jc.IsTrue) 89 } 90 91 func (s *dumpSuite) TestDumpStripped(c *gc.C) { 92 s.patch(c) 93 dumper := s.prep(c, "juju", "admin") 94 s.prepDB(c, "backups") // ignored 95 96 err := dumper.Dump(s.dumpDir) 97 c.Assert(err, jc.ErrorIsNil) 98 99 s.checkDBs(c, "juju", "admin") 100 s.checkStripped(c, "backups") 101 } 102 103 func (s *dumpSuite) TestDumpStrippedMultiple(c *gc.C) { 104 s.patch(c) 105 dumper := s.prep(c, "juju", "admin") 106 s.prepDB(c, "backups") // ignored 107 s.prepDB(c, "presence") // ignored 108 109 err := dumper.Dump(s.dumpDir) 110 c.Assert(err, jc.ErrorIsNil) 111 112 s.checkDBs(c, "juju", "admin") 113 // Only "backups" is actually ignored when dumping. Restore takes 114 // care of removing the other ignored databases (like presence). 115 s.checkDBs(c, "presence") 116 s.checkStripped(c, "backups") 117 } 118 119 func (s *dumpSuite) TestDumpNothingIgnored(c *gc.C) { 120 s.patch(c) 121 dumper := s.prep(c, "juju", "admin") 122 123 err := dumper.Dump(s.dumpDir) 124 c.Assert(err, jc.ErrorIsNil) 125 126 s.checkDBs(c, "juju", "admin") 127 }