-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkeys_test.go
More file actions
123 lines (92 loc) · 2.08 KB
/
keys_test.go
File metadata and controls
123 lines (92 loc) · 2.08 KB
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
package cmd
import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"github.com/deis/deis/client/controller/api"
)
func TestGetKey(t *testing.T) {
t.Parallel()
file, err := ioutil.TempFile("", "deis-key")
if err != nil {
t.Fatal(err)
}
toWrite := []byte("ssh-rsa abc test@example.com")
expected := api.KeyCreateRequest{
ID: "test@example.com",
Public: string(toWrite),
Name: file.Name(),
}
if _, err = file.Write(toWrite); err != nil {
t.Fatal(err)
}
key, err := getKey(file.Name())
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, key) {
t.Errorf("Expected %v, Got %v", expected, key)
}
}
func TestGetKeyNoComment(t *testing.T) {
t.Parallel()
file, err := ioutil.TempFile("", "deis-key")
if err != nil {
t.Fatal(err)
}
toWrite := []byte("ssh-rsa abc")
expected := api.KeyCreateRequest{
ID: path.Base(file.Name()),
Public: string(toWrite),
Name: file.Name(),
}
if _, err = file.Write(toWrite); err != nil {
t.Fatal(err)
}
key, err := getKey(file.Name())
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, key) {
t.Errorf("Expected %v, Got %v", expected, key)
}
}
func TestListKeys(t *testing.T) {
name, err := ioutil.TempDir("", "deis-key")
if err != nil {
t.Fatal(err)
}
os.Setenv("HOME", name)
folder := path.Join(name, ".ssh")
if err = os.Mkdir(folder, 0755); err != nil {
t.Fatal(err)
}
toWrite := []byte("ssh-rsa abc test@example.com")
fileNames := []string{"test1.pub", "test2.pub"}
expected := []api.KeyCreateRequest{
api.KeyCreateRequest{
ID: "test@example.com",
Public: string(toWrite),
Name: path.Join(folder, fileNames[0]),
},
api.KeyCreateRequest{
ID: "test@example.com",
Public: string(toWrite),
Name: path.Join(folder, fileNames[1]),
},
}
for _, file := range fileNames {
if err = ioutil.WriteFile(path.Join(folder, file), toWrite, 0775); err != nil {
t.Fatal(err)
}
}
keys, err := listKeys()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, keys) {
t.Errorf("Expected %v, Got %v", expected, keys)
}
}