github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/state/backups/db_restore_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 "path/filepath" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/agent" 13 "github.com/juju/juju/state/backups" 14 "github.com/juju/juju/testing" 15 "github.com/juju/juju/version" 16 ) 17 18 var _ = gc.Suite(&mongoRestoreSuite{}) 19 20 type mongoRestoreSuite struct { 21 testing.BaseSuite 22 } 23 24 func (s *mongoRestoreSuite) TestMongoRestoreArgsForVersion(c *gc.C) { 25 dir := filepath.Join(agent.DefaultPaths.DataDir, "db") 26 versionNumber := version.Number{} 27 versionNumber.Major = 1 28 versionNumber.Minor = 21 29 args, err := backups.MongoRestoreArgsForVersion(versionNumber, "/some/fake/path") 30 c.Assert(err, jc.ErrorIsNil) 31 c.Assert(args, gc.HasLen, 5) 32 c.Assert(args[0:5], jc.DeepEquals, []string{ 33 "--drop", 34 "--journal", 35 "--dbpath", 36 dir, 37 "/some/fake/path", 38 }) 39 40 versionNumber.Major = 1 41 versionNumber.Minor = 22 42 args, err = backups.MongoRestoreArgsForVersion(versionNumber, "/some/fake/path") 43 c.Assert(err, jc.ErrorIsNil) 44 c.Assert(args, gc.HasLen, 6) 45 c.Assert(args[0:6], jc.DeepEquals, []string{ 46 "--drop", 47 "--journal", 48 "--oplogReplay", 49 "--dbpath", 50 dir, 51 "/some/fake/path", 52 }) 53 54 versionNumber.Major = 0 55 versionNumber.Minor = 0 56 _, err = backups.MongoRestoreArgsForVersion(versionNumber, "/some/fake/path") 57 c.Assert(err, gc.ErrorMatches, "this backup file is incompatible with the current version of juju") 58 } 59 60 func (s *mongoRestoreSuite) TestPlaceNewMongo(c *gc.C) { 61 var argsVersion version.Number 62 var newMongoDumpPath string 63 ranArgs := make([][]string, 0, 3) 64 ranCommands := []string{} 65 66 restorePathCalled := false 67 68 runCommand := func(command string, mongoRestoreArgs ...string) error { 69 mgoArgs := make([]string, len(mongoRestoreArgs), len(mongoRestoreArgs)) 70 for i, v := range mongoRestoreArgs { 71 mgoArgs[i] = v 72 } 73 ranArgs = append(ranArgs, mgoArgs) 74 ranCommands = append(ranCommands, command) 75 return nil 76 } 77 s.PatchValue(backups.RunCommand, runCommand) 78 79 restorePath := func() (string, error) { 80 restorePathCalled = true 81 return "/fake/mongo/restore/path", nil 82 } 83 s.PatchValue(backups.RestorePath, restorePath) 84 85 ver := version.Number{Major: 1, Minor: 22} 86 args := []string{"a", "set", "of", "args"} 87 restoreArgsForVersion := func(versionNumber version.Number, mongoDumpPath string) ([]string, error) { 88 newMongoDumpPath = mongoDumpPath 89 argsVersion = versionNumber 90 return args, nil 91 } 92 s.PatchValue(backups.RestoreArgsForVersion, restoreArgsForVersion) 93 94 err := backups.PlaceNewMongo("fakemongopath", ver) 95 c.Assert(restorePathCalled, jc.IsTrue) 96 c.Assert(err, jc.ErrorIsNil) 97 c.Assert(argsVersion, gc.DeepEquals, ver) 98 c.Assert(newMongoDumpPath, gc.Equals, "fakemongopath") 99 expectedCommands := []string{"initctl", "/fake/mongo/restore/path", "initctl"} 100 c.Assert(ranCommands, gc.DeepEquals, expectedCommands) 101 c.Assert(len(ranArgs), gc.Equals, 3) 102 expectedArgs := [][]string{{"stop", "juju-db"}, {"a", "set", "of", "args"}, {"start", "juju-db"}} 103 c.Assert(ranArgs, gc.DeepEquals, expectedArgs) 104 }