Skip to content
Closed
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
6 changes: 6 additions & 0 deletions Azure.PowerShell.Common.Netcore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Compute.Test.Netcore", "src
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Strategies.Test.Netcore", "src\Strategies.Test\Strategies.Test.Netcore.csproj", "{6756A7F2-1141-4065-BA23-0C555D2A2BC3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFx.Netcore", "src\TestFx\TestFx.Netcore.csproj", "{1E20E5A0-3353-4888-9B29-AE1A2C1C8DD0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -125,6 +127,10 @@ Global
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Release|Any CPU.Build.0 = Release|Any CPU
{1E20E5A0-3353-4888-9B29-AE1A2C1C8DD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E20E5A0-3353-4888-9B29-AE1A2C1C8DD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E20E5A0-3353-4888-9B29-AE1A2C1C8DD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E20E5A0-3353-4888-9B29-AE1A2C1C8DD0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions Azure.PowerShell.Common.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategies.Test", "src\Stra
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScenarioTest", "src\ScenarioTest\ScenarioTest.csproj", "{C1BDA476-A5CC-4394-914D-48B0EC31A710}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestFx", "src\TestFx\TestFx.csproj", "{8C625DE3-0067-454A-AF2C-EFD672EEB31A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -149,6 +151,10 @@ Global
{C1BDA476-A5CC-4394-914D-48B0EC31A710}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1BDA476-A5CC-4394-914D-48B0EC31A710}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1BDA476-A5CC-4394-914D-48B0EC31A710}.Release|Any CPU.Build.0 = Release|Any CPU
{8C625DE3-0067-454A-AF2C-EFD672EEB31A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C625DE3-0067-454A-AF2C-EFD672EEB31A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C625DE3-0067-454A-AF2C-EFD672EEB31A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C625DE3-0067-454A-AF2C-EFD672EEB31A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
55 changes: 25 additions & 30 deletions src/ScenarioTest.ResourceManager/EnvironmentSetupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,7 @@ public void SetupModulesFromCommon(AzureModule mode, params string[] modules)

public void SetupModules(params string[] modules)
{
this.modules = new List<string>();
this.modules.Add("Assert.ps1");
this.modules.Add("Common.ps1");
this.modules = new List<string> {"Assert.ps1", "Common.ps1"};
this.modules.AddRange(modules);
}

Expand All @@ -597,14 +595,18 @@ public virtual Collection<PSObject> RunPowerShellTest(params string[] scripts)
// permissive record matcher.
if (HttpMockServer.Matcher == null || HttpMockServer.Matcher.GetType() == typeof(SimpleRecordMatcher))
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Microsoft.Resources", null);
d.Add("Microsoft.Features", null);
d.Add("Microsoft.Authorization", null);
d.Add("Microsoft.Compute", null);
d.Add("Microsoft.KeyVault", null);
var providersToIgnore = new Dictionary<string, string>();
providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
var d = new Dictionary<string, string>
{
{"Microsoft.Resources", null},
{"Microsoft.Features", null},
{"Microsoft.Authorization", null},
{"Microsoft.Compute", null},
{"Microsoft.KeyVault", null}
};
var providersToIgnore = new Dictionary<string, string>
{
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}
};
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);
}

Expand All @@ -613,26 +615,18 @@ public virtual Collection<PSObject> RunPowerShellTest(params string[] scripts)
SetupPowerShellModules(powershell);

Collection<PSObject> output = null;
for (int i = 0; i < scripts.Length; ++i)
foreach (var script in scripts)
{
if (TracingInterceptor != null)
{
TracingInterceptor.Information(scripts[i]);
}
powershell.AddScript(scripts[i]);
TracingInterceptor?.Information(script);
powershell.AddScript(script);
}
try
{
output = powershell.Invoke();
if (powershell.Streams.Error.Count > 0)
{
throw new RuntimeException(
string.Format(
"Test failed due to a non-empty error stream. First error: {0}{1}",
PowerShellExtensions.FormatErrorRecord(powershell.Streams.Error[0]),
powershell.Streams.Error.Count > 0
? "Check the error stream in the test log for additional errors."
: ""));
$"Test failed due to a non-empty error stream. First error: {PowerShellExtensions.FormatErrorRecord(powershell.Streams.Error[0])}{(powershell.Streams.Error.Count > 0 ? "Check the error stream in the test log for additional errors." : "")}");
}

