PageRenderTime 138ms CodeModel.GetById 48ms RepoModel.GetById 6ms app.codeStats 0ms

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

https://gitlab.com/Red54/machine
Go | 74 lines | 57 code | 16 blank | 1 comment | 4 complexity | 11ce6788e0b28bb6015e71948861d6d3 MD5 | raw file
  1. // +build acceptance
  2. package v2
  3. import (
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "testing"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
  9. "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  10. th "github.com/rackspace/gophercloud/testhelper"
  11. "code.google.com/p/go.crypto/ssh"
  12. )
  13. const keyName = "gophercloud_test_key_pair"
  14. func TestCreateServerWithKeyPair(t *testing.T) {
  15. client, err := newClient()
  16. th.AssertNoErr(t, err)
  17. if testing.Short() {
  18. t.Skip("Skipping test that requires server creation in short mode.")
  19. }
  20. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  21. publicKey := privateKey.PublicKey
  22. pub, err := ssh.NewPublicKey(&publicKey)
  23. th.AssertNoErr(t, err)
  24. pubBytes := ssh.MarshalAuthorizedKey(pub)
  25. pk := string(pubBytes)
  26. kp, err := keypairs.Create(client, keypairs.CreateOpts{
  27. Name: keyName,
  28. PublicKey: pk,
  29. }).Extract()
  30. th.AssertNoErr(t, err)
  31. t.Logf("Created key pair: %s\n", kp)
  32. choices, err := ComputeChoicesFromEnv()
  33. th.AssertNoErr(t, err)
  34. name := tools.RandomString("Gophercloud-", 8)
  35. t.Logf("Creating server [%s] with key pair.", name)
  36. serverCreateOpts := servers.CreateOpts{
  37. Name: name,
  38. FlavorRef: choices.FlavorID,
  39. ImageRef: choices.ImageID,
  40. }
  41. server, err := servers.Create(client, keypairs.CreateOptsExt{
  42. serverCreateOpts,
  43. keyName,
  44. }).Extract()
  45. th.AssertNoErr(t, err)
  46. defer servers.Delete(client, server.ID)
  47. if err = waitForStatus(client, server, "ACTIVE"); err != nil {
  48. t.Fatalf("Unable to wait for server: %v", err)
  49. }
  50. server, err = servers.Get(client, server.ID).Extract()
  51. t.Logf("Created server: %+v\n", server)
  52. th.AssertNoErr(t, err)
  53. th.AssertEquals(t, server.KeyName, keyName)
  54. t.Logf("Deleting key pair [%s]...", kp.Name)
  55. err = keypairs.Delete(client, keyName).ExtractErr()
  56. th.AssertNoErr(t, err)
  57. t.Logf("Deleting server [%s]...", name)
  58. }