Skip to content

Commit

Permalink
Merge pull request #2651 from buildkite/tiarius/fix-job-api-socket-test
Browse files Browse the repository at this point in the history
Fix test of JobAPI requiring socket set
  • Loading branch information
triarius authored Feb 23, 2024
2 parents 2d9bef5 + 76a1b9e commit 625af69
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 4 additions & 2 deletions jobapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

const envURL = "http://job/api/current-job/v0/env"

var errNoSocketEnv = errors.New("BUILDKITE_AGENT_JOB_API_SOCKET empty or undefined")

// Client connects to the Job API.
type Client struct {
client *socket.Client
Expand All @@ -30,11 +32,11 @@ func NewDefaultClient(ctx context.Context) (*Client, error) {
func DefaultSocketPath() (path, token string, err error) {
path = os.Getenv("BUILDKITE_AGENT_JOB_API_SOCKET")
if path == "" {
return "", "", errors.New("BUILDKITE_AGENT_JOB_API_SOCKET empty or undefined")
return "", "", errNoSocketEnv
}
token = os.Getenv("BUILDKITE_AGENT_JOB_API_TOKEN")
if token == "" {
return "", "", errors.New("BUILDKITE_AGENT_JOB_API_TOKEN empty or undefined")
return "", "", errNoSocketEnv
}
return path, token, nil
}
Expand Down
8 changes: 5 additions & 3 deletions jobapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/buildkite/agent/v3/internal/socket"
"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
)

type fakeServer struct {
Expand Down Expand Up @@ -126,9 +127,10 @@ func TestClient_NoSocket(t *testing.T) {
ctx, canc := context.WithTimeout(context.Background(), 10*time.Second)
t.Cleanup(canc)

if _, err := NewDefaultClient(ctx); err == nil {
t.Errorf("NewDefaultClient(ctx) error = %v, want nil", err)
}
// This may be set if the test is being run by a buildkite agent!
os.Unsetenv("BUILDKITE_AGENT_JOB_API_SOCKET")
_, err := NewDefaultClient(ctx)
assert.ErrorIs(t, err, errNoSocketEnv, "NewDefaultClient() error = %v, want %v", err, errNoSocketEnv)
}

func TestClientEnvGet(t *testing.T) {
Expand Down

0 comments on commit 625af69

Please sign in to comment.