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