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

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk/openstack/evs/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 TestCreate(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	MockCreateResponse(t)
    18  
    19  	options := snapshots.CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
    20  	n, err := snapshots.Create(client.ServiceClient(), options).Extract()
    21  	th.AssertNoErr(t, err)
    22  
    23  	th.AssertEquals(t, n.VolumeID, "1234")
    24  	th.AssertEquals(t, n.Name, "snapshot-001")
    25  	th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
    26  }
    27  
    28  func TestGet(t *testing.T) {
    29  	th.SetupHTTP()
    30  	defer th.TeardownHTTP()
    31  
    32  	MockGetResponse(t)
    33  
    34  	v, err := snapshots.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
    35  	th.AssertNoErr(t, err)
    36  
    37  	th.AssertEquals(t, v.Name, "snapshot-001")
    38  	th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
    39  }
    40  
    41  func TestUpdate(t *testing.T) {
    42  	th.SetupHTTP()
    43  	defer th.TeardownHTTP()
    44  
    45  	MockUpdateResponse(t)
    46  
    47  	options := snapshots.UpdateOpts{
    48  		Name:        "snapshot-001-update",
    49  		Description: "Weekly backup",
    50  	}
    51  
    52  	v, err := snapshots.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
    53  
    54  	th.AssertNoErr(t, err)
    55  	th.AssertEquals(t, v.Name, "snapshot-001-update")
    56  	th.AssertEquals(t, v.Description, "Weekly backup")
    57  	th.AssertEquals(t, v.UpdatedAt, time.Date(2020, 3, 27, 15, 55, 3, 0, time.UTC))
    58  }
    59  
    60  func TestList(t *testing.T) {
    61  	th.SetupHTTP()
    62  	defer th.TeardownHTTP()
    63  
    64  	MockListResponse(t)
    65  
    66  	count := 0
    67  
    68  	snapshots.List(client.ServiceClient(), &snapshots.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    69  		count++
    70  		actual, err := snapshots.ExtractSnapshots(page)
    71  		if err != nil {
    72  			t.Errorf("Failed to extract snapshots: %v", err)
    73  			return false, err
    74  		}
    75  
    76  		expected := []snapshots.Snapshot{
    77  			{
    78  				ID:          "289da7f8-6440-407c-9fb4-7db01ec49164",
    79  				Name:        "snapshot-001",
    80  				VolumeID:    "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
    81  				Status:      "available",
    82  				Size:        30,
    83  				CreatedAt:   time.Date(2020, 3, 27, 15, 35, 3, 0, time.UTC),
    84  				Description: "Daily Backup",
    85  			},
    86  			{
    87  				ID:          "96c3bda7-c82a-4f50-be73-ca7621794835",
    88  				Name:        "snapshot-002",
    89  				VolumeID:    "76b8950a-8594-4e5b-8dce-0dfa9c696358",
    90  				Status:      "available",
    91  				Size:        25,
    92  				CreatedAt:   time.Date(2020, 3, 27, 15, 35, 3, 0, time.UTC),
    93  				Description: "Weekly Backup",
    94  			},
    95  		}
    96  
    97  		th.CheckDeepEquals(t, expected, actual)
    98  
    99  		return true, nil
   100  	})
   101  
   102  	if count != 1 {
   103  		t.Errorf("Expected 1 page, got %d", count)
   104  	}
   105  }
   106  
   107  func TestDelete(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  
   111  	MockDeleteResponse(t)
   112  
   113  	res := snapshots.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   114  	th.AssertNoErr(t, res.Err)
   115  }