keypairs_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // +build acceptance compute keypairs
  2. package v2
  3. import (
  4. "testing"
  5. "golang.org/x/crypto/ssh"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/acceptance/clients"
  7. "devel.mephi.ru/iacherepanov/openstack-gophercloud/acceptance/tools"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/compute/v2/extensions/keypairs"
  9. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/compute/v2/servers"
  10. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  11. )
  12. const keyName = "gophercloud_test_key_pair"
  13. func TestKeypairsCreateDelete(t *testing.T) {
  14. client, err := clients.NewComputeV2Client()
  15. th.AssertNoErr(t, err)
  16. keyPair, err := CreateKeyPair(t, client)
  17. th.AssertNoErr(t, err)
  18. defer DeleteKeyPair(t, client, keyPair)
  19. // There was a series of OpenStack releases, between Liberty and Ocata,
  20. // where the returned SSH key was not parsable by Go.
  21. // This checks if the issue is happening again.
  22. _, err = ssh.ParsePrivateKey([]byte(keyPair.PrivateKey))
  23. th.AssertNoErr(t, err)
  24. tools.PrintResource(t, keyPair)
  25. allPages, err := keypairs.List(client).AllPages()
  26. th.AssertNoErr(t, err)
  27. allKeys, err := keypairs.ExtractKeyPairs(allPages)
  28. th.AssertNoErr(t, err)
  29. var found bool
  30. for _, kp := range allKeys {
  31. tools.PrintResource(t, kp)
  32. if kp.Name == keyPair.Name {
  33. found = true
  34. }
  35. }
  36. th.AssertEquals(t, found, true)
  37. }
  38. func TestKeypairsImportPublicKey(t *testing.T) {
  39. client, err := clients.NewComputeV2Client()
  40. th.AssertNoErr(t, err)
  41. publicKey, err := createKey()
  42. th.AssertNoErr(t, err)
  43. keyPair, err := ImportPublicKey(t, client, publicKey)
  44. th.AssertNoErr(t, err)
  45. defer DeleteKeyPair(t, client, keyPair)
  46. tools.PrintResource(t, keyPair)
  47. }
  48. func TestKeypairsServerCreateWithKey(t *testing.T) {
  49. clients.RequireLong(t)
  50. client, err := clients.NewComputeV2Client()
  51. th.AssertNoErr(t, err)
  52. publicKey, err := createKey()
  53. th.AssertNoErr(t, err)
  54. keyPair, err := ImportPublicKey(t, client, publicKey)
  55. th.AssertNoErr(t, err)
  56. defer DeleteKeyPair(t, client, keyPair)
  57. server, err := CreateServerWithPublicKey(t, client, keyPair.Name)
  58. th.AssertNoErr(t, err)
  59. defer DeleteServer(t, client, server)
  60. server, err = servers.Get(client, server.ID).Extract()
  61. th.AssertNoErr(t, err)
  62. th.AssertEquals(t, server.KeyName, keyPair.Name)
  63. }