1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
package packngo
import (
"crypto/rand"
"crypto/rsa"
"reflect"
"testing"
"golang.org/x/crypto/ssh"
)
func makePubKey(t *testing.T) string {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("error generating test private key: %v", err)
}
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
t.Fatalf("error generating test public key: %v", err)
}
return string(ssh.MarshalAuthorizedKey(pub))
}
func createKey(t *testing.T, c *Client, p string) *SSHKey {
req := SSHKeyCreateRequest{
Label: "PACKNGO_TEST_KEY_DELETE_ME-" + randString8(),
ProjectID: p,
Key: makePubKey(t),
}
key, _, err := c.SSHKeys.Create(&req)
if err != nil {
t.Fatalf("errored posting key: %v", err)
}
return key
}
func TestAccSSHKeyList(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, _, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, "")
defer c.SSHKeys.Delete(key.ID)
keys, _, err := c.SSHKeys.List()
if err != nil {
t.Fatalf("failed to get list of sshkeys: %v", err)
}
for _, k := range keys {
if k.ID == key.ID {
return
}
}
t.Error("failed to find created key in list of keys retrieved")
}
func TestAccSSHKeyProjectList(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, projectID)
defer c.SSHKeys.Delete(key.ID)
keys, _, err := c.SSHKeys.ProjectList(projectID)
if err != nil {
t.Fatalf("failed to get list of project sshkeys: %v", err)
}
if len(keys) != 1 {
t.Fatal("there should be exactly one key for the project")
}
for _, k := range keys {
if k.ID == key.ID {
return
}
}
t.Error("failed to find created project key in list of project keys retrieved")
}
func TestAccSSHKeyGet(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
user := createKey(t, c, "")
defer c.SSHKeys.Delete(user.ID)
proj := createKey(t, c, projectID)
for _, k := range []*SSHKey{user, proj} {
got, _, err := c.SSHKeys.Get(k.ID, nil)
if err != nil {
t.Fatalf("failed to retrieve created key")
}
if !reflect.DeepEqual(k, got) {
t.Errorf("keys do not match, want: %v, got:%v", k, got)
}
}
}
func TestAccSSHKeyCreate(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
req := SSHKeyCreateRequest{
Label: "PACKNGO_TEST_KEY_DELETE_ME-" + randString8(),
ProjectID: projectID,
Key: makePubKey(t),
}
key, _, err := c.SSHKeys.Create(&req)
if err != nil {
t.Fatalf("errored posting key: %v", err)
}
if key.Label != req.Label {
t.Fatalf("returned key label does not match, want: %v, got: %v", req.Label, key.Label)
}
if key.Key != req.Key {
t.Fatalf("returned key does not match, want: %v, got: %v", req.Key, key.Key)
}
}
func TestWrongSSHKeyUpdate(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, projectID)
req := SSHKeyUpdateRequest{}
_, _, err := c.SSHKeys.Update(key.ID, &req)
if err == nil {
t.Fatalf("SSHKey update by request without label or key string should be invalid")
}
}
func TestAccSSHKeyStringUpdate(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, projectID)
newKey := makePubKey(t)
req := SSHKeyUpdateRequest{
Key: &newKey,
}
got, _, err := c.SSHKeys.Update(key.ID, &req)
if err != nil {
t.Fatalf("failed to update key: %v", err)
}
if reflect.DeepEqual(key, got) {
t.Fatalf("expected keys to differ, got: %v", key)
}
if got.Key != newKey {
t.Fatalf("expected updated key string, want: %s, got: %s", newKey, got.Key)
}
}
func TestAccSSHKeyLabelUpdate(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, projectID)
kLabel := key.Label + "-updated"
req := SSHKeyUpdateRequest{Label: &kLabel}
got, _, err := c.SSHKeys.Update(key.ID, &req)
if err != nil {
t.Fatalf("failed to update key: %v", err)
}
if reflect.DeepEqual(key, got) {
t.Fatalf("expected keys to differ, got: %v", key)
}
if got.Label != key.Label+"-updated" {
t.Fatalf("expected updated label, want: %s-updated, got: %s", key.Label, got.Label)
}
}
func TestAccSSHKeyUpdate(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, projectID)
newKey := makePubKey(t)
kLabel := key.Label + "-updated"
req := SSHKeyUpdateRequest{
Key: &newKey,
Label: &kLabel,
}
got, _, err := c.SSHKeys.Update(key.ID, &req)
if err != nil {
t.Fatalf("failed to update key: %v", err)
}
if reflect.DeepEqual(key, got) {
t.Fatalf("expected keys to differ, got: %v", key)
}
if got.Label != key.Label+"-updated" {
t.Fatalf("expected updated label, want: %s-updated, got: %s", key.Label, got.Label)
}
if got.Key != newKey {
t.Fatalf("expected updated key string, want: %s, got: %s", newKey, got.Key)
}
}
func TestAccSSHKeyDelete(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
key := createKey(t, c, projectID)
_, err := c.SSHKeys.Delete(key.ID)
if err != nil {
t.Fatalf("unable to delete key: %v", err)
}
unexpected, _, err := c.SSHKeys.Get(key.ID, nil)
if err == nil {
t.Fatalf("expected an error getting key, got: %v", unexpected)
}
}
|