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

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk/openstack/blockstorage/v1/volumes"
     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  	volumes.List(client.ServiceClient(), &volumes.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    22  		count++
    23  		actual, err := volumes.ExtractVolumes(page)
    24  		if err != nil {
    25  			t.Errorf("Failed to extract volumes: %v", err)
    26  			return false, err
    27  		}
    28  
    29  		expected := []volumes.Volume{
    30  			{
    31  				ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
    32  				Name: "vol-001",
    33  			},
    34  			{
    35  				ID:   "96c3bda7-c82a-4f50-be73-ca7621794835",
    36  				Name: "vol-002",
    37  			},
    38  		}
    39  
    40  		th.CheckDeepEquals(t, expected, actual)
    41  
    42  		return true, nil
    43  	})
    44  
    45  	if count != 1 {
    46  		t.Errorf("Expected 1 page, got %d", count)
    47  	}
    48  }
    49  
    50  func TestListAll(t *testing.T) {
    51  	th.SetupHTTP()
    52  	defer th.TeardownHTTP()
    53  
    54  	MockListResponse(t)
    55  
    56  	allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages()
    57  	th.AssertNoErr(t, err)
    58  	actual, err := volumes.ExtractVolumes(allPages)
    59  	th.AssertNoErr(t, err)
    60  
    61  	expected := []volumes.Volume{
    62  		{
    63  			ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
    64  			Name: "vol-001",
    65  		},
    66  		{
    67  			ID:   "96c3bda7-c82a-4f50-be73-ca7621794835",
    68  			Name: "vol-002",
    69  		},
    70  	}
    71  
    72  	th.CheckDeepEquals(t, expected, actual)
    73  
    74  }
    75  
    76  func TestGet(t *testing.T) {
    77  	th.SetupHTTP()
    78  	defer th.TeardownHTTP()
    79  
    80  	MockGetResponse(t)
    81  
    82  	actual, err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
    83  	th.AssertNoErr(t, err)
    84  
    85  	expected := &volumes.Volume{
    86  		Status: "active",
    87  		Name:   "vol-001",
    88  		Attachments: []map[string]interface{}{
    89  			{
    90  				"attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
    91  				"id":            "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
    92  				"volume_id":     "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe",
    93  				"server_id":     "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
    94  				"host_name":     "mitaka",
    95  				"device":        "/",
    96  			},
    97  		},
    98  		AvailabilityZone: "us-east1",
    99  		Bootable:         "false",
   100  		CreatedAt:        time.Date(2012, 2, 14, 20, 53, 07, 0, time.UTC),
   101  		Description:      "Another volume.",
   102  		VolumeType:       "289da7f8-6440-407c-9fb4-7db01ec49164",
   103  		SnapshotID:       "",
   104  		SourceVolID:      "",
   105  		Metadata: map[string]string{
   106  			"contents": "junk",
   107  		},
   108  		ID:   "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
   109  		Size: 30,
   110  	}
   111  
   112  	th.AssertDeepEquals(t, expected, actual)
   113  }
   114  
   115  func TestCreate(t *testing.T) {
   116  	th.SetupHTTP()
   117  	defer th.TeardownHTTP()
   118  
   119  	MockCreateResponse(t)
   120  
   121  	options := &volumes.CreateOpts{
   122  		Size:             75,
   123  		AvailabilityZone: "us-east1",
   124  	}
   125  	n, err := volumes.Create(client.ServiceClient(), options).Extract()
   126  	th.AssertNoErr(t, err)
   127  
   128  	th.AssertEquals(t, n.Size, 4)
   129  	th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   130  }
   131  
   132  func TestDelete(t *testing.T) {
   133  	th.SetupHTTP()
   134  	defer th.TeardownHTTP()
   135  
   136  	MockDeleteResponse(t)
   137  
   138  	res := volumes.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   139  	th.AssertNoErr(t, res.Err)
   140  }
   141  
   142  func TestUpdate(t *testing.T) {
   143  	th.SetupHTTP()
   144  	defer th.TeardownHTTP()
   145  
   146  	MockUpdateResponse(t)
   147  
   148  	options := volumes.UpdateOpts{Name: "vol-002"}
   149  	v, err := volumes.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
   150  	th.AssertNoErr(t, err)
   151  	th.CheckEquals(t, "vol-002", v.Name)
   152  }