Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a flaky test #234

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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