Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<PackageReference Include="System.Management.Automation" Version="6.0.0-alpha17" />
<PackageReference Include="Microsoft.CSharp" Version="4.4.0-preview2-25405-01" />
<PackageReference Include="Newtonsoft.JSON" Version="10.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="config.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
118 changes: 106 additions & 12 deletions PSSwagger.LiveTestFramework/src/PSSwagger.LTF.ConsoleServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,41 @@ namespace PSSwagger.LTF.ConsoleServer
using Lib.Models;
using Lib.PowerShell;
using Lib.ServiceTracing;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;

class Program
{
static void Main(string[] args)
{
ServerArgs serverArgs = new ServerArgs(args);
NamedPipeServer namedPipe = new NamedPipeServer(serverArgs.LogPipeName);
Logger logger = new Logger(namedPipe, namedPipe);
ServerArgs serverArgs = new ServerArgs().Parse(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json")).Parse(args).Validate();

EventLogOutputPipe eventLogOutputPipe = new EventLogOutputPipe();
CompositeLogger logger = new CompositeLogger();
if (serverArgs.EnableEventLog)
{
logger.AddLogger(new Logger(eventLogOutputPipe, eventLogOutputPipe));
}

if (serverArgs.EnablePipeLog)
{
try
{
NamedPipeServer namedPipe = new NamedPipeServer(serverArgs.LogPipeName);
Logger namedPipeLogger = new Logger(namedPipe, namedPipe);
logger.AddLogger(namedPipeLogger);
}
catch (Exception e)
{
logger.LogError("Failed to initialize named pipe logger: " + e.Message);
}
}

if (serverArgs.Errors.Count > 0)
{
logger.LogError("Server arguments had errors.");
Expand Down Expand Up @@ -73,26 +95,36 @@ static void Main(string[] args)
// Wait until server exits (usually means the server ran into an internal error)
while (server.IsRunning)
{
Thread.Sleep(1);
Thread.Sleep(2);
}
}
}

class ServerArgs
{
public List<string> SpecificationPaths { get; set; }
public List<string> ExternalModules { get; set; }
public string ModulePath { get; set; }
public string LogPipeName { get; set; }
public List<string> Errors { get; set; }
public bool EnablePipeLog { get; set; }
public bool EnableEventLog { get; set; }

public ServerArgs(string[] args)
public ServerArgs()
{
this.LogPipeName = "psswagger-ltf-consoleserver";
string lastArg = String.Empty;
this.Errors = new List<string>();
this.SpecificationPaths = new List<string>();
this.ExternalModules = new List<string>();
this.Errors = new List<string>();
this.LogPipeName = "psswagger-ltf-consoleserver";
this.EnablePipeLog = true;
this.EnableEventLog = false;
}

public ServerArgs Parse(string[] args)
{
string lastArg = String.Empty;
bool resetSpecificationPaths = false;
bool resetExternalModules = false;
foreach (string arg in args)
{
if (!arg.StartsWith("/"))
Expand All @@ -103,15 +135,29 @@ public ServerArgs(string[] args)
if (!arg.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
this.Errors.Add("Specification file is not a .json file.");
} else if (!File.Exists(arg))
}
else if (!File.Exists(arg))
{
this.Errors.Add(String.Format(CultureInfo.CurrentCulture, "Specification file does not exist: {0}", arg));
} else
}
else
{
if (!resetSpecificationPaths)
{
resetSpecificationPaths = true;
this.SpecificationPaths.Clear();
}

this.SpecificationPaths.Add(arg);
}
break;
case "extmodule":
if (!resetExternalModules)
{
resetExternalModules = true;
this.ExternalModules.Clear();
}

this.ExternalModules.Add(arg);
break;
case "testmodule":
Expand All @@ -125,16 +171,64 @@ public ServerArgs(string[] args)
break;
}
lastArg = String.Empty;
} else
}
else
{
lastArg = arg.Substring(1);
switch (lastArg.ToLowerInvariant())
{
case "enablepipelog":
lastArg = String.Empty;
this.EnablePipeLog = true;
break;
case "enableeventlog":
lastArg = String.Empty;
this.EnableEventLog = true;
break;
}
}
}

return this;
}

