Ejemplo n.º 1
0
func (s *listSuite) TestList(c *gc.C) {
	cleanup := backups.PatchClientFacadeCall(s.client,
		func(req string, paramsIn interface{}, resp interface{}) error {
			c.Check(req, gc.Equals, "List")

			c.Assert(paramsIn, gc.FitsTypeOf, params.BackupsListArgs{})

			if result, ok := resp.(*params.BackupsListResult); ok {
				result.List = make([]params.BackupsMetadataResult, 1)
				result.List[0] = apiserverbackups.ResultFromMetadata(s.Meta)
			} else {
				c.Fatalf("wrong output structure")
			}
			return nil
		},
	)
	defer cleanup()

	result, err := s.client.List()
	c.Assert(err, jc.ErrorIsNil)

	c.Assert(result.List, gc.HasLen, 1)
	resultItem := result.List[0]
	s.checkMetadataResult(c, &resultItem, s.Meta)
}
Ejemplo n.º 2
0
func (s *backupsUploadSuite) sendValid(c *gc.C, id string) *http.Response {
	s.fake.Meta = backups.NewMetadata()
	s.fake.Meta.SetID("<a new backup ID>")

	var parts bytes.Buffer
	writer := multipart.NewWriter(&parts)

	// Set the metadata part.
	s.meta = backups.NewMetadata()
	metaResult := apiserverbackups.ResultFromMetadata(s.meta)
	header := make(textproto.MIMEHeader)
	header.Set("Content-Disposition", `form-data; name="metadata"`)
	header.Set("Content-Type", apihttp.CTypeJSON)
	part, err := writer.CreatePart(header)
	c.Assert(err, jc.ErrorIsNil)
	err = json.NewEncoder(part).Encode(metaResult)
	c.Assert(err, jc.ErrorIsNil)

	// Set the attached part.
	archive := bytes.NewBufferString("<compressed data>")
	part, err = writer.CreateFormFile("attached", "juju-backup.tar.gz")
	c.Assert(err, jc.ErrorIsNil)
	_, err = io.Copy(part, archive)
	c.Assert(err, jc.ErrorIsNil)

	// Send the request.
	ctype := writer.FormDataContentType()
	resp, err := s.authRequest(c, "PUT", s.backupURL(c), ctype, &parts)
	c.Assert(err, jc.ErrorIsNil)
	return resp
}
Ejemplo n.º 3
0
func (s *uploadSuite) TestSuccessfulRequest(c *gc.C) {
	data := "<compressed archive data>"
	archive := strings.NewReader(data)

	meta := apiserverbackups.ResultFromMetadata(s.Meta)
	meta.ID = ""
	meta.Stored = time.Time{}
	meta.Size = int64(len(data))

	id, err := s.client.Upload(archive, meta)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(id, gc.Matches, `[-\d]+\.[-0-9a-f]+`)

	// Check the stored contents.
	stored, err := s.client.Download(id)
	c.Assert(err, jc.ErrorIsNil)
	storedData, err := ioutil.ReadAll(stored)
	c.Assert(err, jc.ErrorIsNil)
	c.Check(string(storedData), gc.Equals, data)

	// Check the stored metadata.
	storedMeta, err := s.client.Info(id)
	c.Assert(err, jc.ErrorIsNil)
	meta.ID = id
	meta.Stored = storedMeta.Stored
	c.Check(storedMeta, gc.DeepEquals, &meta)
}
Ejemplo n.º 4
0
func (s *createSuite) TestCreate(c *gc.C) {
	cleanup := backups.PatchClientFacadeCall(s.client,
		func(req string, paramsIn interface{}, resp interface{}) error {
			c.Check(req, gc.Equals, "Create")

			c.Assert(paramsIn, gc.FitsTypeOf, params.BackupsCreateArgs{})
			p := paramsIn.(params.BackupsCreateArgs)
			c.Check(p.Notes, gc.Equals, "important")

			if result, ok := resp.(*params.BackupsMetadataResult); ok {
				*result = apiserverbackups.ResultFromMetadata(s.Meta)
				result.Notes = p.Notes
			} else {
				c.Fatalf("wrong output structure")
			}
			return nil
		},
	)
	defer cleanup()

	result, err := s.client.Create("important")
	c.Assert(err, jc.ErrorIsNil)

	meta := backupstesting.UpdateNotes(s.Meta, "important")
	s.checkMetadataResult(c, result, meta)
}
Ejemplo n.º 5
0
func (s *backupsSuite) TestInfoMissingFile(c *gc.C) {
	s.setBackups(c, s.meta, "")
	args := params.BackupsInfoArgs{
		ID: "some-id",
	}
	result, err := s.api.Info(args)
	c.Assert(err, jc.ErrorIsNil)
	expected := backups.ResultFromMetadata(s.meta)

	c.Check(result, gc.DeepEquals, expected)
}
Ejemplo n.º 6
0
func (s *backupsSuite) TestInfoOkay(c *gc.C) {
	impl := s.setBackups(c, s.meta, "")
	impl.Archive = ioutil.NopCloser(bytes.NewBufferString("spamspamspam"))
	args := params.BackupsInfoArgs{
		ID: "some-id",
	}
	result, err := s.api.Info(args)
	c.Assert(err, jc.ErrorIsNil)
	expected := backups.ResultFromMetadata(s.meta)
	c.Check(result, gc.DeepEquals, expected)
}
Ejemplo n.º 7
0
func (s *backupsSuite) TestCreateOkay(c *gc.C) {
	s.PatchValue(backups.WaitUntilReady,
		func(*mgo.Session, int) error { return nil },
	)
	s.setBackups(c, s.meta, "")
	var args params.BackupsCreateArgs
	result, err := s.api.Create(args)
	c.Assert(err, jc.ErrorIsNil)
	expected := backups.ResultFromMetadata(s.meta)

	c.Check(result, gc.DeepEquals, expected)
}
Ejemplo n.º 8
0
func (s *backupsSuite) TestListOkay(c *gc.C) {
	impl := s.setBackups(c, s.meta, "")
	impl.Archive = ioutil.NopCloser(bytes.NewBufferString("spamspamspam"))
	args := params.BackupsListArgs{}
	result, err := s.api.List(args)
	c.Assert(err, jc.ErrorIsNil)

	item := backups.ResultFromMetadata(s.meta)
	expected := params.BackupsListResult{
		List: []params.BackupsMetadataResult{item},
	}

	c.Check(result, gc.DeepEquals, expected)
}
Ejemplo n.º 9
0
func (s *uploadSuite) TestFailedRequest(c *gc.C) {
	data := "<compressed archive data>"
	archive := strings.NewReader(data)

	meta := apiserverbackups.ResultFromMetadata(s.Meta)
	meta.ID = ""
	meta.Size = int64(len(data))
	// The Model field is required, so zero it so that
	// we'll get an error from the endpoint.
	meta.Model = ""

	id, err := s.client.Upload(archive, meta)
	c.Assert(err, gc.ErrorMatches, `.*while storing backup archive: missing Model$`)
	c.Assert(id, gc.Equals, "")
}
Ejemplo n.º 10
0
func (s *uploadSuite) TestSuccess(c *gc.C) {
	s.setSuccess(c, "<a new backup ID>")

	data := "<compressed archive data>"
	archive := ioutil.NopCloser(bytes.NewBufferString(data))

	meta := apiserverbackups.ResultFromMetadata(s.Meta)
	meta.ID = ""
	meta.Stored = time.Time{}

	id, err := s.client.Upload(archive, meta)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(id, gc.Equals, "<a new backup ID>")
	s.FakeClient.CheckCalledReader(c, "backups", archive, &meta, "juju-backup.tar.gz", "SendHTTPRequestReader")
}
Ejemplo n.º 11
0
func (s *backupsSuite) TestCreateNotes(c *gc.C) {
	s.PatchValue(backups.WaitUntilReady,
		func(*mgo.Session, int) error { return nil },
	)
	s.meta.Notes = "this backup is important"
	s.setBackups(c, s.meta, "")
	args := params.BackupsCreateArgs{
		Notes: "this backup is important",
	}
	result, err := s.api.Create(args)
	c.Assert(err, jc.ErrorIsNil)
	expected := backups.ResultFromMetadata(s.meta)
	expected.Notes = "this backup is important"

	c.Check(result, gc.DeepEquals, expected)
}
Ejemplo n.º 12
0
func (s *infoSuite) TestInfo(c *gc.C) {
	cleanup := backups.PatchClientFacadeCall(s.client,
		func(req string, paramsIn interface{}, resp interface{}) error {
			c.Check(req, gc.Equals, "Info")

			c.Assert(paramsIn, gc.FitsTypeOf, params.BackupsInfoArgs{})
			p := paramsIn.(params.BackupsInfoArgs)
			c.Check(p.ID, gc.Equals, "spam")

			if result, ok := resp.(*params.BackupsMetadataResult); ok {
				*result = apiserverbackups.ResultFromMetadata(s.Meta)
			} else {
				c.Fatalf("wrong output structure")
			}
			return nil
		},
	)
	defer cleanup()

	result, err := s.client.Info("spam")
	c.Assert(err, jc.ErrorIsNil)

	s.checkMetadataResult(c, result, s.Meta)
}
Ejemplo n.º 13
0
func (s *baseSuite) metadataResult() *params.BackupsMetadataResult {
	result := apiserverbackups.ResultFromMetadata(s.Meta)
	return &result
}
Ejemplo n.º 14
0
func getArchive(filename string) (rc ArchiveReader, metaResult *params.BackupsMetadataResult, err error) {
	defer func() {
		if err != nil && rc != nil {
			rc.Close()
		}
	}()
	archive, err := os.Open(filename)
	rc = archive
	if err != nil {
		return nil, nil, errors.Trace(err)
	}

	// Extract the metadata.
	ad, err := statebackups.NewArchiveDataReader(archive)
	if err != nil {
		return nil, nil, errors.Trace(err)
	}
	_, err = archive.Seek(0, os.SEEK_SET)
	if err != nil {
		return nil, nil, errors.Trace(err)
	}
	meta, err := ad.Metadata()
	if err != nil {
		if !errors.IsNotFound(err) {
			return nil, nil, errors.Trace(err)
		}
		meta, err = statebackups.BuildMetadata(archive)
		if err != nil {
			return nil, nil, errors.Trace(err)
		}
	}
	// Make sure the file info is set.
	fileMeta, err := statebackups.BuildMetadata(archive)
	if err != nil {
		return nil, nil, errors.Trace(err)
	}
	if meta.Size() == int64(0) {
		if err := meta.SetFileInfo(fileMeta.Size(), "", ""); err != nil {
			return nil, nil, errors.Trace(err)
		}
	}
	if meta.Checksum() == "" {
		err := meta.SetFileInfo(0, fileMeta.Checksum(), fileMeta.ChecksumFormat())
		if err != nil {
			return nil, nil, errors.Trace(err)
		}
	}
	if meta.Finished == nil || meta.Finished.IsZero() {
		meta.Finished = fileMeta.Finished
	}
	_, err = archive.Seek(0, os.SEEK_SET)
	if err != nil {
		return nil, nil, errors.Trace(err)
	}

	// Pack the metadata into a result.
	// TODO(perrito666) change the identity of ResultfromMetadata to
	// return a pointer.
	mResult := apiserverbackups.ResultFromMetadata(meta)
	metaResult = &mResult

	return archive, metaResult, nil
}