|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Mvc; |
| 10 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 11 | +using Microsoft.Azure.WebJobs.Script.WebHost.Controllers; |
| 12 | +using Microsoft.Azure.WebJobs.Script.WebHost.Management; |
| 13 | +using Microsoft.Azure.WebJobs.Script.WebHost.Models; |
| 14 | +using Microsoft.Azure.WebJobs.Script.WebHost.Security; |
| 15 | +using Microsoft.Extensions.Logging; |
| 16 | +using Microsoft.WebJobs.Script.Tests; |
| 17 | +using Moq; |
| 18 | +using Moq.Protected; |
| 19 | +using Newtonsoft.Json; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Managment |
| 23 | +{ |
| 24 | + [Trait(TestTraits.Category, TestTraits.EndToEnd)] |
| 25 | + [Trait(TestTraits.Group, TestTraits.ContainerInstanceTests)] |
| 26 | + public class KubernetesPodControllerTests |
| 27 | + { |
| 28 | + private readonly TestOptionsFactory<ScriptApplicationHostOptions> _optionsFactory = new TestOptionsFactory<ScriptApplicationHostOptions>(new ScriptApplicationHostOptions()); |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public async Task Assignment_Succeeds_With_Encryption_Key() |
| 32 | + { |
| 33 | + var environment = new TestEnvironment(); |
| 34 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1"); |
| 35 | + |
| 36 | + var scriptWebEnvironment = new ScriptWebHostEnvironment(environment); |
| 37 | + |
| 38 | + var loggerFactory = new LoggerFactory(); |
| 39 | + var loggerProvider = new TestLoggerProvider(); |
| 40 | + loggerFactory.AddProvider(loggerProvider); |
| 41 | + |
| 42 | + var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); |
| 43 | + |
| 44 | + handlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", |
| 45 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 46 | + ItExpr.IsAny<CancellationToken>()).ReturnsAsync(new HttpResponseMessage |
| 47 | + { |
| 48 | + StatusCode = HttpStatusCode.OK |
| 49 | + }); |
| 50 | + |
| 51 | + var instanceManager = new InstanceManager(_optionsFactory, new HttpClient(handlerMock.Object), scriptWebEnvironment, environment, loggerFactory.CreateLogger<InstanceManager>(), new TestMetricsLogger(), null); |
| 52 | + var startupContextProvider = new StartupContextProvider(environment, loggerFactory.CreateLogger<StartupContextProvider>()); |
| 53 | + |
| 54 | + InstanceManager.Reset(); |
| 55 | + |
| 56 | + var podController = new KubernetesPodController(environment, instanceManager, loggerFactory, startupContextProvider); |
| 57 | + |
| 58 | + const string podEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg="; |
| 59 | + var hostAssignmentContext = new HostAssignmentContext |
| 60 | + { |
| 61 | + Environment = new Dictionary<string, string>() |
| 62 | + { |
| 63 | + [EnvironmentSettingNames.AzureWebsiteRunFromPackage] = "http://localhost:1234" |
| 64 | + } |
| 65 | + }; |
| 66 | + hostAssignmentContext.Secrets = new FunctionAppSecrets(); |
| 67 | + hostAssignmentContext.IsWarmupRequest = false; |
| 68 | + |
| 69 | + var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), podEncryptionKey.ToKeyBytes()); |
| 70 | + |
| 71 | + var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext() |
| 72 | + { |
| 73 | + EncryptedContext = encryptedHostAssignmentValue |
| 74 | + }; |
| 75 | + |
| 76 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.PodEncryptionKey, podEncryptionKey); |
| 77 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.KubernetesServiceHost, "http://localhost:80"); |
| 78 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.PodNamespace, "k8se-apps"); |
| 79 | + |
| 80 | + var result = await podController.Assign(encryptedHostAssignmentContext); |
| 81 | + Assert.NotNull(startupContextProvider.Context); |
| 82 | + Assert.IsType<AcceptedResult>(result); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task Assignment_Fails_Without_Encryption_Key() |
| 87 | + { |
| 88 | + var environment = new TestEnvironment(); |
| 89 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "1"); |
| 90 | + |
| 91 | + var scriptWebEnvironment = new ScriptWebHostEnvironment(environment); |
| 92 | + |
| 93 | + var loggerFactory = new LoggerFactory(); |
| 94 | + var loggerProvider = new TestLoggerProvider(); |
| 95 | + loggerFactory.AddProvider(loggerProvider); |
| 96 | + |
| 97 | + var handlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict); |
| 98 | + |
| 99 | + handlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", |
| 100 | + ItExpr.IsAny<HttpRequestMessage>(), |
| 101 | + ItExpr.IsAny<CancellationToken>()).ReturnsAsync(new HttpResponseMessage |
| 102 | + { |
| 103 | + StatusCode = HttpStatusCode.OK |
| 104 | + }); |
| 105 | + |
| 106 | + var instanceManager = new InstanceManager(_optionsFactory, new HttpClient(handlerMock.Object), scriptWebEnvironment, environment, loggerFactory.CreateLogger<InstanceManager>(), new TestMetricsLogger(), null); |
| 107 | + var startupContextProvider = new StartupContextProvider(environment, loggerFactory.CreateLogger<StartupContextProvider>()); |
| 108 | + |
| 109 | + InstanceManager.Reset(); |
| 110 | + |
| 111 | + const string podEncryptionKey = "/a/vXvWJ3Hzgx4PFxlDUJJhQm5QVyGiu0NNLFm/ZMMg="; |
| 112 | + var podController = new KubernetesPodController(environment, instanceManager, loggerFactory, startupContextProvider); |
| 113 | + |
| 114 | + var hostAssignmentContext = new HostAssignmentContext |
| 115 | + { |
| 116 | + Environment = new Dictionary<string, string>() |
| 117 | + { |
| 118 | + [EnvironmentSettingNames.AzureWebsiteRunFromPackage] = "http://localhost:1234" |
| 119 | + } |
| 120 | + }; |
| 121 | + hostAssignmentContext.Secrets = new FunctionAppSecrets(); |
| 122 | + hostAssignmentContext.IsWarmupRequest = false; |
| 123 | + |
| 124 | + var encryptedHostAssignmentValue = SimpleWebTokenHelper.Encrypt(JsonConvert.SerializeObject(hostAssignmentContext), podEncryptionKey.ToKeyBytes()); |
| 125 | + |
| 126 | + var encryptedHostAssignmentContext = new EncryptedHostAssignmentContext() |
| 127 | + { |
| 128 | + EncryptedContext = encryptedHostAssignmentValue |
| 129 | + }; |
| 130 | + |
| 131 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.KubernetesServiceHost, "http://localhost:80"); |
| 132 | + environment.SetEnvironmentVariable(EnvironmentSettingNames.PodNamespace, "k8se-apps"); |
| 133 | + |
| 134 | + var ex = await Assert.ThrowsAsync<System.Exception>(async () => |
| 135 | + { |
| 136 | + await (podController.Assign(encryptedHostAssignmentContext)); |
| 137 | + }); |
| 138 | + Assert.Null(startupContextProvider.Context); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | +} |
0 commit comments