|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Xunit; |
| 6 | +using Microsoft.SemanticKernel.Plugins.Core.CodeInterpreter; |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using SemanticKernel.IntegrationTests.TestSettings; |
| 9 | +using System.Net.Http; |
| 10 | +using Azure.Identity; |
| 11 | +using Azure.Core; |
| 12 | +using System.Collections.Generic; |
| 13 | + |
| 14 | +namespace SemanticKernel.IntegrationTests.Plugins.Core; |
| 15 | + |
| 16 | +public sealed class SessionsPythonPluginTests : IDisposable |
| 17 | +{ |
| 18 | + private const string SkipReason = "For manual verification only"; |
| 19 | + |
| 20 | + private readonly SessionsPythonSettings _settings; |
| 21 | + private readonly HttpClientFactory _httpClientFactory; |
| 22 | + private readonly SessionsPythonPlugin _sut; |
| 23 | + |
| 24 | + public SessionsPythonPluginTests() |
| 25 | + { |
| 26 | + var configurationRoot = new ConfigurationBuilder() |
| 27 | + .AddJsonFile(path: "testsettings.json", optional: true, reloadOnChange: true) |
| 28 | + .AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true) |
| 29 | + .AddEnvironmentVariables() |
| 30 | + .AddUserSecrets<SessionsPythonPluginTests>() |
| 31 | + .Build(); |
| 32 | + |
| 33 | + var _configuration = configurationRoot |
| 34 | + .GetSection("AzureContainerAppSessionPool") |
| 35 | + .Get<AzureContainerAppSessionPoolConfiguration>()!; |
| 36 | + |
| 37 | + this._settings = new(sessionId: Guid.NewGuid().ToString(), endpoint: new Uri(_configuration.Endpoint)) |
| 38 | + { |
| 39 | + CodeExecutionType = SessionsPythonSettings.CodeExecutionTypeSetting.Synchronous, |
| 40 | + CodeInputType = SessionsPythonSettings.CodeInputTypeSetting.Inline |
| 41 | + }; |
| 42 | + |
| 43 | + this._httpClientFactory = new HttpClientFactory(); |
| 44 | + |
| 45 | + this._sut = new SessionsPythonPlugin(this._settings, this._httpClientFactory, GetAuthTokenAsync); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact(Skip = SkipReason)] |
| 49 | + public async Task ItShouldUploadFileAsync() |
| 50 | + { |
| 51 | + // Act |
| 52 | + var result = await this._sut.UploadFileAsync("test_file.txt", @"TestData\SessionsPythonPlugin\file_to_upload_1.txt"); |
| 53 | + |
| 54 | + // Assert |
| 55 | + Assert.Equal("test_file.txt", result.Filename); |
| 56 | + Assert.Equal(322, result.Size); |
| 57 | + Assert.NotNull(result.LastModifiedTime); |
| 58 | + Assert.Equal("/mnt/data/test_file.txt", result.FullPath); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact(Skip = SkipReason)] |
| 62 | + public async Task ItShouldDownloadFileAsync() |
| 63 | + { |
| 64 | + // Arrange |
| 65 | + await this._sut.UploadFileAsync("test_file.txt", @"TestData\SessionsPythonPlugin\file_to_upload_1.txt"); |
| 66 | + |
| 67 | + // Act |
| 68 | + var fileContent = await this._sut.DownloadFileAsync("test_file.txt"); |
| 69 | + |
| 70 | + // Assert |
| 71 | + Assert.Equal(322, fileContent.Length); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact(Skip = SkipReason)] |
| 75 | + public async Task ItShouldListFilesAsync() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + await this._sut.UploadFileAsync("test_file_1.txt", @"TestData\SessionsPythonPlugin\file_to_upload_1.txt"); |
| 79 | + await this._sut.UploadFileAsync("test_file_2.txt", @"TestData\SessionsPythonPlugin\file_to_upload_2.txt"); |
| 80 | + |
| 81 | + // Act |
| 82 | + var files = await this._sut.ListFilesAsync(); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.Equal(2, files.Count); |
| 86 | + |
| 87 | + var firstFile = files[0]; |
| 88 | + Assert.Equal("test_file_1.txt", firstFile.Filename); |
| 89 | + Assert.Equal(322, firstFile.Size); |
| 90 | + Assert.NotNull(firstFile.LastModifiedTime); |
| 91 | + Assert.Equal("/mnt/data/test_file_1.txt", firstFile.FullPath); |
| 92 | + |
| 93 | + var secondFile = files[1]; |
| 94 | + Assert.Equal("test_file_2.txt", secondFile.Filename); |
| 95 | + Assert.Equal(336, secondFile.Size); |
| 96 | + Assert.NotNull(secondFile.LastModifiedTime); |
| 97 | + Assert.Equal("/mnt/data/test_file_2.txt", secondFile.FullPath); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact(Skip = SkipReason)] |
| 101 | + public async Task ItShouldExecutePythonCodeAsync() |
| 102 | + { |
| 103 | + // Arrange |
| 104 | + string code = "result = 5 + 3\nprint(result)"; |
| 105 | + |
| 106 | + // Act |
| 107 | + var result = await this._sut.ExecuteCodeAsync(code); |
| 108 | + |
| 109 | + // Assert |
| 110 | + Assert.Contains("8", result); |
| 111 | + Assert.Contains("Success", result); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Acquires authentication token for the Azure Container App Session pool. |
| 116 | + /// </summary> |
| 117 | + private static async Task<string> GetAuthTokenAsync() |
| 118 | + { |
| 119 | + string resource = "https://acasessions.io/.default"; |
| 120 | + |
| 121 | + var credential = new AzureCliCredential(); |
| 122 | + |
| 123 | + AccessToken token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext([resource])).ConfigureAwait(false); |
| 124 | + |
| 125 | + return token.Token; |
| 126 | + } |
| 127 | + |
| 128 | + public void Dispose() |
| 129 | + { |
| 130 | + this._httpClientFactory.Dispose(); |
| 131 | + } |
| 132 | + |
| 133 | + private sealed class HttpClientFactory : IHttpClientFactory, IDisposable |
| 134 | + { |
| 135 | + private readonly List<HttpClient> _httpClients = []; |
| 136 | + |
| 137 | + public HttpClient CreateClient(string name) |
| 138 | + { |
| 139 | + var client = new HttpClient(); |
| 140 | + this._httpClients.Add(client); |
| 141 | + return client; |
| 142 | + } |
| 143 | + |
| 144 | + public void Dispose() |
| 145 | + { |
| 146 | + this._httpClients.ForEach(client => client.Dispose()); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments