Skip to content
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
129 changes: 129 additions & 0 deletions agent/integration/job_environment_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"context"
"strings"
"testing"

"github.com/buildkite/agent/v3/agent"
Expand Down Expand Up @@ -52,6 +53,134 @@ func TestWhenCachePathsSetInJobStep_CachePathsEnvVarIsSet(t *testing.T) {
}
}

func TestCacheSettingsOnSelfHosted_LogsMessage(t *testing.T) {
t.Parallel()

ctx := context.Background()
jobID := "cache-self-hosted-job"
job := &api.Job{
ID: jobID,
ChunksMaxSizeBytes: 1024,
Env: map[string]string{
"BUILDKITE_COMPUTE_TYPE": "self-hosted",
},
Step: pipeline.CommandStep{
Cache: &pipeline.Cache{
Paths: []string{"vendor", "node_modules"},
},
},
Token: "bkaj_job-token",
}

mb := mockBootstrap(t)
defer mb.CheckAndClose(t) //nolint:errcheck // bintest logs to t
mb.Expect().Once().AndExitWith(0)

e := createTestAgentEndpoint()
server := e.server()
defer server.Close()

err := runJob(t, ctx, testRunJobConfig{
job: job,
server: server,
agentCfg: agent.AgentConfiguration{},
mockBootstrap: mb,
})
if err != nil {
t.Fatalf("runJob() error = %v", err)
}

logs := e.logsFor(t, jobID)
if !strings.Contains(logs, "Cache settings detected on self-hosted agent") {
t.Errorf("expected logs to contain cache warning for self-hosted agent, got %q", logs)
}
if !strings.Contains(logs, "vendor, node_modules") {
t.Errorf("expected logs to contain cache paths, got %q", logs)
}
}

func TestCacheSettingsOnHosted_DoesNotLogMessage(t *testing.T) {
t.Parallel()

ctx := context.Background()
jobID := "cache-hosted-job"
job := &api.Job{
ID: jobID,
ChunksMaxSizeBytes: 1024,
Env: map[string]string{
"BUILDKITE_COMPUTE_TYPE": "hosted",
},
Step: pipeline.CommandStep{
Cache: &pipeline.Cache{
Paths: []string{"vendor", "node_modules"},
},
},
Token: "bkaj_job-token",
}

mb := mockBootstrap(t)
defer mb.CheckAndClose(t) //nolint:errcheck // bintest logs to t
mb.Expect().Once().AndExitWith(0)

e := createTestAgentEndpoint()
server := e.server()
defer server.Close()

err := runJob(t, ctx, testRunJobConfig{
job: job,
server: server,
agentCfg: agent.AgentConfiguration{},
mockBootstrap: mb,
})
if err != nil {
t.Fatalf("runJob() error = %v", err)
}

logs := e.logsFor(t, jobID)
if strings.Contains(logs, "Cache settings detected on self-hosted agent") {
t.Errorf("expected logs to NOT contain cache warning for hosted agent, got %q", logs)
}
}

func TestNoCacheSettings_DoesNotLogMessage(t *testing.T) {
t.Parallel()

ctx := context.Background()
jobID := "no-cache-job"
job := &api.Job{
ID: jobID,
ChunksMaxSizeBytes: 1024,
Env: map[string]string{
"BUILDKITE_COMPUTE_TYPE": "self-hosted",
},
Step: pipeline.CommandStep{},
Token: "bkaj_job-token",
}

mb := mockBootstrap(t)
defer mb.CheckAndClose(t) //nolint:errcheck // bintest logs to t
mb.Expect().Once().AndExitWith(0)

e := createTestAgentEndpoint()
server := e.server()
defer server.Close()

err := runJob(t, ctx, testRunJobConfig{
job: job,
server: server,
agentCfg: agent.AgentConfiguration{},
mockBootstrap: mb,
})
if err != nil {
t.Fatalf("runJob() error = %v", err)
}

logs := e.logsFor(t, jobID)
if strings.Contains(logs, "Cache settings detected on self-hosted agent") {
t.Errorf("expected logs to NOT contain cache warning when no cache settings, got %q", logs)
}
}

func TestBuildkiteRequestHeaders(t *testing.T) {
t.Parallel()

Expand Down
10 changes: 10 additions & 0 deletions agent/run_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -169,6 +170,15 @@ func (r *JobRunner) Run(ctx context.Context, ignoreAgentInDispatches *bool) (err
}
}

// Log a message if the job has cache settings but is running on a self-hosted agent.
if cache := job.Step.Cache; cache != nil && !cache.Disabled && len(cache.Paths) > 0 {
if job.Env["BUILDKITE_COMPUTE_TYPE"] == "self-hosted" {
fmt.Fprintln(r.jobLogs, "+++ ⚠️ Cache settings detected on self-hosted agent")
fmt.Fprintf(r.jobLogs, "cache paths: %s\n", strings.Join(cache.Paths, ", "))
r.agentLogger.Info("Job %s has cache settings but is running on a self-hosted agent", job.ID)
}
}

// Validate the repository if the list of allowed repositories is set.
if err := r.validateConfigAllowlists(job); err != nil {
fmt.Fprintln(r.jobLogs, err.Error())
Expand Down