示例#1
0
func (s *archiveDataSuite) TestNewBuffer(c *gc.C) {
	ad, err := backups.NewArchiveDataReader(s.archiveFile)
	c.Assert(err, jc.ErrorIsNil)
	buf := ad.NewBuffer()

	c.Check(buf.Bytes(), jc.DeepEquals, s.data)
}
示例#2
0
func (s *archiveDataSuite) TestNewArchiveDataReader(c *gc.C) {
	ad, err := backups.NewArchiveDataReader(s.archiveFile)
	c.Assert(err, jc.ErrorIsNil)
	data := ad.NewBuffer().Bytes()

	c.Check(ad.ContentDir, gc.Equals, "juju-backup")
	c.Check(data, jc.DeepEquals, s.data)
}
示例#3
0
func (s *archiveDataSuite) TestVersionFound(c *gc.C) {
	ad, err := backups.NewArchiveDataReader(s.archiveFile)
	c.Assert(err, jc.ErrorIsNil)

	version, err := ad.Version()
	c.Assert(err, jc.ErrorIsNil)

	c.Check(version, jc.DeepEquals, &s.meta.Origin.Version)
}
示例#4
0
func (s *archiveDataSuite) TestMetadata(c *gc.C) {
	ad, err := backups.NewArchiveDataReader(s.archiveFile)
	c.Assert(err, jc.ErrorIsNil)

	meta, err := ad.Metadata()
	c.Assert(err, jc.ErrorIsNil)

	c.Check(meta, jc.DeepEquals, s.meta)
}
示例#5
0
func (s *archiveDataSuite) TestVersionNotFound(c *gc.C) {
	archiveFile := s.newArchiveFile(c, nil)
	ad, err := backups.NewArchiveDataReader(archiveFile)
	c.Assert(err, jc.ErrorIsNil)

	version, err := ad.Version()
	c.Assert(err, jc.ErrorIsNil)

	c.Check(version.String(), jc.DeepEquals, "1.20.0")
}
示例#6
0
func (s *archiveDataSuite) TestNewBufferMultiple(c *gc.C) {
	ad, err := backups.NewArchiveDataReader(s.archiveFile)
	c.Assert(err, jc.ErrorIsNil)

	buf1 := ad.NewBuffer()
	buf2 := ad.NewBuffer()

	c.Check(buf2, gc.Not(gc.Equals), buf1)
	c.Check(buf2.Bytes(), jc.DeepEquals, buf1.Bytes())
}
示例#7
0
文件: backups.go 项目: makyo/juju
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
}