|
1 | 1 | package ssh
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net" |
4 | 9 | "os"
|
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
5 | 13 |
|
6 | 14 | "gopkg.in/src-d/go-git.v4/plumbing/transport"
|
7 | 15 | "gopkg.in/src-d/go-git.v4/plumbing/transport/test"
|
8 | 16 |
|
| 17 | + "github.com/gliderlabs/ssh" |
| 18 | + "github.com/src-d/go-git-fixtures" |
| 19 | + stdssh "golang.org/x/crypto/ssh" |
9 | 20 | . "gopkg.in/check.v1"
|
10 | 21 | )
|
11 | 22 |
|
12 | 23 | type UploadPackSuite struct {
|
13 | 24 | test.UploadPackSuite
|
| 25 | + fixtures.Suite |
| 26 | + |
| 27 | + port int |
| 28 | + base string |
14 | 29 | }
|
15 | 30 |
|
16 | 31 | var _ = Suite(&UploadPackSuite{})
|
17 | 32 |
|
18 | 33 | 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") |
21 | 54 |
|
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) |
23 | 65 | c.Assert(err, IsNil)
|
24 |
| - s.UploadPackSuite.Endpoint = ep |
25 | 66 |
|
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) |
27 | 69 | c.Assert(err, IsNil)
|
28 |
| - s.UploadPackSuite.EmptyEndpoint = ep |
29 | 70 |
|
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 | + |
31 | 79 | 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 | + } |
33 | 111 | }
|
34 | 112 |
|
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 |
41 | 125 | }
|
42 | 126 |
|
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 { |
45 | 129 | return
|
46 | 130 | }
|
| 131 | + |
| 132 | + stderr, err = cmd.StderrPipe() |
| 133 | + if err != nil { |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + return |
47 | 138 | }
|
0 commit comments