PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/JamesClonk/machine
Go | 204 lines | 157 code | 46 blank | 1 comment | 9 complexity | acf133d9e6a0547c18f435774333bd3a 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 listServers(t *testing.T, client *gophercloud.ServiceClient) {
  84. t.Logf("> servers.List")
  85. count := 0
  86. err := servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
  87. count++
  88. t.Logf("--- Page %02d ---", count)
  89. s, err := servers.ExtractServers(page)
  90. th.AssertNoErr(t, err)
  91. for index, server := range s {
  92. logServer(t, &server, index)
  93. }
  94. return true, nil
  95. })
  96. th.AssertNoErr(t, err)
  97. }
  98. func changeAdminPassword(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  99. t.Logf("> servers.ChangeAdminPassword")
  100. original := server.AdminPass
  101. t.Logf("Changing server password.")
  102. err := servers.ChangeAdminPassword(client, server.ID, tools.MakeNewPassword(original)).ExtractErr()
  103. th.AssertNoErr(t, err)
  104. err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
  105. th.AssertNoErr(t, err)
  106. t.Logf("Password changed successfully.")
  107. }
  108. func rebootServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  109. t.Logf("> servers.Reboot")
  110. err := servers.Reboot(client, server.ID, os.HardReboot).ExtractErr()
  111. th.AssertNoErr(t, err)
  112. err = servers.WaitForStatus(client, server.ID, "ACTIVE", 300)
  113. th.AssertNoErr(t, err)
  114. t.Logf("Server successfully rebooted.")
  115. }
  116. func rebuildServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  117. t.Logf("> servers.Rebuild")
  118. options, err := optionsFromEnv()
  119. th.AssertNoErr(t, err)
  120. opts := servers.RebuildOpts{
  121. Name: tools.RandomString("RenamedGopher", 16),
  122. AdminPass: tools.MakeNewPassword(server.AdminPass),
  123. ImageID: options.imageID,
  124. DiskConfig: diskconfig.Manual,
  125. }
  126. after, err := servers.Rebuild(client, server.ID, opts).Extract()
  127. th.AssertNoErr(t, err)
  128. th.CheckEquals(t, after.ID, server.ID)
  129. err = servers.WaitForStatus(client, after.ID, "ACTIVE", 300)
  130. th.AssertNoErr(t, err)
  131. t.Logf("Server successfully rebuilt.")
  132. logServer(t, after, -1)
  133. }
  134. func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *os.Server) {
  135. t.Logf("> servers.Delete")
  136. res := servers.Delete(client, server.ID)
  137. th.AssertNoErr(t, res.Err)
  138. t.Logf("Server deleted successfully.")
  139. }
  140. func deleteServerKeyPair(t *testing.T, client *gophercloud.ServiceClient, k *oskey.KeyPair) {
  141. t.Logf("> keypairs.Delete")
  142. err := keypairs.Delete(client, k.Name).ExtractErr()
  143. th.AssertNoErr(t, err)
  144. t.Logf("Keypair deleted successfully.")
  145. }
  146. func TestServerOperations(t *testing.T) {
  147. client, err := newClient()
  148. th.AssertNoErr(t, err)
  149. kp := createServerKeyPair(t, client)
  150. defer deleteServerKeyPair(t, client, kp)
  151. server := createServer(t, client, kp.Name)
  152. defer deleteServer(t, client, server)
  153. getServer(t, client, server)
  154. listServers(t, client)
  155. changeAdminPassword(t, client, server)
  156. rebootServer(t, client, server)
  157. rebuildServer(t, client, server)
  158. }