installer/windows: unit test mocking file system (#3543)

This commit is contained in:
Kaloyan Raev 2019-11-14 14:02:03 +02:00 committed by GitHub
parent 1e64006e32
commit 77ed047428
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 232 additions and 61 deletions

View File

@ -2,6 +2,7 @@ using Microsoft.Deployment.WindowsInstaller;
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.IO.Abstractions;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace Storj namespace Storj
@ -16,7 +17,7 @@ namespace Storj
try try
{ {
CustomActionRunner.ValidateIdentityDir(identityDir); new CustomActionRunner().ValidateIdentityDir(identityDir);
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
@ -37,7 +38,7 @@ namespace Storj
try try
{ {
CustomActionRunner.ValidateWallet(wallet); new CustomActionRunner().ValidateWallet(wallet);
} catch (ArgumentException e) } catch (ArgumentException e)
{ {
// Wallet is invalid // Wallet is invalid
@ -57,7 +58,7 @@ namespace Storj
try try
{ {
CustomActionRunner.ValidateStorageDir(storageDir); new CustomActionRunner().ValidateStorageDir(storageDir);
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
@ -79,7 +80,7 @@ namespace Storj
try try
{ {
CustomActionRunner.ValidateStorage(storageStr, storageDir); new CustomActionRunner().ValidateStorage(storageStr, storageDir);
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
@ -100,7 +101,7 @@ namespace Storj
try try
{ {
CustomActionRunner.ValidateBandwidth(bandwidthStr); new CustomActionRunner().ValidateBandwidth(bandwidthStr);
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
@ -120,7 +121,7 @@ namespace Storj
string line = session["STORJ_SERVICE_COMMAND"]; string line = session["STORJ_SERVICE_COMMAND"];
session.Log($"ExtractInstallDir registry value: {line}"); session.Log($"ExtractInstallDir registry value: {line}");
string path = CustomActionRunner.ExtractInstallDir(line); string path = new CustomActionRunner().ExtractInstallDir(line);
session.Log($"ExtractInstallDir extracted path: {path}"); session.Log($"ExtractInstallDir extracted path: {path}");
session["STORJ_INSTALLDIR"] = path; session["STORJ_INSTALLDIR"] = path;
@ -130,44 +131,55 @@ namespace Storj
public class CustomActionRunner public class CustomActionRunner
{ {
private const long GB = 1000 * 1000 * 1000; public const long GB = 1000 * 1000 * 1000;
private const long TB = (long)1000 * 1000 * 1000 * 1000; public const long TB = (long)1000 * 1000 * 1000 * 1000;
private const long MinFreeSpace = 550 * GB; // (500 GB + 10% overhead) public const long MinFreeSpace = 550 * GB; // (500 GB + 10% overhead)
public static void ValidateIdentityDir(string identityDir) private readonly IFileSystem fs;
public CustomActionRunner() : this(fs: new FileSystem())
{
}
public CustomActionRunner(IFileSystem fs)
{
this.fs = fs;
}
public void ValidateIdentityDir(string identityDir)
{ {
if (string.IsNullOrEmpty(identityDir)) if (string.IsNullOrEmpty(identityDir))
{ {
throw new ArgumentException("You must select an identity folder."); throw new ArgumentException("You must select an identity folder.");
} }
if (!Directory.Exists(identityDir)) if (!fs.Directory.Exists(identityDir))
{ {
throw new ArgumentException(string.Format("Folder '{0}' does not exist.", identityDir)); throw new ArgumentException(string.Format("Folder '{0}' does not exist.", identityDir));
} }
if (!File.Exists(Path.Combine(identityDir, "ca.cert"))) if (!fs.File.Exists(Path.Combine(identityDir, "ca.cert")))
{ {
throw new ArgumentException("File 'ca.cert' not found in the selected folder."); throw new ArgumentException("File 'ca.cert' not found in the selected folder.");
} }
if (!File.Exists(Path.Combine(identityDir, "ca.key"))) if (!fs.File.Exists(Path.Combine(identityDir, "ca.key")))
{ {
throw new ArgumentException("File 'ca.key' not found in the selected folder."); throw new ArgumentException("File 'ca.key' not found in the selected folder.");
} }
if (!File.Exists(Path.Combine(identityDir, "identity.cert"))) if (!fs.File.Exists(Path.Combine(identityDir, "identity.cert")))
{ {
throw new ArgumentException("File 'identity.cert' not found in the selected folder."); throw new ArgumentException("File 'identity.cert' not found in the selected folder.");
} }
if (!File.Exists(Path.Combine(identityDir, "identity.key"))) if (!fs.File.Exists(Path.Combine(identityDir, "identity.key")))
{ {
throw new ArgumentException("File 'identity.key' not found in the selected folder."); throw new ArgumentException("File 'identity.key' not found in the selected folder.");
} }
} }
public static void ValidateWallet(string wallet) public void ValidateWallet(string wallet)
{ {
if (string.IsNullOrEmpty(wallet)) if (string.IsNullOrEmpty(wallet))
{ {
@ -190,15 +202,15 @@ namespace Storj
// TODO validate address checksum // TODO validate address checksum
} }
public static void ValidateStorageDir(string storageDir) public void ValidateStorageDir(string storageDir)
{ {
if (string.IsNullOrEmpty(storageDir)) if (string.IsNullOrEmpty(storageDir))
{ {
throw new ArgumentException("You must select a storage folder."); throw new ArgumentException("You must select a storage folder.");
} }
DirectoryInfo dir = new DirectoryInfo(storageDir); IDirectoryInfo dir = fs.DirectoryInfo.FromDirectoryName(storageDir);
DriveInfo drive = new DriveInfo(dir.Root.FullName); IDriveInfo drive = fs.DriveInfo.FromDriveName(dir.Root.FullName);
// TODO: Find a way to calculate the available free space + total size of existing pieces // TODO: Find a way to calculate the available free space + total size of existing pieces
if (drive.TotalSize < MinFreeSpace) if (drive.TotalSize < MinFreeSpace)
@ -208,7 +220,7 @@ namespace Storj
} }
} }
public static void ValidateStorage(string storageStr, string storageDir) public void ValidateStorage(string storageStr, string storageDir)
{ {
if (string.IsNullOrEmpty(storageStr)) if (string.IsNullOrEmpty(storageStr))
{ {
@ -240,8 +252,8 @@ namespace Storj
throw new ArgumentException(string.Format("{0} TB is too large value for allocated storage.", storage)); throw new ArgumentException(string.Format("{0} TB is too large value for allocated storage.", storage));
} }
DirectoryInfo dir = new DirectoryInfo(storageDir); IDirectoryInfo dir = fs.DirectoryInfo.FromDirectoryName(storageDir);
DriveInfo drive = new DriveInfo(dir.Root.FullName); IDriveInfo drive = fs.DriveInfo.FromDriveName(dir.Root.FullName);
// TODO: Find a way to calculate the available free space + total size of existing pieces // TODO: Find a way to calculate the available free space + total size of existing pieces
if (drive.TotalSize < storagePlusOverhead) if (drive.TotalSize < storagePlusOverhead)
@ -251,7 +263,7 @@ namespace Storj
} }
} }
public static void ValidateBandwidth(string bandwidthStr) public void ValidateBandwidth(string bandwidthStr)
{ {
if (string.IsNullOrEmpty(bandwidthStr)) if (string.IsNullOrEmpty(bandwidthStr))
{ {
@ -269,7 +281,7 @@ namespace Storj
} }
} }
public static string ExtractInstallDir(string serviceCmd) public string ExtractInstallDir(string serviceCmd)
{ {
if (string.IsNullOrEmpty(serviceCmd)) if (string.IsNullOrEmpty(serviceCmd))
{ {

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" InitialTargets="EnsureWixToolsetInstalled" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="12.0" DefaultTargets="Build" InitialTargets="EnsureWixToolsetInstalled" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform> <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Storj</RootNamespace> <RootNamespace>Storj</RootNamespace>
<AssemblyName>Storj</AssemblyName> <AssemblyName>Storj</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
@ -34,7 +34,11 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.IO.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.7.0.7\lib\net40\System.IO.Abstractions.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="Microsoft.Deployment.WindowsInstaller"> <Reference Include="Microsoft.Deployment.WindowsInstaller">
@ -46,6 +50,9 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="CustomAction.config" /> <Content Include="CustomAction.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(WixCATargetsPath)" Condition=" '$(WixCATargetsPath)' != '' " /> <Import Project="$(WixCATargetsPath)" Condition=" '$(WixCATargetsPath)' != '' " />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.CA.targets" Condition=" '$(WixCATargetsPath)' == '' AND Exists('$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.CA.targets') " /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.CA.targets" Condition=" '$(WixCATargetsPath)' == '' AND Exists('$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.CA.targets') " />

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.IO.Abstractions" version="7.0.7" targetFramework="net452" />
</packages>

View File

@ -10,26 +10,26 @@ namespace StorjTests
[TestMethod] [TestMethod]
public void NullServiceCmd() public void NullServiceCmd()
{ {
Assert.IsNull(CustomActionRunner.ExtractInstallDir(null)); Assert.IsNull(new CustomActionRunner().ExtractInstallDir(null));
} }
[TestMethod] [TestMethod]
public void EmptyServiceCmd() public void EmptyServiceCmd()
{ {
Assert.IsNull(CustomActionRunner.ExtractInstallDir("")); Assert.IsNull(new CustomActionRunner().ExtractInstallDir(""));
} }
[TestMethod] [TestMethod]
public void MissingConfigDirFlag() public void MissingConfigDirFlag()
{ {
Assert.IsNull(CustomActionRunner.ExtractInstallDir("\"C:\\Program Files\\Storj\\Storage Node\\storagenode.exe\" run")); Assert.IsNull(new CustomActionRunner().ExtractInstallDir("\"C:\\Program Files\\Storj\\Storage Node\\storagenode.exe\" run"));
} }
[TestMethod] [TestMethod]
public void ValidServiceCmd() public void ValidServiceCmd()
{ {
Assert.AreEqual("C:\\Program Files\\Storj\\Storage Node\\\\", Assert.AreEqual("C:\\Program Files\\Storj\\Storage Node\\\\",
CustomActionRunner.ExtractInstallDir("\"C:\\Program Files\\Storj\\Storage Node\\storagenode.exe\" run --config-dir \"C:\\Program Files\\Storj\\Storage Node\\\\\"")); new CustomActionRunner().ExtractInstallDir("\"C:\\Program Files\\Storj\\Storage Node\\storagenode.exe\" run --config-dir \"C:\\Program Files\\Storj\\Storage Node\\\\\""));
} }
} }
} }

View File

@ -0,0 +1,34 @@
using System;
using System.IO.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Storj;
namespace StorjTests
{
public class MockHelpers
{
public static IFileSystem MockFileSystemTotalSize(long totalSize)
{
var dir = Mock.Of<IDirectoryInfo>();
Mock.Get(dir).Setup(d => d.Root).Returns(dir);
Mock.Get(dir).Setup(d => d.FullName).Returns("X:\\");
var dirFactory = Mock.Of<IDirectoryInfoFactory>();
Mock.Get(dirFactory).Setup(d => d.FromDirectoryName(It.IsAny<string>())).Returns(dir);
var drive = Mock.Of<IDriveInfo>();
Mock.Get(drive).Setup(d => d.Name).Returns("X:\\");
Mock.Get(drive).Setup(d => d.TotalSize).Returns(totalSize);
var driveFactory = Mock.Of<IDriveInfoFactory>();
Mock.Get(driveFactory).Setup(d => d.FromDriveName(It.IsAny<string>())).Returns(drive);
var fs = Mock.Of<IFileSystem>();
Mock.Get(fs).Setup(f => f.DriveInfo).Returns(driveFactory);
Mock.Get(fs).Setup(f => f.DirectoryInfo).Returns(dirFactory);
return fs;
}
}
}

View File

@ -47,6 +47,10 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Deployment.WindowsInstaller, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ce35f76fcda82bad, processorArchitecture=MSIL" /> <Reference Include="Microsoft.Deployment.WindowsInstaller, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ce35f76fcda82bad, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath> <HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
@ -54,14 +58,31 @@
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath> <HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference> </Reference>
<Reference Include="Moq, Version=4.13.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.13.1\lib\net45\Moq.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.IO.Abstractions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.7.0.7\lib\net40\System.IO.Abstractions.dll</HintPath>
</Reference>
<Reference Include="System.IO.Abstractions.TestingHelpers, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.TestingHelpers.7.0.7\lib\net40\System.IO.Abstractions.TestingHelpers.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ExpectedExceptionWithMessage.cs" /> <Compile Include="ExpectedExceptionWithMessage.cs" />
<Compile Include="ExtractInstallDirTests.cs" /> <Compile Include="ExtractInstallDirTests.cs" />
<Compile Include="ValidateIdentityDirTests.cs" /> <Compile Include="ValidateIdentityDirTests.cs" />
<Compile Include="ValidateStorageDirTests.cs" /> <Compile Include="ValidateStorageDirTests.cs" />
<Compile Include="MockHelpers.cs" />
<Compile Include="ValidateStorageTests.cs" /> <Compile Include="ValidateStorageTests.cs" />
<Compile Include="ValidateBandwidthTests.cs" /> <Compile Include="ValidateBandwidthTests.cs" />
<Compile Include="ValidateWalletTests.cs" /> <Compile Include="ValidateWalletTests.cs" />

View File

@ -11,34 +11,34 @@ namespace StorjTests
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")]
public void NullBandwidth() public void NullBandwidth()
{ {
CustomActionRunner.ValidateBandwidth(null); new CustomActionRunner().ValidateBandwidth(null);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")]
public void EmptyBandwidth() public void EmptyBandwidth()
{ {
CustomActionRunner.ValidateBandwidth(""); new CustomActionRunner().ValidateBandwidth("");
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "'some random text' is not a valid number.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "'some random text' is not a valid number.")]
public void InvalidNumber() public void InvalidNumber()
{ {
CustomActionRunner.ValidateBandwidth("some random text"); new CustomActionRunner().ValidateBandwidth("some random text");
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The allocated bandwidth cannot be less than 2 TB.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The allocated bandwidth cannot be less than 2 TB.")]
public void TooSmall() public void TooSmall()
{ {
CustomActionRunner.ValidateBandwidth("1.41"); new CustomActionRunner().ValidateBandwidth("1.41");
} }
[TestMethod] [TestMethod]
public void ValidBandwidth() public void ValidBandwidth()
{ {
CustomActionRunner.ValidateBandwidth("3.14"); new CustomActionRunner().ValidateBandwidth("3.14");
} }
} }
} }

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.IO.Abstractions.TestingHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Storj; using Storj;
@ -11,23 +13,86 @@ namespace StorjTests
[ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select an identity folder.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select an identity folder.")]
public void NullIdentityDir() public void NullIdentityDir()
{ {
CustomActionRunner.ValidateIdentityDir(null); new CustomActionRunner().ValidateIdentityDir(null);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select an identity folder.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select an identity folder.")]
public void EmptyIdentityDir() public void EmptyIdentityDir()
{ {
CustomActionRunner.ValidateIdentityDir(""); new CustomActionRunner().ValidateIdentityDir("");
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "Folder 'X:\\Some\\Nonexistent\\Folder' does not exist.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "Folder 'X:\\Some\\Nonexistent\\Folder' does not exist.")]
public void NonexistentIdentityDir() public void NonexistentIdentityDir()
{ {
CustomActionRunner.ValidateIdentityDir("X:\\Some\\Nonexistent\\Folder"); new CustomActionRunner().ValidateIdentityDir("X:\\Some\\Nonexistent\\Folder");
} }
// TODO: add tests that mock the file system [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "File 'ca.cert' not found in the selected folder.")]
public void MissingCACertFile()
{
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"X:\\Some\\Identity\\Folder\\ca.key", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.key", new MockFileData("") }
});
new CustomActionRunner(fs).ValidateIdentityDir("X:\\Some\\Identity\\Folder");
}
[TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "File 'ca.key' not found in the selected folder.")]
public void MissingCAKeyFile()
{
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"X:\\Some\\Identity\\Folder\\ca.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.key", new MockFileData("") }
});
new CustomActionRunner(fs).ValidateIdentityDir("X:\\Some\\Identity\\Folder");
}
[TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "File 'identity.cert' not found in the selected folder.")]
public void MissingIdentityCertFile()
{
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"X:\\Some\\Identity\\Folder\\ca.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\ca.key", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.key", new MockFileData("") }
});
new CustomActionRunner(fs).ValidateIdentityDir("X:\\Some\\Identity\\Folder");
}
[TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "File 'identity.key' not found in the selected folder.")]
public void MissingIdentityKeyFile()
{
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"X:\\Some\\Identity\\Folder\\ca.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\ca.key", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.cert", new MockFileData("") }
});
new CustomActionRunner(fs).ValidateIdentityDir("X:\\Some\\Identity\\Folder");
}
[TestMethod]
public void ValidIdentityDir()
{
var fs = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"X:\\Some\\Identity\\Folder\\ca.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\ca.key", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.cert", new MockFileData("") },
{ @"X:\\Some\\Identity\\Folder\\identity.key", new MockFileData("") },
});
new CustomActionRunner(fs).ValidateIdentityDir("X:\\Some\\Identity\\Folder");
}
} }
} }

