Skip to content

Commit

Permalink
fix a flaky test (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman authored Jan 17, 2024
1 parent a7441b2 commit 62f8e6a
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions internal/infra/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package infra

import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"reflect"
Expand All @@ -14,53 +17,50 @@ import (
)

func Test_checkCredAccess(t *testing.T) {
addr := "127.0.0.1:3000"

startTestServer := func() *http.Server {
testServer := &http.Server{
ReadHeaderTimeout: time.Second,
Addr: addr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-OAuth-Scopes", "repo, write:packages")
_, _ = w.Write([]byte("SUCCESS"))
}),
}
go func() {
_ = testServer.ListenAndServe()
}()
time.Sleep(1 * time.Millisecond) // allow time for the server to start
return testServer
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal("Failed to create listener: ", err.Error())
}

testServer := &http.Server{
ReadHeaderTimeout: time.Second,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-OAuth-Scopes", "repo, write:packages")
_, _ = w.Write([]byte("SUCCESS"))
}),
}
go func() {
_ = testServer.Serve(l)
}()

t.Cleanup(func() {
testServer.Shutdown(context.Background())
l.Close()
})

addr := fmt.Sprintf("http://127.0.0.1:%v", l.Addr().(*net.TCPAddr).Port)

t.Run("returns error if the credential has write access", func(t *testing.T) {
defaultApiEndpoint = "http://127.0.0.1:3000"
testServer := startTestServer()
defer func() {
_ = testServer.Shutdown(context.Background())
}()
defaultApiEndpoint = addr

credentials := []model.Credential{{
"token": "ghp_fake",
}}
err := checkCredAccess(context.Background(), nil, credentials)
if err != ErrWriteAccess {
if !errors.Is(err, ErrWriteAccess) {
t.Error("unexpected error", err)
}
})

t.Run("it works with GitHub Enterprise", func(t *testing.T) {
testServer := startTestServer()
defer func() {
_ = testServer.Shutdown(context.Background())
}()
defaultApiEndpoint = "http://example.com" // ensure it's not used

credentials := []model.Credential{{
"token": "ghp_fake",
}}
apiEndpoint := "http://" + addr
job := &model.Job{Source: model.Source{APIEndpoint: &apiEndpoint}}
job := &model.Job{Source: model.Source{APIEndpoint: &addr}}
err := checkCredAccess(context.Background(), job, credentials)
if err != ErrWriteAccess {
if !errors.Is(err, ErrWriteAccess) {
t.Error("unexpected error", err)
}
})
Expand Down

0 comments on commit 62f8e6a

Please sign in to comment.