diff --git a/agent/integration/job_environment_integration_test.go b/agent/integration/job_environment_integration_test.go index 7cd36bde2f..cb20646e8e 100644 --- a/agent/integration/job_environment_integration_test.go +++ b/agent/integration/job_environment_integration_test.go @@ -2,6 +2,7 @@ package integration import ( "context" + "strings" "testing" "github.com/buildkite/agent/v3/agent" @@ -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() diff --git a/agent/run_job.go b/agent/run_job.go index 09296ac3be..28eff695c5 100644 --- a/agent/run_job.go +++ b/agent/run_job.go @@ -9,6 +9,7 @@ import ( "os" "regexp" "strconv" + "strings" "sync" "time" @@ -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())