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

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/compute/v2/servers_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 523 lines | 420 code | 102 blank | 1 comment | 170 complexity | 77b1763998aa1a7f0d324d454a449fb7 MD5 | raw file
  1. // +build acceptance compute servers
  2. package v2
  3. import (
  4. "strings"
  5. "testing"
  6. "github.com/gophercloud/gophercloud"
  7. "github.com/gophercloud/gophercloud/acceptance/clients"
  8. "github.com/gophercloud/gophercloud/acceptance/tools"
  9. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces"
  10. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
  11. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/extendedstatus"
  12. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/lockunlock"
  13. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/pauseunpause"
  14. "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/suspendresume"
  15. "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
  16. th "github.com/gophercloud/gophercloud/testhelper"
  17. )
  18. func TestServersList(t *testing.T) {
  19. client, err := clients.NewComputeV2Client()
  20. if err != nil {
  21. t.Fatalf("Unable to create a compute client: %v", err)
  22. }
  23. allPages, err := servers.List(client, servers.ListOpts{}).AllPages()
  24. if err != nil {
  25. t.Fatalf("Unable to retrieve servers: %v", err)
  26. }
  27. allServers, err := servers.ExtractServers(allPages)
  28. if err != nil {
  29. t.Fatalf("Unable to extract servers: %v", err)
  30. }
  31. for _, server := range allServers {
  32. tools.PrintResource(t, server)
  33. }
  34. }
  35. func TestServersCreateDestroy(t *testing.T) {
  36. client, err := clients.NewComputeV2Client()
  37. if err != nil {
  38. t.Fatalf("Unable to create a compute client: %v", err)
  39. }
  40. choices, err := clients.AcceptanceTestChoicesFromEnv()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. server, err := CreateServer(t, client)
  45. if err != nil {
  46. t.Fatalf("Unable to create server: %v", err)
  47. }
  48. defer DeleteServer(t, client, server)
  49. newServer, err := servers.Get(client, server.ID).Extract()
  50. if err != nil {
  51. t.Errorf("Unable to retrieve server: %v", err)
  52. }
  53. tools.PrintResource(t, newServer)
  54. allAddressPages, err := servers.ListAddresses(client, server.ID).AllPages()
  55. if err != nil {
  56. t.Errorf("Unable to list server addresses: %v", err)
  57. }
  58. allAddresses, err := servers.ExtractAddresses(allAddressPages)
  59. if err != nil {
  60. t.Errorf("Unable to extract server addresses: %v", err)
  61. }
  62. for network, address := range allAddresses {
  63. t.Logf("Addresses on %s: %+v", network, address)
  64. }
  65. allInterfacePages, err := attachinterfaces.List(client, server.ID).AllPages()
  66. if err != nil {
  67. t.Errorf("Unable to list server Interfaces: %v", err)
  68. }
  69. allInterfaces, err := attachinterfaces.ExtractInterfaces(allInterfacePages)
  70. if err != nil {
  71. t.Errorf("Unable to extract server Interfaces: %v", err)
  72. }
  73. for _, Interface := range allInterfaces {
  74. t.Logf("Interfaces: %+v", Interface)
  75. }
  76. allNetworkAddressPages, err := servers.ListAddressesByNetwork(client, server.ID, choices.NetworkName).AllPages()
  77. if err != nil {
  78. t.Errorf("Unable to list server addresses: %v", err)
  79. }
  80. allNetworkAddresses, err := servers.ExtractNetworkAddresses(allNetworkAddressPages)
  81. if err != nil {
  82. t.Errorf("Unable to extract server addresses: %v", err)
  83. }
  84. t.Logf("Addresses on %s:", choices.NetworkName)
  85. for _, address := range allNetworkAddresses {
  86. t.Logf("%+v", address)
  87. }
  88. }
  89. func TestServersCreateDestroyWithExtensions(t *testing.T) {
  90. var extendedServer struct {
  91. servers.Server
  92. availabilityzones.ServerAvailabilityZoneExt
  93. extendedstatus.ServerExtendedStatusExt
  94. }
  95. client, err := clients.NewComputeV2Client()
  96. if err != nil {
  97. t.Fatalf("Unable to create a compute client: %v", err)
  98. }
  99. server, err := CreateServer(t, client)
  100. if err != nil {
  101. t.Fatalf("Unable to create server: %v", err)
  102. }
  103. defer DeleteServer(t, client, server)
  104. err = servers.Get(client, server.ID).ExtractInto(&extendedServer)
  105. if err != nil {
  106. t.Errorf("Unable to retrieve server: %v", err)
  107. }
  108. tools.PrintResource(t, extendedServer)
  109. t.Logf("Availability Zone: %s\n", extendedServer.AvailabilityZone)
  110. t.Logf("Power State: %s\n", extendedServer.PowerState)
  111. t.Logf("Task State: %s\n", extendedServer.TaskState)
  112. t.Logf("VM State: %s\n", extendedServer.VmState)
  113. }
  114. func TestServersWithoutImageRef(t *testing.T) {
  115. client, err := clients.NewComputeV2Client()
  116. if err != nil {
  117. t.Fatalf("Unable to create a compute client: %v", err)
  118. }
  119. server, err := CreateServerWithoutImageRef(t, client)
  120. if err != nil {
  121. if err400, ok := err.(*gophercloud.ErrUnexpectedResponseCode); ok {
  122. if !strings.Contains("Missing imageRef attribute", string(err400.Body)) {
  123. defer DeleteServer(t, client, server)
  124. }
  125. }
  126. }
  127. }
  128. func TestServersUpdate(t *testing.T) {
  129. client, err := clients.NewComputeV2Client()
  130. if err != nil {
  131. t.Fatalf("Unable to create a compute client: %v", err)
  132. }
  133. server, err := CreateServer(t, client)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. defer DeleteServer(t, client, server)
  138. alternateName := tools.RandomString("ACPTTEST", 16)
  139. for alternateName == server.Name {
  140. alternateName = tools.RandomString("ACPTTEST", 16)
  141. }
  142. t.Logf("Attempting to rename the server to %s.", alternateName)
  143. updateOpts := servers.UpdateOpts{
  144. Name: alternateName,
  145. }
  146. updated, err := servers.Update(client, server.ID, updateOpts).Extract()
  147. if err != nil {
  148. t.Fatalf("Unable to rename server: %v", err)
  149. }
  150. if updated.ID != server.ID {
  151. t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
  152. }
  153. err = tools.WaitFor(func() (bool, error) {
  154. latest, err := servers.Get(client, updated.ID).Extract()
  155. if err != nil {
  156. return false, err
  157. }
  158. return latest.Name == alternateName, nil
  159. })
  160. }
  161. func TestServersMetadata(t *testing.T) {
  162. t.Parallel()
  163. client, err := clients.NewComputeV2Client()
  164. if err != nil {
  165. t.Fatalf("Unable to create a compute client: %v", err)
  166. }
  167. server, err := CreateServer(t, client)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. defer DeleteServer(t, client, server)
  172. metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
  173. "foo": "bar",
  174. "this": "that",
  175. }).Extract()
  176. if err != nil {
  177. t.Fatalf("Unable to update metadata: %v", err)
  178. }
  179. t.Logf("UpdateMetadata result: %+v\n", metadata)
  180. err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
  181. if err != nil {
  182. t.Fatalf("Unable to delete metadatum: %v", err)
  183. }
  184. metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
  185. "foo": "baz",
  186. }).Extract()
  187. if err != nil {
  188. t.Fatalf("Unable to create metadatum: %v", err)
  189. }
  190. t.Logf("CreateMetadatum result: %+v\n", metadata)
  191. metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
  192. if err != nil {
  193. t.Fatalf("Unable to get metadatum: %v", err)
  194. }
  195. t.Logf("Metadatum result: %+v\n", metadata)
  196. th.AssertEquals(t, "baz", metadata["foo"])
  197. metadata, err = servers.Metadata(client, server.ID).Extract()
  198. if err != nil {
  199. t.Fatalf("Unable to get metadata: %v", err)
  200. }
  201. t.Logf("Metadata result: %+v\n", metadata)
  202. metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
  203. if err != nil {
  204. t.Fatalf("Unable to reset metadata: %v", err)
  205. }
  206. t.Logf("ResetMetadata result: %+v\n", metadata)
  207. th.AssertDeepEquals(t, map[string]string{}, metadata)
  208. }
  209. func TestServersActionChangeAdminPassword(t *testing.T) {
  210. t.Parallel()
  211. client, err := clients.NewComputeV2Client()
  212. if err != nil {
  213. t.Fatalf("Unable to create a compute client: %v", err)
  214. }
  215. server, err := CreateServer(t, client)
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. defer DeleteServer(t, client, server)
  220. randomPassword := tools.MakeNewPassword(server.AdminPass)
  221. res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
  222. if res.Err != nil {
  223. t.Fatal(res.Err)
  224. }
  225. if err = WaitForComputeStatus(client, server, "PASSWORD"); err != nil {
  226. t.Fatal(err)
  227. }
  228. if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
  229. t.Fatal(err)
  230. }
  231. }
  232. func TestServersActionReboot(t *testing.T) {
  233. t.Parallel()
  234. client, err := clients.NewComputeV2Client()
  235. if err != nil {
  236. t.Fatalf("Unable to create a compute client: %v", err)
  237. }
  238. server, err := CreateServer(t, client)
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. defer DeleteServer(t, client, server)
  243. rebootOpts := &servers.RebootOpts{
  244. Type: servers.SoftReboot,
  245. }
  246. t.Logf("Attempting reboot of server %s", server.ID)
  247. res := servers.Reboot(client, server.ID, rebootOpts)
  248. if res.Err != nil {
  249. t.Fatalf("Unable to reboot server: %v", res.Err)
  250. }
  251. if err = WaitForComputeStatus(client, server, "REBOOT"); err != nil {
  252. t.Fatal(err)
  253. }
  254. if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
  255. t.Fatal(err)
  256. }
  257. }
  258. func TestServersActionRebuild(t *testing.T) {
  259. t.Parallel()
  260. client, err := clients.NewComputeV2Client()
  261. if err != nil {
  262. t.Fatalf("Unable to create a compute client: %v", err)
  263. }
  264. choices, err := clients.AcceptanceTestChoicesFromEnv()
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. server, err := CreateServer(t, client)
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. defer DeleteServer(t, client, server)
  273. t.Logf("Attempting to rebuild server %s", server.ID)
  274. rebuildOpts := servers.RebuildOpts{
  275. Name: tools.RandomString("ACPTTEST", 16),
  276. AdminPass: tools.MakeNewPassword(server.AdminPass),
  277. ImageID: choices.ImageID,
  278. }
  279. rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. if rebuilt.ID != server.ID {
  284. t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
  285. }
  286. if err = WaitForComputeStatus(client, rebuilt, "REBUILD"); err != nil {
  287. t.Fatal(err)
  288. }
  289. if err = WaitForComputeStatus(client, rebuilt, "ACTIVE"); err != nil {
  290. t.Fatal(err)
  291. }
  292. }
  293. func TestServersActionResizeConfirm(t *testing.T) {
  294. t.Parallel()
  295. client, err := clients.NewComputeV2Client()
  296. if err != nil {
  297. t.Fatalf("Unable to create a compute client: %v", err)
  298. }
  299. server, err := CreateServer(t, client)
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. defer DeleteServer(t, client, server)
  304. t.Logf("Attempting to resize server %s", server.ID)
  305. ResizeServer(t, client, server)
  306. t.Logf("Attempting to confirm resize for server %s", server.ID)
  307. if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
  308. t.Fatal(res.Err)
  309. }
  310. if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
  311. t.Fatal(err)
  312. }
  313. }
  314. func TestServersActionResizeRevert(t *testing.T) {
  315. t.Parallel()
  316. client, err := clients.NewComputeV2Client()
  317. if err != nil {
  318. t.Fatalf("Unable to create a compute client: %v", err)
  319. }
  320. server, err := CreateServer(t, client)
  321. if err != nil {
  322. t.Fatal(err)
  323. }
  324. defer DeleteServer(t, client, server)
  325. t.Logf("Attempting to resize server %s", server.ID)
  326. ResizeServer(t, client, server)
  327. t.Logf("Attempting to revert resize for server %s", server.ID)
  328. if res := servers.RevertResize(client, server.ID); res.Err != nil {
  329. t.Fatal(res.Err)
  330. }
  331. if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
  332. t.Fatal(err)
  333. }
  334. }
  335. func TestServersActionPause(t *testing.T) {
  336. t.Parallel()
  337. client, err := clients.NewComputeV2Client()
  338. if err != nil {
  339. t.Fatalf("Unable to create a compute client: %v", err)
  340. }
  341. server, err := CreateServer(t, client)
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. defer DeleteServer(t, client, server)
  346. t.Logf("Attempting to pause server %s", server.ID)
  347. err = pauseunpause.Pause(client, server.ID).ExtractErr()
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. err = WaitForComputeStatus(client, server, "PAUSED")
  352. if err != nil {
  353. t.Fatal(err)
  354. }
  355. err = pauseunpause.Unpause(client, server.ID).ExtractErr()
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. err = WaitForComputeStatus(client, server, "ACTIVE")
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. }
  364. func TestServersActionSuspend(t *testing.T) {
  365. t.Parallel()
  366. client, err := clients.NewComputeV2Client()
  367. if err != nil {
  368. t.Fatalf("Unable to create a compute client: %v", err)
  369. }
  370. server, err := CreateServer(t, client)
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. defer DeleteServer(t, client, server)
  375. t.Logf("Attempting to suspend server %s", server.ID)
  376. err = suspendresume.Suspend(client, server.ID).ExtractErr()
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. err = WaitForComputeStatus(client, server, "SUSPENDED")
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. err = suspendresume.Resume(client, server.ID).ExtractErr()
  385. if err != nil {
  386. t.Fatal(err)
  387. }
  388. err = WaitForComputeStatus(client, server, "ACTIVE")
  389. if err != nil {
  390. t.Fatal(err)
  391. }
  392. }
  393. func TestServersActionLock(t *testing.T) {
  394. t.Parallel()
  395. client, err := clients.NewComputeV2Client()
  396. if err != nil {
  397. t.Fatalf("Unable to create a compute client: %v", err)
  398. }
  399. server, err := CreateServer(t, client)
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. defer DeleteServer(t, client, server)
  404. t.Logf("Attempting to Lock server %s", server.ID)
  405. err = lockunlock.Lock(client, server.ID).ExtractErr()
  406. if err != nil {
  407. t.Fatal(err)
  408. }
  409. err = servers.Delete(client, server.ID).ExtractErr()
  410. if err == nil {
  411. t.Fatalf("Should not have been able to delete the server")
  412. }
  413. err = lockunlock.Unlock(client, server.ID).ExtractErr()
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. err = WaitForComputeStatus(client, server, "ACTIVE")
  418. if err != nil {
  419. t.Fatal(err)
  420. }
  421. }