blob: b9fb57aad8d1d9651cda213e4282ac3f415c6d90 [file] [log] [blame]
Joe Topjianc3820782016-07-25 04:15:42 +00001// +build acceptance compute keypairs
Jon Perritt5fdb7222014-11-17 14:41:54 -07002
3package v2
4
5import (
6 "crypto/rand"
7 "crypto/rsa"
8 "testing"
9
Joe Topjianc3820782016-07-25 04:15:42 +000010 "github.com/gophercloud/gophercloud"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/acceptance/tools"
12 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
13 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt5fdb7222014-11-17 14:41:54 -070014
JackSpirou0e587122015-04-11 10:46:23 -050015 "golang.org/x/crypto/ssh"
Jon Perritt5fdb7222014-11-17 14:41:54 -070016)
17
Jon Perrittd1e861a2014-11-18 08:10:13 -070018const keyName = "gophercloud_test_key_pair"
19
Joe Topjianc3820782016-07-25 04:15:42 +000020func TestKeypairsList(t *testing.T) {
Jon Perritt5fdb7222014-11-17 14:41:54 -070021 client, err := newClient()
Joe Topjianc3820782016-07-25 04:15:42 +000022 if err != nil {
23 t.Fatalf("Unable to create a compute client: %v", err)
24 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070025
Joe Topjianc3820782016-07-25 04:15:42 +000026 allPages, err := keypairs.List(client).AllPages()
27 if err != nil {
28 t.Fatalf("Unable to retrieve keypairs: %s", err)
29 }
30
31 allKeys, err := keypairs.ExtractKeyPairs(allPages)
32 if err != nil {
33 t.Fatalf("Unable to extract keypairs results: %s", err)
34 }
35
36 for _, keypair := range allKeys {
37 printKeyPair(t, &keypair)
38 }
39}
40
41func TestKeypairsCreate(t *testing.T) {
42 client, err := newClient()
43 if err != nil {
44 t.Fatalf("Unable to create a compute client: %v", err)
45 }
46
47 createOpts := keypairs.CreateOpts{
48 Name: keyName,
49 }
50 keyPair, err := keypairs.Create(client, createOpts).Extract()
51 if err != nil {
52 t.Fatalf("Unable to create keypair: %s", err)
53 }
54 defer deleteKeyPair(t, client, keyPair)
55
56 printKeyPair(t, keyPair)
57}
58
59func TestKeypairsImportPublicKey(t *testing.T) {
60 client, err := newClient()
61 if err != nil {
62 t.Fatalf("Unable to create a compute client: %v", err)
63 }
64
65 publicKey, err := createKey()
66 if err != nil {
67 t.Fatalf("Unable to create public key: %s", err)
68 }
69
70 createOpts := keypairs.CreateOpts{
71 Name: keyName,
72 PublicKey: publicKey,
73 }
74 keyPair, err := keypairs.Create(client, createOpts).Extract()
75 if err != nil {
76 t.Fatalf("Unable to create keypair: %s", err)
77 }
78 defer deleteKeyPair(t, client, keyPair)
79
80 printKeyPair(t, keyPair)
81}
82
83func TestKeypairsServerCreateWithKey(t *testing.T) {
Jon Perritt5fdb7222014-11-17 14:41:54 -070084 if testing.Short() {
85 t.Skip("Skipping test that requires server creation in short mode.")
86 }
87
Joe Topjianc3820782016-07-25 04:15:42 +000088 client, err := newClient()
89 if err != nil {
90 t.Fatalf("Unable to create a compute client: %v", err)
91 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070092
93 choices, err := ComputeChoicesFromEnv()
Joe Topjianc3820782016-07-25 04:15:42 +000094 if err != nil {
95 t.Fatal(err)
96 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070097
Joe Topjianc3820782016-07-25 04:15:42 +000098 publicKey, err := createKey()
99 if err != nil {
100 t.Fatalf("Unable to create public key: %s", err)
101 }
102
103 createOpts := keypairs.CreateOpts{
104 Name: keyName,
105 PublicKey: publicKey,
106 }
107
108 keyPair, err := keypairs.Create(client, createOpts).Extract()
109 if err != nil {
110 t.Fatalf("Unable to create keypair: %s", err)
111 }
112 defer deleteKeyPair(t, client, keyPair)
113
114 networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName)
115 if err != nil {
116 t.Fatalf("Failed to obtain network ID: %v", err)
117 }
118
119 name := tools.RandomString("ACPTTEST", 16)
120 t.Logf("Attempting to create server: %s", name)
Jon Perritt5fdb7222014-11-17 14:41:54 -0700121
122 serverCreateOpts := servers.CreateOpts{
123 Name: name,
124 FlavorRef: choices.FlavorID,
125 ImageRef: choices.ImageID,
Joe Topjianc3820782016-07-25 04:15:42 +0000126 Networks: []servers.Network{
127 servers.Network{UUID: networkID},
128 },
Jon Perritt5fdb7222014-11-17 14:41:54 -0700129 }
130
131 server, err := servers.Create(client, keypairs.CreateOptsExt{
132 serverCreateOpts,
Jon Perrittd1e861a2014-11-18 08:10:13 -0700133 keyName,
Jon Perritt5fdb7222014-11-17 14:41:54 -0700134 }).Extract()
Joe Topjianc3820782016-07-25 04:15:42 +0000135 if err != nil {
136 t.Fatalf("Unable to create server: %s", err)
137 }
138
Jon Perrittd1e861a2014-11-18 08:10:13 -0700139 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
140 t.Fatalf("Unable to wait for server: %v", err)
141 }
Joe Topjianc3820782016-07-25 04:15:42 +0000142 defer deleteServer(t, client, server)
Jon Perrittd1e861a2014-11-18 08:10:13 -0700143
144 server, err = servers.Get(client, server.ID).Extract()
Joe Topjianc3820782016-07-25 04:15:42 +0000145 if err != nil {
146 t.Fatalf("Unable to retrieve server: %s", err)
147 }
Jon Perritt5fdb7222014-11-17 14:41:54 -0700148
Joe Topjianc3820782016-07-25 04:15:42 +0000149 if server.KeyName != keyName {
150 t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyName)
151 }
152}
Jon Perritt5fdb7222014-11-17 14:41:54 -0700153
Joe Topjianc3820782016-07-25 04:15:42 +0000154func createKey() (string, error) {
155 privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
156 if err != nil {
157 return "", err
158 }
159
160 publicKey := privateKey.PublicKey
161 pub, err := ssh.NewPublicKey(&publicKey)
162 if err != nil {
163 return "", err
164 }
165
166 pubBytes := ssh.MarshalAuthorizedKey(pub)
167 pk := string(pubBytes)
168 return pk, nil
169}
170
171func deleteKeyPair(t *testing.T, client *gophercloud.ServiceClient, keyPair *keypairs.KeyPair) {
172 err := keypairs.Delete(client, keyPair.Name).ExtractErr()
173 if err != nil {
174 t.Fatalf("Unable to delete keypair %s: %v", keyPair.Name, err)
175 }
176
177 t.Logf("Deleted keypair: %s", keyPair.Name)
178}
179
180func printKeyPair(t *testing.T, keypair *keypairs.KeyPair) {
181 t.Logf("Name: %s", keypair.Name)
182 t.Logf("Fingerprint: %s", keypair.Fingerprint)
183 t.Logf("Public Key: %s", keypair.PublicKey)
184 t.Logf("Private Key: %s", keypair.PrivateKey)
185 t.Logf("UserID: %s", keypair.UserID)
Jon Perritt5fdb7222014-11-17 14:41:54 -0700186}