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 | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 6 | "fmt" |
| 7 | "os" |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 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" |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 13 | ) |
| 14 | |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 15 | func waitForSnapshot(client *gophercloud.ServiceClient, id string) error { |
| 16 | for secondsSlept := 0; secondsSlept < 240; secondsSlept++ { |
| 17 | fmt.Printf("Seconds slept waiting for snapshot: %d\n", secondsSlept) |
| 18 | gss, err := snapshots.Get(client, id).ExtractSnapshot() |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 19 | if err != nil { |
| 20 | return err |
| 21 | } |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 22 | if gss.Status == "available" { |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 23 | return nil |
| 24 | } |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 25 | if gss.Status == "error" { |
| 26 | return fmt.Errorf("Error waiting for snapshot to create. Snapshot status is 'error'.") |
| 27 | } |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 28 | time.Sleep(1 * time.Second) |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 29 | } |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 30 | gss, err := snapshots.Get(client, id).ExtractSnapshot() |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | return fmt.Errorf("Time out waiting for snapshot to become available: %+v", gss) |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 37 | func TestSnapshots(t *testing.T) { |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 38 | |
| 39 | volumeID := os.Getenv("OS_VOLUME_ID") |
| 40 | if volumeID == "" { |
| 41 | t.Errorf("Expect OS_VOLUME_ID environment variable. Skipping create and delete snapshot functions.") |
| 42 | return |
| 43 | } |
| 44 | |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 45 | client, err := newClient() |
| 46 | if err != nil { |
| 47 | t.Fatalf("Failed to create Block Storage v1 client: %v", err) |
| 48 | } |
| 49 | |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 50 | css, err := snapshots.Create(client, snapshots.CreateOpts{ |
| 51 | Name: "gophercloud-test-snapshot", |
| 52 | VolumeID: volumeID, |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 53 | }) |
| 54 | if err != nil { |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 55 | t.Errorf("Failed to create snapshot: %v\n", err) |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 58 | err = waitForSnapshot(client, css.ID) |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 59 | if err != nil { |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 60 | t.Errorf("Failed to create snapshot: %v\n", err) |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 61 | } |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 62 | |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame^] | 63 | err = waitForSnapshot(client, css.ID) |
| 64 | |
| 65 | t.Logf("Created snapshots: %+v\n", *css) |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 66 | |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 67 | } |