Skip to content

Commit 2b6aa24

Browse files
committed
Fix jobs when used with cmdlets that require parametersetname
1 parent 339519b commit 2b6aa24

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/Common/Commands.Common/AzureLongRunningJob.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public static U CopyCmdlet<U>(U cmdlet) where U : AzurePSCmdlet
196196
field.SafeCopyValue(source: cmdlet, target: returnValue);
197197
}
198198

199+
cmdlet.SafeCopyParameterSet(returnValue);
199200
return returnValue as U;
200201
}
201202

src/Common/Commands.Common/Extensions/CmdletExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,30 @@ public static void SafeCopyValue<T>(this FieldInfo field, T source, T target)
178178
}
179179
}
180180

181+
/// <summary>
182+
/// Safely copy the selected parameter set from one cmdlet to another
183+
/// </summary>
184+
/// <typeparam name="T">The cmdlet type</typeparam>
185+
/// <param name="source">The cmdlet to copy the parameter set name from</param>
186+
/// <param name="target">The cmdlet to copy to</param>
187+
public static void SafeCopyParameterSet<T>(this T source, T target) where T : AzurePSCmdlet
188+
{
189+
if (source != null && target != null)
190+
{
191+
if (!string.IsNullOrWhiteSpace(source.ParameterSetName))
192+
{
193+
try
194+
{
195+
target.SetParameterSet(source.ParameterSetName);
196+
}
197+
catch
198+
{
199+
200+
}
201+
}
202+
}
203+
}
204+
181205
public static string AsAbsoluteLocation(this string realtivePath)
182206
{
183207
return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, realtivePath));

src/ResourceManager/Profile/Commands.Profile.Test/LongRunningCmdletTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,29 @@ public void CanHandleShouldContinueException()
192192
Assert.True(job.Error.Count > 0 && job.Error.Any(e => e.Exception != null && e.Exception.GetType() == typeof(InvalidOperationException) && e.Exception.Message.Contains("Force")));
193193
}
194194

195+
[Fact]
196+
[Trait(Category.AcceptanceType, Category.CheckIn)]
197+
public void JobCopiesCmdletParameterSet()
198+
{
199+
Mock<ICommandRuntime> mock = new Mock<ICommandRuntime>();
200+
var cmdlet = new AzureParameterSetCmdlet();
201+
cmdlet.SetParameterSet("ParameterSetIsSet");
202+
cmdlet.CommandRuntime = mock.Object;
203+
var job = cmdlet.ExecuteAsJob("Test parameter set job") as AzureLongRunningJob<AzureParameterSetCmdlet>;
204+
int times = 0;
205+
while (times++ < 20 && job.StatusMessage != "Failed" && job.StatusMessage != "Completed")
206+
{
207+
Thread.Sleep(TimeSpan.FromSeconds(1));
208+
if (job.StatusMessage == "Blocked")
209+
{
210+
job.TryStart();
211+
}
212+
}
213+
214+
Assert.Equal("Completed", job.StatusMessage);
215+
Assert.False(job.Error.Any());
216+
}
217+
195218

196219
AzureStreamTestCmdlet SetupCmdlet(bool CallShouldProcess, bool CallShouldContinue, out Mock<ICommandRuntime> mockRuntime)
197220
{
@@ -317,5 +340,37 @@ static void WriteValues<T>(IEnumerable<T> collection, Action<T> writer)
317340
}
318341
}
319342
}
343+
344+
public class AzureParameterSetCmdlet: AzurePSCmdlet
345+
{
346+
protected override string DataCollectionWarning
347+
{
348+
get
349+
{
350+
return "";
351+
}
352+
}
353+
354+
protected override IAzureContext DefaultContext
355+
{
356+
get
357+
{
358+
return null;
359+
}
360+
}
361+
362+
public override void ExecuteCmdlet()
363+
{
364+
if (String.IsNullOrEmpty(this.ParameterSetName))
365+
{
366+
throw new InvalidOperationException("Parameter set must be set");
367+
}
368+
}
369+
370+
protected override void InitializeQosEvent()
371+
{
372+
373+
}
374+
}
320375
}
321376
}

0 commit comments

Comments
 (0)