Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 3 | package v1 |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 4 | |
| 5 | import ( |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame^] | 6 | "errors" |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 7 | "strconv" |
| 8 | "testing" |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame^] | 9 | "time" |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 10 | |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame^] | 11 | "github.com/rackspace/gophercloud" |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots" |
| 13 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
| 14 | ) |
| 15 | |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame^] | 16 | func 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 Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 30 | |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame^] | 31 | return errors.New("Time out waiting for volume to become available") |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | var numSnapshots = 1 |
| 35 | |
| 36 | func 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 Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame^] | 50 | err = waitForVolume(client, cv.ID) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 54 | |
| 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 | } |