github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/backups/create_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  	"bytes"
     8  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cmd/juju/backups"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type createSuite struct {
    20  	BaseBackupsSuite
    21  	wrappedCommand  cmd.Command
    22  	command         *backups.CreateCommand
    23  	defaultFilename string
    24  }
    25  
    26  var _ = gc.Suite(&createSuite{})
    27  
    28  func (s *createSuite) SetUpTest(c *gc.C) {
    29  	s.BaseBackupsSuite.SetUpTest(c)
    30  	s.wrappedCommand, s.command = backups.NewCreateCommandForTest()
    31  	s.defaultFilename = "juju-backup-<date>-<time>.tar.gz"
    32  }
    33  
    34  func (s *createSuite) setDownload() *fakeAPIClient {
    35  	client := s.BaseBackupsSuite.setDownload()
    36  	return client
    37  }
    38  
    39  func (s *createSuite) checkDownloadStd(c *gc.C, ctx *cmd.Context) {
    40  	c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
    41  
    42  	out := ctx.Stdout.(*bytes.Buffer).String()
    43  	if !s.command.Log.Quiet {
    44  		parts := strings.Split(out, MetaResultString)
    45  		c.Assert(parts, gc.HasLen, 2)
    46  		c.Assert(parts[0], gc.Equals, "")
    47  		out = parts[1]
    48  	}
    49  
    50  	parts := strings.Split(out, "\n")
    51  	c.Assert(parts, gc.HasLen, 3)
    52  	c.Assert(parts[2], gc.Equals, "")
    53  
    54  	c.Check(parts[0], gc.Equals, s.metaresult.ID)
    55  
    56  	// Check the download message.
    57  	parts = strings.Split(parts[1], "downloading to ")
    58  	c.Assert(parts, gc.HasLen, 2)
    59  	c.Assert(parts[0], gc.Equals, "")
    60  	s.filename = parts[1]
    61  }
    62  
    63  func (s *createSuite) checkDownload(c *gc.C, ctx *cmd.Context) {
    64  	s.checkDownloadStd(c, ctx)
    65  	s.checkArchive(c)
    66  }
    67  
    68  func (s *createSuite) TestNoArgs(c *gc.C) {
    69  	client := s.BaseBackupsSuite.setDownload()
    70  	_, err := testing.RunCommand(c, s.wrappedCommand, "--quiet")
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	client.Check(c, s.metaresult.ID, "", "Create", "Download")
    74  }
    75  
    76  func (s *createSuite) TestDefaultDownload(c *gc.C) {
    77  	s.setDownload()
    78  	ctx, err := testing.RunCommand(c, s.wrappedCommand, "--quiet", "--filename", s.defaultFilename)
    79  	c.Assert(err, jc.ErrorIsNil)
    80  
    81  	s.checkDownload(c, ctx)
    82  	c.Check(s.command.Filename, gc.Not(gc.Equals), "")
    83  	c.Check(s.command.Filename, gc.Equals, backups.NotSet)
    84  }
    85  
    86  func (s *createSuite) TestQuiet(c *gc.C) {
    87  	client := s.BaseBackupsSuite.setDownload()
    88  	ctx, err := testing.RunCommand(c, s.wrappedCommand, "--quiet")
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	client.Check(c, s.metaresult.ID, "", "Create", "Download")
    92  
    93  	c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
    94  	out := ctx.Stdout.(*bytes.Buffer).String()
    95  	c.Check(out, gc.Not(jc.Contains), MetaResultString)
    96  	c.Check(out, jc.HasPrefix, s.metaresult.ID+"\n")
    97  	s.checkDownloadStd(c, ctx)
    98  }
    99  
   100  func (s *createSuite) TestNotes(c *gc.C) {
   101  	client := s.BaseBackupsSuite.setDownload()
   102  	_, err := testing.RunCommand(c, s.wrappedCommand, "spam", "--quiet")
   103  	c.Assert(err, jc.ErrorIsNil)
   104  
   105  	client.Check(c, s.metaresult.ID, "spam", "Create", "Download")
   106  }
   107  
   108  func (s *createSuite) TestFilename(c *gc.C) {
   109  	client := s.setDownload()
   110  	ctx, err := testing.RunCommand(c, s.wrappedCommand, "--filename", "backup.tgz", "--quiet")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	client.Check(c, s.metaresult.ID, "", "Create", "Download")
   114  	s.checkDownload(c, ctx)
   115  	c.Check(s.command.Filename, gc.Equals, "backup.tgz")
   116  }
   117  
   118  func (s *createSuite) TestNoDownload(c *gc.C) {
   119  	client := s.setSuccess()
   120  	ctx, err := testing.RunCommand(c, s.wrappedCommand, "--no-download")
   121  	c.Assert(err, jc.ErrorIsNil)
   122  
   123  	client.Check(c, "", "", "Create")
   124  	out := MetaResultString + s.metaresult.ID + "\n"
   125  	s.checkStd(c, ctx, out, backups.DownloadWarning+"\n")
   126  	c.Check(s.command.Filename, gc.Equals, backups.NotSet)
   127  }
   128  
   129  func (s *createSuite) TestFilenameAndNoDownload(c *gc.C) {
   130  	s.setSuccess()
   131  	_, err := testing.RunCommand(c, s.wrappedCommand, "--no-download", "--filename", "backup.tgz")
   132  
   133  	c.Check(err, gc.ErrorMatches, "cannot mix --no-download and --filename")
   134  }
   135  
   136  func (s *createSuite) TestError(c *gc.C) {
   137  	s.setFailure("failed!")
   138  	_, err := testing.RunCommand(c, s.wrappedCommand)
   139  
   140  	c.Check(errors.Cause(err), gc.ErrorMatches, "failed!")
   141  }