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

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumehost"
     8  	"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumetenants"
     9  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
    10  	"github.com/gophercloud/gophercloud/pagination"
    11  	th "github.com/gophercloud/gophercloud/testhelper"
    12  	"github.com/gophercloud/gophercloud/testhelper/client"
    13  )
    14  
    15  func TestListWithExtensions(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	MockListResponse(t)
    20  
    21  	count := 0
    22  
    23  	volumes.List(client.ServiceClient(), &volumes.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    24  		count++
    25  		actual, err := volumes.ExtractVolumes(page)
    26  		if err != nil {
    27  			t.Errorf("Failed to extract volumes: %v", err)
    28  			return false, err
    29  		}
    30  
    31  		expected := []volumes.Volume{
    32  			{
    33  				ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
    34  				Name: "vol-001",
    35  				Attachments: []volumes.Attachment{{
    36  					ServerID:     "83ec2e3b-4321-422b-8706-a84185f52a0a",
    37  					AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a",
    38  					AttachedAt:   time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC),
    39  					HostName:     "foobar",
    40  					VolumeID:     "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
    41  					Device:       "/dev/vdc",
    42  					ID:           "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
    43  				}},
    44  				AvailabilityZone:   "nova",
    45  				Bootable:           "false",
    46  				ConsistencyGroupID: "",
    47  				CreatedAt:          time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC),
    48  				Description:        "",
    49  				Encrypted:          false,
    50  				Metadata:           map[string]string{"foo": "bar"},
    51  				Multiattach:        false,
    52  				//TenantID:                  "304dc00909ac4d0da6c62d816bcb3459",
    53  				//ReplicationDriverData:     "",
    54  				//ReplicationExtendedStatus: "",
    55  				ReplicationStatus: "disabled",
    56  				Size:              75,
    57  				SnapshotID:        "",
    58  				SourceVolID:       "",
    59  				Status:            "available",
    60  				UserID:            "ff1ce52c03ab433aaba9108c2e3ef541",
    61  				VolumeType:        "lvmdriver-1",
    62  			},
    63  			{
    64  				ID:                 "96c3bda7-c82a-4f50-be73-ca7621794835",
    65  				Name:               "vol-002",
    66  				Attachments:        []volumes.Attachment{},
    67  				AvailabilityZone:   "nova",
    68  				Bootable:           "false",
    69  				ConsistencyGroupID: "",
    70  				CreatedAt:          time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC),
    71  				Description:        "",
    72  				Encrypted:          false,
    73  				Metadata:           map[string]string{},
    74  				Multiattach:        false,
    75  				//TenantID:                  "304dc00909ac4d0da6c62d816bcb3459",
    76  				//ReplicationDriverData:     "",
    77  				//ReplicationExtendedStatus: "",
    78  				ReplicationStatus: "disabled",
    79  				Size:              75,
    80  				SnapshotID:        "",
    81  				SourceVolID:       "",
    82  				Status:            "available",
    83  				UserID:            "ff1ce52c03ab433aaba9108c2e3ef541",
    84  				VolumeType:        "lvmdriver-1",
    85  			},
    86  		}
    87  
    88  		th.CheckDeepEquals(t, expected, actual)
    89  
    90  		return true, nil
    91  	})
    92  
    93  	if count != 1 {
    94  		t.Errorf("Expected 1 page, got %d", count)
    95  	}
    96  }
    97  
    98  func TestListAllWithExtensions(t *testing.T) {
    99  	th.SetupHTTP()
   100  	defer th.TeardownHTTP()
   101  
   102  	MockListResponse(t)
   103  
   104  	type VolumeWithExt struct {
   105  		volumes.Volume
   106  		volumetenants.VolumeTenantExt
   107  		volumehost.VolumeHostExt
   108  	}
   109  
   110  	allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages()
   111  	th.AssertNoErr(t, err)
   112  
   113  	var actual []VolumeWithExt
   114  	err = volumes.ExtractVolumesInto(allPages, &actual)
   115  	th.AssertNoErr(t, err)
   116  	th.AssertEquals(t, 2, len(actual))
   117  	th.AssertEquals(t, "host-001", actual[0].Host)
   118  	th.AssertEquals(t, "", actual[1].Host)
   119  	th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", actual[0].TenantID)
   120  }
   121  
   122  func TestListAll(t *testing.T) {
   123  	th.SetupHTTP()
   124  	defer th.TeardownHTTP()
   125  
   126  	MockListResponse(t)
   127  
   128  	allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages()
   129  	th.AssertNoErr(t, err)
   130  	actual, err := volumes.ExtractVolumes(allPages)
   131  	th.AssertNoErr(t, err)
   132  
   133  	expected := []volumes.Volume{
   134  		{
   135  			ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
   136  			Name: "vol-001",
   137  			Attachments: []volumes.Attachment{{
   138  				ServerID:     "83ec2e3b-4321-422b-8706-a84185f52a0a",
   139  				AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a",
   140  				AttachedAt:   time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC),
   141  				HostName:     "foobar",
   142  				VolumeID:     "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   143  				Device:       "/dev/vdc",
   144  				ID:           "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   145  			}},
   146  			AvailabilityZone:   "nova",
   147  			Bootable:           "false",
   148  			ConsistencyGroupID: "",
   149  			CreatedAt:          time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC),
   150  			Description:        "",
   151  			Encrypted:          false,
   152  			Metadata:           map[string]string{"foo": "bar"},
   153  			Multiattach:        false,
   154  			//TenantID:                  "304dc00909ac4d0da6c62d816bcb3459",
   155  			//ReplicationDriverData:     "",
   156  			//ReplicationExtendedStatus: "",
   157  			ReplicationStatus: "disabled",
   158  			Size:              75,
   159  			SnapshotID:        "",
   160  			SourceVolID:       "",
   161  			Status:            "available",
   162  			UserID:            "ff1ce52c03ab433aaba9108c2e3ef541",
   163  			VolumeType:        "lvmdriver-1",
   164  		},
   165  		{
   166  			ID:                 "96c3bda7-c82a-4f50-be73-ca7621794835",
   167  			Name:               "vol-002",
   168  			Attachments:        []volumes.Attachment{},
   169  			AvailabilityZone:   "nova",
   170  			Bootable:           "false",
   171  			ConsistencyGroupID: "",
   172  			CreatedAt:          time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC),
   173  			Description:        "",
   174  			Encrypted:          false,
   175  			Metadata:           map[string]string{},
   176  			Multiattach:        false,
   177  			//TenantID:                  "304dc00909ac4d0da6c62d816bcb3459",
   178  			//ReplicationDriverData:     "",
   179  			//ReplicationExtendedStatus: "",
   180  			ReplicationStatus: "disabled",
   181  			Size:              75,
   182  			SnapshotID:        "",
   183  			SourceVolID:       "",
   184  			Status:            "available",
   185  			UserID:            "ff1ce52c03ab433aaba9108c2e3ef541",
   186  			VolumeType:        "lvmdriver-1",
   187  		},
   188  	}
   189  
   190  	th.CheckDeepEquals(t, expected, actual)
   191  
   192  }
   193  
   194  func TestGet(t *testing.T) {
   195  	th.SetupHTTP()
   196  	defer th.TeardownHTTP()
   197  
   198  	MockGetResponse(t)
   199  
   200  	v, err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
   201  	th.AssertNoErr(t, err)
   202  
   203  	th.AssertEquals(t, v.Name, "vol-001")
   204  	th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   205  }
   206  
   207  func TestCreate(t *testing.T) {
   208  	th.SetupHTTP()
   209  	defer th.TeardownHTTP()
   210  
   211  	MockCreateResponse(t)
   212  
   213  	options := &volumes.CreateOpts{Size: 75, Name: "vol-001"}
   214  	n, err := volumes.Create(client.ServiceClient(), options).Extract()
   215  	th.AssertNoErr(t, err)
   216  
   217  	th.AssertEquals(t, n.Size, 75)
   218  	th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   219  }
   220  
   221  func TestDelete(t *testing.T) {
   222  	th.SetupHTTP()
   223  	defer th.TeardownHTTP()
   224  
   225  	MockDeleteResponse(t)
   226  
   227  	res := volumes.Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", volumes.DeleteOpts{})
   228  	th.AssertNoErr(t, res.Err)
   229  }
   230  
   231  func TestUpdate(t *testing.T) {
   232  	th.SetupHTTP()
   233  	defer th.TeardownHTTP()
   234  
   235  	MockUpdateResponse(t)
   236  
   237  	var name = "vol-002"
   238  	options := volumes.UpdateOpts{Name: &name}
   239  	v, err := volumes.Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
   240  	th.AssertNoErr(t, err)
   241  	th.CheckEquals(t, "vol-002", v.Name)
   242  }
   243  
   244  func TestGetWithExtensions(t *testing.T) {
   245  	th.SetupHTTP()
   246  	defer th.TeardownHTTP()
   247  
   248  	MockGetResponse(t)
   249  
   250  	var s struct {
   251  		volumes.Volume
   252  		volumetenants.VolumeTenantExt
   253  	}
   254  	err := volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&s)
   255  	th.AssertNoErr(t, err)
   256  	th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", s.TenantID)
   257  	th.AssertEquals(t, "centos", s.Volume.VolumeImageMetadata["image_name"])
   258  
   259  	err = volumes.Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(s)
   260  	if err == nil {
   261  		t.Errorf("Expected error when providing non-pointer struct")
   262  	}
   263  }
   264  
   265  func TestCreateFromBackup(t *testing.T) {
   266  	th.SetupHTTP()
   267  	defer th.TeardownHTTP()
   268  
   269  	MockCreateVolumeFromBackupResponse(t)
   270  
   271  	options := volumes.CreateOpts{
   272  		Name:     "vol-001",
   273  		BackupID: "20c792f0-bb03-434f-b653-06ef238e337e",
   274  	}
   275  
   276  	v, err := volumes.Create(client.ServiceClient(), options).Extract()
   277  
   278  	th.AssertNoErr(t, err)
   279  	th.AssertEquals(t, v.Size, 30)
   280  	th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   281  	th.AssertEquals(t, *v.BackupID, "20c792f0-bb03-434f-b653-06ef238e337e")
   282  }