github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/snapshots/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots" 8 "github.com/gophercloud/gophercloud/pagination" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 func TestList(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 MockListResponse(t) 18 19 count := 0 20 21 snapshots.List(client.ServiceClient(), &snapshots.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 22 count++ 23 actual, err := snapshots.ExtractSnapshots(page) 24 if err != nil { 25 t.Errorf("Failed to extract snapshots: %v", err) 26 return false, err 27 } 28 29 expected := []snapshots.Snapshot{ 30 { 31 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 32 Name: "snapshot-001", 33 VolumeID: "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 34 Status: "available", 35 Size: 30, 36 CreatedAt: time.Date(2017, 5, 30, 3, 35, 3, 0, time.UTC), 37 Description: "Daily Backup", 38 }, 39 { 40 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 41 Name: "snapshot-002", 42 VolumeID: "76b8950a-8594-4e5b-8dce-0dfa9c696358", 43 Status: "available", 44 Size: 25, 45 CreatedAt: time.Date(2017, 5, 30, 3, 35, 3, 0, time.UTC), 46 Description: "Weekly Backup", 47 }, 48 } 49 50 th.CheckDeepEquals(t, expected, actual) 51 52 return true, nil 53 }) 54 55 if count != 1 { 56 t.Errorf("Expected 1 page, got %d", count) 57 } 58 } 59 60 func TestGet(t *testing.T) { 61 th.SetupHTTP() 62 defer th.TeardownHTTP() 63 64 MockGetResponse(t) 65 66 v, err := snapshots.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 67 th.AssertNoErr(t, err) 68 69 th.AssertEquals(t, v.Name, "snapshot-001") 70 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 71 } 72 73 func TestCreate(t *testing.T) { 74 th.SetupHTTP() 75 defer th.TeardownHTTP() 76 77 MockCreateResponse(t) 78 79 options := snapshots.CreateOpts{VolumeID: "1234", Name: "snapshot-001"} 80 n, err := snapshots.Create(client.ServiceClient(), options).Extract() 81 th.AssertNoErr(t, err) 82 83 th.AssertEquals(t, n.VolumeID, "1234") 84 th.AssertEquals(t, n.Name, "snapshot-001") 85 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 86 } 87 88 func TestUpdateMetadata(t *testing.T) { 89 th.SetupHTTP() 90 defer th.TeardownHTTP() 91 92 MockUpdateMetadataResponse(t) 93 94 expected := map[string]interface{}{"key": "v1"} 95 96 options := &snapshots.UpdateMetadataOpts{ 97 Metadata: map[string]interface{}{ 98 "key": "v1", 99 }, 100 } 101 102 actual, err := snapshots.UpdateMetadata(client.ServiceClient(), "123", options).ExtractMetadata() 103 104 th.AssertNoErr(t, err) 105 th.AssertDeepEquals(t, actual, expected) 106 } 107 108 func TestDelete(t *testing.T) { 109 th.SetupHTTP() 110 defer th.TeardownHTTP() 111 112 MockDeleteResponse(t) 113 114 res := snapshots.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") 115 th.AssertNoErr(t, res.Err) 116 } 117 118 func TestUpdate(t *testing.T) { 119 th.SetupHTTP() 120 defer th.TeardownHTTP() 121 122 MockUpdateResponse(t) 123 124 var name = "snapshot-002" 125 var description = "Daily backup 002" 126 options := snapshots.UpdateOpts{Name: &name, Description: &description} 127 v, err := snapshots.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() 128 th.AssertNoErr(t, err) 129 th.CheckEquals(t, "snapshot-002", v.Name) 130 th.CheckEquals(t, "Daily backup 002", v.Description) 131 } 132 133 func TestResetStatus(t *testing.T) { 134 th.SetupHTTP() 135 defer th.TeardownHTTP() 136 137 MockResetStatusResponse(t) 138 139 opts := &snapshots.ResetStatusOpts{ 140 Status: "error", 141 } 142 res := snapshots.ResetStatus(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", opts) 143 th.AssertNoErr(t, res.Err) 144 } 145 146 func TestUpdateStatus(t *testing.T) { 147 th.SetupHTTP() 148 defer th.TeardownHTTP() 149 150 MockUpdateStatusResponse(t) 151 152 opts := &snapshots.UpdateStatusOpts{ 153 Status: "error", 154 Progress: "80%", 155 } 156 res := snapshots.UpdateStatus(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", opts) 157 th.AssertNoErr(t, res.Err) 158 } 159 160 func TestForceDelete(t *testing.T) { 161 th.SetupHTTP() 162 defer th.TeardownHTTP() 163 164 MockForceDeleteResponse(t) 165 166 res := snapshots.ForceDelete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") 167 th.AssertNoErr(t, res.Err) 168 }