PageRenderTime 89ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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