github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/backups/upload_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 "archive/tar" 8 "compress/gzip" 9 "os" 10 "strings" 11 12 "github.com/juju/cmd/cmdtesting" 13 "github.com/juju/errors" 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/cmd/juju/backups" 18 "github.com/juju/juju/testing" 19 ) 20 21 type uploadSuite struct { 22 BaseBackupsSuite 23 subcommand *backups.UploadCommand 24 filename string 25 } 26 27 var _ = gc.Suite(&uploadSuite{}) 28 29 func (s *uploadSuite) SetUpTest(c *gc.C) { 30 s.BaseBackupsSuite.SetUpTest(c) 31 32 s.subcommand = &backups.UploadCommand{} 33 s.filename = "juju-backup-20140912-130755.abcd-spam-deadbeef-eggs.tar.gz" 34 s.subcommand.Filename = s.filename 35 } 36 37 func (s *uploadSuite) TearDownTest(c *gc.C) { 38 if err := os.Remove(s.filename); err != nil { 39 if !os.IsNotExist(err) { 40 c.Check(err, jc.ErrorIsNil) 41 } 42 } 43 44 s.BaseBackupsSuite.TearDownTest(c) 45 } 46 47 func (s *uploadSuite) createArchive(c *gc.C) { 48 archive, err := os.Create(s.filename) 49 c.Assert(err, jc.ErrorIsNil) 50 defer archive.Close() 51 52 compressed := gzip.NewWriter(archive) 53 defer compressed.Close() 54 55 tarball := tar.NewWriter(compressed) 56 defer tarball.Close() 57 58 var files = []struct{ Name, Body string }{ 59 {"root.tar", "<state config files>"}, 60 {"dump/oplog.bson", "<something here>"}, 61 } 62 for _, file := range files { 63 hdr := &tar.Header{ 64 Name: file.Name, 65 Size: int64(len(file.Body)), 66 } 67 err := tarball.WriteHeader(hdr) 68 c.Assert(err, jc.ErrorIsNil) 69 _, err = tarball.Write([]byte(file.Body)) 70 c.Assert(err, jc.ErrorIsNil) 71 } 72 } 73 74 func (s *uploadSuite) TestHelp(c *gc.C) { 75 ctx, err := testing.RunCommand(c, s.command, "upload", "--help") 76 c.Assert(err, jc.ErrorIsNil) 77 78 info := s.subcommand.Info() 79 expected := "(?sm)usage: juju backups upload [options] " + info.Args + "$.*" 80 expected = strings.Replace(expected, "[", `\[`, -1) 81 c.Check(testing.Stdout(ctx), gc.Matches, expected) 82 expected = "(?sm).*^purpose: " + info.Purpose + "$.*" 83 c.Check(testing.Stdout(ctx), gc.Matches, expected) 84 expected = "(?sm).*^" + info.Doc + "$.*" 85 c.Check(testing.Stdout(ctx), gc.Matches, expected) 86 } 87 88 func (s *uploadSuite) TestOkay(c *gc.C) { 89 s.createArchive(c) 90 s.setSuccess() 91 ctx := cmdtesting.Context(c) 92 err := s.subcommand.Run(ctx) 93 c.Check(err, jc.ErrorIsNil) 94 95 out := MetaResultString 96 s.checkStd(c, ctx, out, "") 97 } 98 99 func (s *uploadSuite) TestFileMissing(c *gc.C) { 100 s.setSuccess() 101 ctx := cmdtesting.Context(c) 102 err := s.subcommand.Run(ctx) 103 104 c.Check(os.IsNotExist(errors.Cause(err)), jc.IsTrue) 105 } 106 107 func (s *uploadSuite) TestError(c *gc.C) { 108 s.createArchive(c) 109 s.setFailure("failed!") 110 ctx := cmdtesting.Context(c) 111 err := s.subcommand.Run(ctx) 112 113 c.Check(errors.Cause(err), gc.ErrorMatches, "failed!") 114 }