return output;
Expand Down Expand Up @@ -660,13 +654,14 @@ private void SetupPowerShellModules(System.Management.Automation.PowerShell powe
}
#endif
powershell.AddScript("$error.clear()");
powershell.AddScript(string.Format("Write-Debug \"current directory: {0}\"", System.AppDomain.CurrentDomain.BaseDirectory));
powershell.AddScript(string.Format("Write-Debug \"current executing assembly: {0}\"", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
powershell.AddScript(string.Format("cd \"{0}\"", System.AppDomain.CurrentDomain.BaseDirectory));
powershell.AddScript($"Write-Debug \"current directory: {System.AppDomain.CurrentDomain.BaseDirectory}\"");
powershell.AddScript(
$"Write-Debug \"current executing assembly: {Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\"");
powershell.AddScript($"cd \"{System.AppDomain.CurrentDomain.BaseDirectory}\"");

foreach (string moduleName in modules)
foreach (var moduleName in modules)
{
powershell.AddScript(string.Format("Import-Module \"{0}\"", moduleName.AsAbsoluteLocation()));
powershell.AddScript($"Import-Module \"{moduleName.AsAbsoluteLocation()}\"");
if (moduleName.EndsWith(".psd1"))
{
#if NETSTANDARD
Expand All @@ -682,8 +677,8 @@ private void SetupPowerShellModules(System.Management.Automation.PowerShell powe

powershell.AddScript("Disable-AzureRmDataCollection -ErrorAction Ignore");
powershell.AddScript(
string.Format("set-location \"{0}\"", System.AppDomain.CurrentDomain.BaseDirectory));
powershell.AddScript(string.Format(@"$TestOutputRoot='{0}'", System.AppDomain.CurrentDomain.BaseDirectory));
$"set-location \"{System.AppDomain.CurrentDomain.BaseDirectory}\"");
powershell.AddScript($@"$TestOutputRoot='{System.AppDomain.CurrentDomain.BaseDirectory}'");
powershell.AddScript("$VerbosePreference='Continue'");
powershell.AddScript("$DebugPreference='Continue'");
powershell.AddScript("$ErrorActionPreference='Stop'");
Expand Down
21 changes: 21 additions & 0 deletions src/TestFx/ITestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.TestFx
{
public interface ITestRunner
{
void RunTestScript(params string[] scripts);
}
}
32 changes: 32 additions & 0 deletions src/TestFx/ITestRunnerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

namespace Microsoft.Azure.Commands.TestFx
{
public interface ITestRunnerFactory
{
ITestRunner Build();
ITestRunnerFactory WithProjectSubfolderForTests(string folderName);
ITestRunnerFactory WithCommonPsScripts(string[] psScriptList);
ITestRunnerFactory WithNewPsScriptFilename(string psScriptName);
ITestRunnerFactory WithExtraRmModules(Func<EnvironmentSetupHelper, string[]> buildModuleList);
ITestRunnerFactory WithNewRmModules(Func<EnvironmentSetupHelper, string[]> buildModuleList);
ITestRunnerFactory WithExtraUserAgentsToIgnore(Dictionary<string, string> userAgentsToIgnore);
ITestRunnerFactory WithBuildMatcher(BuildMatcherDelegate buildMatcher);
}
}
Binary file added src/TestFx/MSSharedLibKey.snk
Binary file not shown.
32 changes: 32 additions & 0 deletions src/TestFx/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Microsoft Azure PowerShell TestFx")]
[assembly: AssemblyDescription("Microsoft Azure PowerShell TestFx library. Only for use with the Azure PowerShell runtime. Not intended for general development use.")]
[assembly: AssemblyProduct("Microsoft Azure PowerShell")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyCopyright("Copyright © Microsoft Corporation")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8c625de3-0067-454a-af2c-efd672eeb31a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
144 changes: 144 additions & 0 deletions src/TestFx/TestClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Hyak.Common;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Graph.RBAC.Version1_6;
using Microsoft.Rest;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

namespace Microsoft.Azure.Commands.TestFx
{
public class TestClientFactory : IClientFactory
{
private readonly MockContext _mockContext;

public TestClientFactory(MockContext mockContext)
{
_mockContext = mockContext ?? throw new ArgumentNullException(nameof(mockContext));
}

public TClient CreateArmClient<TClient>(IAzureContext context, string endpoint) where TClient : Rest.ServiceClient<TClient>
{
if (typeof(TClient) != typeof(GraphRbacManagementClient))
{
return _mockContext.GetServiceClient<TClient>();
}

var graphClient = _mockContext.GetGraphServiceClient<GraphRbacManagementClient>();
graphClient.TenantID = context.Tenant.Id;
return graphClient as TClient;
}

public TClient CreateCustomArmClient<TClient>(params object[] parameters) where TClient : Rest.ServiceClient<TClient>
{
return _mockContext.GetServiceClient<TClient>();
}

public HttpClient CreateHttpClient(string endpoint, ICredentials credentials)
{
throw new NotImplementedException();
}

public HttpClient CreateHttpClient(string endpoint, HttpMessageHandler effectiveHandler)
{
throw new NotImplementedException();
}

#region Action and Handler

public void AddAction(IClientAction action)
{
// Do nothing
}

public void RemoveAction(Type actionType)
{
// Do nothing
}

public void AddHandler<T>(T handler) where T : DelegatingHandler, ICloneable
{
// Do nothing
}

public void RemoveHandler(Type handlerType)
{
// Do nothing
}
public DelegatingHandler[] GetCustomHandlers()
{
// Do nothing
return new DelegatingHandler[0];
}

#endregion

#region UserAgent

public HashSet<ProductInfoHeaderValue> UniqueUserAgents { get; set; } = new HashSet<ProductInfoHeaderValue>();

public void AddUserAgent(string productName, string productVersion)
{
UniqueUserAgents.Add(new ProductInfoHeaderValue(productName, productVersion));
}

public void AddUserAgent(string productName)
{
AddUserAgent(productName, string.Empty);
}

public void RemoveUserAgent(string name)
{
UniqueUserAgents.RemoveWhere(p => string.Equals(p.Product.Name, name, StringComparison.OrdinalIgnoreCase));
}

public ProductInfoHeaderValue[] UserAgents => UniqueUserAgents.ToArray();

#endregion

#region Hyak

public TClient CreateClient<TClient>(IAzureContext context, string endpoint) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateClient<TClient>(IAzureContextContainer profile, string endpoint) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateClient<TClient>(IAzureContextContainer profile, IAzureSubscription subscription, string endpoint) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateCustomClient<TClient>(params object[] parameters) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

#endregion

}
}
Loading