blob: 3c6abb4dc2973b3e0bdf85135c2b3b5bb669134b [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"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
10 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt5fdb7222014-11-17 14:41:54 -070011)
12
Jon Perrittd1e861a2014-11-18 08:10:13 -070013const keyName = "gophercloud_test_key_pair"
14
Joe Topjianc3820782016-07-25 04:15:42 +000015func TestKeypairsList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060016 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000017 if err != nil {
18 t.Fatalf("Unable to create a compute client: %v", err)
19 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070020
Joe Topjianc3820782016-07-25 04:15:42 +000021 allPages, err := keypairs.List(client).AllPages()
22 if err != nil {
23 t.Fatalf("Unable to retrieve keypairs: %s", err)
24 }
25
26 allKeys, err := keypairs.ExtractKeyPairs(allPages)
27 if err != nil {
28 t.Fatalf("Unable to extract keypairs results: %s", err)
29 }
30
31 for _, keypair := range allKeys {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060032 PrintKeyPair(t, &keypair)
Joe Topjianc3820782016-07-25 04:15:42 +000033 }
34}
35
36func TestKeypairsCreate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060037 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000038 if err != nil {
39 t.Fatalf("Unable to create a compute client: %v", err)
40 }
41
Joe Topjian1c15e3f2016-08-08 10:48:38 -060042 keyPair, err := CreateKeyPair(t, client)
Joe Topjianc3820782016-07-25 04:15:42 +000043 if err != nil {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060044 t.Fatalf("Unable to create key pair: %v", err)
Joe Topjianc3820782016-07-25 04:15:42 +000045 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060046 defer DeleteKeyPair(t, client, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000047
Joe Topjian1c15e3f2016-08-08 10:48:38 -060048 PrintKeyPair(t, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000049}
50
51func TestKeypairsImportPublicKey(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060052 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000053 if err != nil {
54 t.Fatalf("Unable to create a compute client: %v", err)
55 }
56
57 publicKey, err := createKey()
58 if err != nil {
59 t.Fatalf("Unable to create public key: %s", err)
60 }
61
Joe Topjian1c15e3f2016-08-08 10:48:38 -060062 keyPair, err := ImportPublicKey(t, client, publicKey)
Joe Topjianc3820782016-07-25 04:15:42 +000063 if err != nil {
64 t.Fatalf("Unable to create keypair: %s", err)
65 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060066 defer DeleteKeyPair(t, client, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000067
Joe Topjian1c15e3f2016-08-08 10:48:38 -060068 PrintKeyPair(t, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000069}
70
71func TestKeypairsServerCreateWithKey(t *testing.T) {
Jon Perritt5fdb7222014-11-17 14:41:54 -070072 if testing.Short() {
73 t.Skip("Skipping test that requires server creation in short mode.")
74 }
75
Joe Topjian1c15e3f2016-08-08 10:48:38 -060076 client, err := clients.NewComputeV2Client()
Joe Topjianc3820782016-07-25 04:15:42 +000077 if err != nil {
78 t.Fatalf("Unable to create a compute client: %v", err)
79 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070080
Joe Topjian1c15e3f2016-08-08 10:48:38 -060081 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjianc3820782016-07-25 04:15:42 +000082 if err != nil {
83 t.Fatal(err)
84 }
Jon Perritt5fdb7222014-11-17 14:41:54 -070085
Joe Topjianc3820782016-07-25 04:15:42 +000086 publicKey, err := createKey()
87 if err != nil {
88 t.Fatalf("Unable to create public key: %s", err)
89 }
90
Joe Topjian1c15e3f2016-08-08 10:48:38 -060091 keyPair, err := ImportPublicKey(t, client, publicKey)
Joe Topjianc3820782016-07-25 04:15:42 +000092 if err != nil {
93 t.Fatalf("Unable to create keypair: %s", err)
94 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060095 defer DeleteKeyPair(t, client, keyPair)
Joe Topjianc3820782016-07-25 04:15:42 +000096
Joe Topjian1c15e3f2016-08-08 10:48:38 -060097 server, err := CreateServerWithPublicKey(t, client, choices, keyPair.Name)
Joe Topjianc3820782016-07-25 04:15:42 +000098 if err != nil {
99 t.Fatalf("Unable to create server: %s", err)
100 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600101 defer DeleteServer(t, client, server)
Jon Perrittd1e861a2014-11-18 08:10:13 -0700102
103 server, err = servers.Get(client, server.ID).Extract()
Joe Topjianc3820782016-07-25 04:15:42 +0000104 if err != nil {
105 t.Fatalf("Unable to retrieve server: %s", err)
106 }
Jon Perritt5fdb7222014-11-17 14:41:54 -0700107
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600108 if server.KeyName != keyPair.Name {
109 t.Fatalf("key name of server %s is %s, not %s", server.ID, server.KeyName, keyPair.Name)
Joe Topjianc3820782016-07-25 04:15:42 +0000110 }
111}