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

Emit a better error if the job API token is missing #2789

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
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
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