github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 11 "github.com/juju/cmd" 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 uploadSuite struct { 21 BaseBackupsSuite 22 command cmd.Command 23 filename string 24 } 25 26 var _ = gc.Suite(&uploadSuite{}) 27 28 func (s *uploadSuite) SetUpTest(c *gc.C) { 29 s.BaseBackupsSuite.SetUpTest(c) 30 31 s.command = backups.NewUploadCommandForTest() 32 s.filename = "juju-backup-20140912-130755.abcd-spam-deadbeef-eggs.tar.gz" 33 } 34 35 func (s *uploadSuite) TearDownTest(c *gc.C) { 36 if err := os.Remove(s.filename); err != nil { 37 if !os.IsNotExist(err) { 38 c.Check(err, jc.ErrorIsNil) 39 } 40 } 41 42 s.BaseBackupsSuite.TearDownTest(c) 43 } 44 45 func (s *uploadSuite) createArchive(c *gc.C) { 46 archive, err := os.Create(s.filename) 47 c.Assert(err, jc.ErrorIsNil) 48 defer archive.Close() 49 50 compressed := gzip.NewWriter(archive) 51 defer compressed.Close() 52 53 tarball := tar.NewWriter(compressed) 54 defer tarball.Close() 55 56 var files = []struct{ Name, Body string }{ 57 {"root.tar", "<state config files>"}, 58 {"dump/oplog.bson", "<something here>"}, 59 } 60 for _, file := range files { 61 hdr := &tar.Header{ 62 Name: file.Name, 63 Size: int64(len(file.Body)), 64 } 65 err := tarball.WriteHeader(hdr) 66 c.Assert(err, jc.ErrorIsNil) 67 _, err = tarball.Write([]byte(file.Body)) 68 c.Assert(err, jc.ErrorIsNil) 69 } 70 } 71 72 func (s *uploadSuite) TestOkay(c *gc.C) { 73 s.createArchive(c) 74 s.setSuccess() 75 ctx, err := testing.RunCommand(c, s.command, s.filename) 76 c.Check(err, jc.ErrorIsNil) 77 78 out := MetaResultString 79 s.checkStd(c, ctx, out, "") 80 } 81 82 func (s *uploadSuite) TestFileMissing(c *gc.C) { 83 s.setSuccess() 84 _, err := testing.RunCommand(c, s.command, s.filename) 85 c.Check(os.IsNotExist(errors.Cause(err)), jc.IsTrue) 86 } 87 88 func (s *uploadSuite) TestError(c *gc.C) { 89 s.createArchive(c) 90 s.setFailure("failed!") 91 _, err := testing.RunCommand(c, s.command, s.filename) 92 c.Check(errors.Cause(err), gc.ErrorMatches, "failed!") 93 }