PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/rackspace/compute/v2/volumeattach_test.go

https://gitlab.com/Red54/machine
Go | 130 lines | 106 code | 23 blank | 1 comment | 19 complexity | 1fb98dd1d6cfd2e22eea52007de326ac MD5 | raw file
  1. // +build acceptance compute servers
  2. package v2
  3. import (
  4. "os"
  5. "testing"
  6. "github.com/rackspace/gophercloud"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. "github.com/rackspace/gophercloud/openstack"
  9. osVolumes "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
  10. osVolumeAttach "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach"
  11. osServers "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  12. "github.com/rackspace/gophercloud/rackspace"
  13. "github.com/rackspace/gophercloud/rackspace/blockstorage/v1/volumes"
  14. "github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
  15. "github.com/rackspace/gophercloud/rackspace/compute/v2/volumeattach"
  16. th "github.com/rackspace/gophercloud/testhelper"
  17. )
  18. func newBlockClient(t *testing.T) (*gophercloud.ServiceClient, error) {
  19. ao, err := rackspace.AuthOptionsFromEnv()
  20. th.AssertNoErr(t, err)
  21. client, err := rackspace.AuthenticatedClient(ao)
  22. th.AssertNoErr(t, err)
  23. return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
  24. Region: os.Getenv("RS_REGION_NAME"),
  25. })
  26. }
  27. func createVAServer(t *testing.T, computeClient *gophercloud.ServiceClient, choices *serverOpts) (*osServers.Server, error) {
  28. if testing.Short() {
  29. t.Skip("Skipping test that requires server creation in short mode.")
  30. }
  31. name := tools.RandomString("ACPTTEST", 16)
  32. t.Logf("Attempting to create server: %s\n", name)
  33. pwd := tools.MakeNewPassword("")
  34. server, err := servers.Create(computeClient, osServers.CreateOpts{
  35. Name: name,
  36. FlavorRef: choices.flavorID,
  37. ImageRef: choices.imageID,
  38. AdminPass: pwd,
  39. }).Extract()
  40. if err != nil {
  41. t.Fatalf("Unable to create server: %v", err)
  42. }
  43. th.AssertEquals(t, pwd, server.AdminPass)
  44. return server, err
  45. }
  46. func createVAVolume(t *testing.T, blockClient *gophercloud.ServiceClient) (*volumes.Volume, error) {
  47. volume, err := volumes.Create(blockClient, &osVolumes.CreateOpts{
  48. Size: 80,
  49. Name: "gophercloud-test-volume",
  50. }).Extract()
  51. th.AssertNoErr(t, err)
  52. defer func() {
  53. err = osVolumes.WaitForStatus(blockClient, volume.ID, "available", 60)
  54. th.AssertNoErr(t, err)
  55. }()
  56. return volume, err
  57. }
  58. func createVolumeAttachment(t *testing.T, computeClient *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, serverID string, volumeID string) {
  59. va, err := volumeattach.Create(computeClient, serverID, &osVolumeAttach.CreateOpts{
  60. VolumeID: volumeID,
  61. }).Extract()
  62. th.AssertNoErr(t, err)
  63. defer func() {
  64. err = osVolumes.WaitForStatus(blockClient, volumeID, "in-use", 60)
  65. th.AssertNoErr(t, err)
  66. err = volumeattach.Delete(computeClient, serverID, va.ID).ExtractErr()
  67. th.AssertNoErr(t, err)
  68. err = osVolumes.WaitForStatus(blockClient, volumeID, "available", 60)
  69. th.AssertNoErr(t, err)
  70. }()
  71. t.Logf("Attached volume to server: %+v", va)
  72. }
  73. func TestAttachVolume(t *testing.T) {
  74. choices, err := optionsFromEnv()
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. computeClient, err := newClient()
  79. if err != nil {
  80. t.Fatalf("Unable to create a compute client: %v", err)
  81. }
  82. blockClient, err := newBlockClient(t)
  83. if err != nil {
  84. t.Fatalf("Unable to create a blockstorage client: %v", err)
  85. }
  86. server, err := createVAServer(t, computeClient, choices)
  87. if err != nil {
  88. t.Fatalf("Unable to create server: %v", err)
  89. }
  90. defer func() {
  91. servers.Delete(computeClient, server.ID)
  92. t.Logf("Server deleted.")
  93. }()
  94. if err = osServers.WaitForStatus(computeClient, server.ID, "ACTIVE", 300); err != nil {
  95. t.Fatalf("Unable to wait for server: %v", err)
  96. }
  97. volume, err := createVAVolume(t, blockClient)
  98. if err != nil {
  99. t.Fatalf("Unable to create volume: %v", err)
  100. }
  101. defer func() {
  102. err = volumes.Delete(blockClient, volume.ID).ExtractErr()
  103. th.AssertNoErr(t, err)
  104. t.Logf("Volume deleted.")
  105. }()
  106. createVolumeAttachment(t, computeClient, blockClient, server.ID, volume.ID)
  107. }