Skip to content

Commit 0692c61

Browse files
author
Aaron Schlesinger
committed
fix(pkg/sshd/server_test.go): add test for concurrent deletes
1 parent c40c5b8 commit 0692c61

1 file changed

Lines changed: 68 additions & 4 deletions

File tree

pkg/sshd/server_test.go

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
const (
1515
testingServerAddr = "127.0.0.1:2244"
1616
testingServerAddr2 = "127.0.0.1:2245"
17+
testingServerAddr3 = "127.0.0.1:2246"
18+
gitHome = "/git"
1719
)
1820

1921
// TestServer tests the SSH server.
@@ -35,7 +37,9 @@ func TestReceive(t *testing.T) {
3537
cfg.AddHostKey(key)
3638

3739
c := NewCircuit()
38-
cxt := runServer(&cfg, c, testingServerAddr, t)
40+
pushLock := NewInMemoryRepositoryLock()
41+
deleteLock := NewInMemoryRepositoryLock()
42+
cxt := runServer(&cfg, c, pushLock, deleteLock, testingServerAddr, t)
3943

4044
// Give server time to initialize.
4145
time.Sleep(200 * time.Millisecond)
@@ -94,7 +98,9 @@ func TestPush(t *testing.T) {
9498
cfg.AddHostKey(key)
9599

96100
c := NewCircuit()
97-
runServer(&cfg, c, testingServerAddr2, t)
101+
pushLock := NewInMemoryRepositoryLock()
102+
deleteLock := NewInMemoryRepositoryLock()
103+
runServer(&cfg, c, pushLock, deleteLock, testingServerAddr2, t)
98104

99105
// Give server time to initialize.
100106
time.Sleep(200 * time.Millisecond)
@@ -146,12 +152,70 @@ func TestPush(t *testing.T) {
146152
sess.Close()
147153
}
148154

155+
func TestDelete(t *testing.T) {
156+
key, err := sshTestingHostKey()
157+
if err != nil {
158+
t.Fatal(err)
159+
}
160+
161+
cfg := ssh.ServerConfig{
162+
NoClientAuth: true,
163+
}
164+
cfg.AddHostKey(key)
165+
166+
c := NewCircuit()
167+
pushLock := NewInMemoryRepositoryLock()
168+
deleteLock := NewInMemoryRepositoryLock()
169+
runServer(&cfg, c, pushLock, deleteLock, testingServerAddr3, t)
170+
171+
// Give server time to initialize.
172+
time.Sleep(200 * time.Millisecond)
173+
174+
if c.State() != ClosedState {
175+
t.Fatalf("circuit was not in closed state")
176+
}
177+
178+
// Connect to the server and issue env var set. This should return true.
179+
client, err := ssh.Dial("tcp", testingServerAddr2, &ssh.ClientConfig{})
180+
if err != nil {
181+
t.Fatalf("Failed to connect client to local server: %s", err)
182+
}
183+
sess, err := client.NewSession()
184+
if err != nil {
185+
t.Fatalf("Failed to create client session: %s", err)
186+
}
187+
188+
// check for invalid length of arguments
189+
if out, err := sess.Output("git-upload-pack"); err == nil {
190+
t.Errorf("Expected an error but '%s' was received", out)
191+
} else if string(out) != "" {
192+
t.Errorf("Expected , got '%s'", out)
193+
}
194+
sess.Close()
195+
196+
repoName := "demo"
197+
if err := deleteLock.Lock(repoName, time.Duration(0)); err != nil {
198+
t.Fatalf("Error locking the delete lock (%s)", err)
199+
}
200+
201+
sess, err = client.NewSession()
202+
if err != nil {
203+
t.Fatalf("Failed to create client session: %s", err)
204+
}
205+
if out, err := sess.Output("git-upload-pack /" + repoName + ".git"); err == nil {
206+
t.Error("Expected concurrent delete error, got nothing")
207+
} else if string(out) == "OK" {
208+
t.Errorf("Expected error output, got %s", string(out))
209+
}
210+
sess.Close()
211+
}
212+
149213
// sshTestingHostKey loads the testing key.
150214
func sshTestingHostKey() (ssh.Signer, error) {
151215
return ssh.ParsePrivateKey([]byte(testingHostKey))
152216
}
153217

154-
func runServer(config *ssh.ServerConfig, c *Circuit, testAddr string, t *testing.T) cookoo.Context {
218+
func runServer(config *ssh.ServerConfig, c *Circuit, pushLock RepositoryLock, deleteLock RepositoryLock, testAddr string, t *testing.T) cookoo.Context {
155219
reg, router, cxt := cookoo.Cookoo()
156220
cxt.Put(ServerConfig, config)
157221
cxt.Put(Address, testAddr)
@@ -206,7 +270,7 @@ func runServer(config *ssh.ServerConfig, c *Circuit, testAddr string, t *testing
206270
})
207271

208272
go func() {
209-
if err := Serve(reg, router, c, cxt); err != nil {
273+
if err := Serve(reg, router, c, gitHome, pushLock, deleteLock, cxt); err != nil {
210274
t.Fatalf("Failed serving with %s", err)
211275
}
212276
}()

0 commit comments

Comments
 (0)