github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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  	"github.com/juju/collections/set"
    11  	jc "github.com/juju/testing/checkers"
    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{
    34  		Address: "a", Username: "b", Password: "c",
    35  		Targets:      targets,
    36  		ApproxSizeMB: 100}
    37  	s.targets = targets
    38  	s.dumpDir = c.MkDir()
    39  }
    40  
    41  func (s *dumpSuite) patch(c *gc.C) {
    42  	s.PatchValue(backups.GetMongodumpPath, func() (string, error) {
    43  		return "bogusmongodump", nil
    44  	})
    45  
    46  	s.PatchValue(backups.RunCommand, func(cmd string, args ...string) error {
    47  		s.ranCommand = true
    48  		return nil
    49  	})
    50  }
    51  
    52  func (s *dumpSuite) prepDB(c *gc.C, name string) string {
    53  	dirName := filepath.Join(s.dumpDir, name)
    54  	err := os.Mkdir(dirName, 0777)
    55  	c.Assert(err, jc.ErrorIsNil)
    56  	return dirName
    57  }
    58  
    59  func (s *dumpSuite) prep(c *gc.C, targetDBs ...string) backups.DBDumper {
    60  	dumper, err := backups.NewDBDumper(s.dbInfo)
    61  	c.Assert(err, jc.ErrorIsNil)
    62  
    63  	// Prep each of the target databases.
    64  	for _, dbName := range targetDBs {
    65  		s.prepDB(c, dbName)
    66  	}
    67  
    68  	return dumper
    69  }
    70  
    71  func (s *dumpSuite) checkDBs(c *gc.C, dbNames ...string) {
    72  	for _, dbName := range dbNames {
    73  		_, err := os.Stat(filepath.Join(s.dumpDir, dbName))
    74  		c.Check(err, jc.ErrorIsNil)
    75  	}
    76  }
    77  
    78  func (s *dumpSuite) checkStripped(c *gc.C, dbName string) {
    79  	dirName := filepath.Join(s.dumpDir, dbName)
    80  	_, err := os.Stat(dirName)
    81  	c.Check(err, jc.Satisfies, os.IsNotExist)
    82  }
    83  
    84  func (s *dumpSuite) TestDumpRanCommand(c *gc.C) {
    85  	s.patch(c)
    86  	dumper := s.prep(c, "juju", "admin")
    87  
    88  	err := dumper.Dump(s.dumpDir)
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	c.Check(s.ranCommand, jc.IsTrue)
    92  }
    93  
    94  func (s *dumpSuite) TestDumpStripped(c *gc.C) {
    95  	s.patch(c)
    96  	dumper := s.prep(c, "juju", "admin")
    97  	s.prepDB(c, "backups") // ignored
    98  
    99  	err := dumper.Dump(s.dumpDir)
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	s.checkDBs(c, "juju", "admin")
   103  	s.checkStripped(c, "backups")
   104  }
   105  
   106  func (s *dumpSuite) TestDumpStrippedAdmin(c *gc.C) {
   107  	s.dbInfo.Targets = set.NewStrings("juju")
   108  	s.patch(c)
   109  	dumper := s.prep(c, "juju")
   110  	s.prepDB(c, "backups") // ignored
   111  	s.prepDB(c, "admin")   // ignored
   112  
   113  	err := dumper.Dump(s.dumpDir)
   114  	c.Assert(err, jc.ErrorIsNil)
   115  
   116  	s.checkDBs(c, "juju")
   117  	s.checkStripped(c, "backups")
   118  	s.checkStripped(c, "admin")
   119  }
   120  
   121  func (s *dumpSuite) TestDumpStrippedMultiple(c *gc.C) {
   122  	s.patch(c)
   123  	dumper := s.prep(c, "juju", "admin")
   124  	s.prepDB(c, "backups")  // ignored
   125  	s.prepDB(c, "presence") // ignored
   126  
   127  	err := dumper.Dump(s.dumpDir)
   128  	c.Assert(err, jc.ErrorIsNil)
   129  
   130  	s.checkDBs(c, "juju", "admin")
   131  	// Only "backups" is actually ignored when dumping.  Restore takes
   132  	// care of removing the other ignored databases (like presence).
   133  	s.checkDBs(c, "presence")
   134  	s.checkStripped(c, "backups")
   135  }
   136  
   137  func (s *dumpSuite) TestDumpNothingIgnored(c *gc.C) {
   138  	s.patch(c)
   139  	dumper := s.prep(c, "juju", "admin")
   140  
   141  	err := dumper.Dump(s.dumpDir)
   142  	c.Assert(err, jc.ErrorIsNil)
   143  
   144  	s.checkDBs(c, "juju", "admin")
   145  }