github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v2/volumes/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/blockstorage/v2/volumes"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/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  	err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, 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  	th.AssertNoErr(t, err)
    93  
    94  	if count != 1 {
    95  		t.Errorf("Expected 1 page, got %d", count)
    96  	}
    97  }
    98  
    99  func TestListAllWithExtensions(t *testing.T) {
   100  	th.SetupHTTP()
   101  	defer th.TeardownHTTP()
   102  
   103  	MockListResponse(t)
   104  
   105  	allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages(context.TODO())
   106  	th.AssertNoErr(t, err)
   107  
   108  	var actual []volumes.Volume
   109  	err = volumes.ExtractVolumesInto(allPages, &actual)
   110  	th.AssertNoErr(t, err)
   111  	th.AssertEquals(t, 2, len(actual))
   112  	th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", actual[0].TenantID)
   113  }
   114  
   115  func TestListAll(t *testing.T) {
   116  	th.SetupHTTP()
   117  	defer th.TeardownHTTP()
   118  
   119  	MockListResponse(t)
   120  
   121  	allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages(context.TODO())
   122  	th.AssertNoErr(t, err)
   123  	actual, err := volumes.ExtractVolumes(allPages)
   124  	th.AssertNoErr(t, err)
   125  
   126  	expected := []volumes.Volume{
   127  		{
   128  			ID:   "289da7f8-6440-407c-9fb4-7db01ec49164",
   129  			Name: "vol-001",
   130  			Attachments: []volumes.Attachment{{
   131  				ServerID:     "83ec2e3b-4321-422b-8706-a84185f52a0a",
   132  				AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a",
   133  				AttachedAt:   time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC),
   134  				HostName:     "foobar",
   135  				VolumeID:     "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   136  				Device:       "/dev/vdc",
   137  				ID:           "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
   138  			}},
   139  			AvailabilityZone:   "nova",
   140  			Bootable:           "false",
   141  			ConsistencyGroupID: "",
   142  			CreatedAt:          time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC),
   143  			Description:        "",
   144  			Encrypted:          false,
   145  			Metadata:           map[string]string{"foo": "bar"},
   146  			Multiattach:        false,
   147  			TenantID:           "304dc00909ac4d0da6c62d816bcb3459",
   148  			//ReplicationDriverData:     "",
   149  			//ReplicationExtendedStatus: "",
   150  			ReplicationStatus: "disabled",
   151  			Size:              75,
   152  			SnapshotID:        "",
   153  			SourceVolID:       "",
   154  			Status:            "available",
   155  			UserID:            "ff1ce52c03ab433aaba9108c2e3ef541",
   156  			VolumeType:        "lvmdriver-1",
   157  		},
   158  		{
   159  			ID:                 "96c3bda7-c82a-4f50-be73-ca7621794835",
   160  			Name:               "vol-002",
   161  			Attachments:        []volumes.Attachment{},
   162  			AvailabilityZone:   "nova",
   163  			Bootable:           "false",
   164  			ConsistencyGroupID: "",
   165  			CreatedAt:          time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC),
   166  			Description:        "",
   167  			Encrypted:          false,
   168  			Metadata:           map[string]string{},
   169  			Multiattach:        false,
   170  			TenantID:           "304dc00909ac4d0da6c62d816bcb3459",
   171  			//ReplicationDriverData:     "",
   172  			//ReplicationExtendedStatus: "",
   173  			ReplicationStatus: "disabled",
   174  			Size:              75,
   175  			SnapshotID:        "",
   176  			SourceVolID:       "",
   177  			Status:            "available",
   178  			UserID:            "ff1ce52c03ab433aaba9108c2e3ef541",
   179  			VolumeType:        "lvmdriver-1",
   180  		},
   181  	}
   182  
   183  	th.CheckDeepEquals(t, expected, actual)
   184  
   185  }
   186  
   187  func TestGet(t *testing.T) {
   188  	th.SetupHTTP()
   189  	defer th.TeardownHTTP()
   190  
   191  	MockGetResponse(t)
   192  
   193  	v, err := volumes.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
   194  	th.AssertNoErr(t, err)
   195  
   196  	th.AssertEquals(t, v.Name, "vol-001")
   197  	th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   198  }
   199  
   200  func TestCreate(t *testing.T) {
   201  	th.SetupHTTP()
   202  	defer th.TeardownHTTP()
   203  
   204  	MockCreateResponse(t)
   205  
   206  	options := &volumes.CreateOpts{Size: 75, Name: "vol-001"}
   207  	n, err := volumes.Create(context.TODO(), client.ServiceClient(), options, nil).Extract()
   208  	th.AssertNoErr(t, err)
   209  
   210  	th.AssertEquals(t, n.Size, 75)
   211  	th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   212  }
   213  
   214  func TestCreateSchedulerHints(t *testing.T) {
   215  	base := volumes.SchedulerHintOpts{
   216  		DifferentHost: []string{
   217  			"a0cf03a5-d921-4877-bb5c-86d26cf818e1",
   218  			"8c19174f-4220-44f0-824a-cd1eeef10287",
   219  		},
   220  		SameHost: []string{
   221  			"a0cf03a5-d921-4877-bb5c-86d26cf818e1",
   222  			"8c19174f-4220-44f0-824a-cd1eeef10287",
   223  		},
   224  		LocalToInstance:      "0ffb2c1b-d621-4fc1-9ae4-88d99c088ff6",
   225  		AdditionalProperties: map[string]any{"mark": "a0cf03a5-d921-4877-bb5c-86d26cf818e1"},
   226  	}
   227  	expected := `
   228  		{
   229  			"OS-SCH-HNT:scheduler_hints": {
   230  				"different_host": [
   231  					"a0cf03a5-d921-4877-bb5c-86d26cf818e1",
   232  					"8c19174f-4220-44f0-824a-cd1eeef10287"
   233  				],
   234  				"same_host": [
   235  					"a0cf03a5-d921-4877-bb5c-86d26cf818e1",
   236  					"8c19174f-4220-44f0-824a-cd1eeef10287"
   237  				],
   238  				"local_to_instance": "0ffb2c1b-d621-4fc1-9ae4-88d99c088ff6",
   239  				"mark": "a0cf03a5-d921-4877-bb5c-86d26cf818e1"
   240  			}
   241  		}
   242  	`
   243  	actual, err := base.ToSchedulerHintsMap()
   244  	th.AssertNoErr(t, err)
   245  	th.CheckJSONEquals(t, expected, actual)
   246  }
   247  
   248  func TestDelete(t *testing.T) {
   249  	th.SetupHTTP()
   250  	defer th.TeardownHTTP()
   251  
   252  	MockDeleteResponse(t)
   253  
   254  	res := volumes.Delete(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", volumes.DeleteOpts{})
   255  	th.AssertNoErr(t, res.Err)
   256  }
   257  
   258  func TestUpdate(t *testing.T) {
   259  	th.SetupHTTP()
   260  	defer th.TeardownHTTP()
   261  
   262  	MockUpdateResponse(t)
   263  
   264  	var name = "vol-002"
   265  	options := volumes.UpdateOpts{Name: &name}
   266  	v, err := volumes.Update(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
   267  	th.AssertNoErr(t, err)
   268  	th.CheckEquals(t, "vol-002", v.Name)
   269  }
   270  
   271  func TestGetWithExtensions(t *testing.T) {
   272  	th.SetupHTTP()
   273  	defer th.TeardownHTTP()
   274  
   275  	MockGetResponse(t)
   276  
   277  	var v volumes.Volume
   278  	err := volumes.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&v)
   279  	th.AssertNoErr(t, err)
   280  	th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", v.TenantID)
   281  
   282  	err = volumes.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(v)
   283  	if err == nil {
   284  		t.Errorf("Expected error when providing non-pointer struct")
   285  	}
   286  }
   287  
   288  func TestAttach(t *testing.T) {
   289  	th.SetupHTTP()
   290  	defer th.TeardownHTTP()
   291  
   292  	MockAttachResponse(t)
   293  
   294  	options := &volumes.AttachOpts{
   295  		MountPoint:   "/mnt",
   296  		Mode:         "rw",
   297  		InstanceUUID: "50902f4f-a974-46a0-85e9-7efc5e22dfdd",
   298  	}
   299  	err := volumes.Attach(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   300  	th.AssertNoErr(t, err)
   301  }
   302  
   303  func TestBeginDetaching(t *testing.T) {
   304  	th.SetupHTTP()
   305  	defer th.TeardownHTTP()
   306  
   307  	MockBeginDetachingResponse(t)
   308  
   309  	err := volumes.BeginDetaching(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").ExtractErr()
   310  	th.AssertNoErr(t, err)
   311  }
   312  
   313  func TestDetach(t *testing.T) {
   314  	th.SetupHTTP()
   315  	defer th.TeardownHTTP()
   316  
   317  	MockDetachResponse(t)
   318  
   319  	err := volumes.Detach(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", &volumes.DetachOpts{}).ExtractErr()
   320  	th.AssertNoErr(t, err)
   321  }
   322  
   323  func TestUploadImage(t *testing.T) {
   324  	th.SetupHTTP()
   325  	defer th.TeardownHTTP()
   326  	MockUploadImageResponse(t)
   327  	options := &volumes.UploadImageOpts{
   328  		ContainerFormat: "bare",
   329  		DiskFormat:      "raw",
   330  		ImageName:       "test",
   331  		Force:           true,
   332  	}
   333  
   334  	actual, err := volumes.UploadImage(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract()
   335  	th.AssertNoErr(t, err)
   336  
   337  	expected := volumes.VolumeImage{
   338  		VolumeID:        "cd281d77-8217-4830-be95-9528227c105c",
   339  		ContainerFormat: "bare",
   340  		DiskFormat:      "raw",
   341  		Description:     "",
   342  		ImageID:         "ecb92d98-de08-45db-8235-bbafe317269c",
   343  		ImageName:       "test",
   344  		Size:            5,
   345  		Status:          "uploading",
   346  		UpdatedAt:       time.Date(2017, 7, 17, 9, 29, 22, 0, time.UTC),
   347  		VolumeType: volumes.ImageVolumeType{
   348  			ID:          "b7133444-62f6-4433-8da3-70ac332229b7",
   349  			Name:        "basic.ru-2a",
   350  			Description: "",
   351  			IsPublic:    true,
   352  			ExtraSpecs:  map[string]any{"volume_backend_name": "basic.ru-2a"},
   353  			QosSpecsID:  "",
   354  			Deleted:     false,
   355  			DeletedAt:   time.Time{},
   356  			CreatedAt:   time.Date(2016, 5, 4, 8, 54, 14, 0, time.UTC),
   357  			UpdatedAt:   time.Date(2016, 5, 4, 9, 15, 33, 0, time.UTC),
   358  		},
   359  	}
   360  	th.AssertDeepEquals(t, expected, actual)
   361  }
   362  
   363  func TestReserve(t *testing.T) {
   364  	th.SetupHTTP()
   365  	defer th.TeardownHTTP()
   366  
   367  	MockReserveResponse(t)
   368  
   369  	err := volumes.Reserve(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").ExtractErr()
   370  	th.AssertNoErr(t, err)
   371  }
   372  
   373  func TestUnreserve(t *testing.T) {
   374  	th.SetupHTTP()
   375  	defer th.TeardownHTTP()
   376  
   377  	MockUnreserveResponse(t)
   378  
   379  	err := volumes.Unreserve(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").ExtractErr()
   380  	th.AssertNoErr(t, err)
   381  }
   382  
   383  func TestInitializeConnection(t *testing.T) {
   384  	th.SetupHTTP()
   385  	defer th.TeardownHTTP()
   386  
   387  	MockInitializeConnectionResponse(t)
   388  
   389  	options := &volumes.InitializeConnectionOpts{
   390  		IP:        "127.0.0.1",
   391  		Host:      "stack",
   392  		Initiator: "iqn.1994-05.com.redhat:17cf566367d2",
   393  		Multipath: gophercloud.Disabled,
   394  		Platform:  "x86_64",
   395  		OSType:    "linux2",
   396  	}
   397  	_, err := volumes.InitializeConnection(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract()
   398  	th.AssertNoErr(t, err)
   399  }
   400  
   401  func TestTerminateConnection(t *testing.T) {
   402  	th.SetupHTTP()
   403  	defer th.TeardownHTTP()
   404  
   405  	MockTerminateConnectionResponse(t)
   406  
   407  	options := &volumes.TerminateConnectionOpts{
   408  		IP:        "127.0.0.1",
   409  		Host:      "stack",
   410  		Initiator: "iqn.1994-05.com.redhat:17cf566367d2",
   411  		Multipath: gophercloud.Enabled,
   412  		Platform:  "x86_64",
   413  		OSType:    "linux2",
   414  	}
   415  	err := volumes.TerminateConnection(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   416  	th.AssertNoErr(t, err)
   417  }
   418  
   419  func TestExtendSize(t *testing.T) {
   420  	th.SetupHTTP()
   421  	defer th.TeardownHTTP()
   422  
   423  	MockExtendSizeResponse(t)
   424  
   425  	options := &volumes.ExtendSizeOpts{
   426  		NewSize: 3,
   427  	}
   428  
   429  	err := volumes.ExtendSize(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   430  	th.AssertNoErr(t, err)
   431  }
   432  
   433  func TestForceDelete(t *testing.T) {
   434  	th.SetupHTTP()
   435  	defer th.TeardownHTTP()
   436  
   437  	MockForceDeleteResponse(t)
   438  
   439  	res := volumes.ForceDelete(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
   440  	th.AssertNoErr(t, res.Err)
   441  }
   442  
   443  func TestSetImageMetadata(t *testing.T) {
   444  	th.SetupHTTP()
   445  	defer th.TeardownHTTP()
   446  
   447  	MockSetImageMetadataResponse(t)
   448  
   449  	options := &volumes.ImageMetadataOpts{
   450  		Metadata: map[string]string{
   451  			"label": "test",
   452  		},
   453  	}
   454  
   455  	err := volumes.SetImageMetadata(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   456  	th.AssertNoErr(t, err)
   457  }
   458  
   459  func TestSetBootable(t *testing.T) {
   460  	th.SetupHTTP()
   461  	defer th.TeardownHTTP()
   462  
   463  	MockSetBootableResponse(t)
   464  
   465  	options := volumes.BootableOpts{
   466  		Bootable: true,
   467  	}
   468  
   469  	err := volumes.SetBootable(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   470  	th.AssertNoErr(t, err)
   471  }
   472  
   473  func TestReImage(t *testing.T) {
   474  	th.SetupHTTP()
   475  	defer th.TeardownHTTP()
   476  
   477  	MockReImageResponse(t)
   478  
   479  	options := volumes.ReImageOpts{
   480  		ImageID:         "71543ced-a8af-45b6-a5c4-a46282108a90",
   481  		ReImageReserved: false,
   482  	}
   483  
   484  	err := volumes.ReImage(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   485  	th.AssertNoErr(t, err)
   486  }
   487  
   488  func TestChangeType(t *testing.T) {
   489  	th.SetupHTTP()
   490  	defer th.TeardownHTTP()
   491  
   492  	MockChangeTypeResponse(t)
   493  
   494  	options := &volumes.ChangeTypeOpts{
   495  		NewType:         "ssd",
   496  		MigrationPolicy: "on-demand",
   497  	}
   498  
   499  	err := volumes.ChangeType(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   500  	th.AssertNoErr(t, err)
   501  }
   502  
   503  func TestResetStatus(t *testing.T) {
   504  	th.SetupHTTP()
   505  	defer th.TeardownHTTP()
   506  
   507  	MockResetStatusResponse(t)
   508  
   509  	options := &volumes.ResetStatusOpts{
   510  		Status:          "error",
   511  		AttachStatus:    "detached",
   512  		MigrationStatus: "migrating",
   513  	}
   514  
   515  	err := volumes.ResetStatus(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr()
   516  	th.AssertNoErr(t, err)
   517  }