View File

@ -11,16 +11,29 @@ namespace StorjTests
[ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select a storage folder.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select a storage folder.")]
public void NullStorageDir() public void NullStorageDir()
{ {
CustomActionRunner.ValidateStorageDir(null); new CustomActionRunner().ValidateStorageDir(null);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select a storage folder.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "You must select a storage folder.")]
public void EmptyStorageDir() public void EmptyStorageDir()
{ {
CustomActionRunner.ValidateStorageDir(""); new CustomActionRunner().ValidateStorageDir("");
} }
// TODO: add tests that mock the file system [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The selected drive 'X:\\' has only 200 GB disk size. The minimum required is 550 GB.")]
public void NotEnoughSpace()
{
var fs = MockHelpers.MockFileSystemTotalSize(200 * CustomActionRunner.GB);
new CustomActionRunner(fs).ValidateStorageDir("X:\\Storage");
}
[TestMethod]
public void ValidStorageDir()
{
var fs = MockHelpers.MockFileSystemTotalSize(2 * CustomActionRunner.TB);
new CustomActionRunner(fs).ValidateStorageDir("X:\\Storage");
}
} }
} }

View File

@ -13,56 +13,64 @@ namespace StorjTests
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")]
public void NullStorage() public void NullStorage()
{ {
CustomActionRunner.ValidateStorage(null, StorageDir); new CustomActionRunner().ValidateStorage(null, StorageDir);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The value cannot be empty.")]
public void EmptyStorage() public void EmptyStorage()
{ {
CustomActionRunner.ValidateStorage("", StorageDir); new CustomActionRunner().ValidateStorage("", StorageDir);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "'some random text' is not a valid number.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "'some random text' is not a valid number.")]
public void InvalidNumber() public void InvalidNumber()
{ {
CustomActionRunner.ValidateStorage("some random text", StorageDir); new CustomActionRunner().ValidateStorage("some random text", StorageDir);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The allocated disk space cannot be less than 0.5 TB.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The allocated disk space cannot be less than 0.5 TB.")]
public void TooSmall() public void TooSmall()
{ {
CustomActionRunner.ValidateStorage("0.41", StorageDir); new CustomActionRunner().ValidateStorage("0.41", StorageDir);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "10000000 TB is too large value for allocated storage.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "10000000 TB is too large value for allocated storage.")]
public void TooLarge() public void TooLarge()
{ {
CustomActionRunner.ValidateStorage("10000000", StorageDir); new CustomActionRunner().ValidateStorage("10000000", StorageDir);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The storage directory cannot be empty")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The storage directory cannot be empty")]
public void NullStorageDir() public void NullStorageDir()
{ {
CustomActionRunner.ValidateStorage("3.14", null); new CustomActionRunner().ValidateStorage("3.14", null);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The storage directory cannot be empty")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The storage directory cannot be empty")]
public void EmptyStorageDir() public void EmptyStorageDir()
{ {
CustomActionRunner.ValidateStorage("3.14", ""); new CustomActionRunner().ValidateStorage("3.14", "");
} }
// TODO: add tests that mock the file system [TestMethod]
// [TestMethod] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The disk size (0.2 TB) on the selected drive X:\\ is less than the allocated disk space plus the 10% overhead (3.45 TB total).")]
// public void ValidStorage() public void NotEnoughSpace()
// { {
// CustomActionRunner.ValidateStorage("3.14", StorageDir); var fs = MockHelpers.MockFileSystemTotalSize(200 * CustomActionRunner.GB);
// } new CustomActionRunner(fs).ValidateStorage("3.14", StorageDir);
}
[TestMethod]
public void ValidStorage()
{
var fs = MockHelpers.MockFileSystemTotalSize(4 * CustomActionRunner.TB);
new CustomActionRunner(fs).ValidateStorage("3.14", StorageDir);
}
} }
} }

View File

@ -11,41 +11,41 @@ namespace StorjTests
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address cannot be empty.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address cannot be empty.")]
public void NullWallet() public void NullWallet()
{ {
CustomActionRunner.ValidateWallet(null); new CustomActionRunner().ValidateWallet(null);
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address cannot be empty.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address cannot be empty.")]
public void EmptyWallet() public void EmptyWallet()
{ {
CustomActionRunner.ValidateWallet(""); new CustomActionRunner().ValidateWallet("");
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address must start with a '0x' prefix.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address must start with a '0x' prefix.")]
public void PrefixMissing() public void PrefixMissing()
{ {
CustomActionRunner.ValidateWallet("e857955cfCd98bAe1073d4e314c3F9526799357A"); new CustomActionRunner().ValidateWallet("e857955cfCd98bAe1073d4e314c3F9526799357A");
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address must have 40 characters after the '0x' prefix.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address must have 40 characters after the '0x' prefix.")]
public void TooShortWallet() public void TooShortWallet()
{ {
CustomActionRunner.ValidateWallet("0xe857955cfCd98bAe1073d4e314c3F9526799357"); new CustomActionRunner().ValidateWallet("0xe857955cfCd98bAe1073d4e314c3F9526799357");
} }
[TestMethod] [TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address must have 40 characters after the '0x' prefix.")] [ExpectedExceptionWithMessage(typeof(ArgumentException), "The payout address must have 40 characters after the '0x' prefix.")]
public void TooLongWallet() public void TooLongWallet()
{ {
CustomActionRunner.ValidateWallet("0xe857955cfCd98bAe1073d4e314c3F9526799357A0"); new CustomActionRunner().ValidateWallet("0xe857955cfCd98bAe1073d4e314c3F9526799357A0");
} }
[TestMethod] [TestMethod]
public void ValidWallet() public void ValidWallet()
{ {
CustomActionRunner.ValidateWallet("0xe857955cfCd98bAe1073d4e314c3F9526799357A"); new CustomActionRunner().ValidateWallet("0xe857955cfCd98bAe1073d4e314c3F9526799357A");
} }
} }
} }

View File

@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Castle.Core" version="4.4.0" targetFramework="net452" />
<package id="Moq" version="4.13.1" targetFramework="net452" />
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net45" /> <package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net45" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net45" /> <package id="MSTest.TestFramework" version="1.3.2" targetFramework="net45" />
<package id="System.IO.Abstractions" version="7.0.7" targetFramework="net452" />
<package id="System.IO.Abstractions.TestingHelpers" version="7.0.7" targetFramework="net452" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net452" />
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net452" />
</packages> </packages>

View File

@ -1,4 +1,5 @@
rem install NuGet packages rem install NuGet packages
nuget install installer\windows\Storj\packages.config -o installer\windows\packages
nuget install installer\windows\StorjTests\packages.config -o installer\windows\packages nuget install installer\windows\StorjTests\packages.config -o installer\windows\packages
rem build the test project rem build the test project