Example #1
0
// HandlePutImageDataSuccessfully setup
func HandlePutImageDataSuccessfully(t *testing.T) {
	th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file", func(w http.ResponseWriter, r *http.Request) {
		th.TestMethod(t, r, "PUT")
		th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)

		b, err := ioutil.ReadAll(r.Body)
		if err != nil {
			t.Errorf("Unable to read request body: %v", err)
		}

		th.AssertByteArrayEquals(t, []byte{5, 3, 7, 24}, b)

		w.WriteHeader(http.StatusNoContent)
	})
}
Example #2
0
func TestDownload(t *testing.T) {
	th.SetupHTTP()
	defer th.TeardownHTTP()

	HandleGetImageDataSuccessfully(t)

	rdr, err := Download(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea").Extract()

	th.AssertNoErr(t, err)

	bs, err := ioutil.ReadAll(rdr)

	th.AssertNoErr(t, err)

	th.AssertByteArrayEquals(t, []byte{34, 87, 0, 23, 23, 23, 56, 255, 254, 0}, bs)
}
func TestUploadDownloadImage(t *testing.T) {
	client := newClient(t)

	//creating image
	imageName := tools.RandomString("ACCPT", 16)
	containerFormat := "ami"
	createResult := images.Create(client, images.CreateOpts{Name: &imageName,
		ContainerFormat: &containerFormat,
		DiskFormat:      &containerFormat})
	th.AssertNoErr(t, createResult.Err)
	image, err := createResult.Extract()
	th.AssertNoErr(t, err)
	t.Logf("Image %v", image)

	//checking status
	image, err = images.Get(client, image.ID).Extract()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, image.Status, images.ImageStatusQueued)

	//uploading image data
	data := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
	putImageResult := images.PutImageData(client, image.ID, bytes.NewReader(data))
	th.AssertNoErr(t, putImageResult.Err)

	//checking status
	image, err = images.Get(client, image.ID).Extract()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, image.Status, images.ImageStatusActive)
	th.AssertEquals(t, *image.SizeBytes, 9)

	//downloading image data
	reader, err := images.GetImageData(client, image.ID).Extract()
	th.AssertNoErr(t, err)
	receivedData, err := ioutil.ReadAll(reader)
	t.Logf("Received data %v", receivedData)
	th.AssertNoErr(t, err)
	th.AssertByteArrayEquals(t, data, receivedData)

	//deteting image
	deleteResult := images.Delete(client, image.ID)
	th.AssertNoErr(t, deleteResult.Err)

}