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