1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
# -*- cperl -*-
use ExtUtils::testlib;
use Test::More;
use Test::Exception;
use Test::Memory::Cycle;
use Test::Log::Log4perl;
use Config::Model;
use Config::Model::Tester::Setup qw/init_test/;
use Config::Model::Value;
use strict;
use warnings;
Test::Log::Log4perl->ignore_priority("info");
my ($model, $trace) = init_test();
$model->create_config_class(
name => "Master",
'element' => [
# obsolete element cannot be used at all
'obsolete_p' => {
type => 'leaf',
value_type => 'enum',
choice => [qw/cds perl ini custom/],
status => 'obsolete',
description => 'obsolete_p is replaced by non_obso',
},
'deprecated_p' => {
type => 'leaf',
value_type => 'enum',
choice => [qw/cds perl ini custom/],
status => 'deprecated',
description => 'deprecated_p is replaced by new_from_deprecated',
},
'new_from_deprecated' => {
type => 'leaf',
value_type => 'enum',
choice => [qw/cds_file perl_file ini_file augeas custom/],
migrate_from => {
formula => '$replace{$old}',
variables => { old => '- deprecated_p' },
replace => {
perl => 'perl_file',
ini => 'ini_file',
cds => 'cds_file',
},
},
},
'hidden_p' => {
type => 'leaf',
value_type => 'enum',
choice => [qw/cds perl ini custom/],
level => 'hidden',
description => 'hidden_p is replaced by new_from_hidden',
},
] );
$model->create_config_class(
name => "UrlMigration",
'element' => [
'old_url' => {
type => 'leaf',
value_type => 'uniline',
status => 'deprecated',
},
'host' => {
type => 'leaf',
value_type => 'uniline',
mandatory => 1,
migrate_from => {
formula => '$old =~ m!http://([\w\.]+)!; $1 ;',
variables => { old => '- old_url' },
use_eval => 1,
},
},
'port' => {
type => 'leaf',
value_type => 'uniline',
migrate_from => {
formula => '$old =~ m!http://[\w\.]+:(\d+)!; $1 ;',
variables => { old => '- old_url' },
use_eval => 1,
},
},
'path' => {
type => 'leaf',
value_type => 'uniline',
migrate_from => {
formula => '$old =~ m!http://[\w\.]+(?::\d+)?(/.*)!; $1 ;',
variables => { old => '- old_url' },
use_eval => 1,
},
},
],
);
my $inst = $model->instance(
root_class_name => 'Master',
instance_name => 'test1'
);
ok( $inst, "created dummy instance" );
my $root = $inst->config_root;
# emulate start of file read
$inst->initial_load_start;
throws_ok { $root->fetch_element('obsolete_p'); }
'Config::Model::Exception::ObsoleteElement',
'tried to fetch obsolete element';
my $dp;
{
my $foo = Test::Log::Log4perl->expect([
User => warn => qr/Element 'deprecated_p' of node 'Master' is deprecated/
]);
$dp = $root->fetch_element('deprecated_p');
}
my $nfd = $root->fetch_element('new_from_deprecated');
is( $nfd->fetch, undef, "undef old and undef new" );
# does not generate a warning
$dp->store('ini');
$inst->initial_load_stop;
is( $nfd->fetch, 'ini_file', "old is 'ini' and new is 'ini_file'" );
is( $nfd->fetch_custom, 'ini_file', "likewise for custom_value" );
is( $nfd->fetch('non_upstream_default'), 'ini_file', "likewise for non_builtin_default" );
is( $nfd->fetch_standard, undef, "but standard value is undef" );
# check element list
is_deeply( [ $root->get_element_name ],
[qw/new_from_deprecated/], "check that deprecated and obsolete parameters are hidden" );
is( $root->dump_tree, "new_from_deprecated=ini_file -\n", "check dump tree" );
# now override the migrated value
$nfd->store('perl_file');
is( $nfd->fetch, 'perl_file', "overridden value is 'perl_file'" );
is( $nfd->fetch_custom, 'perl_file', "likewise for custom_value" );
is( $nfd->fetch('non_upstream_default'), 'perl_file', "likewise for non_builtin_default" );
is( $nfd->fetch_standard, undef, "but standard value is undef" );
# test migration with regexp value
my $uinst = $model->instance(
root_class_name => 'UrlMigration',
instance_name => 'urltest'
);
ok( $uinst, "created url test instance" );
my $uroot = $uinst->config_root;
# emulate start of file read
$uinst->initial_load_start;
my $host = 'foo.gre.hp.com';
my $port = 2345;
my $path = '/bar/baz.html';
my $url = "http://$host:$port$path";
# check element list
is_deeply( [ $uroot->get_element_name ],
[qw/host port path/], "check that url deprecated and obsolete parameters are hidden" );
{
# check warning when fetching deprecated element
my $foo = Test::Log::Log4perl->expect([
User => warn => qr/Element 'old_url' of node 'UrlMigration' is deprecated/
]);
$dp = $uroot->fetch_element('old_url')->store($url);
}
$uinst->initial_load_stop;
my $h = $uroot->fetch_element('host');
is( $h->fetch, $host, "check extracted host" );
is( $uroot->fetch_element('port')->fetch, $port, "check extracted port" );
is( $uroot->fetch_element('path')->fetch, $path, "check extracted path" );
memory_cycle_ok( $model, "test memory cycles" );
done_testing;
|