blob: c4b91ec8542e9303840829fd29daa00d2cef261f [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 (
Jon Perritt5fdb7222014-11-17 14:41:54 -07006 "testing"
7
Joe Topjian1c15e3f2016-08-08 10:48:38 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian66a046c2017-01-19 22:07:26 -07009 "github.com/gophercloud/gophercloud/acceptance/tools"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
11 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt5fdb7222014-11-17 14:41:54 -070012)
13
Jon Perrittd1e861a2014-11-18 08:10:13 -070014const keyName = "gophercloud_test_key_pair"
15
Joe Topjianc3820782016-07-25 04:15:42 +000016func TestKeypairsList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060017 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000018 if err != nil {
19 t.Fatalf("Unable to create a compute client: %v", err)
20 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070021
Joe Topjianc3820782016-07-25 04:15:42 +000022 allPages, err := keypairs.List(client).AllPages()
23 if err != nil {
24 t.Fatalf("Unable to retrieve keypairs: %s", err)
25 }
26
27 allKeys, err := keypairs.ExtractKeyPairs(allPages)
28 if err != nil {
29 t.Fatalf("Unable to extract keypairs results: %s", err)
30 }
31
32 for _, keypair := range allKeys {
Joe Topjian66a046c2017-01-19 22:07:26 -070033 tools.PrintResource(t, keypair)
Joe Topjianc3820782016-07-25 04:15:42 +000034 }
35}
36
37func TestKeypairsCreate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060038 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000039 if err != nil {
40 t.Fatalf("Unable to create a compute client: %v", err)
41 }
42
Joe Topjian1c15e3f2016-08-08 10:48:38 -060043 keyPair, err := CreateKeyPair(t, client)
Joe Topjianc3820782016-07-25 04:15:42 +000044 if err != nil {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060045 t.Fatalf("Unable to create key pair: %v", err)
Joe Topjianc3820782016-07-25 04:15:42 +000046 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060047 defer DeleteKeyPair(t, client, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000048
Joe Topjian66a046c2017-01-19 22:07:26 -070049 tools.PrintResource(t, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000050}
51
52func TestKeypairsImportPublicKey(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060053 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000054 if err != nil {
55 t.Fatalf("Unable to create a compute client: %v", err)
56 }
57
58 publicKey, err := createKey()
59 if err != nil {
60 t.Fatalf("Unable to create public key: %s", err)
61 }
62
Joe Topjian1c15e3f2016-08-08 10:48:38 -060063 keyPair, err := ImportPublicKey(t, client, publicKey)
Joe Topjianc3820782016-07-25 04:15:42 +000064 if err != nil {
65 t.Fatalf("Unable to create keypair: %s", err)
66 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060067 defer DeleteKeyPair(t, client, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000068
Joe Topjian66a046c2017-01-19 22:07:26 -070069 tools.PrintResource(t, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000070}
71
72func TestKeypairsServerCreateWithKey(t *testing.T) {
Jon Perritt5fdb7222014-11-17 14:41:54 -070073 if testing.Short() {
74 t.Skip("Skipping test that requires server creation in short mode.")
75 }
76
Joe Topjian1c15e3f2016-08-08 10:48:38 -060077 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000078 if err != nil {
79 t.Fatalf("Unable to create a compute client: %v", err)
80 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070081
Joe Topjianc3820782016-07-25 04:15:42 +000082 publicKey, err := createKey()
83 if err != nil {
84 t.Fatalf("Unable to create public key: %s", err)
85 }
86
Joe Topjian1c15e3f2016-08-08 10:48:38 -060087 keyPair, err := ImportPublicKey(t, client, publicKey)
Joe Topjianc3820782016-07-25 04:15:42 +000088 if err != nil {
89 t.Fatalf("Unable to create keypair: %s", err)
90 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060091 defer DeleteKeyPair(t, client, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000092
Joe Topjian66a046c2017-01-19 22:07:26 -070093 server, err := CreateServerWithPublicKey(t, client, keyPair.Name)
Joe Topjianc3820782016-07-25 04:15:42 +000094 if err != nil {
95 t.Fatalf("Unable to create server: %s", err)
96 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060097 defer DeleteServer(t, client, server)
Jon Perrittd1e861a2014-11-18 08:10:13 -070098
99 server, err = servers.Get(client, server.ID).Extract()
Joe Topjianc3820782016-07-25 04:15:42 +0000100 if err != nil {
101 t.Fatalf("Unable to retrieve server: %s", err)
102 }
Jon Perritt5fdb7222014-11-17 14:41:54 -0700103
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600104 if server.KeyName != keyPair.Name {
105 t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyPair.Name)
Joe Topjianc3820782016-07-25 04:15:42 +0000106 }
107}