Commit 29d303c5 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Refactor tests and add comments

parent 7113b09d
...@@ -23,6 +23,12 @@ const testRepo = "test.git" ...@@ -23,6 +23,12 @@ const testRepo = "test.git"
var remote = fmt.Sprintf("http://%s/%s", servAddr, testRepo) var remote = fmt.Sprintf("http://%s/%s", servAddr, testRepo)
func TestAllowedClone(t *testing.T) { func TestAllowedClone(t *testing.T) {
// Prepare clone directory
if err := os.RemoveAll(scratchDir); err != nil {
t.Fatal(err)
}
// Prepare test server and backend
ts := testAuthServer(200, `{"GL_ID":"user-123"}`) ts := testAuthServer(200, `{"GL_ID":"user-123"}`)
defer ts.Close() defer ts.Close()
cmd, err := startServer(ts) cmd, err := startServer(ts)
...@@ -30,9 +36,11 @@ func TestAllowedClone(t *testing.T) { ...@@ -30,9 +36,11 @@ func TestAllowedClone(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer cleanUpProcessGroup(cmd) defer cleanUpProcessGroup(cmd)
if err := os.RemoveAll(scratchDir); err != nil { if err := waitServer(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Do the git clone
cloneCmd := exec.Command("git", "clone", remote, path.Join(scratchDir, "test")) cloneCmd := exec.Command("git", "clone", remote, path.Join(scratchDir, "test"))
if out, err := cloneCmd.CombinedOutput(); err != nil { if out, err := cloneCmd.CombinedOutput(); err != nil {
t.Logf("%s", out) t.Logf("%s", out)
...@@ -67,6 +75,9 @@ func TestAllowedPush(t *testing.T) { ...@@ -67,6 +75,9 @@ func TestAllowedPush(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer cleanUpProcessGroup(cmd) defer cleanUpProcessGroup(cmd)
if err := waitServer(); err != nil {
t.Fatal(err)
}
// Perform the git push // Perform the git push
pushCmd := exec.Command("git", "push", remote, branch) pushCmd := exec.Command("git", "push", remote, branch)
...@@ -84,29 +95,22 @@ func testAuthServer(code int, body string) *httptest.Server { ...@@ -84,29 +95,22 @@ func testAuthServer(code int, body string) *httptest.Server {
})) }))
} }
func startServer(ts *httptest.Server) (cmd *exec.Cmd, err error) { func startServer(ts *httptest.Server) (*exec.Cmd, error) {
var conn net.Conn cmd := exec.Command("go", "run", "main.go", fmt.Sprintf("-authBackend=%s", ts.URL), fmt.Sprintf("-listenAddr=%s", servAddr), testRepoRoot)
// Start our server process
cmd = exec.Command("go", "run", "main.go", fmt.Sprintf("-authBackend=%s", ts.URL), fmt.Sprintf("-listenAddr=%s", servAddr), testRepoRoot)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start() return cmd, cmd.Start()
if err != nil { }
return
} func waitServer() (err error) {
var conn net.Conn
// Wait for the server to start accepting TCP connections
for i := 0; i < servWaitListen/servWaitSleep; i++ { for i := 0; i < servWaitListen/servWaitSleep; i++ {
conn, err = net.Dial("tcp", servAddr) conn, err = net.Dial("tcp", servAddr)
if err == nil { if err == nil {
conn.Close() conn.Close()
break return
} }
time.Sleep(servWaitSleep * time.Millisecond) time.Sleep(servWaitSleep * time.Millisecond)
} }
if err != nil {
cleanUpProcessGroup(cmd)
}
return return
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment