PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Red54/machine
Go | 217 lines | 168 code | 48 blank | 1 comment | 9 complexity | 3c74f88f18ce52b24e77b7c3584ed9c7 MD5 | raw file
  1. // +build acceptance
  2. package v2
  3. import (
  4. "testing"
  5. "github.com/rackspace/gophercloud"
  6. "github.com/rackspace/gophercloud/acceptance/tools"
  7. "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig"
  8. oskey "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
  9. os "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  10. "github.com/rackspace/gophercloud/pagination"
  11. "github.com/rackspace/gophercloud/rackspace/compute/v2/keypairs"
  12. "github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
  13. th "github.com/rackspace/gophercloud/testhelper"
  14. )
  15. func createServerKeyPair(t *testing.T, client *gophercloud.ServiceClient) *oskey.KeyPair {
  16. name := tools.RandomString("importedkey-", 8)
  17. pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDlIQ3r+zd97kb9Hzmujd3V6pbO53eb3Go4q2E8iqVGWQfZTrFdL9KACJnqJIm9HmncfRkUTxE37hqeGCCv8uD+ZPmPiZG2E60OX1mGDjbbzAyReRwYWXgXHopggZTLak5k4mwZYaxwaufbVBDRn847e01lZnaXaszEToLM37NLw+uz29sl3TwYy2R0RGHPwPc160aWmdLjSyd1Nd4c9pvvOP/EoEuBjIC6NJJwg2Rvg9sjjx9jYj0QUgc8CqKLN25oMZ69kNJzlFylKRUoeeVr89txlR59yehJWk6Uw6lYFTdJmcmQOFVAJ12RMmS1hLWCM8UzAgtw+EDa0eqBxBDl smash@winter"
  18. k, err := keypairs.Create(client, oskey.CreateOpts{
  19. Name: name,
  20. PublicKey: pubkey,
  21. }).Extract()
  22. th.AssertNoErr(t, err)
  23. return k
  24. }
  25. func createServer(t *testing.T, client *gophercloud.ServiceClient, keyName string) *os.Server {
  26. if testing.Short() {
  27. t.Skip("Skipping test that requires server creation in short mode.")
  28. }
  29. options, err := optionsFromEnv()
  30. th.AssertNoErr(t, err)
  31. name := tools.RandomString("Gophercloud-", 8)
  32. pwd := tools.MakeNewPassword("")
  33. opts := &servers.CreateOpts{
  34. Name: name,
  35. ImageRef: options.imageID,
  36. FlavorRef: options.flavorID,
  37. DiskConfig: diskconfig.Manual,
  38. AdminPass: pwd,
  39. }
  40. if keyName != "" {
  41. opts.KeyPair = keyName
  42. }
  43. t.Logf("Creating server [%s].", name)
  44. s, err := servers.Create(client, opts).Extract()
  45. th.AssertNoErr(t, err)
  46. t.Logf("Creating server.")
  47. err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
  48. th.AssertNoErr(t, err)
  49. t.Logf("Server created successfully.")
  50. th.CheckEquals(t, pwd, s.AdminPass)
  51. return s
  52. }
  53. func logServer(t *testing.T, server *os.Server, index int) {
  54. if index == -1 {
  55. t.Logf(" id=[%s]", server.ID)
  56. } else {
  57. t.Logf("[%02d] id=[%s]", index, server.ID)
  58. }
  59. t.Logf(" name=[%s]", server.Name)
  60. t.Logf(" tenant ID=[%s]", server.TenantID)
  61. t.Logf(" user ID=[%s]", server.UserID)
  62. t.Logf(" updated=[%s]", server.Updated)
  63. t.Logf(" created=[%s]", server.Created)
  64. t.Logf(" host ID=[%s]", server.HostID)
  65. t.Logf(" access IPv4=[%s]", server.AccessIPv4)
  66. t.Logf(" access IPv6=[%s]", server.AccessIPv6)
  67. t.Logf(" image=[%v]", server.Image)
  68. t.Logf(" flavor=[%v]", server.Flavor)
  69. t.Logf(" addresses=[%v]", server.Addresses)
  70. t.Logf(" metadata=[%v]", server.Metadata)
  71. t.Logf(" links=[%v]", server.Links)
  72. t.Logf(" keyname=[%s]", server.KeyName)
  73. t.Logf(" admin password=[%s]", server.AdminPass)
  74. t.Logf(" status=[%s]", server.Status)
  75. t.Logf(" progress=[%d]", server.Progress)
  76. }
  77. func getServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  78. t.Logf("> servers.Get")
  79. details, err := servers.Get(client, server.ID).Extract()
  80. th.AssertNoErr(t, err)
  81. logServer(t, details, -1)
  82. }
  83. func updateServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  84. t.Logf("> servers.Get")
  85. opts := os.UpdateOpts{
  86. Name: "updated-server",
  87. }
  88. updatedServer, err := servers.Update(client, server.ID, opts).Extract()
  89. th.AssertNoErr(t, err)
  90. th.AssertEquals(t, "updated-server", updatedServer.Name)
  91. logServer(t, updatedServer, -1)
  92. }
  93. func listServers(t *testing.T, client *gophercloud.ServiceClient) {
  94. t.Logf("> servers.List")
  95. count := 0
  96. err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
  97. count++
  98. t.Logf("--- Page %02d ---", count)
  99. s, err := servers.ExtractServers(page)
  100. th.AssertNoErr(t, err)
  101. for index, server := range s {
  102. logServer(t, &server, index)
  103. }
  104. return true, nil
  105. })
  106. th.AssertNoErr(t, err)
  107. }
  108. func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  109. t.Logf("> servers.ChangeAdminPassword")
  110. original := server.AdminPass
  111. t.Logf("Changing server password.")
  112. err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).ExtractErr()
  113. th.AssertNoErr(t, err)
  114. err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
  115. th.AssertNoErr(t, err)
  116. t.Logf("Password changed successfully.")
  117. }
  118. func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  119. t.Logf("> servers.Reboot")
  120. err := servers.Reboot(client, server.ID, os.HardReboot).ExtractErr()
  121. th.AssertNoErr(t, err)
  122. err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
  123. th.AssertNoErr(t, err)
  124. t.Logf("Server successfully rebooted.")
  125. }
  126. func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  127. t.Logf("> servers.Rebuild")
  128. options, err := optionsFromEnv()
  129. th.AssertNoErr(t, err)
  130. opts := servers.RebuildOpts{
  131. Name: tools.RandomString("RenamedGopher", 16),
  132. AdminPass: tools.MakeNewPassword(server.AdminPass),
  133. ImageID: options.imageID,
  134. DiskConfig: diskconfig.Manual,
  135. }
  136. after, err := servers.Rebuild(client, server.ID, opts).Extract()
  137. th.AssertNoErr(t, err)
  138. th.CheckEquals(t, after.ID, server.ID)
  139. err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
  140. th.AssertNoErr(t, err)
  141. t.Logf("Server successfully rebuilt.")
  142. logServer(t, after, -1)
  143. }
  144. func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  145. t.Logf("> servers.Delete")
  146. res := servers.Delete(client, server.ID)
  147. th.AssertNoErr(t, res.Err)
  148. t.Logf("Server deleted successfully.")
  149. }
  150. func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) {
  151. t.Logf("> keypairs.Delete")
  152. err := keypairs.Delete(client, k.Name).ExtractErr()
  153. th.AssertNoErr(t, err)
  154. t.Logf("Keypair deleted successfully.")
  155. }
  156. func TestServerOperations(t *testing.T) {
  157. client, err := newClient()
  158. th.AssertNoErr(t, err)
  159. kp := createServerKeyPair(t, client)
  160. defer deleteServerKeyPair(t, client, kp)
  161. server := createServer(t, client, kp.Name)
  162. defer deleteServer(t, client, server)
  163. getServer(t, client, server)
  164. updateServer(t, client, server)
  165. listServers(t, client)
  166. changeAdminPassword(t, client, server)
  167. rebootServer(t, client, server)
  168. rebuildServer(t, client, server)
  169. }