Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package openstack |
| 4 | |
| 5 | import ( |
Jon Perritt | 60c3b2e | 2014-05-27 18:38:33 -0500 | [diff] [blame] | 6 | "fmt" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 7 | blockstorage "github.com/rackspace/gophercloud/openstack/blockstorage/v1" |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
Jon Perritt | 60c3b2e | 2014-05-27 18:38:33 -0500 | [diff] [blame] | 10 | identity "github.com/rackspace/gophercloud/openstack/identity/v2" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack/utils" |
| 12 | "os" |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 13 | "strconv" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 14 | "testing" |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 15 | "time" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 16 | ) |
| 17 | |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 18 | var numVols = 2 |
| 19 | |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 20 | func getClient() (*blockstorage.Client, error) { |
| 21 | ao, err := utils.AuthOptions() |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | |
| 26 | r, err := identity.Authenticate(ao) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | sc, err := identity.GetServiceCatalog(r) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | |
| 36 | ces, err := sc.CatalogEntries() |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | var eps []identity.Endpoint |
| 42 | for _, ce := range ces { |
| 43 | if ce.Type == "volume" { |
| 44 | eps = ce.Endpoints |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | region := os.Getenv("OS_REGION_NAME") |
| 49 | rep := "" |
| 50 | for _, ep := range eps { |
| 51 | if ep.Region == region { |
| 52 | rep = ep.PublicURL |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | client := blockstorage.NewClient(rep, r, ao) |
| 57 | |
| 58 | return client, nil |
| 59 | |
| 60 | } |
| 61 | |
| 62 | func TestVolumes(t *testing.T) { |
| 63 | client, err := getClient() |
| 64 | if err != nil { |
| 65 | t.Error(err) |
| 66 | return |
| 67 | } |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 68 | |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 69 | var cv volumes.Volume |
| 70 | for i := 0; i < numVols; i++ { |
| 71 | cv, err := volumes.Create(client, volumes.CreateOpts{ |
| 72 | "size": 1, |
| 73 | "display_name": "test-volume" + strconv.Itoa(i), |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 74 | }) |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 75 | if err != nil { |
| 76 | t.Error(err) |
| 77 | return |
| 78 | } |
| 79 | defer func() { |
| 80 | time.Sleep(10000 * time.Millisecond) |
| 81 | err = volumes.Delete(client, volumes.DeleteOpts{ |
| 82 | "id": cv.Id, |
| 83 | }) |
| 84 | if err != nil { |
| 85 | t.Error(err) |
| 86 | return |
| 87 | } |
| 88 | }() |
| 89 | } |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 90 | |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 91 | vols, err := volumes.List(client, volumes.ListOpts{ |
| 92 | "full": true, |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 93 | }) |
| 94 | if err != nil { |
| 95 | t.Error(err) |
| 96 | return |
| 97 | } |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 98 | if len(vols) != numVols { |
| 99 | t.Errorf("Expected %d volumes, got %d", numVols, len(vols)) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | vols, err = volumes.List(client, volumes.ListOpts{ |
| 104 | "full": false, |
| 105 | }) |
| 106 | if err != nil { |
| 107 | t.Error(err) |
| 108 | return |
| 109 | } |
| 110 | if len(vols) != numVols { |
| 111 | t.Errorf("Expected %d volumes, got %d", numVols, len(vols)) |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | _, err = volumes.Get(client, volumes.GetOpts{ |
| 116 | "id": cv.Id, |
| 117 | }) |
| 118 | if err != nil { |
| 119 | t.Error(err) |
| 120 | return |
| 121 | } |
| 122 | |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 123 | } |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 124 | |
| 125 | func TestSnapshots(t *testing.T) { |
| 126 | client, err := getClient() |
| 127 | if err != nil { |
| 128 | t.Error(err) |
| 129 | return |
| 130 | } |
| 131 | |
Jon Perritt | e3af81d | 2014-05-06 14:52:28 -0500 | [diff] [blame] | 132 | var css snapshots.Snapshot |
| 133 | |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 134 | cv, err := volumes.Create(client, volumes.CreateOpts{ |
| 135 | "size": 1, |
| 136 | "display_name": "test-volume", |
| 137 | }) |
| 138 | if err != nil { |
| 139 | t.Error(err) |
| 140 | return |
| 141 | } |
| 142 | defer func() { |
Jon Perritt | e3af81d | 2014-05-06 14:52:28 -0500 | [diff] [blame] | 143 | for i := 0; i < 60; i++ { |
| 144 | gss, _ := snapshots.Get(client, snapshots.GetOpts{ |
| 145 | "id": css.Id, |
| 146 | }) |
| 147 | if gss.Status == "" { |
| 148 | err = volumes.Delete(client, volumes.DeleteOpts{ |
| 149 | "id": cv.Id, |
| 150 | }) |
| 151 | if err != nil { |
| 152 | t.Error(err) |
| 153 | return |
| 154 | } |
| 155 | break |
| 156 | } |
| 157 | time.Sleep(5000 * time.Millisecond) |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 158 | } |
| 159 | }() |
| 160 | |
| 161 | for i := 0; i < 60; i++ { |
| 162 | gv, err := volumes.Get(client, volumes.GetOpts{ |
| 163 | "id": cv.Id, |
| 164 | }) |
| 165 | if err != nil { |
| 166 | t.Error(err) |
| 167 | return |
| 168 | } |
| 169 | if gv.Status == "available" { |
| 170 | break |
| 171 | } |
| 172 | time.Sleep(2000 * time.Millisecond) |
| 173 | } |
| 174 | |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 175 | css, err = snapshots.Create(client, snapshots.CreateOpts{ |
Jon Perritt | 982c86d | 2014-05-05 21:13:54 -0500 | [diff] [blame] | 176 | "volume_id": cv.Id, |
| 177 | "display_name": "test-snapshot", |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 178 | }) |
| 179 | if err != nil { |
| 180 | t.Error(err) |
| 181 | return |
| 182 | } |
Jon Perritt | 982c86d | 2014-05-05 21:13:54 -0500 | [diff] [blame] | 183 | defer func() { |
Jon Perritt | e3af81d | 2014-05-06 14:52:28 -0500 | [diff] [blame] | 184 | for i := 0; i < 60; i++ { |
| 185 | gss, err := snapshots.Get(client, snapshots.GetOpts{ |
| 186 | "id": css.Id, |
| 187 | }) |
| 188 | if err != nil { |
| 189 | t.Error(err) |
| 190 | return |
| 191 | } |
| 192 | if gss.Status == "available" { |
| 193 | err = snapshots.Delete(client, snapshots.DeleteOpts{ |
| 194 | "id": css.Id, |
| 195 | }) |
| 196 | if err != nil { |
| 197 | t.Error(err) |
| 198 | return |
| 199 | } |
| 200 | break |
| 201 | } |
| 202 | time.Sleep(2000 * time.Millisecond) |
Jon Perritt | 982c86d | 2014-05-05 21:13:54 -0500 | [diff] [blame] | 203 | } |
| 204 | }() |
Jon Perritt | 60c3b2e | 2014-05-27 18:38:33 -0500 | [diff] [blame] | 205 | |
| 206 | lss, err := snapshots.List(client, snapshots.ListOpts{ |
| 207 | Full: true, |
| 208 | }) |
| 209 | if err != nil { |
| 210 | t.Error(err) |
| 211 | return |
| 212 | } |
Jon Perritt | 687c7d0 | 2014-05-05 18:44:54 -0500 | [diff] [blame] | 213 | } |