PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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