github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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/cmd/cmdtesting"
    12  	"github.com/juju/errors"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/cmd/juju/backups"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type createSuite struct {
    21  	BaseBackupsSuite
    22  	subcommand *backups.CreateCommand
    23  }
    24  
    25  var _ = gc.Suite(&createSuite{})
    26  
    27  func (s *createSuite) SetUpTest(c *gc.C) {
    28  	s.BaseBackupsSuite.SetUpTest(c)
    29  	s.subcommand = &backups.CreateCommand{}
    30  	s.subcommand.Filename = "juju-backup-<date>-<time>.tar.gz"
    31  }
    32  
    33  func (s *createSuite) setDownload() *fakeAPIClient {
    34  	client := s.BaseBackupsSuite.setDownload()
    35  	s.subcommand.Quiet = true
    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.subcommand.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) TestHelp(c *gc.C) {
    69  	ctx, err := testing.RunCommand(c, s.command, "create", "--help")
    70  	c.Assert(err, jc.ErrorIsNil)
    71  
    72  	info := s.subcommand.Info()
    73  	expected := "(?sm)usage: juju backups create [options] " + info.Args + "$.*"
    74  	expected = strings.Replace(expected, "[", `\[`, -1)
    75  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    76  	expected = "(?sm).*^purpose: " + info.Purpose + "$.*"
    77  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    78  	expected = "(?sm).*^" + info.Doc + "$.*"
    79  	c.Check(testing.Stdout(ctx), gc.Matches, expected)
    80  }
    81  
    82  func (s *createSuite) TestNoArgs(c *gc.C) {
    83  	client := s.BaseBackupsSuite.setDownload()
    84  	_, err := testing.RunCommand(c, s.command, "create")
    85  	c.Assert(err, jc.ErrorIsNil)
    86  
    87  	client.Check(c, s.metaresult.ID, "", "Create", "Download")
    88  }
    89  
    90  func (s *createSuite) TestDefaultDownload(c *gc.C) {
    91  	s.setDownload()
    92  	ctx := cmdtesting.Context(c)
    93  	err := s.subcommand.Run(ctx)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  
    96  	s.checkDownload(c, ctx)
    97  	c.Check(s.filename, gc.Not(gc.Equals), "")
    98  	c.Check(s.subcommand.Filename, gc.Equals, backups.NotSet)
    99  }
   100  
   101  func (s *createSuite) TestQuiet(c *gc.C) {
   102  	client := s.BaseBackupsSuite.setDownload()
   103  	s.subcommand.Quiet = true
   104  	ctx := cmdtesting.Context(c)
   105  	err := s.subcommand.Run(ctx)
   106  	c.Assert(err, jc.ErrorIsNil)
   107  
   108  	client.Check(c, s.metaresult.ID, "", "Create", "Download")
   109  
   110  	c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
   111  	out := ctx.Stdout.(*bytes.Buffer).String()
   112  	c.Check(out, gc.Not(jc.Contains), MetaResultString)
   113  	c.Check(out, jc.HasPrefix, s.metaresult.ID+"\n")
   114  	s.checkDownloadStd(c, ctx)
   115  }
   116  
   117  func (s *createSuite) TestNotes(c *gc.C) {
   118  	client := s.BaseBackupsSuite.setDownload()
   119  	_, err := testing.RunCommand(c, s.command, "create", "spam")
   120  	c.Assert(err, jc.ErrorIsNil)
   121  
   122  	client.Check(c, s.metaresult.ID, "spam", "Create", "Download")
   123  }
   124  
   125  func (s *createSuite) TestFilename(c *gc.C) {
   126  	client := s.setDownload()
   127  	s.subcommand.Filename = "backup.tgz"
   128  	ctx := cmdtesting.Context(c)
   129  	err := s.subcommand.Run(ctx)
   130  	c.Assert(err, jc.ErrorIsNil)
   131  
   132  	client.Check(c, s.metaresult.ID, "", "Create", "Download")
   133  	s.checkDownload(c, ctx)
   134  	c.Check(s.subcommand.Filename, gc.Equals, s.filename)
   135  }
   136  
   137  func (s *createSuite) TestNoDownload(c *gc.C) {
   138  	client := s.setSuccess()
   139  	s.subcommand.NoDownload = true
   140  	ctx := cmdtesting.Context(c)
   141  	err := s.subcommand.Run(ctx)
   142  	c.Assert(err, jc.ErrorIsNil)
   143  
   144  	client.Check(c, "", "", "Create")
   145  	out := MetaResultString + s.metaresult.ID + "\n"
   146  	s.checkStd(c, ctx, out, backups.DownloadWarning+"\n")
   147  	c.Check(s.subcommand.Filename, gc.Equals, backups.NotSet)
   148  }
   149  
   150  func (s *createSuite) TestFilenameAndNoDownload(c *gc.C) {
   151  	s.setSuccess()
   152  	_, err := testing.RunCommand(c, s.command, "create", "--no-download", "--filename", "backup.tgz")
   153  
   154  	c.Check(err, gc.ErrorMatches, "cannot mix --no-download and --filename")
   155  }
   156  
   157  func (s *createSuite) TestError(c *gc.C) {
   158  	s.setFailure("failed!")
   159  	ctx := cmdtesting.Context(c)
   160  	err := s.subcommand.Run(ctx)
   161  
   162  	c.Check(errors.Cause(err), gc.ErrorMatches, "failed!")
   163  }