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