This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathssh_test.go
More file actions
185 lines (153 loc) · 4.29 KB
/
ssh_test.go
File metadata and controls
185 lines (153 loc) · 4.29 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
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
package main
import (
"io/ioutil"
"os"
"path"
"testing"
"koding/kites/tunnelproxy/discover/discovertest"
"koding/klient/kiteerrortypes"
"koding/klient/remote/restypes"
"koding/klient/testutil"
"koding/klient/util"
"koding/klientctl/klient"
"koding/klientctl/list"
"koding/klientctl/ssh"
"github.com/koding/kite/dnode"
. "github.com/smartystreets/goconvey/convey"
)
func TestSSHCommand(t *testing.T) {
Convey("", t, func() {
tempSSHDir, err := ioutil.TempDir("", "")
So(err, ShouldBeNil)
defer os.Remove(tempSSHDir)
teller := newFakeTransport()
s := ssh.SSHCommand{
SSHKey: &ssh.SSHKey{
Log: testutil.DiscardLogger,
KeyPath: tempSSHDir,
KeyName: "key",
// Create a klient, with the fake transport to satisfy the Teller interface.
Klient: &klient.Klient{
Teller: teller,
},
},
}
Convey("Given PrepareForSSH is called", func() {
Convey("When it returns a dialing error", func() {
kiteErr := util.KiteErrorf(
kiteerrortypes.DialingFailed, "Failed to dial.",
)
teller.TripErrors["remote.sshKeysAdd"] = kiteErr
teller.TripResponses["remote.currentUsername"] = &dnode.Partial{
Raw: []byte(`"foo"`),
}
Convey("It should return ErrRemoteDialingFailed", func() {
So(s.PrepareForSSH("foo"), ShouldEqual, kiteErr)
})
})
})
Convey("It should return public key path", func() {
key := s.PublicKeyPath()
So(key, ShouldEqual, path.Join(tempSSHDir, "key.pub"))
})
Convey("It should return private key path", func() {
key := s.PrivateKeyPath()
So(key, ShouldEqual, path.Join(tempSSHDir, "key"))
})
Convey("It should return error if invalid key exists", func() {
err := ioutil.WriteFile(s.PrivateKeyPath(), []byte("a"), 0700)
So(err, ShouldBeNil)
err = ioutil.WriteFile(s.PublicKeyPath(), []byte("a"), 0700)
So(err, ShouldBeNil)
err = s.PrepareForSSH("name")
So(err, ShouldNotBeNil)
So(os.IsExist(err), ShouldBeFalse)
})
Convey("It should create ssh folder if it doesn't exist", func() {
teller.TripResponses["remote.currentUsername"] = &dnode.Partial{
Raw: []byte(`"foo"`),
}
So(s.PrepareForSSH("name"), ShouldBeNil)
_, err := os.Stat(s.KeyPath)
So(err, ShouldBeNil)
})
Convey("It generates and saves key to remote if key doesn't exist", func() {
teller.TripResponses["remote.currentUsername"] = &dnode.Partial{
Raw: []byte(`"foo"`),
}
err := s.PrepareForSSH("name")
So(err, ShouldBeNil)
firstContents, err := ioutil.ReadFile(s.PublicKeyPath())
So(err, ShouldBeNil)
publicExists := s.PublicKeyExists()
So(publicExists, ShouldBeTrue)
privateExists := s.PrivateKeyExists()
So(privateExists, ShouldBeTrue)
Convey("It returns key if it exists", func() {
err := s.PrepareForSSH("name")
So(err, ShouldBeNil)
secondContents, err := ioutil.ReadFile(s.PublicKeyPath())
So(err, ShouldBeNil)
So(string(firstContents), ShouldEqual, string(secondContents))
})
})
})
}
func TestSSHKey(t *testing.T) {
Convey("It should discover remote IP for tunneled connection", t, func() {
srv := discovertest.Server{
"ssh": {{
Addr: "apple.rafal.grape.koding.me:2222",
Local: false,
}},
}
l, err := srv.Start()
So(err, ShouldBeNil)
defer l.Close()
k := &fakeKlient{
RemoteUsername: "root",
Remotes: list.KiteInfos{{
ListMachineInfo: restypes.ListMachineInfo{
Hostname: "root",
VMName: "remote",
IP: l.Addr().String(),
},
}},
}
s := ssh.SSHKey{
Klient: k,
}
userhost, port, err := s.GetSSHAddr("remote")
So(err, ShouldBeNil)
So(userhost, ShouldEqual, "root@apple.rafal.grape.koding.me")
So(port, ShouldEqual, "2222")
})
Convey("It should discover local IP for tunneled connection", t, func() {
srv := discovertest.Server{
"ssh": {{
Addr: "127.0.0.1:2222",
Local: true,
}},
}
l, err := srv.Start()
So(err, ShouldBeNil)
defer l.Close()
k := &fakeKlient{
RemoteUsername: "root",
Remotes: list.KiteInfos{{
ListMachineInfo: restypes.ListMachineInfo{
Hostname: "root",
VMName: "local",
IP: l.Addr().String(),
},
}},
}
s := ssh.SSHKey{
Klient: k,
}
userhost, port, err := s.GetSSHAddr("local")
So(err, ShouldBeNil)
So(userhost, ShouldEqual, "root@127.0.0.1")
So(port, ShouldEqual, "2222")
})
}