Skip to content

Commit

Permalink
-Implemented BackupConfigDatabase, ClearSystemCache, LoadConfigFiles,…
Browse files Browse the repository at this point in the history
… RestartProbe and RestartCore methods

-Implemented Backup-PrtgConfig, Clear-PrtgCache, Load-PrtgConfigFile, Restart-Probe and Restart-PrtgCore cmdlets

For #5
  • Loading branch information
lordmilko committed Nov 19, 2017
1 parent ae8cbd9 commit 3985a88
Show file tree
Hide file tree
Showing 32 changed files with 1,397 additions and 10 deletions.
57 changes: 57 additions & 0 deletions PrtgAPI.Tests.IntegrationTests/ActionTests/AdminToolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PrtgAPI.Tests.IntegrationTests.ActionTests
{
[TestClass]
public class AdminToolTests : BasePrtgClientTest
{
private static string PrtgBackups => $"\\\\{Settings.Server}\\c$\\ProgramData\\Paessler\\PRTG Network Monitor\\Configuration Auto-Backups";

[TestMethod]
public void Action_BackupConfig_SuccessfullyBacksUpConfig()
{
var originalFiles = GetBackupFiles();

client.BackupConfigDatabase();

ValidateBackupCreated(originalFiles);
}

[TestMethod]
public async Task Action_BackupConfig_SuccessfullyBacksUpConfigAsync()
{
var originalFiles = GetBackupFiles();

await client.BackupConfigDatabaseAsync();

ValidateBackupCreated(originalFiles);
}

public static List<FileInfo> GetBackupFiles() => Impersonator.ExecuteAction(new DirectoryInfo(PrtgBackups).GetFiles).ToList();

public static void RemoveBackupFile(string fileName) => Impersonator.ExecuteAction(() => File.Delete(fileName));

private void ValidateBackupCreated(List<FileInfo> originalFiles)
{
Logger.LogTest("Pausing for 10 seconds while backup is created");
Thread.Sleep(10000);

var newFiles = GetBackupFiles();

Assert2.AreEqual(originalFiles.Count + 1, newFiles.Count, "New backup file was not created");

var diff = newFiles.Select(f => f.FullName).Except(originalFiles.Select(fn => fn.FullName)).ToList();

Assert2.AreEqual(1, diff.Count, "Backup file was not successfully created");

var firstFile = diff.First();

RemoveBackupFile(firstFile);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
. $PSScriptRoot\Support\IntegrationTestSafe.ps1

Describe "Backup-PrtgConfig_IT" {
It "can execute" {
$originalFiles = [PrtgAPI.Tests.IntegrationTests.ActionTests.AdminToolTests]::GetBackupFiles() | select -ExpandProperty FullName

$originalFiles.Count | Should BeGreaterThan 0

Backup-PrtgConfig

LogTest "Pausing for 10 seconds while backup is created"
Sleep 10

$newFiles = [PrtgAPI.Tests.IntegrationTests.ActionTests.AdminToolTests]::GetBackupFiles() | select -ExpandProperty FullName

$newFiles.Count | Should Be ($originalFiles.Count + 1)

$diff = @($newFiles | where { $originalFiles -notcontains $_ })

$diff.Count | Should Be 1

$firstFile = $diff | select -First 1

[PrtgAPI.Tests.IntegrationTests.ActionTests.AdminToolTests]::RemoveBackupFile($firstFile)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
. $PSScriptRoot\Support\IntegrationTest.ps1

Describe "Clear-PrtgCache_IT" {
It "clears general caches" {
Clear-PrtgCache General
}

It "clears graph data" {
Clear-PrtgCache GraphData -Force

# Wait on the service restarting
Restart-PrtgCore -Force -Wait
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
. $PSScriptRoot\Support\IntegrationTest.ps1

Describe "Load-PrtgConfigFile_IT" {
It "loads general files" {
Load-PrtgConfigFile General
}

It "loads sensor lookups" {
Load-PrtgConfigFile Lookups
}
}
31 changes: 31 additions & 0 deletions PrtgAPI.Tests.IntegrationTests/PowerShell/Restart-Probe.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
. $PSScriptRoot\Support\IntegrationTest.ps1

Describe "Restart-Probe_IT" {
It "waits for all probes to restart" {
Restart-Probe

$probes = Get-Probe

foreach($probe in $probes)
{
$probe.Condition | Should Be Connected
}
}

It "waits for a probe to restart" {
$probe = Get-Probe -Id (Settings Probe)

$probe | Restart-Probe

$newProbe = Get-Probe -Id (Settings Probe)

$probe.Condition | Should Be Connected
}

It "times out restarting a probe" {
{ Restart-Probe -Timeout 1 } | Should Throw "Timed out waiting for 1 probe to restart"

# Wait for the server to come back online
Restart-Probe -Wait
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
. $PSScriptRoot\Support\IntegrationTest.ps1

Describe "Restart-PrtgCore_IT" {

It "waits for PRTG to restart" {
Restart-PrtgCore

$sensor = Get-Sensor -Id (Settings UpSensor)

$sensor.Id | Should Be (Settings UpSensor)
}

It "times out restarting PRTG" {
{ Restart-PrtgCore -Timeout 1 } | Should Throw "Timed out waiting for PRTG Core Service to restart"

# Wait for the server to come back online
Restart-PrtgCore -Wait
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</Choose>
<ItemGroup>
<Compile Include="ActionTests\AddSensorTests.cs" />
<Compile Include="ActionTests\AdminToolTests.cs" />
<Compile Include="ActionTests\NotificationTriggerTests.cs" />
<Compile Include="ActionTests\SetObjectPropertyTests.cs" />
<Compile Include="ActionTests\Types\ExeXmlRawSensorParameters.cs" />
Expand Down Expand Up @@ -96,6 +97,8 @@
<None Include="New-TestSettings.ps1" />
<None Include="PowerShell\Add-NotificationTrigger.Tests.ps1" />
<None Include="PowerShell\Add-Sensor.Tests.ps1" />
<None Include="PowerShell\Backup-PrtgConfig.Tests.ps1" />
<None Include="PowerShell\Clear-PrtgCache.Tests.ps1" />
<None Include="PowerShell\CodeGeneration.Tests.ps1" />
<None Include="PowerShell\Edit-NotificationTriggerProperty.Tests.ps1" />
<None Include="PowerShell\Get-Channel.Tests.ps1" />
Expand All @@ -111,12 +114,15 @@
<None Include="PowerShell\Get-Sensor.Tests.ps1" />
<None Include="PowerShell\Get-SensorFactorySource.Tests.ps1" />
<None Include="PowerShell\Get-SensorHistory.Tests.ps1" />
<None Include="PowerShell\Load-PrtgConfigFile.Tests.ps1" />
<None Include="PowerShell\ObjectProperty\Set-ObjectProperty.Container.Tests.ps1" />
<None Include="PowerShell\ObjectProperty\Set-ObjectProperty.DeviceOrGroup.Tests.ps1" />
<None Include="PowerShell\ObjectProperty\Set-ObjectProperty.Group.Tests.ps1" />
<None Include="PowerShell\ObjectProperty\Set-ObjectProperty.Probe.Tests.ps1" />
<None Include="PowerShell\Pause-Object.Tests.ps1" />
<None Include="PowerShell\Acknowledge-Sensor.Tests.ps1" />
<None Include="PowerShell\Restart-Probe.Tests.ps1" />
<None Include="PowerShell\Restart-PrtgCore.Tests.ps1" />
<None Include="PowerShell\Set-ChannelProperty.Tests.ps1" />
<None Include="PowerShell\ObjectProperty\Set-ObjectProperty.Device.Tests.ps1" />
<None Include="PowerShell\ObjectProperty\Set-ObjectProperty.Sensor.Tests.ps1" />
Expand Down
10 changes: 9 additions & 1 deletion PrtgAPI.Tests.IntegrationTests/Support/Impersonator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Impersonator : IDisposable
{
[DllImport("advapi32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool LogonUser(
static extern bool LogonUser(
[MarshalAs(UnmanagedType.LPStr)] string pszUserName,
[MarshalAs(UnmanagedType.LPStr)] string pszDomain,
[MarshalAs(UnmanagedType.LPStr)] string pszPassword,
Expand Down Expand Up @@ -74,5 +74,13 @@ public static void ExecuteAction(Action action)
action();
}
}

public static T ExecuteAction<T>(Func<T> action)
{
using (var impersonator = new Impersonator(Settings.Server, Settings.WindowsUserName, Settings.WindowsPassword))
{
return action();
}
}
}
}
80 changes: 80 additions & 0 deletions PrtgAPI.Tests.UnitTests/ObjectTests/CSharp/AdminToolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PrtgAPI.Tests.UnitTests.ObjectTests.TestResponses;

namespace PrtgAPI.Tests.UnitTests.ObjectTests.CSharp
{
[TestClass]
public class AdminToolTests : BaseTest
{
PrtgClient client = Initialize_Client(new BasicResponse(string.Empty));

[TestMethod]
public void AdminTool_BackupConfig_CanExecute()
{
client.BackupConfigDatabase();
}

[TestMethod]
public async Task AdminTool_BackupConfig_CanExecuteAsync()
{
await client.BackupConfigDatabaseAsync();
}

[TestMethod]
public void AdminTool_ClearCache_CanExecute()
{
client.ClearSystemCache(SystemCacheType.General);
client.ClearSystemCache(SystemCacheType.GraphData);
}

[TestMethod]
public async Task AdminTool_ClearCache_CanExecuteAsync()
{
await client.ClearSystemCacheAsync(SystemCacheType.General);
await client.ClearSystemCacheAsync(SystemCacheType.GraphData);
}

[TestMethod]
public void AdminTool_LoadConfigFiles_CanExecute()
{
client.LoadConfigFiles(ConfigFileType.General);
client.LoadConfigFiles(ConfigFileType.Lookups);
}

[TestMethod]
public async Task AdminTool_LoadConfigFiles_CanExecuteAsync()
{
await client.LoadConfigFilesAsync(ConfigFileType.General);
await client.LoadConfigFilesAsync(ConfigFileType.Lookups);
}

[TestMethod]
public void AdminTool_RestartCore_CanExecute()
{
client.RestartCore();
}

[TestMethod]
public async Task AdminTool_RestartCore_CanExecuteAsync()
{
await client.RestartCoreAsync();
}

[TestMethod]
public void AdminTool_RestartProbe_CanExecute()
{
client.RestartProbe(1001);
}

[TestMethod]
public async Task AdminTool_RestartProbe_CanExecuteAsync()
{
await client.RestartProbeAsync(1001);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
. $PSScriptRoot\Support\Standalone.ps1

Describe "Backup-PrtgConfig" -Tag @("PowerShell", "UnitTest") {
It "can execute" {
SetAddressValidatorResponse "api/savenow.htm?" $true

Backup-PrtgConfig
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
. $PSScriptRoot\Support\Standalone.ps1

Describe "Clear-PrtgCache" -Tag @("PowerShell", "UnitTest") {
It "clears general caches" {
SetAddressValidatorResponse "api/clearcache.htm?" $true

Clear-PrtgCache General
}

It "clears the graph cache" {
SetAddressValidatorResponse "api/recalccache.htm?" $true

Clear-PrtgCache GraphData -Force
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
. $PSScriptRoot\Support\Standalone.ps1

Describe "Load-PrtgConfigFile" -Tag @("PowerShell", "UnitTest") {
It "loads general files" {
SetAddressValidatorResponse "api/reloadfilelists.htm?" $true

Load-PrtgConfigFile General
}

It "loads sensor lookups" {
SetAddressValidatorResponse "api/loadlookups.htm?" $true

Load-PrtgConfigFile Lookups
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
. $PSScriptRoot\Support\Standalone.ps1

function SetAddressValidatorResponse($str)
{
SetResponseAndClientWithArguments "AddressValidatorResponse" $str
}

Describe "New-SearchFilter" {
Context "Enum Transformation" {

Expand Down
Loading

0 comments on commit 3985a88

Please sign in to comment.