mirror of
https://github.com/actions/runner.git
synced 2026-01-10 04:28:20 +08:00
WIP add test
This commit is contained in:
@@ -21,4 +21,4 @@
|
|||||||
},
|
},
|
||||||
"postCreateCommand": "dotnet restore src/Test && dotnet restore src/Runner.PluginHost",
|
"postCreateCommand": "dotnet restore src/Test && dotnet restore src/Runner.PluginHost",
|
||||||
"remoteUser": "vscode"
|
"remoteUser": "vscode"
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ namespace GitHub.Runner.Common
|
|||||||
[ServiceLocator(Default = typeof(BrokerServer))]
|
[ServiceLocator(Default = typeof(BrokerServer))]
|
||||||
public interface IBrokerServer : IRunnerService
|
public interface IBrokerServer : IRunnerService
|
||||||
{
|
{
|
||||||
Task ConnectAsync(Uri serverUrl, VssCredentials credentials);
|
Task<BrokerSession> ConnectAsync(Uri serverUrl, VssCredentials credentials);
|
||||||
|
|
||||||
Task<TaskAgentMessage> GetRunnerMessageAsync(CancellationToken token, TaskAgentStatus status, string version);
|
Task<TaskAgentMessage> GetRunnerMessageAsync(CancellationToken token, TaskAgentStatus status, string version);
|
||||||
}
|
}
|
||||||
@@ -26,19 +26,25 @@ namespace GitHub.Runner.Common
|
|||||||
private Uri _brokerUri;
|
private Uri _brokerUri;
|
||||||
private RawConnection _connection;
|
private RawConnection _connection;
|
||||||
private BrokerHttpClient _brokerHttpClient;
|
private BrokerHttpClient _brokerHttpClient;
|
||||||
|
private bool _hasSession;
|
||||||
|
private BrokerSession _session;
|
||||||
|
|
||||||
public async Task ConnectAsync(Uri serverUri, VssCredentials credentials)
|
public async Task<BrokerSession> ConnectAsync(Uri serverUri, VssCredentials credentials)
|
||||||
{
|
{
|
||||||
_brokerUri = serverUri;
|
_brokerUri = serverUri;
|
||||||
|
|
||||||
_connection = VssUtil.CreateRawConnection(serverUri, credentials);
|
_connection = VssUtil.CreateRawConnection(serverUri, credentials);
|
||||||
_brokerHttpClient = await _connection.GetClientAsync<BrokerHttpClient>();
|
_brokerHttpClient = await _connection.GetClientAsync<BrokerHttpClient>();
|
||||||
|
_session = await _brokerHttpClient.CreateSessionAsync();
|
||||||
_hasConnection = true;
|
_hasConnection = true;
|
||||||
|
_hasSession = true;
|
||||||
|
|
||||||
|
return _session;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckConnection()
|
private void CheckConnection()
|
||||||
{
|
{
|
||||||
if (!_hasConnection)
|
if (!_hasConnection || !_hasSession)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"SetConnection");
|
throw new InvalidOperationException($"SetConnection");
|
||||||
}
|
}
|
||||||
@@ -48,7 +54,7 @@ namespace GitHub.Runner.Common
|
|||||||
{
|
{
|
||||||
CheckConnection();
|
CheckConnection();
|
||||||
var jobMessage = RetryRequest<TaskAgentMessage>(
|
var jobMessage = RetryRequest<TaskAgentMessage>(
|
||||||
async () => await _brokerHttpClient.GetRunnerMessageAsync(version, status, cancellationToken), cancellationToken);
|
async () => await _brokerHttpClient.GetRunnerMessageAsync(_session.id, version, status, cancellationToken), cancellationToken);
|
||||||
|
|
||||||
return jobMessage;
|
return jobMessage;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ namespace GitHub.Runner.Listener
|
|||||||
private CancellationTokenSource _getMessagesTokenSource;
|
private CancellationTokenSource _getMessagesTokenSource;
|
||||||
private IBrokerServer _brokerServer;
|
private IBrokerServer _brokerServer;
|
||||||
|
|
||||||
|
public string _sessionId;
|
||||||
|
|
||||||
public override void Initialize(IHostContext hostContext)
|
public override void Initialize(IHostContext hostContext)
|
||||||
{
|
{
|
||||||
base.Initialize(hostContext);
|
base.Initialize(hostContext);
|
||||||
@@ -203,7 +205,8 @@ namespace GitHub.Runner.Listener
|
|||||||
|
|
||||||
var credMgr = HostContext.GetService<ICredentialManager>();
|
var credMgr = HostContext.GetService<ICredentialManager>();
|
||||||
VssCredentials creds = credMgr.LoadCredentials();
|
VssCredentials creds = credMgr.LoadCredentials();
|
||||||
await _brokerServer.ConnectAsync(new Uri(_settings.ServerUrlV2), creds);
|
var sessionResponse = await _brokerServer.ConnectAsync(new Uri(_settings.ServerUrlV2), creds);
|
||||||
|
_sessionId = sessionResponse.id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,32 @@ namespace GitHub.Actions.RunService.WebApi
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<BrokerSession> CreateSessionAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var requestUri = new Uri(Client.BaseAddress, "session");
|
||||||
|
|
||||||
|
var result = await SendAsync<BrokerSession>(
|
||||||
|
new HttpMethod("POST"),
|
||||||
|
requestUri: requestUri,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.IsSuccess)
|
||||||
|
{
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.StatusCode == HttpStatusCode.Forbidden)
|
||||||
|
{
|
||||||
|
throw new AccessDeniedException(result.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception($"Failed to get job message: {result.Error}");
|
||||||
|
}
|
||||||
public async Task<TaskAgentMessage> GetRunnerMessageAsync(
|
public async Task<TaskAgentMessage> GetRunnerMessageAsync(
|
||||||
|
string sessionID,
|
||||||
string runnerVersion,
|
string runnerVersion,
|
||||||
TaskAgentStatus? status,
|
TaskAgentStatus? status,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
@@ -66,6 +91,10 @@ namespace GitHub.Actions.RunService.WebApi
|
|||||||
|
|
||||||
List<KeyValuePair<string, string>> queryParams = new List<KeyValuePair<string, string>>();
|
List<KeyValuePair<string, string>> queryParams = new List<KeyValuePair<string, string>>();
|
||||||
|
|
||||||
|
if (sessionID != null)
|
||||||
|
{
|
||||||
|
queryParams.Add("sessionID", runnerVersion);
|
||||||
|
}
|
||||||
if (status != null)
|
if (status != null)
|
||||||
{
|
{
|
||||||
queryParams.Add("status", status.Value.ToString());
|
queryParams.Add("status", status.Value.ToString());
|
||||||
|
|||||||
9
src/Sdk/WebApi/WebApi/BrokerSession.cs
Normal file
9
src/Sdk/WebApi/WebApi/BrokerSession.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace GitHub.Actions.RunService.WebApi
|
||||||
|
{
|
||||||
|
public sealed class BrokerSession
|
||||||
|
{
|
||||||
|
public string id;
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/Test/L0/Listener/BrokerMessageListenerL0.cs
Normal file
61
src/Test/L0/Listener/BrokerMessageListenerL0.cs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using GitHub.Actions.RunService.WebApi;
|
||||||
|
using GitHub.Runner.Listener;
|
||||||
|
using GitHub.Runner.Listener.Configuration;
|
||||||
|
using GitHub.Services.Common;
|
||||||
|
using Moq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace GitHub.Runner.Common.Tests.Listener
|
||||||
|
{
|
||||||
|
public sealed class BrokerMessageListenerL0
|
||||||
|
{
|
||||||
|
private readonly RunnerSettings _settings;
|
||||||
|
private readonly Mock<IConfigurationManager> _config;
|
||||||
|
private readonly Mock<IBrokerServer> _brokerServer;
|
||||||
|
private readonly Mock<ICredentialManager> _credMgr;
|
||||||
|
|
||||||
|
public BrokerMessageListenerL0()
|
||||||
|
{
|
||||||
|
_settings = new RunnerSettings { AgentId = 1, AgentName = "myagent", PoolId = 123, PoolName = "default", ServerUrlV2 = "http://myserver", WorkFolder = "_work" };
|
||||||
|
_config = new Mock<IConfigurationManager>();
|
||||||
|
_config.Setup(x => x.LoadSettings()).Returns(_settings);
|
||||||
|
_brokerServer = new Mock<IBrokerServer>();
|
||||||
|
_credMgr = new Mock<ICredentialManager>();
|
||||||
|
_credMgr.Setup(x => x.LoadCredentials()).Returns(new VssCredentials());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Level", "L0")]
|
||||||
|
[Trait("Category", "Runner")]
|
||||||
|
public async void CreatesSessionAsync()
|
||||||
|
{
|
||||||
|
using TestHostContext tc = CreateTestContext();
|
||||||
|
using var tokenSource = new CancellationTokenSource();
|
||||||
|
|
||||||
|
// Arrange.
|
||||||
|
_brokerServer.Setup(x => x.ConnectAsync(new Uri(_settings.ServerUrlV2), It.Is<VssCredentials>(y => y != null))).Returns(Task.FromResult(new BrokerSession { id = "my-phony-session-id" }));
|
||||||
|
BrokerMessageListener listener = new();
|
||||||
|
listener.Initialize(tc);
|
||||||
|
|
||||||
|
// Act.
|
||||||
|
bool result = await listener.CreateSessionAsync(tokenSource.Token);
|
||||||
|
|
||||||
|
// Assert.
|
||||||
|
Assert.True(result);
|
||||||
|
Assert.Equal("my-phony-session-id", listener._sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TestHostContext CreateTestContext([CallerMemberName] String testName = "")
|
||||||
|
{
|
||||||
|
TestHostContext tc = new(this, testName);
|
||||||
|
tc.SetSingleton<IConfigurationManager>(_config.Object);
|
||||||
|
tc.SetSingleton<IBrokerServer>(_brokerServer.Object);
|
||||||
|
tc.SetSingleton<ICredentialManager>(_credMgr.Object);
|
||||||
|
return tc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user