public ServerArgs Parse(string jsonFilePath)
{
if (File.Exists(jsonFilePath))
{
ServerArgs fromFile = JsonConvert.DeserializeObject<ServerArgs>(File.ReadAllText(jsonFilePath));
if (!String.IsNullOrWhiteSpace(fromFile.ModulePath))
{
this.ModulePath = fromFile.ModulePath;
}

if (!String.IsNullOrWhiteSpace(fromFile.LogPipeName))
{
this.LogPipeName = fromFile.LogPipeName;
}

if (fromFile.ExternalModules != null && fromFile.ExternalModules.Count > 0)
{
this.ExternalModules = fromFile.ExternalModules;
}

if (fromFile.SpecificationPaths != null && fromFile.SpecificationPaths.Count > 0)
{
this.SpecificationPaths = fromFile.SpecificationPaths;
}
}

return this;
}

public ServerArgs Validate()
{
if (String.IsNullOrEmpty(this.ModulePath))
{
this.Errors.Add("No test module path specified.");
}

return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -43,6 +47,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\Program.cs" />
<Content Include="..\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PSSwagger.LTF.IO.Lib\vs-csproj\PSSwagger.LTF.IO.Lib.csproj">
Expand All @@ -54,6 +61,9 @@
<Name>PSSwagger.LTF.Lib</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net452" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

// Licensed under the MIT license.
namespace PSSwagger.LTF.Lib.IO
{
using Interfaces;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

/// <summary>
/// Read from STDOUT.
/// </summary>
public class EventLogOutputPipe : IOutputPipe
{
public Task Write(char b)
{
throw new NotImplementedException();
}

public Task WriteBlock<T>(T msg) where T : class
{
throw new NotImplementedException();
}

public Task WriteBlockAsync<T>(T msg) where T : class
{
throw new NotImplementedException();
}

public async Task WriteLine(string line)
{
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry(line);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<T> ReadBlock<T>() where T : class
bytes[i] = await this.characterReader.ReadByte();
}

string jsonString = Encoding.ASCII.GetString(bytes);
string jsonString = Encoding.UTF8.GetString(bytes);

if (jsonString.StartsWith("["))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
if ((val != null) || serializer.NullValueHandling == NullValueHandling.Include)
{
string propertyName = pi.Name.ToLowerInvariant();
if (this.typeData.Properties.ContainsKey(propertyName) && !String.IsNullOrEmpty(this.typeData.Properties[propertyName].JsonName))
if (this.typeData.Properties.ContainsKey(propertyName) && !String.IsNullOrEmpty(this.typeData.Properties[propertyName].SubNodeName))
{
// AutoRest's TransformationJsonConverter expects these properties to be inside a "properties" object
ParameterData subObjectPropertyInfo = this.typeData.Properties[propertyName];
propertyName = this.typeData.Properties[propertyName].SubNodeName; // This is "properties"
Dictionary<string, object> subObjectNode;
if (dict.ContainsKey(propertyName))
{
subObjectNode = (Dictionary<string, object>)dict[propertyName];
} else
{
subObjectNode = new Dictionary<string, object>();
}

subObjectNode[subObjectPropertyInfo.RawJsonName] = val;
val = subObjectNode;
}
else if (this.typeData.Properties.ContainsKey(propertyName) && !String.IsNullOrEmpty(this.typeData.Properties[propertyName].JsonName))
{
propertyName = this.typeData.Properties[propertyName].JsonName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ public void RegisterSelf(JsonSerializerSettings settings)
{
RegisterTypeConverter(parameter.Type, settings);
}

// Don't bother registering the ResponseType for a page response
if (operation.ResponseType != null)
{
RegisterTypeConverter(operation.ResponseType.TypeData, settings);
}
}
}

private void RegisterTypeConverter(RuntimeTypeData typeData, JsonSerializerSettings settings)
{
if (typeData != null && typeData.Type != null && typeData.Type.GetConstructor(new Type[] { }) != null)
if (typeData != null && typeData.Type != null && typeData.Type.GetConstructor(new Type[] { }) != null && typeData.Properties.Count > 0)
{
Log("Registering JSON converter for type: {0}", typeData.Type.FullName);
settings.Converters.Add(new DynamicTypedObjectConverter(typeData));
foreach (ParameterData property in typeData.Properties.Values)
{
Expand Down Expand Up @@ -181,5 +188,13 @@ private bool ConvertObject(object val, Type expectedType, JsonSerializer seriali

return success;
}

private void Log(string msg, params object[] args)
{
if (this.module.Logger != null && !String.IsNullOrEmpty(msg))
{
this.module.Logger.Log(msg, args);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

// Licensed under the MIT license.
namespace PSSwagger.LTF.Lib.Interfaces
{
using Models;

/// <summary>
/// Process command results before returning to client.
/// </summary>
public interface ICommandPostProcessor
{
CommandExecutionResult Process(CommandExecutionResult result);
}
}
Loading