Sharp.Augeas/Sharp.Augeas.Test/AugeasTests.cs

120 lines
3.3 KiB
C#
Raw Normal View History

2022-11-14 00:40:47 +03:00
using System.Text;
2022-11-08 20:57:02 +03:00
namespace Sharp.Augeas.Test;
public class AugeasTests
{
private Augeas _augeas;
2022-11-14 00:40:47 +03:00
private const string EXAMPLE_CONF_1 = "/etc/apache2/sites-available/example.com.conf";
private const string EXAMPLE_CONF_2 = "/etc/apache2/sites-available/example2.com.conf";
2022-11-12 21:03:16 +03:00
public static string RandomString(int length)
{
var random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
2022-11-14 00:40:47 +03:00
2022-11-08 20:57:02 +03:00
[SetUp]
public void Setup()
{
var rootDir = Environment.CurrentDirectory + "/root";
var lensDir = Environment.CurrentDirectory + "/lens";
_augeas = new Augeas(new AugSettings(rootDir, lensDir));
2022-11-14 00:40:47 +03:00
_augeas.LoadFile(EXAMPLE_CONF_1);
_augeas.LoadFile(EXAMPLE_CONF_2);
2022-11-08 20:57:02 +03:00
}
[Test]
public void NoExceptionThrownWhenPrintingVirtualhostTree()
{
2022-11-14 00:40:47 +03:00
var virtualHostConfig = EXAMPLE_CONF_1;
2022-11-08 20:57:02 +03:00
_augeas.PrintVirtualHostTree(virtualHostConfig);
Assert.Pass();
}
2022-11-12 01:08:45 +03:00
2022-11-08 20:57:02 +03:00
[Test]
public void NoExceptionThrownWhenPrintingPreview()
{
2022-11-14 00:40:47 +03:00
var virtualHostConfig = EXAMPLE_CONF_1;
_augeas.PrintPreview(virtualHostConfig);
Assert.Pass();
2022-11-08 20:57:02 +03:00
}
2022-11-12 01:08:45 +03:00
2022-11-08 23:42:09 +03:00
[Test]
public void GetPreviewReturnsValid()
{
2022-11-14 00:40:47 +03:00
var preview = _augeas.GetPreview("/files/etc/apache2/sites-available/example.com.conf");
var stringInvalid = string.IsNullOrEmpty(preview);
Assert.That(!stringInvalid);
2022-11-08 23:42:09 +03:00
}
2022-11-14 00:40:47 +03:00
2022-11-12 21:03:16 +03:00
[Test]
public void AfterSettingNodeGetNodeReturnsNewValue()
{
var nodePath = $"/files/etc/apache2/sites-available/example.com.conf/VirtualHost/directive[1]/arg";
var newValue = "example.com";
_augeas.SetNode(nodePath, newValue);
var retrieveVal = _augeas.GetNode(nodePath);
Assert.That(retrieveVal == newValue);
}
2022-11-14 00:40:47 +03:00
2022-11-12 21:03:16 +03:00
[Test]
public void SettingNodeAndSave()
{
var nodePath = $"/files/etc/apache2/sites-available/example.com.conf/VirtualHost/directive[1]/arg";
var newValue = RandomString(10);
_augeas.SetNode(nodePath, newValue);
var successSaving = _augeas.Save();
if (!successSaving)
{
Assert.Fail("Unable to write changes to disk.");
}
var retrieveVal = _augeas.GetNode(nodePath);
Assert.That(retrieveVal == newValue);
}
2022-11-14 00:40:47 +03:00
2022-11-12 21:03:16 +03:00
[Test]
public void InsertNode()
{
var nodePath = $"/files/etc/apache2/sites-available/example.com.conf/VirtualHost/directive[3]";
_augeas.InsertNode(nodePath, "Test");
var node = _augeas.GetNode(nodePath);
Assert.Pass();
}
2022-11-14 00:40:47 +03:00
2022-11-08 20:57:02 +03:00
[Test]
2022-11-12 01:08:45 +03:00
public void MatchReturnsTwoSites()
2022-11-08 20:57:02 +03:00
{
2022-11-12 01:08:45 +03:00
var sites = _augeas.Match("/files/etc/apache2/sites-available/*");
Assert.That(sites.Length == 2);
2022-11-08 20:57:02 +03:00
}
2022-11-14 00:40:47 +03:00
[Test]
public void MatchCanReturnMultipleDirectives()
{
var nodePath = $"/files/etc/apache2/sites-available/example.com.conf/VirtualHost/directive[7]/*";
var sites = _augeas.Match(nodePath);
Assert.That(sites.Length > 0);
}
2022-11-12 01:08:45 +03:00
[Test]
public void GetTreeVirtualHostReturnsDictionaryWithKeys()
{
2022-11-14 00:40:47 +03:00
var path = EXAMPLE_CONF_1;
2022-11-12 01:08:45 +03:00
var tree = _augeas.GetVirtualHostTree(path);
2022-11-14 00:40:47 +03:00
Assert.That(tree.Count > 0);
2022-11-12 01:08:45 +03:00
}
2022-11-08 20:57:02 +03:00
}