122 lines
3.5 KiB
C#
122 lines
3.5 KiB
C#
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Sharp.Augeas.Test;
|
|
|
|
public class AugeasTests
|
|
{
|
|
private Augeas _augeas;
|
|
|
|
private const string ExampleConf1 = "/etc/apache2/sites-available/example.com.conf";
|
|
private const string ExampleConf2 = "/etc/apache2/sites-available/example2.com.conf";
|
|
|
|
private 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());
|
|
}
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
var rootDir = Environment.CurrentDirectory + "/root";
|
|
var lensDir = Environment.CurrentDirectory + "/lens";
|
|
_augeas = new Augeas(new AugSettings(rootDir, lensDir));
|
|
_augeas.LoadFile(ExampleConf1);
|
|
_augeas.LoadFile(ExampleConf2);
|
|
}
|
|
|
|
[Test]
|
|
public void GetTreeHasDirectivesAndArguments()
|
|
{
|
|
var tree = _augeas.GetTree("VirtualHost", $"/files{ExampleConf1}/VirtualHost/*");
|
|
Assert.That(tree.Arguments.Count > 0 && tree.Directives.Count > 0);
|
|
}
|
|
|
|
[Test]
|
|
public void TreeIsChangeable()
|
|
{
|
|
var tree = _augeas.GetTree("VirtualHost", $"/files{ExampleConf1}/VirtualHost/*");
|
|
var directive = tree.GetDirective("ServerName");
|
|
var newValue = RandomString(10);
|
|
directive.Set($"ServerName {newValue}");
|
|
bool save = directive.Save();
|
|
Assert.That(save);
|
|
}
|
|
|
|
[Test]
|
|
public void NoExceptionThrownWhenPrintingPreview()
|
|
{
|
|
var virtualHostConfig = ExampleConf1;
|
|
_augeas.PrintPreview(virtualHostConfig);
|
|
Assert.Pass();
|
|
}
|
|
|
|
[Test]
|
|
public void GetPreviewReturnsValid()
|
|
{
|
|
var preview = _augeas.GetPreview("/files/etc/apache2/sites-available/example.com.conf");
|
|
var stringInvalid = string.IsNullOrEmpty(preview);
|
|
Assert.That(!stringInvalid);
|
|
}
|
|
|
|
|
|
[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);
|
|
}
|
|
|
|
|
|
[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);
|
|
}
|
|
|
|
|
|
[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();
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void MatchReturnsTwoSites()
|
|
{
|
|
var sites = _augeas.Match("/files/etc/apache2/sites-available/*");
|
|
Assert.That(sites.Length == 2);
|
|
}
|
|
|
|
|
|
[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);
|
|
}
|
|
|
|
|
|
} |