Skip to content

Commit

Permalink
Merge pull request #479 from zooba/fastcgi-test
Browse files Browse the repository at this point in the history
Fixes FastCgi tests.
  • Loading branch information
zooba committed Jun 19, 2015
2 parents 4955347 + cba48af commit 528e7d9
Show file tree
Hide file tree
Showing 22 changed files with 365 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Python/Tests/FastCgi/FastCgiTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<Link>ProcessOutput.cs</Link>
</Compile>
<Compile Include="FastCgiTests.cs" />
<Compile Include="FastCgiTests33.cs" />
<Compile Include="FastCgiTests3x.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="..\TestProjectAfter.settings" />
Expand Down
109 changes: 48 additions & 61 deletions Python/Tests/FastCgi/FastCgiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static void DoDeployment(TestContext context) {

[TestInitialize]
public void CloseRunningIisExpress() {
PythonVersion.AssertInstalled();

IEnumerable<Process> running;
while ((running = Process.GetProcessesByName("iisexpress")).Any()) {
foreach (var p in running) {
Expand All @@ -64,30 +66,11 @@ public void CloseRunningIisExpress() {
}
}

[TestMethod, Priority(0)]
public void DjangoNewApp() {
using (var site = ConfigureIISForDjango(AppCmdPath, InterpreterPath, "DjangoApplication.settings")) {
site.StartServer();

CopyDir("TestData", site.SiteDir);

var response = site.Request("");
Console.WriteLine(response.ContentType);
var stream = response.GetResponseStream();
var content = new StreamReader(stream).ReadToEnd();
Console.WriteLine(content);

Assert.IsTrue(content.IndexOf("Welcome to Django") != -1, "Expected \"Welcome to Django\".\r\nActual:\r\n" + content);
}
}

[TestMethod, Priority(0)]
public void DjangoHelloWorld() {
using (var site = ConfigureIISForDjango(AppCmdPath, InterpreterPath, "DjangoTestApp.settings")) {
site.StartServer();

CopyDir("TestData", site.SiteDir);

var response = site.Request("");
Console.WriteLine(response.ContentType);
var stream = response.GetResponseStream();
Expand Down Expand Up @@ -121,12 +104,8 @@ public void ConfigVariables() {
[TestMethod, Priority(0)]
public void LargeResponse() {
using (var site = ConfigureIISForDjango(AppCmdPath, InterpreterPath, "DjangoTestApp.settings")) {
File.Copy("TestData\\DjangoTestApp\\web.config", Path.Combine(site.SiteDir, "web.config"));

site.StartServer();

CopyDir("TestData", site.SiteDir);

var response = site.Request("large_response");
Console.WriteLine(response.ContentType);
var stream = response.GetResponseStream();
Expand All @@ -151,8 +130,6 @@ public void DjangoHelloWorldParallel() {
using (var site = ConfigureIISForDjango(AppCmdPath, InterpreterPath, "DjangoTestApp.settings")) {
site.StartServer();

CopyDir("TestData", site.SiteDir);

const int threadCnt = 12;
const int requests = 1000;
var tasks = new Task[threadCnt];
Expand Down Expand Up @@ -180,8 +157,6 @@ public void DjangoHelloWorldParallel() {
[TestMethod, Priority(0)]
public void CustomHandler() {
using (var site = ConfigureIISForCustomHandler(AppCmdPath, InterpreterPath, "custom_handler.handler")) {
CopyDir("TestData", site.SiteDir);

site.StartServer();

var response = site.Request("");
Expand All @@ -196,8 +171,6 @@ public void CustomHandler() {
[TestMethod, Priority(0)]
public void CustomCallableHandler() {
using (var site = ConfigureIISForCustomHandler(AppCmdPath, InterpreterPath, "custom_handler.callable_handler()")) {
CopyDir("TestData", site.SiteDir);

site.StartServer();

var response = site.Request("");
Expand All @@ -210,8 +183,6 @@ public void CustomCallableHandler() {
[TestMethod, Priority(0)]
public void ErrorHandler() {
using (var site = ConfigureIISForCustomHandler(AppCmdPath, InterpreterPath, "custom_handler.error_handler")) {
CopyDir("TestData", site.SiteDir);

site.StartServer();
try {
var response = site.Request("");
Expand Down Expand Up @@ -246,7 +217,7 @@ private static string CreateSite() {
public static void ConfigureIIS(string appCmd, string appHostConfig, string python, string wfastcgi, Dictionary<string, string> envVars) {
using (var p = ProcessOutput.RunHiddenAndCapture(
appCmd, "set", "config", "/section:system.webServer/fastCGI",
string.Format("/+[fullPath='{0}', arguments='\"\"\"{1}\"\"\"']", python, wfastcgi),
string.Format("/+[fullPath='{0}', arguments='\"{1}\"']", python, wfastcgi),
"/AppHostConfig:" + appHostConfig
)) {
p.Wait();
Expand All @@ -257,7 +228,7 @@ public static void ConfigureIIS(string appCmd, string appHostConfig, string pyth
using (var p = ProcessOutput.RunHiddenAndCapture(
appCmd, "set", "config", "/section:system.webServer/handlers",
string.Format(
"/+[name='Python_via_FastCGI',path='*',verb='*',modules='FastCgiModule',scriptProcessor='{0}|\"\"\"{1}\"\"\"',resourceType='Unspecified']",
"/+[name='Python_via_FastCGI',path='*',verb='*',modules='FastCgiModule',scriptProcessor='{0}|\"{1}\"',resourceType='Unspecified']",
python, wfastcgi
),
"/AppHostConfig:" + appHostConfig
Expand All @@ -269,9 +240,9 @@ public static void ConfigureIIS(string appCmd, string appHostConfig, string pyth

foreach (var keyValue in envVars) {
using (var p = ProcessOutput.RunHiddenAndCapture(
appCmd, "set", "config", "-section:system.webServer/fastCgi",
appCmd, "set", "config", "/section:system.webServer/fastCgi",
string.Format(
"/+\"[fullPath='{0}', arguments='\"\"\"{1}\"\"\"'].environmentVariables.[name='{2}',value='{3}']",
"/+[fullPath='{0}', arguments='\"{1}\"'].environmentVariables.[name='{2}',value='{3}']",
python, wfastcgi, keyValue.Key, keyValue.Value
),
"/commit:apphost",
Expand All @@ -296,6 +267,7 @@ public static void ConfigureIIS(string appCmd, string appHostConfig, string pyth
}

private static void DumpOutput(ProcessOutput process) {
Console.WriteLine(process.Arguments);
foreach (var line in process.StandardOutputLines) {
Console.WriteLine(line);
}
Expand Down Expand Up @@ -334,9 +306,15 @@ private static WebSite ConfigureIISForDjango(string appCmd, string python, strin
{ "PYTHONPATH", "" },
{ "WSGI_HANDLER", "django.core.handlers.wsgi.WSGIHandler()" }
}
);

var module = djangoSettings.Split(new[] { '.' }, 2)[0];
FileUtils.CopyDirectory(
TestData.GetPath(Path.Combine("TestData", "WFastCgi", module)),
Path.Combine(site, module)
);


Console.WriteLine("Site created at {0}", site);
return new WebSite(site);
}
Expand All @@ -354,16 +332,33 @@ private static WebSite ConfigureIISForCustomHandler(string appCmd, string python
{ "WSGI_HANDLER", handler },
{ "WSGI_LOG", Path.Combine(site, "log.txt") }
}

);

var module = handler.Split(new[] { '.' }, 2)[0];
try {
File.Copy(
TestData.GetPath("TestData\\WFastCGI\\" + module + ".py"),
Path.Combine(site, module + ".py"),
true
);
} catch (IOException ex) {
Console.WriteLine("Failed to copy {0}.py: {1}", module, ex);
}


Console.WriteLine("Site created at {0}", site);
return new WebSite(site);
}

public virtual string InterpreterPath {
public virtual PythonVersion PythonVersion {
get {
return PythonPaths.Python27.InterpreterPath;
return PythonPaths.Python27 ?? PythonPaths.Python27_x64;
}
}

public string InterpreterPath {
get {
return PythonVersion.InterpreterPath;
}
}

Expand All @@ -376,21 +371,6 @@ public virtual string AppCmdPath {
}
}

private static void CopyDir(string source, string target) {
foreach (var dir in Directory.GetDirectories(source)) {
var targetDir = Path.Combine(target, Path.GetFileName(dir));
//Console.WriteLine("Creating dir: {0}", targetDir);
Directory.CreateDirectory(targetDir);
CopyDir(dir, targetDir);
}

foreach (var file in Directory.GetFiles(source)) {
var targetFile = Path.Combine(target, Path.GetFileName(file));
//Console.WriteLine("Deploying: {0} -> {1}", file, targetFile);
File.Copy(file, targetFile);
}
}

class WebSite : IDisposable {
private readonly string _dir;
private ProcessOutput _process;
Expand Down Expand Up @@ -420,8 +400,13 @@ public void StartServer() {
public WebResponse Request(string uri) {
WebRequest req = WebRequest.Create(
"http://localhost:8181/" + uri
);
return req.GetResponse();
);
try {
return req.GetResponse();
} catch (WebException ex) {
Console.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
throw;
}
}

public void StopServer() {
Expand Down Expand Up @@ -580,7 +565,7 @@ public void TestEnvironment() {
[TestMethod, Priority(0), TestCategory("Core")]
public void TestFileSystemChanges() {
var location = TestData.GetTempPath(randomSubPath: true);
CopyDir(TestData.GetPath(@"TestData\WFastCgi\FileSystemChanges"), location);
FileUtils.CopyDirectory(TestData.GetPath(@"TestData\WFastCgi\FileSystemChanges"), location);

IisExpressTest(
location,
Expand Down Expand Up @@ -609,7 +594,7 @@ public void TestFileSystemChanges() {
[TestMethod, Priority(0), TestCategory("Core")]
public void TestFileSystemChangesPackage() {
var location = TestData.GetTempPath(randomSubPath: true);
CopyDir(TestData.GetPath(@"TestData\WFastCgi\FileSystemChangesPackage"), location);
FileUtils.CopyDirectory(TestData.GetPath(@"TestData\WFastCgi\FileSystemChangesPackage"), location);

IisExpressTest(
location,
Expand All @@ -633,7 +618,7 @@ public void TestFileSystemChangesPackage() {
[TestMethod, Priority(0), TestCategory("Core")]
public void TestFileSystemChangesCustomRegex() {
var location = TestData.GetTempPath(randomSubPath: true);
CopyDir(TestData.GetPath(@"TestData\WFastCgi\FileSystemChangesCustomRegex"), location);
FileUtils.CopyDirectory(TestData.GetPath(@"TestData\WFastCgi\FileSystemChangesCustomRegex"), location);

IisExpressTest(
location,
Expand All @@ -655,7 +640,7 @@ public void TestFileSystemChangesCustomRegex() {
[TestMethod, Priority(0), TestCategory("Core")]
public void TestFileSystemChangesDisabled() {
var location = TestData.GetTempPath(randomSubPath: true);
CopyDir(TestData.GetPath(@"TestData\WFastCgi\FileSystemChangesDisabled"), location);
FileUtils.CopyDirectory(TestData.GetPath(@"TestData\WFastCgi\FileSystemChangesDisabled"), location);

IisExpressTest(
location,
Expand Down Expand Up @@ -1060,6 +1045,7 @@ public static implicit operator Action(GetAndValidateErrorUrl self) {
}

private Action CollectStaticFiles(string location) {
location = TestData.GetPath(location);
return () => {
using (var p = ProcessOutput.Run(
InterpreterPath,
Expand Down Expand Up @@ -1168,7 +1154,7 @@ params Action[] actions
IisExpressPath,
new[] { "/config:" + appConfig, "/site:WebSite1", "/systray:false", "/trace:info" },
null,
new[] { new KeyValuePair<string, string>("WSGI_LOG", Path.Combine(location, "log.txt")) },
env,
false,
new OutputRedirector("IIS")
)) {
Expand Down Expand Up @@ -1205,7 +1191,8 @@ public override void WriteErrorLine(string line) {

private static string IisExpressPath {
get {
var iisExpressPath = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\IISExpress\\8.0", "InstallPath", null) as string;
var iisExpressPath = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\IISExpress\\10.0", "InstallPath", null) as string ??
Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\IISExpress\\8.0", "InstallPath", null) as string;
if (iisExpressPath == null) {
Assert.Inconclusive("Can't find IIS Express");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

namespace FastCgiTest {
[TestClass]
public class FastCgiTests33 : FastCgiTests {
public class FastCgiTests3x : FastCgiTests {
[ClassInitialize]
public static new void DoDeployment(TestContext context) {
AssertListener.Initialize();
PythonTestData.Deploy();
}

public override string InterpreterPath {
public override PythonVersion PythonVersion {
get {
return PythonPaths.Python33.InterpreterPath;
return PythonPaths.Python34 ?? PythonPaths.Python34_x64 ??
PythonPaths.Python33 ?? PythonPaths.Python33_x64;
}
}

}
}
2 changes: 1 addition & 1 deletion Python/Tests/TestData/WFastCgi/DjangoApp/web.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</system.diagnostics>

<appSettings>
<add key="WSGI_HANDLER" value="django.core.handlers.wsgi.WSGIHandler()"/>
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
<add key="DJANGO_SETTINGS_MODULE" value="DjangoApplication.settings" />
<add key="PYTHONPATH" value="[SITEPATH]" />
</appSettings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</system.diagnostics>

<appSettings>
<add key="WSGI_HANDLER" value="django.core.handlers.wsgi.WSGIHandler()"/>
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
<add key="DJANGO_SETTINGS_MODULE" value="DjangoApplication.settings" />
<add key="PYTHONPATH" value="[SITEPATH]" />
</appSettings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</system.diagnostics>

<appSettings>
<add key="WSGI_HANDLER" value="django.core.handlers.wsgi.WSGIHandler()"/>
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
<add key="DJANGO_SETTINGS_MODULE" value="DjangoApplication.settings" />
<add key="PYTHONPATH" value="[SITEPATH]" />
</appSettings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</system.diagnostics>

<appSettings>
<add key="WSGI_HANDLER" value="django.core.handlers.wsgi.WSGIHandler()"/>
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
<add key="DJANGO_SETTINGS_MODULE" value="DjangoApplication.settings" />
<add key="PYTHONPATH" value="[SITEPATH]" />
</appSettings>
Expand Down
Loading

0 comments on commit 528e7d9

Please sign in to comment.