github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/backups/backups_test.go (about)

     1  // Copyright 2013,2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups_test
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"time"
    10  
    11  	"github.com/juju/errors"
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/utils/set"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/mongo"
    17  	"github.com/juju/juju/state/backups"
    18  	backupstesting "github.com/juju/juju/state/backups/testing"
    19  )
    20  
    21  type backupsSuite struct {
    22  	backupstesting.BaseSuite
    23  
    24  	api backups.Backups
    25  }
    26  
    27  var _ = gc.Suite(&backupsSuite{}) // Register the suite.
    28  
    29  func (s *backupsSuite) SetUpTest(c *gc.C) {
    30  	s.BaseSuite.SetUpTest(c)
    31  
    32  	s.api = backups.NewBackups(s.Storage)
    33  }
    34  
    35  func (s *backupsSuite) setStored(id string) *time.Time {
    36  	s.Storage.ID = id
    37  	s.Storage.Meta = backupstesting.NewMetadataStarted()
    38  	s.Storage.Meta.SetID(id)
    39  	stored := time.Now().UTC()
    40  	s.Storage.Meta.SetStored(&stored)
    41  	return &stored
    42  }
    43  
    44  type fakeDumper struct{}
    45  
    46  func (*fakeDumper) Dump(dumpDir string) error {
    47  	return nil
    48  }
    49  
    50  func (s *backupsSuite) checkFailure(c *gc.C, expected string) {
    51  	s.PatchValue(backups.GetDBDumper, func(*backups.DBInfo) (backups.DBDumper, error) {
    52  		return &fakeDumper{}, nil
    53  	})
    54  
    55  	paths := backups.Paths{DataDir: "/var/lib/juju"}
    56  	targets := set.NewStrings("juju", "admin")
    57  	dbInfo := backups.DBInfo{"a", "b", "c", targets, mongo.Mongo32wt}
    58  	meta := backupstesting.NewMetadataStarted()
    59  	meta.Notes = "some notes"
    60  	err := s.api.Create(meta, &paths, &dbInfo)
    61  
    62  	c.Check(err, gc.ErrorMatches, expected)
    63  }
    64  
    65  func (s *backupsSuite) TestNewBackups(c *gc.C) {
    66  	api := backups.NewBackups(s.Storage)
    67  
    68  	c.Check(api, gc.NotNil)
    69  }
    70  
    71  func (s *backupsSuite) TestCreateOkay(c *gc.C) {
    72  
    73  	// Patch the internals.
    74  	archiveFile := ioutil.NopCloser(bytes.NewBufferString("<compressed tarball>"))
    75  	result := backups.NewTestCreateResult(archiveFile, 10, "<checksum>")
    76  	received, testCreate := backups.NewTestCreate(result)
    77  	s.PatchValue(backups.RunCreate, testCreate)
    78  
    79  	rootDir := "<was never set>"
    80  	s.PatchValue(backups.TestGetFilesToBackUp, func(root string, paths *backups.Paths, oldmachine string) ([]string, error) {
    81  		rootDir = root
    82  		return []string{"<some file>"}, nil
    83  	})
    84  
    85  	var receivedDBInfo *backups.DBInfo
    86  	s.PatchValue(backups.GetDBDumper, func(info *backups.DBInfo) (backups.DBDumper, error) {
    87  		receivedDBInfo = info
    88  		return nil, nil
    89  	})
    90  
    91  	stored := s.setStored("spam")
    92  
    93  	// Run the backup.
    94  	paths := backups.Paths{DataDir: "/var/lib/juju"}
    95  	targets := set.NewStrings("juju", "admin")
    96  	dbInfo := backups.DBInfo{"a", "b", "c", targets, mongo.Mongo32wt}
    97  	meta := backupstesting.NewMetadataStarted()
    98  	backupstesting.SetOrigin(meta, "<model ID>", "<machine ID>", "<hostname>")
    99  	meta.Notes = "some notes"
   100  	err := s.api.Create(meta, &paths, &dbInfo)
   101  
   102  	// Test the call values.
   103  	s.Storage.CheckCalled(c, "spam", meta, archiveFile, "Add", "Metadata")
   104  	filesToBackUp, _ := backups.ExposeCreateArgs(received)
   105  	c.Check(filesToBackUp, jc.SameContents, []string{"<some file>"})
   106  
   107  	c.Check(receivedDBInfo.Address, gc.Equals, "a")
   108  	c.Check(receivedDBInfo.Username, gc.Equals, "b")
   109  	c.Check(receivedDBInfo.Password, gc.Equals, "c")
   110  	c.Check(receivedDBInfo.Targets, gc.DeepEquals, targets)
   111  
   112  	c.Check(rootDir, gc.Equals, "")
   113  
   114  	// Check the resulting metadata.
   115  	c.Check(meta, gc.Equals, s.Storage.MetaArg)
   116  	c.Check(meta.ID(), gc.Equals, "spam")
   117  	c.Check(meta.Size(), gc.Equals, int64(10))
   118  	c.Check(meta.Checksum(), gc.Equals, "<checksum>")
   119  	c.Check(meta.Stored().Unix(), gc.Equals, stored.Unix())
   120  	c.Check(meta.Origin.Model, gc.Equals, "<model ID>")
   121  	c.Check(meta.Origin.Machine, gc.Equals, "<machine ID>")
   122  	c.Check(meta.Origin.Hostname, gc.Equals, "<hostname>")
   123  	c.Check(meta.Notes, gc.Equals, "some notes")
   124  
   125  	// Check the file storage.
   126  	s.Storage.Meta = meta
   127  	s.Storage.File = archiveFile
   128  	storedMeta, storedFile, err := s.Storage.Get(meta.ID())
   129  	c.Check(err, jc.ErrorIsNil)
   130  	c.Check(storedMeta, gc.DeepEquals, meta)
   131  	data, err := ioutil.ReadAll(storedFile)
   132  	c.Assert(err, jc.ErrorIsNil)
   133  	c.Check(string(data), gc.Equals, "<compressed tarball>")
   134  }
   135  
   136  func (s *backupsSuite) TestCreateFailToListFiles(c *gc.C) {
   137  	s.PatchValue(backups.TestGetFilesToBackUp, func(root string, paths *backups.Paths, oldmachine string) ([]string, error) {
   138  		return nil, errors.New("failed!")
   139  	})
   140  
   141  	s.checkFailure(c, "while listing files to back up: failed!")
   142  }
   143  
   144  func (s *backupsSuite) TestCreateFailToCreate(c *gc.C) {
   145  	s.PatchValue(backups.TestGetFilesToBackUp, func(root string, paths *backups.Paths, oldmachine string) ([]string, error) {
   146  		return []string{}, nil
   147  	})
   148  	s.PatchValue(backups.RunCreate, backups.NewTestCreateFailure("failed!"))
   149  
   150  	s.checkFailure(c, "while creating backup archive: failed!")
   151  }
   152  
   153  func (s *backupsSuite) TestCreateFailToFinishMeta(c *gc.C) {
   154  	s.PatchValue(backups.TestGetFilesToBackUp, func(root string, paths *backups.Paths, oldmachine string) ([]string, error) {
   155  		return []string{}, nil
   156  	})
   157  	_, testCreate := backups.NewTestCreate(nil)
   158  	s.PatchValue(backups.RunCreate, testCreate)
   159  	s.PatchValue(backups.FinishMeta, backups.NewTestMetaFinisher("failed!"))
   160  
   161  	s.checkFailure(c, "while updating metadata: failed!")
   162  }
   163  
   164  func (s *backupsSuite) TestCreateFailToStoreArchive(c *gc.C) {
   165  	s.PatchValue(backups.TestGetFilesToBackUp, func(root string, paths *backups.Paths, oldmachine string) ([]string, error) {
   166  		return []string{}, nil
   167  	})
   168  	_, testCreate := backups.NewTestCreate(nil)
   169  	s.PatchValue(backups.RunCreate, testCreate)
   170  	s.PatchValue(backups.FinishMeta, backups.NewTestMetaFinisher(""))
   171  	s.PatchValue(backups.StoreArchiveRef, backups.NewTestArchiveStorer("failed!"))
   172  
   173  	s.checkFailure(c, "while storing backup archive: failed!")
   174  }
   175  
   176  func (s *backupsSuite) TestStoreArchive(c *gc.C) {
   177  	stored := s.setStored("spam")
   178  
   179  	meta := backupstesting.NewMetadataStarted()
   180  	c.Assert(meta.ID(), gc.Equals, "")
   181  	c.Assert(meta.Stored(), gc.IsNil)
   182  	archive := &bytes.Buffer{}
   183  	err := backups.StoreArchive(s.Storage, meta, archive)
   184  	c.Assert(err, jc.ErrorIsNil)
   185  
   186  	s.Storage.CheckCalled(c, "spam", meta, archive, "Add", "Metadata")
   187  	c.Assert(meta.ID(), gc.Equals, "spam")
   188  	c.Assert(meta.Stored(), jc.DeepEquals, stored)
   189  }