Skip to content

Commit

Permalink
Merge pull request #2789 from buildkite/better-no-job-socket-token-error
Browse files Browse the repository at this point in the history
Emit a better error if the job API token is missing
  • Loading branch information
moskyb authored May 22, 2024
2 parents 8228511 + 3b0e6e5 commit d31d6cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 8 additions & 3 deletions jobapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const (
redactionsURL = "http://job/api/current-job/v0/redactions"
)

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

// Client connects to the Job API.
type Client struct {
Expand All @@ -36,12 +39,14 @@ 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 "", "", errNoSocketEnv
return "", "", errNoJobAPISocketEnv
}

token = os.Getenv("BUILDKITE_AGENT_JOB_API_TOKEN")
if token == "" {
return "", "", errNoSocketEnv
return "", "", errNoJobAPITokenEnv
}

return path, token, nil
}

Expand Down
20 changes: 17 additions & 3 deletions jobapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,27 @@ func (f *fakeServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func TestClient_NoSocket(t *testing.T) {
// t.Parallel() // Can't be parallelised, because it uses the t.Setenv() function

ctx, canc := context.WithTimeout(context.Background(), 10*time.Second)
t.Cleanup(canc)

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

func TestClient_NoToken(t *testing.T) {
// t.Parallel() // Can't be parallelised, because it uses the t.Setenv() function

ctx, canc := context.WithTimeout(context.Background(), 10*time.Second)
t.Cleanup(canc)

t.Setenv("BUILDKITE_AGENT_JOB_API_SOCKET", "/tmp/fake-socket") // Just to make sure it's set
t.Setenv("BUILDKITE_AGENT_JOB_API_TOKEN", "") // This may be set if the test is being run by a buildkite agent!

_, err := NewDefaultClient(ctx)
assert.ErrorIs(t, err, errNoSocketEnv, "NewDefaultClient() error = %v, want %v", err, errNoSocketEnv)
assert.ErrorIs(t, err, errNoJobAPITokenEnv, "NewDefaultClient() error = %v, want %v", err, errNoJobAPITokenEnv)
}

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

0 comments on commit d31d6cc

Please sign in to comment.