github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/backups/testing/requests_test.go (about)

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