blob: 5038c8ed2ad1198ad8c35d2f255a8b62260efd33 [file] [log] [blame]
Jon Perrittd0399572014-09-22 18:03:02 -05001// +build acceptance
2
Jon Perrittb71a28a2014-09-17 18:16:32 -05003package v1
Jon Perrittd0399572014-09-22 18:03:02 -05004
5import (
Jon Perritt56d43b22014-09-22 20:47:11 -05006 "errors"
Jon Perrittd0399572014-09-22 18:03:02 -05007 "strconv"
8 "testing"
Jon Perritt56d43b22014-09-22 20:47:11 -05009 "time"
Jon Perrittd0399572014-09-22 18:03:02 -050010
Jon Perritt56d43b22014-09-22 20:47:11 -050011 "github.com/rackspace/gophercloud"
Jon Perrittd0399572014-09-22 18:03:02 -050012 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
13 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
14)
15
Jon Perritt56d43b22014-09-22 20:47:11 -050016func waitForVolume(client *gophercloud.ServiceClient, id string) error {
17 notReady := true
18 secondsSlept := 0
19 for notReady && secondsSlept < 20 {
20 gv, err := volumes.Get(client, id).ExtractVolume()
21 if err != nil {
22 return err
23 }
24 if gv.Status == "available" {
25 return nil
26 }
27 time.Sleep(1 * time.Millisecond)
28 secondsSlept = secondsSlept + 1
29 }
Jon Perrittd0399572014-09-22 18:03:02 -050030
Jon Perritt56d43b22014-09-22 20:47:11 -050031 return errors.New("Time out waiting for volume to become available")
Jon Perrittd0399572014-09-22 18:03:02 -050032}
33
34var numSnapshots = 1
35
36func TestSnapshots(t *testing.T) {
37 client, err := newClient()
38 if err != nil {
39 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
40 }
41
42 cv, err := volumes.Create(client, volumes.CreateOpts{
43 Size: 1,
44 Name: "gophercloud-test-volume",
45 })
46 if err != nil {
47 t.Fatalf("Failed to create volume: %v", err)
48 }
49
Jon Perritt56d43b22014-09-22 20:47:11 -050050 err = waitForVolume(client, cv.ID)
51 if err != nil {
52 t.Fatal(err)
53 }
Jon Perrittd0399572014-09-22 18:03:02 -050054
55 var sss []*snapshots.Snapshot
56 for i := 0; i < numSnapshots; i++ {
57 css, err := snapshots.Create(client, snapshots.CreateOpts{
58 Name: "gophercloud-test-snapshot-" + strconv.Itoa(i),
59 VolumeID: cv.ID,
60 })
61 if err != nil {
62 t.Errorf("Failed to create snapshot: %v\n", err)
63 }
64 sss = append(sss, css)
65 }
66
67 t.Logf("Created snapshots: %+v\n", sss)
68}