Beispiel #1
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
}
Beispiel #2
0
func (s *metadataSuite) TestAsJSONBuffer(c *gc.C) {
	meta := backups.NewMetadata()
	meta.Origin = backups.Origin{
		Environment: "asdf-zxcv-qwe",
		Machine:     "0",
		Hostname:    "myhost",
		Version:     version.MustParse("1.21-alpha3"),
	}
	meta.Started = time.Date(2014, time.Month(9), 9, 11, 59, 34, 0, time.UTC)

	meta.SetID("20140909-115934.asdf-zxcv-qwe")
	err := meta.MarkComplete(10, "123af2cef")
	c.Assert(err, jc.ErrorIsNil)

	finished := meta.Started.Add(time.Minute)
	meta.Finished = &finished

	buf, err := meta.AsJSONBuffer()
	c.Assert(err, jc.ErrorIsNil)

	c.Check(buf.(*bytes.Buffer).String(), gc.Equals, `{`+
		`"ID":"20140909-115934.asdf-zxcv-qwe",`+
		`"Checksum":"123af2cef",`+
		`"ChecksumFormat":"SHA-1, base64 encoded",`+
		`"Size":10,`+
		`"Stored":"0001-01-01T00:00:00Z",`+
		`"Started":"2014-09-09T11:59:34Z",`+
		`"Finished":"2014-09-09T12:00:34Z",`+
		`"Notes":"",`+
		`"Environment":"asdf-zxcv-qwe",`+
		`"Machine":"0",`+
		`"Hostname":"myhost",`+
		`"Version":"1.21-alpha3"`+
		`}`+"\n")
}
Beispiel #3
0
// NewMetadataStarted returns a Metadata to use for testing.
func NewMetadataStarted() *backups.Metadata {
	meta := backups.NewMetadata()
	meta.Started = meta.Started.Truncate(time.Second)
	meta.Origin.Model = envID
	meta.Origin.Machine = "0"
	meta.Origin.Hostname = "main-host"
	return meta
}
Beispiel #4
0
func (s *storageSuite) metadata(c *gc.C) *backups.Metadata {
	meta := backups.NewMetadata()
	meta.Origin.Model = s.State.ModelUUID()
	meta.Origin.Machine = "0"
	meta.Origin.Hostname = "localhost"
	err := meta.MarkComplete(int64(42), "some hash")
	c.Assert(err, jc.ErrorIsNil)
	return meta
}
Beispiel #5
0
// MetadataFromResult returns a new Metadata based on the result. The ID
// of the metadata is not set. Call meta.SetID() if that is desired.
// Likewise with Stored and meta.SetStored().
func MetadataFromResult(result params.BackupsMetadataResult) *backups.Metadata {
	meta := backups.NewMetadata()
	meta.Started = result.Started
	if !result.Finished.IsZero() {
		meta.Finished = &result.Finished
	}
	meta.Origin.Environment = result.Environment
	meta.Origin.Machine = result.Machine
	meta.Origin.Hostname = result.Hostname
	meta.Origin.Version = result.Version
	meta.Notes = result.Notes
	meta.SetFileInfo(result.Size, result.Checksum, result.ChecksumFormat)
	return meta
}
Beispiel #6
0
func (s *storageSuite) TestAddBackupMetadataEmpty(c *gc.C) {
	original := backups.NewMetadata()
	_, err := backups.AddBackupMetadata(s.State, original)

	c.Check(err, gc.NotNil)
}