From b1eb6fd15902e2dcbf2de9f47c618fa6caea16e5 Mon Sep 17 00:00:00 2001 From: Philip Gai Date: Thu, 2 Jul 2026 10:49:31 -0500 Subject: [PATCH] test: add regression and coverage tests for ACTIONS_CACHE_MODE Add JobExtension L0 tests asserting the job-start cache-mode log line is emitted for each mode and absent when the variable is unset, plus handler regression tests covering coexistence with ACTIONS_CACHE_SERVICE_V2 and that baseline runtime env is unaffected when no cache-mode is set. --- src/Test/L0/Worker/HandlerL0.cs | 35 ++++++++++++++++++++ src/Test/L0/Worker/JobExtensionL0.cs | 49 ++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/Test/L0/Worker/HandlerL0.cs b/src/Test/L0/Worker/HandlerL0.cs index 111bfc524..f04e1b47f 100644 --- a/src/Test/L0/Worker/HandlerL0.cs +++ b/src/Test/L0/Worker/HandlerL0.cs @@ -141,6 +141,41 @@ namespace GitHub.Runner.Common.Tests.Worker } } + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async Task RunAsync_CacheModeCoexistsWithCacheServiceV2() + { + using (TestHostContext hc = CreateTestContext()) + { + var environment = await RunNodeScriptActionHandlerAsync(hc, new Dictionary + { + { "actions_uses_cache_service_v2", "true" }, + { "actions_cache_mode", "read" } + }); + + Assert.Equal(bool.TrueString, environment["ACTIONS_CACHE_SERVICE_V2"]); + Assert.Equal("read", environment["ACTIONS_CACHE_MODE"]); + } + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async Task RunAsync_DoesNotAffectRuntimeEnv_WhenCacheModeAbsent() + { + using (TestHostContext hc = CreateTestContext()) + { + var environment = await RunNodeScriptActionHandlerAsync(hc, new Dictionary()); + + // Baseline runtime env is still exported and cache-mode adds nothing. + Assert.Equal("https://pipelines.actions.githubusercontent.com/", environment["ACTIONS_RUNTIME_URL"]); + Assert.Equal("token", environment["ACTIONS_RUNTIME_TOKEN"]); + Assert.False(environment.ContainsKey("ACTIONS_CACHE_MODE")); + Assert.False(environment.ContainsKey("ACTIONS_CACHE_SERVICE_V2")); + } + } + private async Task> RunNodeScriptActionHandlerAsync(TestHostContext hc, IDictionary variables) { var actionDirectory = Path.Combine(hc.GetDirectory(WellKnownDirectory.Work), Guid.NewGuid().ToString()); diff --git a/src/Test/L0/Worker/JobExtensionL0.cs b/src/Test/L0/Worker/JobExtensionL0.cs index 7204ff5e9..8dfb7627e 100644 --- a/src/Test/L0/Worker/JobExtensionL0.cs +++ b/src/Test/L0/Worker/JobExtensionL0.cs @@ -238,6 +238,55 @@ namespace GitHub.Runner.Common.Tests.Worker } } + [Theory] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + [InlineData("read")] + [InlineData("none")] + [InlineData("write")] + [InlineData("write-only")] + public async Task InitializeJob_LogsCacheMode_WhenVariableSet(string mode) + { + using (TestHostContext hc = CreateTestContext()) + { + _message.Variables["actions_cache_mode"] = mode; + _jobEc.InitializeJob(_message, _tokenSource.Token); + + var jobExtension = new JobExtension(); + jobExtension.Initialize(hc); + + _actionManager.Setup(x => x.PrepareActionsAsync(It.IsAny(), It.IsAny>(), It.IsAny())) + .Returns(Task.FromResult(new PrepareResult(new List(), new Dictionary()))); + + await jobExtension.InitializeJob(_jobEc, _message); + + _jobServerQueue.Verify( + x => x.QueueWebConsoleLine(It.IsAny(), It.Is(m => m.Contains($"Actions cache-mode: {mode}")), It.IsAny()), + Times.Once); + } + } + + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async Task InitializeJob_DoesNotLogCacheMode_WhenVariableAbsent() + { + using (TestHostContext hc = CreateTestContext()) + { + var jobExtension = new JobExtension(); + jobExtension.Initialize(hc); + + _actionManager.Setup(x => x.PrepareActionsAsync(It.IsAny(), It.IsAny>(), It.IsAny())) + .Returns(Task.FromResult(new PrepareResult(new List(), new Dictionary()))); + + await jobExtension.InitializeJob(_jobEc, _message); + + _jobServerQueue.Verify( + x => x.QueueWebConsoleLine(It.IsAny(), It.Is(m => m.Contains("Actions cache-mode:")), It.IsAny()), + Times.Never); + } + } + [Fact] [Trait("Level", "L0")] [Trait("Category", "Worker")]