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

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

https://gitlab.com/JamesClonk/machine
Go | 450 lines | 354 code | 95 blank | 1 comment | 129 complexity | 893dc6c41449d2bd60aa886f1a339384 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/compute/v2/servers"
  10. "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
  11. "github.com/rackspace/gophercloud/pagination"
  12. th "github.com/rackspace/gophercloud/testhelper"
  13. )
  14. func TestListServers(t *testing.T) {
  15. client, err := newClient()
  16. if err != nil {
  17. t.Fatalf("Unable to create a compute client: %v", err)
  18. }
  19. t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6")
  20. pager := servers.List(client, servers.ListOpts{})
  21. count, pages := 0, 0
  22. pager.EachPage(func(page pagination.Page) (bool, error) {
  23. pages++
  24. t.Logf("---")
  25. servers, err := servers.ExtractServers(page)
  26. if err != nil {
  27. return false, err
  28. }
  29. for _, s := range servers {
  30. t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6)
  31. count++
  32. }
  33. return true, nil
  34. })
  35. t.Logf("--------\n%d servers listed on %d pages.\n", count, pages)
  36. }
  37. func networkingClient() (*gophercloud.ServiceClient, error) {
  38. opts, err := openstack.AuthOptionsFromEnv()
  39. if err != nil {
  40. return nil, err
  41. }
  42. provider, err := openstack.AuthenticatedClient(opts)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
  47. Name: "neutron",
  48. Region: os.Getenv("OS_REGION_NAME"),
  49. })
  50. }
  51. func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
  52. if testing.Short() {
  53. t.Skip("Skipping test that requires server creation in short mode.")
  54. }
  55. var network networks.Network
  56. networkingClient, err := networkingClient()
  57. if err != nil {
  58. t.Fatalf("Unable to create a networking client: %v", err)
  59. }
  60. pager := networks.List(networkingClient, networks.ListOpts{Name: "public", Limit: 1})
  61. pager.EachPage(func(page pagination.Page) (bool, error) {
  62. networks, err := networks.ExtractNetworks(page)
  63. if err != nil {
  64. t.Errorf("Failed to extract networks: %v", err)
  65. return false, err
  66. }
  67. if len(networks) == 0 {
  68. t.Fatalf("No networks to attach to server")
  69. return false, err
  70. }
  71. network = networks[0]
  72. return false, nil
  73. })
  74. name := tools.RandomString("ACPTTEST", 16)
  75. t.Logf("Attempting to create server: %s\n", name)
  76. pwd := tools.MakeNewPassword("")
  77. server, err := servers.Create(client, servers.CreateOpts{
  78. Name: name,
  79. FlavorRef: choices.FlavorID,
  80. ImageRef: choices.ImageID,
  81. Networks: []servers.Network{
  82. servers.Network{UUID: network.ID},
  83. },
  84. AdminPass: pwd,
  85. }).Extract()
  86. if err != nil {
  87. t.Fatalf("Unable to create server: %v", err)
  88. }
  89. th.AssertEquals(t, pwd, server.AdminPass)
  90. return server, err
  91. }
  92. func TestCreateDestroyServer(t *testing.T) {
  93. choices, err := ComputeChoicesFromEnv()
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. client, err := newClient()
  98. if err != nil {
  99. t.Fatalf("Unable to create a compute client: %v", err)
  100. }
  101. server, err := createServer(t, client, choices)
  102. if err != nil {
  103. t.Fatalf("Unable to create server: %v", err)
  104. }
  105. defer func() {
  106. servers.Delete(client, server.ID)
  107. t.Logf("Server deleted.")
  108. }()
  109. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  110. t.Fatalf("Unable to wait for server: %v", err)
  111. }
  112. }
  113. func TestUpdateServer(t *testing.T) {
  114. client, err := newClient()
  115. if err != nil {
  116. t.Fatalf("Unable to create a compute client: %v", err)
  117. }
  118. choices, err := ComputeChoicesFromEnv()
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. server, err := createServer(t, client, choices)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. defer servers.Delete(client, server.ID)
  127. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  128. t.Fatal(err)
  129. }
  130. alternateName := tools.RandomString("ACPTTEST", 16)
  131. for alternateName == server.Name {
  132. alternateName = tools.RandomString("ACPTTEST", 16)
  133. }
  134. t.Logf("Attempting to rename the server to %s.", alternateName)
  135. updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
  136. if err != nil {
  137. t.Fatalf("Unable to rename server: %v", err)
  138. }
  139. if updated.ID != server.ID {
  140. t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
  141. }
  142. err = tools.WaitFor(func() (bool, error) {
  143. latest, err := servers.Get(client, updated.ID).Extract()
  144. if err != nil {
  145. return false, err
  146. }
  147. return latest.Name == alternateName, nil
  148. })
  149. }
  150. func TestActionChangeAdminPassword(t *testing.T) {
  151. t.Parallel()
  152. client, err := newClient()
  153. if err != nil {
  154. t.Fatalf("Unable to create a compute client: %v", err)
  155. }
  156. choices, err := ComputeChoicesFromEnv()
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. server, err := createServer(t, client, choices)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. defer servers.Delete(client, server.ID)
  165. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  166. t.Fatal(err)
  167. }
  168. randomPassword := tools.MakeNewPassword(server.AdminPass)
  169. res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
  170. if res.Err != nil {
  171. t.Fatal(err)
  172. }
  173. if err = waitForStatus(client, server, "PASSWORD"); err != nil {
  174. t.Fatal(err)
  175. }
  176. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  177. t.Fatal(err)
  178. }
  179. }
  180. func TestActionReboot(t *testing.T) {
  181. t.Parallel()
  182. client, err := newClient()
  183. if err != nil {
  184. t.Fatalf("Unable to create a compute client: %v", err)
  185. }
  186. choices, err := ComputeChoicesFromEnv()
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. server, err := createServer(t, client, choices)
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. defer servers.Delete(client, server.ID)
  195. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  196. t.Fatal(err)
  197. }
  198. res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
  199. if res.Err == nil {
  200. t.Fatal("Expected the SDK to provide an ArgumentError here")
  201. }
  202. t.Logf("Attempting reboot of server %s", server.ID)
  203. res = servers.Reboot(client, server.ID, servers.OSReboot)
  204. if res.Err != nil {
  205. t.Fatalf("Unable to reboot server: %v", err)
  206. }
  207. if err = waitForStatus(client, server, "REBOOT"); err != nil {
  208. t.Fatal(err)
  209. }
  210. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  211. t.Fatal(err)
  212. }
  213. }
  214. func TestActionRebuild(t *testing.T) {
  215. t.Parallel()
  216. client, err := newClient()
  217. if err != nil {
  218. t.Fatalf("Unable to create a compute client: %v", err)
  219. }
  220. choices, err := ComputeChoicesFromEnv()
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. server, err := createServer(t, client, choices)
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. defer servers.Delete(client, server.ID)
  229. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  230. t.Fatal(err)
  231. }
  232. t.Logf("Attempting to rebuild server %s", server.ID)
  233. rebuildOpts := servers.RebuildOpts{
  234. Name: tools.RandomString("ACPTTEST", 16),
  235. AdminPass: tools.MakeNewPassword(server.AdminPass),
  236. ImageID: choices.ImageID,
  237. }
  238. rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. if rebuilt.ID != server.ID {
  243. t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
  244. }
  245. if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
  246. t.Fatal(err)
  247. }
  248. if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
  249. t.Fatal(err)
  250. }
  251. }
  252. func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
  253. if err := waitForStatus(client, server, "ACTIVE"); err != nil {
  254. t.Fatal(err)
  255. }
  256. t.Logf("Attempting to resize server [%s]", server.ID)
  257. opts := &servers.ResizeOpts{
  258. FlavorRef: choices.FlavorIDResize,
  259. }
  260. if res := servers.Resize(client, server.ID, opts); res.Err != nil {
  261. t.Fatal(res.Err)
  262. }
  263. if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
  264. t.Fatal(err)
  265. }
  266. }
  267. func TestActionResizeConfirm(t *testing.T) {
  268. t.Parallel()
  269. choices, err := ComputeChoicesFromEnv()
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. client, err := newClient()
  274. if err != nil {
  275. t.Fatalf("Unable to create a compute client: %v", err)
  276. }
  277. server, err := createServer(t, client, choices)
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. defer servers.Delete(client, server.ID)
  282. resizeServer(t, client, server, choices)
  283. t.Logf("Attempting to confirm resize for server %s", server.ID)
  284. if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
  285. t.Fatal(err)
  286. }
  287. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  288. t.Fatal(err)
  289. }
  290. }
  291. func TestActionResizeRevert(t *testing.T) {
  292. t.Parallel()
  293. choices, err := ComputeChoicesFromEnv()
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. client, err := newClient()
  298. if err != nil {
  299. t.Fatalf("Unable to create a compute client: %v", err)
  300. }
  301. server, err := createServer(t, client, choices)
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. defer servers.Delete(client, server.ID)
  306. resizeServer(t, client, server, choices)
  307. t.Logf("Attempting to revert resize for server %s", server.ID)
  308. if res := servers.RevertResize(client, server.ID); res.Err != nil {
  309. t.Fatal(err)
  310. }
  311. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  312. t.Fatal(err)
  313. }
  314. }
  315. func TestServerMetadata(t *testing.T) {
  316. t.Parallel()
  317. choices, err := ComputeChoicesFromEnv()
  318. th.AssertNoErr(t, err)
  319. client, err := newClient()
  320. if err != nil {
  321. t.Fatalf("Unable to create a compute client: %v", err)
  322. }
  323. server, err := createServer(t, client, choices)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. defer servers.Delete(client, server.ID)
  328. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  329. t.Fatal(err)
  330. }
  331. metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
  332. "foo": "bar",
  333. "this": "that",
  334. }).Extract()
  335. th.AssertNoErr(t, err)
  336. t.Logf("UpdateMetadata result: %+v\n", metadata)
  337. err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
  338. th.AssertNoErr(t, err)
  339. metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
  340. "foo": "baz",
  341. }).Extract()
  342. th.AssertNoErr(t, err)
  343. t.Logf("CreateMetadatum result: %+v\n", metadata)
  344. metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
  345. th.AssertNoErr(t, err)
  346. t.Logf("Metadatum result: %+v\n", metadata)
  347. th.AssertEquals(t, "baz", metadata["foo"])
  348. metadata, err = servers.Metadata(client, server.ID).Extract()
  349. th.AssertNoErr(t, err)
  350. t.Logf("Metadata result: %+v\n", metadata)
  351. metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
  352. th.AssertNoErr(t, err)
  353. t.Logf("ResetMetadata result: %+v\n", metadata)
  354. th.AssertDeepEquals(t, map[string]string{}, metadata)
  355. }