github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/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  	"io/ioutil"
     8  	"strings"
     9  	"time"
    10  
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	apiserverbackups "github.com/juju/juju/apiserver/backups"
    15  )
    16  
    17  type uploadSuite struct {
    18  	baseSuite
    19  }
    20  
    21  var _ = gc.Suite(&uploadSuite{})
    22  
    23  func (s *uploadSuite) TestSuccessfulRequest(c *gc.C) {
    24  	data := "<compressed archive data>"
    25  	archive := strings.NewReader(data)
    26  
    27  	meta := apiserverbackups.ResultFromMetadata(s.Meta)
    28  	meta.ID = ""
    29  	meta.Stored = time.Time{}
    30  	meta.Size = int64(len(data))
    31  
    32  	id, err := s.client.Upload(archive, meta)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  
    35  	c.Check(id, gc.Matches, `[-\d]+\.[-0-9a-f]+`)
    36  
    37  	// Check the stored contents.
    38  	stored, err := s.client.Download(id)
    39  	c.Assert(err, jc.ErrorIsNil)
    40  	storedData, err := ioutil.ReadAll(stored)
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	c.Check(string(storedData), gc.Equals, data)
    43  
    44  	// Check the stored metadata.
    45  	storedMeta, err := s.client.Info(id)
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	meta.ID = id
    48  	meta.Stored = storedMeta.Stored
    49  	c.Check(storedMeta, gc.DeepEquals, &meta)
    50  }
    51  
    52  func (s *uploadSuite) TestFailedRequest(c *gc.C) {
    53  	data := "<compressed archive data>"
    54  	archive := strings.NewReader(data)
    55  
    56  	meta := apiserverbackups.ResultFromMetadata(s.Meta)
    57  	meta.ID = ""
    58  	meta.Size = int64(len(data))
    59  	// The Model field is required, so zero it so that
    60  	// we'll get an error from the endpoint.
    61  	meta.Model = ""
    62  
    63  	id, err := s.client.Upload(archive, meta)
    64  	c.Assert(err, gc.ErrorMatches, `PUT https://.*/model/.*/backups: while storing backup archive: missing Model`)
    65  	c.Assert(id, gc.Equals, "")
    66  }