github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v2/backups/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "encoding/json" 6 "testing" 7 "time" 8 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/blockstorage/v2/backups" 10 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 11 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 12 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 13 ) 14 15 func TestList(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 MockListResponse(t) 20 21 count := 0 22 23 err := backups.List(client.ServiceClient(), &backups.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 24 count++ 25 actual, err := backups.ExtractBackups(page) 26 if err != nil { 27 t.Errorf("Failed to extract backups: %v", err) 28 return false, err 29 } 30 31 expected := []backups.Backup{ 32 { 33 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 34 Name: "backup-001", 35 }, 36 { 37 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 38 Name: "backup-002", 39 }, 40 } 41 42 th.CheckDeepEquals(t, expected, actual) 43 44 return true, nil 45 }) 46 if err != nil { 47 t.Errorf("EachPage returned error: %s", err) 48 } 49 50 if count != 1 { 51 t.Errorf("Expected 1 page, got %d", count) 52 } 53 } 54 55 func TestListDetail(t *testing.T) { 56 th.SetupHTTP() 57 defer th.TeardownHTTP() 58 59 MockListDetailResponse(t) 60 61 count := 0 62 63 err := backups.ListDetail(client.ServiceClient(), &backups.ListDetailOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 64 count++ 65 actual, err := backups.ExtractBackups(page) 66 if err != nil { 67 t.Errorf("Failed to extract backups: %v", err) 68 return false, err 69 } 70 71 expected := []backups.Backup{ 72 { 73 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 74 Name: "backup-001", 75 VolumeID: "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 76 Status: "available", 77 Size: 30, 78 CreatedAt: time.Date(2017, 5, 30, 3, 35, 3, 0, time.UTC), 79 Description: "Daily Backup", 80 }, 81 { 82 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 83 Name: "backup-002", 84 VolumeID: "76b8950a-8594-4e5b-8dce-0dfa9c696358", 85 Status: "available", 86 Size: 25, 87 CreatedAt: time.Date(2017, 5, 30, 3, 35, 3, 0, time.UTC), 88 Description: "Weekly Backup", 89 }, 90 } 91 92 th.CheckDeepEquals(t, expected, actual) 93 94 return true, nil 95 }) 96 if err != nil { 97 t.Errorf("EachPage returned error: %s", err) 98 } 99 100 if count != 1 { 101 t.Errorf("Expected 1 page, got %d", count) 102 } 103 } 104 105 func TestGet(t *testing.T) { 106 th.SetupHTTP() 107 defer th.TeardownHTTP() 108 109 MockGetResponse(t) 110 111 v, err := backups.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 112 th.AssertNoErr(t, err) 113 114 th.AssertEquals(t, v.Name, "backup-001") 115 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 116 } 117 118 func TestCreate(t *testing.T) { 119 th.SetupHTTP() 120 defer th.TeardownHTTP() 121 122 MockCreateResponse(t) 123 124 options := backups.CreateOpts{VolumeID: "1234", Name: "backup-001"} 125 n, err := backups.Create(context.TODO(), client.ServiceClient(), options).Extract() 126 th.AssertNoErr(t, err) 127 128 th.AssertEquals(t, n.VolumeID, "1234") 129 th.AssertEquals(t, n.Name, "backup-001") 130 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 131 } 132 133 func TestRestore(t *testing.T) { 134 th.SetupHTTP() 135 defer th.TeardownHTTP() 136 137 MockRestoreResponse(t) 138 139 options := backups.RestoreOpts{VolumeID: "1234", Name: "vol-001"} 140 n, err := backups.RestoreFromBackup(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() 141 th.AssertNoErr(t, err) 142 143 th.AssertEquals(t, n.VolumeID, "1234") 144 th.AssertEquals(t, n.VolumeName, "vol-001") 145 th.AssertEquals(t, n.BackupID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 146 } 147 148 func TestDelete(t *testing.T) { 149 th.SetupHTTP() 150 defer th.TeardownHTTP() 151 152 MockDeleteResponse(t) 153 154 res := backups.Delete(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") 155 th.AssertNoErr(t, res.Err) 156 } 157 158 func TestExport(t *testing.T) { 159 th.SetupHTTP() 160 defer th.TeardownHTTP() 161 162 MockExportResponse(t) 163 164 n, err := backups.Export(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 165 th.AssertNoErr(t, err) 166 167 th.AssertEquals(t, n.BackupService, "cinder.backup.drivers.swift.SwiftBackupDriver") 168 th.AssertDeepEquals(t, n.BackupURL, backupURL) 169 170 tmp := backups.ImportBackup{} 171 err = json.Unmarshal(backupURL, &tmp) 172 th.AssertNoErr(t, err) 173 th.AssertDeepEquals(t, tmp, backupImport) 174 } 175 176 func TestImport(t *testing.T) { 177 th.SetupHTTP() 178 defer th.TeardownHTTP() 179 180 MockImportResponse(t) 181 182 options := backups.ImportOpts{ 183 BackupService: "cinder.backup.drivers.swift.SwiftBackupDriver", 184 BackupURL: backupURL, 185 } 186 n, err := backups.Import(context.TODO(), client.ServiceClient(), options).Extract() 187 th.AssertNoErr(t, err) 188 189 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 190 } 191 192 func TestResetStatus(t *testing.T) { 193 th.SetupHTTP() 194 defer th.TeardownHTTP() 195 196 MockResetStatusResponse(t) 197 198 opts := &backups.ResetStatusOpts{ 199 Status: "error", 200 } 201 res := backups.ResetStatus(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", opts) 202 th.AssertNoErr(t, res.Err) 203 } 204 205 func TestForceDelete(t *testing.T) { 206 th.SetupHTTP() 207 defer th.TeardownHTTP() 208 209 MockForceDeleteResponse(t) 210 211 res := backups.ForceDelete(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") 212 th.AssertNoErr(t, res.Err) 213 }