github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/mongo" 15 "github.com/juju/juju/state/backups" 16 "github.com/juju/juju/testing" 17 ) 18 19 type dumpSuite struct { 20 testing.BaseSuite 21 22 targets set.Strings 23 dbInfo *backups.DBInfo 24 dumpDir string 25 ranCommand bool 26 } 27 28 var _ = gc.Suite(&dumpSuite{}) // Register the suite. 29 30 func (s *dumpSuite) SetUpTest(c *gc.C) { 31 s.BaseSuite.SetUpTest(c) 32 33 targets := set.NewStrings("juju", "admin") 34 s.dbInfo = &backups.DBInfo{"a", "b", "c", targets, mongo.Mongo24} 35 s.targets = targets 36 s.dumpDir = c.MkDir() 37 } 38 39 func (s *dumpSuite) patch(c *gc.C) { 40 s.PatchValue(backups.GetMongodumpPath, func() (string, error) { 41 return "bogusmongodump", nil 42 }) 43 44 s.PatchValue(backups.RunCommand, func(cmd string, args ...string) error { 45 s.ranCommand = true 46 return nil 47 }) 48 } 49 50 func (s *dumpSuite) prepDB(c *gc.C, name string) string { 51 dirName := filepath.Join(s.dumpDir, name) 52 err := os.Mkdir(dirName, 0777) 53 c.Assert(err, jc.ErrorIsNil) 54 return dirName 55 } 56 57 func (s *dumpSuite) prep(c *gc.C, targetDBs ...string) backups.DBDumper { 58 dumper, err := backups.NewDBDumper(s.dbInfo) 59 c.Assert(err, jc.ErrorIsNil) 60 61 // Prep each of the target databases. 62 for _, dbName := range targetDBs { 63 s.prepDB(c, dbName) 64 } 65 66 return dumper 67 } 68 69 func (s *dumpSuite) checkDBs(c *gc.C, dbNames ...string) { 70 for _, dbName := range dbNames { 71 _, err := os.Stat(filepath.Join(s.dumpDir, dbName)) 72 c.Check(err, jc.ErrorIsNil) 73 } 74 } 75 76 func (s *dumpSuite) checkStripped(c *gc.C, dbName string) { 77 dirName := filepath.Join(s.dumpDir, dbName) 78 _, err := os.Stat(dirName) 79 c.Check(err, jc.Satisfies, os.IsNotExist) 80 } 81 82 func (s *dumpSuite) TestDumpRanCommand(c *gc.C) { 83 s.patch(c) 84 dumper := s.prep(c, "juju", "admin") 85 86 err := dumper.Dump(s.dumpDir) 87 c.Assert(err, jc.ErrorIsNil) 88 89 c.Check(s.ranCommand, jc.IsTrue) 90 } 91 92 func (s *dumpSuite) TestDumpStripped(c *gc.C) { 93 s.patch(c) 94 dumper := s.prep(c, "juju", "admin") 95 s.prepDB(c, "backups") // ignored 96 97 err := dumper.Dump(s.dumpDir) 98 c.Assert(err, jc.ErrorIsNil) 99 100 s.checkDBs(c, "juju", "admin") 101 s.checkStripped(c, "backups") 102 } 103 104 func (s *dumpSuite) TestDumpStrippedAdmin(c *gc.C) { 105 s.dbInfo.MongoVersion = mongo.Mongo32wt 106 s.dbInfo.Targets = set.NewStrings("juju") 107 s.patch(c) 108 dumper := s.prep(c, "juju") 109 s.prepDB(c, "backups") // ignored 110 s.prepDB(c, "admin") // ignored 111 112 err := dumper.Dump(s.dumpDir) 113 c.Assert(err, jc.ErrorIsNil) 114 115 s.checkDBs(c, "juju") 116 s.checkStripped(c, "backups") 117 s.checkStripped(c, "admin") 118 } 119 120 func (s *dumpSuite) TestDumpNotStrippedAdmin(c *gc.C) { 121 s.patch(c) 122 dumper := s.prep(c, "juju") 123 s.prepDB(c, "backups") // ignored 124 s.prepDB(c, "admin") 125 126 err := dumper.Dump(s.dumpDir) 127 c.Assert(err, jc.ErrorIsNil) 128 129 s.checkDBs(c, "juju", "admin") 130 s.checkStripped(c, "backups") 131 } 132 133 func (s *dumpSuite) TestDumpStrippedMultiple(c *gc.C) { 134 s.patch(c) 135 dumper := s.prep(c, "juju", "admin") 136 s.prepDB(c, "backups") // ignored 137 s.prepDB(c, "presence") // ignored 138 139 err := dumper.Dump(s.dumpDir) 140 c.Assert(err, jc.ErrorIsNil) 141 142 s.checkDBs(c, "juju", "admin") 143 // Only "backups" is actually ignored when dumping. Restore takes 144 // care of removing the other ignored databases (like presence). 145 s.checkDBs(c, "presence") 146 s.checkStripped(c, "backups") 147 } 148 149 func (s *dumpSuite) TestDumpNothingIgnored(c *gc.C) { 150 s.patch(c) 151 dumper := s.prep(c, "juju", "admin") 152 153 err := dumper.Dump(s.dumpDir) 154 c.Assert(err, jc.ErrorIsNil) 155 156 s.checkDBs(c, "juju", "admin") 157 }