blob: 1e639de8d50adfe5a3258cf2de2f0070a58e189e [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 Perrittd0399572014-09-22 18:03:02 -05006 "testing"
Jon Perrittd0399572014-09-22 18:03:02 -05007
Jon Perritt56d43b22014-09-22 20:47:11 -05008 "github.com/rackspace/gophercloud"
Jon Perritt57ba7632014-10-02 20:32:22 -05009 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
10 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Jon Perrittd0399572014-09-22 18:03:02 -050011)
12
Jon Perrittd0399572014-09-22 18:03:02 -050013func TestSnapshots(t *testing.T) {
Jon Perrittd4788f92014-09-24 12:05:27 -050014
Jon Perrittd0399572014-09-22 18:03:02 -050015 client, err := newClient()
16 if err != nil {
17 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
18 }
19
Jon Perritt57ba7632014-10-02 20:32:22 -050020 v, err := volumes.Create(client, &volumes.CreateOpts{
21 Name: "gophercloud-test-volume",
22 Size: 1,
23 }).Extract()
24 if err != nil {
25 t.Fatalf("Failed to create volume: %v\n", err)
26 }
27
28 err = volumes.WaitForStatus(client, v.ID, "available", 120)
29 if err != nil {
30 t.Fatalf("Failed to create volume: %v\n", err)
31 }
32
33 t.Logf("Created volume: %v\n", v)
34
35 ss, err := snapshots.Create(client, &snapshots.CreateOpts{
Jon Perrittd4788f92014-09-24 12:05:27 -050036 Name: "gophercloud-test-snapshot",
Jon Perritt57ba7632014-10-02 20:32:22 -050037 VolumeID: v.ID,
38 }).Extract()
39 if err != nil {
40 t.Fatalf("Failed to create snapshot: %v\n", err)
41 }
42
43 err = snapshots.WaitForStatus(client, ss.ID, "available", 120)
44 if err != nil {
45 t.Fatalf("Failed to create snapshot: %v\n", err)
46 }
47
48 t.Logf("Created snapshot: %+v\n", ss)
49
Jon Perritt1f617782014-10-27 11:37:39 -050050 err = snapshots.Delete(client, ss.ID).Extract()
51 if err != nil {
Jon Perritt57ba7632014-10-02 20:32:22 -050052 t.Fatalf("Failed to delete snapshot: %v", err)
53 }
54
55 err = gophercloud.WaitFor(120, func() (bool, error) {
56 _, err := snapshots.Get(client, ss.ID).Extract()
57 if err != nil {
58 return true, nil
59 }
60
61 return false, nil
Jon Perrittd0399572014-09-22 18:03:02 -050062 })
63 if err != nil {
Jon Perritt57ba7632014-10-02 20:32:22 -050064 t.Fatalf("Failed to delete snapshot: %v", err)
Jon Perrittd0399572014-09-22 18:03:02 -050065 }
66
Jon Perritt57ba7632014-10-02 20:32:22 -050067 t.Log("Deleted snapshot\n")
68
Jon Perritt1f617782014-10-27 11:37:39 -050069 err = volumes.Delete(client, v.ID).Extract()
70 if err != nil {
Jon Perritt57ba7632014-10-02 20:32:22 -050071 t.Errorf("Failed to delete volume: %v", err)
Jon Perritt56d43b22014-09-22 20:47:11 -050072 }
Jon Perrittd0399572014-09-22 18:03:02 -050073
Jon Perritt57ba7632014-10-02 20:32:22 -050074 err = gophercloud.WaitFor(120, func() (bool, error) {
75 _, err := volumes.Get(client, v.ID).Extract()
76 if err != nil {
77 return true, nil
78 }
Jon Perrittd4788f92014-09-24 12:05:27 -050079
Jon Perritt57ba7632014-10-02 20:32:22 -050080 return false, nil
81 })
82 if err != nil {
83 t.Errorf("Failed to delete volume: %v", err)
84 }
Jon Perrittd7468632014-09-22 21:58:59 -050085
Jon Perritt57ba7632014-10-02 20:32:22 -050086 t.Log("Deleted volume\n")
Jon Perrittd0399572014-09-22 18:03:02 -050087}