Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 336a7d1

Browse files
committed
transport: ssh, mocked SSH server, fixes #332
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 47fc5cb commit 336a7d1

File tree

2 files changed

+107
-25
lines changed

2 files changed

+107
-25
lines changed

.travis.yml

-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ before_install:
2323
- git config --global user.email "[email protected]"
2424
- git config --global user.name "Travis CI"
2525

26-
# we only decrypt the SSH key when we aren't in a pull request
27-
- >
28-
if [ "$TRAVIS_PULL_REQUEST" = "false" ] ; then \
29-
bash .travis/install_key.sh; \
30-
export SSH_TEST_PRIVATE_KEY=$HOME/.travis/deploy.pem; \
31-
else \
32-
export SSH_AUTH_SOCK=""; \
33-
fi
34-
3526
install:
3627
- go get -v -t ./...
3728

+107-16
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,138 @@
11
package ssh
22

33
import (
4+
"fmt"
5+
"io"
6+
"io/ioutil"
7+
"log"
8+
"net"
49
"os"
10+
"os/exec"
11+
"path/filepath"
12+
"strings"
513

614
"gopkg.in/src-d/go-git.v4/plumbing/transport"
715
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"
816

17+
"github.com/gliderlabs/ssh"
18+
"github.com/src-d/go-git-fixtures"
19+
stdssh "golang.org/x/crypto/ssh"
920
. "gopkg.in/check.v1"
1021
)
1122

1223
type UploadPackSuite struct {
1324
test.UploadPackSuite
25+
fixtures.Suite
26+
27+
port int
28+
base string
1429
}
1530

1631
var _ = Suite(&UploadPackSuite{})
1732

1833
func (s *UploadPackSuite) SetUpSuite(c *C) {
19-
s.setAuthBuilder(c)
20-
s.UploadPackSuite.Client = DefaultClient
34+
s.Suite.SetUpSuite(c)
35+
36+
l, err := net.Listen("tcp", "localhost:0")
37+
c.Assert(err, IsNil)
38+
39+
s.port = l.Addr().(*net.TCPAddr).Port
40+
s.base, err = ioutil.TempDir(os.TempDir(), fmt.Sprintf("go-git-ssh-%d", s.port))
41+
c.Assert(err, IsNil)
42+
43+
DefaultAuthBuilder = func(user string) (AuthMethod, error) {
44+
return &Password{User: user}, nil
45+
}
46+
47+
s.UploadPackSuite.Client = NewClient(&stdssh.ClientConfig{
48+
HostKeyCallback: stdssh.InsecureIgnoreHostKey(),
49+
})
50+
51+
s.UploadPackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
52+
s.UploadPackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
53+
s.UploadPackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
2154

22-
ep, err := transport.NewEndpoint("[email protected]:git-fixtures/basic.git")
55+
server := &ssh.Server{Handler: handlerSSH}
56+
go func() {
57+
log.Fatal(server.Serve(l))
58+
}()
59+
}
60+
61+
func (s *UploadPackSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
62+
fs := f.DotGit()
63+
64+
err := fixtures.EnsureIsBare(fs)
2365
c.Assert(err, IsNil)
24-
s.UploadPackSuite.Endpoint = ep
2566

26-
ep, err = transport.NewEndpoint("[email protected]:git-fixtures/empty.git")
67+
path := filepath.Join(s.base, name)
68+
err = os.Rename(fs.Root(), path)
2769
c.Assert(err, IsNil)
28-
s.UploadPackSuite.EmptyEndpoint = ep
2970

30-
ep, err = transport.NewEndpoint("[email protected]:git-fixtures/non-existent.git")
71+
return s.newEndpoint(c, name)
72+
}
73+
74+
func (s *UploadPackSuite) newEndpoint(c *C, name string) transport.Endpoint {
75+
ep, err := transport.NewEndpoint(fmt.Sprintf(
76+
"ssh://git@localhost:%d/%s/%s", s.port, filepath.ToSlash(s.base), name,
77+
))
78+
3179
c.Assert(err, IsNil)
32-
s.UploadPackSuite.NonExistentEndpoint = ep
80+
return ep
81+
}
82+
83+
func handlerSSH(s ssh.Session) {
84+
cmd, stdin, stderr, stdout, err := buildCommand(s.Command())
85+
if err != nil {
86+
fmt.Println(err)
87+
return
88+
}
89+
90+
if err := cmd.Start(); err != nil {
91+
fmt.Println(err)
92+
return
93+
}
94+
95+
go func() {
96+
defer stdin.Close()
97+
io.Copy(stdin, s)
98+
}()
99+
100+
go func() {
101+
defer stderr.Close()
102+
io.Copy(s.Stderr(), stderr)
103+
}()
104+
105+
defer stdout.Close()
106+
io.Copy(s, stdout)
107+
108+
if err := cmd.Wait(); err != nil {
109+
return
110+
}
33111
}
34112

35-
func (s *UploadPackSuite) setAuthBuilder(c *C) {
36-
privateKey := os.Getenv("SSH_TEST_PRIVATE_KEY")
37-
if privateKey != "" {
38-
DefaultAuthBuilder = func(user string) (AuthMethod, error) {
39-
return NewPublicKeysFromFile(user, privateKey, "")
40-
}
113+
func buildCommand(c []string) (cmd *exec.Cmd, stdin io.WriteCloser, stderr, stdout io.ReadCloser, err error) {
114+
if len(c) != 2 {
115+
err = fmt.Errorf("invalid command")
116+
return
117+
}
118+
119+
path := strings.Replace(c[1], "/C:/", "C:/", 1)
120+
fmt.Println(c, path)
121+
cmd = exec.Command(c[0], path)
122+
stdout, err = cmd.StdoutPipe()
123+
if err != nil {
124+
return
41125
}
42126

43-
if privateKey == "" && os.Getenv("SSH_AUTH_SOCK") == "" {
44-
c.Skip("SSH_AUTH_SOCK or SSH_TEST_PRIVATE_KEY are required")
127+
stdin, err = cmd.StdinPipe()
128+
if err != nil {
45129
return
46130
}
131+
132+
stderr, err = cmd.StderrPipe()
133+
if err != nil {
134+
return
135+
}
136+
137+
return
47138
}

0 commit comments

Comments
 (0)