Skip to content

Commit 836e859

Browse files
committed
schedule automation cmdlets, unit tests and infrastructure for automation cmdlets
1 parent 35edea9 commit 836e859

17 files changed

+895
-49
lines changed

src/ServiceManagement/Automation/Commands.Automation.Test/Commands.Automation.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
<ItemGroup>
9494
<Compile Include="Properties\AssemblyInfo.cs" />
9595
<Compile Include="UnitTests\GetAzureAutomationScheduleTest.cs" />
96+
<Compile Include="UnitTests\NewAzureAutomationScheduleTest.cs" />
97+
<Compile Include="UnitTests\RemoveAzureAutomationScheduleTest.cs" />
98+
<Compile Include="UnitTests\SetAzureAutomationScheduleTest.cs" />
9699
</ItemGroup>
97100
<ItemGroup>
98101
<ProjectReference Include="..\..\..\Common\Commands.Common.Test\Commands.Common.Test.csproj">
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Linq;
17+
using Microsoft.Azure.Commands.Automation.Cmdlet;
18+
using Microsoft.Azure.Commands.Automation.Common;
19+
using Microsoft.Azure.Commands.Automation.Model;
20+
using Microsoft.VisualStudio.TestTools.UnitTesting;
21+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
22+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
23+
using Moq;
24+
25+
namespace Microsoft.Azure.Commands.Automation.Test.UnitTests
26+
{
27+
[TestClass]
28+
public class NewAzureAutomationScheduleTest : TestBase
29+
{
30+
private Mock<IAutomationClient> mockAutomationClient;
31+
32+
private MockCommandRuntime mockCommandRuntime;
33+
34+
private NewAzureAutomationSchedule cmdlet;
35+
36+
[TestInitialize]
37+
public void SetupTest()
38+
{
39+
this.mockAutomationClient = new Mock<IAutomationClient>();
40+
this.mockCommandRuntime = new MockCommandRuntime();
41+
this.cmdlet = new NewAzureAutomationSchedule
42+
{
43+
AutomationClient = this.mockAutomationClient.Object,
44+
CommandRuntime = this.mockCommandRuntime
45+
};
46+
}
47+
48+
[TestMethod]
49+
public void NewAzureAutomationScheduleByOneTimeSuccessfull()
50+
{
51+
// Setup
52+
string accountName = "automation";
53+
string scheduleName = "schedule";
54+
55+
this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()));
56+
57+
this.cmdlet.AutomationAccountName = accountName;
58+
this.cmdlet.Name = scheduleName;
59+
this.cmdlet.StartTime = DateTimeOffset.Now;
60+
this.cmdlet.OneTime = true;
61+
this.cmdlet.ExecuteCmdlet();
62+
63+
// Assert
64+
this.mockAutomationClient
65+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
66+
}
67+
68+
[TestMethod]
69+
public void NewAzureAutomationScheduleByDailySuccessfull()
70+
{
71+
// Setup
72+
string accountName = "automation";
73+
string scheduleName = "schedule";
74+
byte dayInterval = 1;
75+
76+
this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()));
77+
78+
this.cmdlet.AutomationAccountName = accountName;
79+
this.cmdlet.Name = scheduleName;
80+
this.cmdlet.StartTime = DateTimeOffset.Now;
81+
this.cmdlet.DayInterval = dayInterval;
82+
this.cmdlet.ExecuteCmdlet();
83+
84+
// Assert
85+
this.mockAutomationClient
86+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
87+
}
88+
89+
[TestMethod]
90+
public void NewAzureAutomationScheduleByHourlySuccessfull()
91+
{
92+
// Setup
93+
string accountName = "automation";
94+
string scheduleName = "schedule";
95+
byte hourInterval = 1;
96+
97+
this.mockAutomationClient.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()));
98+
99+
this.cmdlet.AutomationAccountName = accountName;
100+
this.cmdlet.Name = scheduleName;
101+
this.cmdlet.StartTime = DateTimeOffset.Now;
102+
this.cmdlet.HourInterval = hourInterval;
103+
this.cmdlet.ExecuteCmdlet();
104+
105+
// Assert
106+
this.mockAutomationClient
107+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
108+
}
109+
110+
[TestMethod]
111+
public void NewAzureAutomationScheduleByDailyWithDefaultExpiryTimeDayIntervalSuccessfull()
112+
{
113+
// Setup
114+
string accountName = "automation";
115+
string scheduleName = "schedule";
116+
byte dayInterval = 1;
117+
118+
this.mockAutomationClient
119+
.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()))
120+
.Returns((string a, Schedule s) => s);
121+
122+
this.cmdlet.AutomationAccountName = accountName;
123+
this.cmdlet.Name = scheduleName;
124+
this.cmdlet.StartTime = DateTimeOffset.Now;
125+
this.cmdlet.DayInterval = dayInterval;
126+
this.cmdlet.ExecuteCmdlet();
127+
128+
// Assert
129+
this.mockAutomationClient
130+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
131+
132+
Assert.AreEqual<int>(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count);
133+
var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime)
134+
.OutputPipeline
135+
.FirstOrDefault();
136+
Assert.IsNotNull(schedule);
137+
Assert.AreEqual(scheduleName, schedule.Name, "Schedule name is unexpectedly {0}", schedule.Name);
138+
139+
// Test for default values
140+
Assert.AreEqual(
141+
Constants.DefaultScheduleExpiryTime,
142+
schedule.ExpiryTime,
143+
"Expiry time is unexpectedly {0}",
144+
schedule.ExpiryTime);
145+
Assert.AreEqual(
146+
dayInterval,
147+
schedule.Interval,
148+
"Day Interval is unexpectedly {0}",
149+
schedule.Interval);
150+
Assert.AreEqual(
151+
ScheduleFrequency.Day,
152+
schedule.Frequency,
153+
"Day Frequency is unexpectedly {0}",
154+
schedule.Frequency);
155+
}
156+
157+
[TestMethod]
158+
public void NewAzureAutomationScheduleByHourlyWithDefaultExpiryTimeDayIntervalSuccessfull()
159+
{
160+
// Setup
161+
string accountName = "automation";
162+
string scheduleName = "schedule";
163+
byte hourInterval = 1;
164+
165+
this.mockAutomationClient
166+
.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()))
167+
.Returns((string a, Schedule s) => s);
168+
169+
this.cmdlet.AutomationAccountName = accountName;
170+
this.cmdlet.Name = scheduleName;
171+
this.cmdlet.StartTime = DateTimeOffset.Now;
172+
this.cmdlet.HourInterval = hourInterval;
173+
this.cmdlet.ExecuteCmdlet();
174+
175+
// Assert
176+
this.mockAutomationClient
177+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
178+
179+
Assert.AreEqual<int>(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count);
180+
var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime)
181+
.OutputPipeline
182+
.FirstOrDefault();
183+
Assert.IsNotNull(schedule);
184+
Assert.AreEqual(scheduleName, schedule.Name, "Schedule name is unexpectedly {0}", schedule.Name);
185+
186+
// Test for default values
187+
Assert.AreEqual(
188+
Constants.DefaultScheduleExpiryTime,
189+
schedule.ExpiryTime,
190+
"Expiry time is unexpectedly {0}",
191+
schedule.ExpiryTime);
192+
Assert.AreEqual(
193+
hourInterval,
194+
schedule.Interval,
195+
"Hour Interval is unexpectedly {0}",
196+
schedule.Interval);
197+
Assert.AreEqual(
198+
ScheduleFrequency.Hour,
199+
schedule.Frequency,
200+
"Hour Frequency is unexpectedly {0}",
201+
schedule.Frequency);
202+
}
203+
204+
[TestMethod]
205+
public void NewAzureAutomationScheduleByDailyWithExpiryTimeSuccessfull()
206+
{
207+
// Setup
208+
string accountName = "automation";
209+
string scheduleName = "schedule";
210+
byte dayInterval = 1;
211+
var startTime = DateTimeOffset.Now;
212+
var expiryTime = startTime.AddDays(10);
213+
214+
this.mockAutomationClient
215+
.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()))
216+
.Returns((string a, Schedule s) => s);
217+
218+
this.cmdlet.AutomationAccountName = accountName;
219+
this.cmdlet.Name = scheduleName;
220+
this.cmdlet.StartTime = startTime;
221+
this.cmdlet.ExpiryTime = expiryTime;
222+
this.cmdlet.DayInterval = dayInterval;
223+
this.cmdlet.ExecuteCmdlet();
224+
225+
// Assert
226+
this.mockAutomationClient
227+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
228+
229+
Assert.AreEqual<int>(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count);
230+
var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime)
231+
.OutputPipeline
232+
.FirstOrDefault();
233+
Assert.IsNotNull(schedule);
234+
Assert.AreEqual(scheduleName, schedule.Name, "Schedule name is unexpectedly {0}", schedule.Name);
235+
236+
Assert.AreEqual(
237+
expiryTime,
238+
schedule.ExpiryTime,
239+
"Expiry time is unexpectedly {0}",
240+
schedule.ExpiryTime);
241+
Assert.AreEqual(
242+
dayInterval,
243+
schedule.Interval,
244+
"Day Interval is unexpectedly {0}",
245+
schedule.Interval);
246+
Assert.AreEqual(
247+
ScheduleFrequency.Day,
248+
schedule.Frequency,
249+
"Day Frequency is unexpectedly {0}",
250+
schedule.Frequency);
251+
}
252+
253+
[TestMethod]
254+
public void NewAzureAutomationScheduleByHourlyWithExpiryTimeSuccessfull()
255+
{
256+
// Setup
257+
string accountName = "automation";
258+
string scheduleName = "schedule";
259+
byte hourInterval = 2;
260+
var startTime = DateTimeOffset.Now;
261+
var expiryTime = startTime.AddDays(10);
262+
263+
this.mockAutomationClient
264+
.Setup(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()))
265+
.Returns((string a, Schedule s) => s);
266+
267+
this.cmdlet.AutomationAccountName = accountName;
268+
this.cmdlet.Name = scheduleName;
269+
this.cmdlet.StartTime = startTime;
270+
this.cmdlet.ExpiryTime = expiryTime;
271+
this.cmdlet.HourInterval = hourInterval;
272+
this.cmdlet.ExecuteCmdlet();
273+
274+
// Assert
275+
this.mockAutomationClient
276+
.Verify(f => f.CreateSchedule(accountName, It.IsAny<Schedule>()), Times.Once());
277+
278+
Assert.AreEqual<int>(1, ((MockCommandRuntime)this.cmdlet.CommandRuntime).OutputPipeline.Count);
279+
var schedule = (Schedule)((MockCommandRuntime)this.cmdlet.CommandRuntime)
280+
.OutputPipeline
281+
.FirstOrDefault();
282+
Assert.IsNotNull(schedule);
283+
Assert.AreEqual(scheduleName, schedule.Name, "Schedule name is unexpectedly {0}", schedule.Name);
284+
285+
// Test for default values
286+
Assert.AreEqual(
287+
expiryTime,
288+
schedule.ExpiryTime,
289+
"Expiry time is unexpectedly {0}",
290+
schedule.ExpiryTime);
291+
Assert.AreEqual(
292+
hourInterval,
293+
schedule.Interval,
294+
"Hour Interval is unexpectedly {0}",
295+
schedule.Interval);
296+
Assert.AreEqual(
297+
ScheduleFrequency.Hour,
298+
schedule.Frequency,
299+
"Hour Frequency is unexpectedly {0}",
300+
schedule.Frequency);
301+
}
302+
}
303+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using Microsoft.Azure.Commands.Automation.Cmdlet;
17+
using Microsoft.Azure.Commands.Automation.Common;
18+
using Microsoft.VisualStudio.TestTools.UnitTesting;
19+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
20+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
21+
using Moq;
22+
23+
namespace Microsoft.Azure.Commands.Automation.Test.UnitTests
24+
{
25+
[TestClass]
26+
public class RemoveAzureAutomationScheduleTest : TestBase
27+
{
28+
private Mock<IAutomationClient> mockAutomationClient;
29+
30+
private MockCommandRuntime mockCommandRuntime;
31+
32+
private RemoveAzureAutomationSchedule cmdlet;
33+
34+
[TestInitialize]
35+
public void SetupTest()
36+
{
37+
this.mockAutomationClient = new Mock<IAutomationClient>();
38+
this.mockCommandRuntime = new MockCommandRuntime();
39+
this.cmdlet = new RemoveAzureAutomationSchedule
40+
{
41+
AutomationClient = this.mockAutomationClient.Object,
42+
CommandRuntime = this.mockCommandRuntime
43+
};
44+
}
45+
46+
[TestMethod]
47+
public void RemoveAzureAutomationScheduleByNameSuccessfull()
48+
{
49+
// Setup
50+
string accountName = "automation";
51+
string scheduleName = "schedule";
52+
53+
this.mockAutomationClient.Setup(f => f.DeleteSchedule(accountName, scheduleName));
54+
55+
// Test
56+
this.cmdlet.AutomationAccountName = accountName;
57+
this.cmdlet.Name = scheduleName;
58+
this.cmdlet.Force = true;
59+
this.cmdlet.ExecuteCmdlet();
60+
61+
// Assert
62+
this.mockAutomationClient.Verify(f => f.DeleteSchedule(accountName, scheduleName), Times.Once());
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)