-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathsshkeys_test.go
142 lines (104 loc) · 3.75 KB
/
sshkeys_test.go
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
package commands
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/bryanl/doit"
"github.com/bryanl/doit/do"
"github.com/digitalocean/godo"
"github.com/stretchr/testify/assert"
)
var (
testKey = do.SSHKey{Key: &godo.Key{ID: 1, Fingerprint: "fingerprint"}}
testKeyList = do.SSHKeys{testKey}
)
func TestSSHKeysCommand(t *testing.T) {
cmd := SSHKeys()
assert.NotNil(t, cmd)
assertCommandNames(t, cmd, "create", "delete", "get", "import", "list", "update")
}
func TestKeysList(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.keys.On("List").Return(testKeyList, nil)
err := RunKeyList(config)
assert.NoError(t, err)
})
}
func TestKeysGetByID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.keys.On("Get", "1").Return(&testKey, nil)
config.Args = append(config.Args, "1")
err := RunKeyGet(config)
assert.NoError(t, err)
})
}
func TestKeysGetByFingerprint(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.keys.On("Get", testKey.Fingerprint).Return(&testKey, nil)
config.Args = append(config.Args, testKey.Fingerprint)
err := RunKeyGet(config)
assert.NoError(t, err)
})
}
func TestKeysCreate(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
kcr := &godo.KeyCreateRequest{Name: "the key", PublicKey: "fingerprint"}
tm.keys.On("Create", kcr).Return(&testKey, nil)
config.Args = append(config.Args, "the key")
config.Doit.Set(config.NS, doit.ArgKeyPublicKey, "fingerprint")
err := RunKeyCreate(config)
assert.NoError(t, err)
})
}
func TestKeysDeleteByID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.keys.On("Delete", "1").Return(nil)
config.Args = append(config.Args, "1")
err := RunKeyDelete(config)
assert.NoError(t, err)
})
}
func TestKeysDeleteByFingerprint(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.keys.On("Delete", "fingerprint").Return(nil)
config.Args = append(config.Args, "fingerprint")
err := RunKeyDelete(config)
assert.NoError(t, err)
})
}
func TestKeysUpdateByID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
kur := &godo.KeyUpdateRequest{Name: "the key"}
tm.keys.On("Update", "1", kur).Return(&testKey, nil)
config.Args = append(config.Args, "1")
config.Doit.Set(config.NS, doit.ArgKeyName, "the key")
err := RunKeyUpdate(config)
assert.NoError(t, err)
})
}
func TestKeysUpdateByFingerprint(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
kur := &godo.KeyUpdateRequest{Name: "the key"}
tm.keys.On("Update", "fingerprint", kur).Return(&testKey, nil)
config.Args = append(config.Args, "fingerprint")
config.Doit.Set(config.NS, doit.ArgKeyName, "the key")
err := RunKeyUpdate(config)
assert.NoError(t, err)
})
}
func TestSSHPublicKeyImportWithName(t *testing.T) {
pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCn6eZ8ve0ha04rPRZuoPXK1AQ/h21qslWCzoDcOciXn5OcyafkZw+31k/afaBTeW62D8fXd8e/1xWbFfp/2GqmslYpNCTPrtpNhsE8I0yKjJ8FxX9FfsCOu/Sv83dWgSpiT7pNWVKarZjW9KdKKRQljq1i+H5pX3r5Q9I1v+66mYTe7qsKGas9KWy0vkGoNSqmTCl+d+Y0286chtqBqBjSCUCI8oLKPnJB86Lj344tFGmzDIsJKXMVHTL0dF8n3u6iWN4qiRU+JvkoIkI3v0JvyZXxhR2uPIS1yUAY2GC+2O5mfxydJQzBdtag5Uw8Y7H5yYR1gar/h16bAy5XzRvp testkey"
path := filepath.Join(os.TempDir(), "key.pub")
err := ioutil.WriteFile(path, []byte(pubkey), 0600)
assert.NoError(t, err)
defer os.Remove(path)
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
kcr := &godo.KeyCreateRequest{Name: "custom", PublicKey: pubkey}
tm.keys.On("Create", kcr).Return(&testKey, nil)
config.Args = append(config.Args, "custom")
config.Doit.Set(config.NS, doit.ArgKeyPublicKeyFile, path)
err := RunKeyImport(config)
assert.NoError(t, err)
})
}