Configd
view release on metacpan or search on metacpan
t/Configd.t view on Meta::CPAN
#!/usr/bin/env perl
use 5.034;
use strict;
use warnings FATAL => 'all';
use re '/aa';
=head1 NAME
t/Configd.t - adopting a file, generating it from its fragments, wrapping the
service and handing it all back
=cut
use Test::More;
use Test::Fatal qw{exception};
use FindBin::libs;
use Test::Configd qw{scratch fragment};
use Configd();
use Configd::Unit();
use Configd::Language();
use Configd::Language::opendkim(); ## no critic (ProhibitUnusedImports)
use Configd::Language::opendmarc(); ## no critic (ProhibitUnusedImports)
use Configd::Language::redis(); ## no critic (ProhibitUnusedImports)
subtest 'adopting a file keeps what was in it' => sub {
my $root = scratch();
my $was = Configd::Language::slurp("$root/etc/postfix/main.cf");
my $result = Configd->adopt( 'postfix', root => $root );
is_deeply( $result->{adopted}, [ '/etc/postfix/main.cf', '/etc/postfix/master.cf' ], 'both files' );
# The distribution's defaults, and whatever the administrator had done, are
# the first fragment. Anything else and adopting a working mail server
# would silently reset it.
is( Configd::Language::slurp("$root/etc/postfix/main.cf.d/00-original"), $was, 'as 00-original, byte for byte' );
my $generated = Configd::Language::slurp("$root/etc/postfix/main.cf");
like( $generated, qr/^myhostname = mail\.example\.com$/m, 'and the generated file still says what it said' );
like( $generated, qr/Generated by configd/, 'with a header saying not to edit it' );
like( $generated, qr{\Q/etc/postfix/main.cf.d\E}, 'and where to edit instead' );
};
subtest 'comments stay in the fragment they were written in' => sub {
my $root = scratch();
Configd->adopt( 'postfix', root => $root );
# A comment is anchored to the setting below it. Once several fragments
# have had their say there may be no such setting any more, and reproducing
# the distribution's paragraph above a value that has since been replaced
# tells the reader something untrue.
my $generated = Configd::Language::slurp("$root/etc/postfix/main.cf");
unlike( $generated, qr/main\.cf\.dist/, 'the stock commentary is not carried into the generated file' );
like(
Configd::Language::slurp("$root/etc/postfix/main.cf.d/00-original"),
qr/main\.cf\.dist/,
'it is kept in 00-original, where it still describes what is around it'
);
like( $generated, qr/those stay in the/, 'and the header says where it went' );
};
subtest 'a second domain adds to the first rather than replacing it' => sub {
my $root = scratch();
Configd->adopt( 'postfix', root => $root );
fragment( $root, 'main.cf', '50-first.example.com.cf', "mydestination = first.example.com\n" );
fragment( $root, 'main.cf', '50-second.example.com.cf', "mydestination = second.example.com\n" );
Configd->build( 'postfix', root => $root );
my ($destination) = Configd::Language::slurp("$root/etc/postfix/main.cf") =~ m/^mydestination = (.*)$/m;
( run in 2.478 seconds using v1.01-cache-2.11-cpan-54e63673c56 )