github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v2/snapshots/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk/openstack/blockstorage/v2/snapshots"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/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  }