blob: 9d8f0d30502a850fc31b25d55277a2fd2d748558 [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 Perrittd4788f92014-09-24 12:05:27 -05006 "fmt"
7 "os"
Jon Perrittd0399572014-09-22 18:03:02 -05008 "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"
Jon Perrittd0399572014-09-22 18:03:02 -050013)
14
Jon Perrittd4788f92014-09-24 12:05:27 -050015func 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 Perritt56d43b22014-09-22 20:47:11 -050019 if err != nil {
20 return err
21 }
Jon Perrittd4788f92014-09-24 12:05:27 -050022 if gss.Status == "available" {
Jon Perritt56d43b22014-09-22 20:47:11 -050023 return nil
24 }
Jon Perrittd4788f92014-09-24 12:05:27 -050025 if gss.Status == "error" {
26 return fmt.Errorf("Error waiting for snapshot to create. Snapshot status is 'error'.")
27 }
Jon Perrittd7468632014-09-22 21:58:59 -050028 time.Sleep(1 * time.Second)
Jon Perritt56d43b22014-09-22 20:47:11 -050029 }
Jon Perrittd4788f92014-09-24 12:05:27 -050030 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 Perrittd0399572014-09-22 18:03:02 -050035}
36
Jon Perrittd0399572014-09-22 18:03:02 -050037func TestSnapshots(t *testing.T) {
Jon Perrittd4788f92014-09-24 12:05:27 -050038
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 Perrittd0399572014-09-22 18:03:02 -050045 client, err := newClient()
46 if err != nil {
47 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
48 }
49
Jon Perrittd4788f92014-09-24 12:05:27 -050050 css, err := snapshots.Create(client, snapshots.CreateOpts{
51 Name: "gophercloud-test-snapshot",
52 VolumeID: volumeID,
Jon Perrittd0399572014-09-22 18:03:02 -050053 })
54 if err != nil {
Jon Perrittd4788f92014-09-24 12:05:27 -050055 t.Errorf("Failed to create snapshot: %v\n", err)
Jon Perrittd0399572014-09-22 18:03:02 -050056 }
57
Jon Perrittd4788f92014-09-24 12:05:27 -050058 err = waitForSnapshot(client, css.ID)
Jon Perritt56d43b22014-09-22 20:47:11 -050059 if err != nil {
Jon Perrittd4788f92014-09-24 12:05:27 -050060 t.Errorf("Failed to create snapshot: %v\n", err)
Jon Perritt56d43b22014-09-22 20:47:11 -050061 }
Jon Perrittd0399572014-09-22 18:03:02 -050062
Jon Perrittd4788f92014-09-24 12:05:27 -050063 err = waitForSnapshot(client, css.ID)
64
65 t.Logf("Created snapshots: %+v\n", *css)
Jon Perrittd7468632014-09-22 21:58:59 -050066
Jon Perrittd0399572014-09-22 18:03:02 -050067}