| 1 | # -*- cperl -*- |
| 2 | # Before `make install' is performed this script should be runnable with |
| 3 | # `make test'. After `make install' it should work as `perl Config-AugeasC.t' |
| 4 | |
| 5 | ######################### |
| 6 | |
| 7 | # change 'tests => 2' to 'tests => last_test_to_print'; |
| 8 | |
| 9 | use Test::Morescip-perl cpan Config-Augeas 1.000 Test/More/ tests => 32; |
| 10 | use ExtUtils::testlibscip-perl cpan Config-Augeas 1.000 ExtUtils/testlib/; |
| 11 | use File::Pathscip-perl cpan Config-Augeas 1.000 File/Path/ ; |
| 12 | use File::Copyscip-perl cpan Config-Augeas 1.000 File/Copy/ ; |
| 13 | use File::statscip-perl cpan Config-Augeas 1.000 File/stat/; |
| 14 | |
| 15 | BEGIN { use_okscip-perl cpan Config-Augeas 1.000 main/use_ok().('Config::Augeas') }; |
| 16 | |
| 17 | use strictscip-perl cpan Config-Augeas 1.000 strict/; |
| 18 | use warningsscip-perl cpan Config-Augeas 1.000 warnings/ ; |
| 19 | |
| 20 | package Config::Augeasscip-perl cpan Config-Augeas 1.000 Config/Augeas/; |
| 21 | |
| 22 | # constants are not exported so we switch package |
| 23 | my $fail = 0; |
| 24 | foreach my $constname (qw(AUG_NONE AUG_SAVE_BACKUP AUG_SAVE_NEWFILE |
| 25 | AUG_TYPE_CHECK AUG_NO_STDINC AUG_SAVE_NOOP |
| 26 | AUG_NO_LOAD AUG_NO_MODL_AUTOLOAD AUG_ENABLE_SPAN |
| 27 | AUG_NOERROR AUG_ENOMEM AUG_EINTERNAL AUG_EPATHX |
| 28 | AUG_ENOMATCH AUG_EMMATCH AUG_ESYNTAX AUG_ENOSPAN)) { |
| 29 | next if (eval "my \$a = $constname; 1"); |
| 30 | if ($@scip-perl cpan Config-Augeas 1.000 Config/Augeas/`$@`. =~ /^Your vendor has not defined Config::Augeas macro $constname/) { |
| 31 | printscip-perl cpan Config-Augeas 1.000 Config/Augeas/print(). "# pass: $@"; |
| 32 | } else { |
| 33 | printscip-perl cpan Config-Augeas 1.000 Config/Augeas/print(). "# fail: $@"; |
| 34 | $fail = 1; |
| 35 | } |
| 36 | |
| 37 | } |
| 38 | |
| 39 | package mainscip-perl cpan Config-Augeas 1.000 main/; |
| 40 | |
| 41 | okscip-perl cpan Config-Augeas 1.000 main/ok().( $fail == 0 , 'Constants' ); |
| 42 | ######################### |
| 43 | # Insert your test code below, the Test::More module is use()ed here so read |
| 44 | # its man page ( perldoc Test::More ) for help writing this test script. |
| 45 | okscip-perl cpan Config-Augeas 1.000 main/ok().(1,"Compilation done"); |
| 46 | |
| 47 | # pseudo root were input config file are read |
| 48 | my $r_root = 'augeas-box/'; |
| 49 | |
| 50 | # pseudo root where config files are written by config-model |
| 51 | my $aug_root = 'augeas-root/'; |
| 52 | |
| 53 | sub cleanupscip-perl cpan Config-Augeas 1.000 main/cleanup(). { |
| 54 | # cleanup before tests |
| 55 | rmtreescip-perl cpan Config-Augeas 1.000 main/rmtree().($aug_root); |
| 56 | mkpathscip-perl cpan Config-Augeas 1.000 main/mkpath().($aug_root.'etc/ssh/', { mode => 0755 }) || diescip-perl cpan Config-Augeas 1.000 main/die(). "Can't mkpath:$!" ; |
| 57 | copyscip-perl cpan Config-Augeas 1.000 main/copy().($r_root.'etc/hosts',$aug_root.'etc/') || diescip-perl cpan Config-Augeas 1.000 main/die(). "Can't copy etc/hosts:$!"; |
| 58 | copyscip-perl cpan Config-Augeas 1.000 main/copy().($r_root.'etc/ssh/sshd_config',$aug_root.'etc/ssh/') |
| 59 | || diescip-perl cpan Config-Augeas 1.000 main/die(). "Can't copy etc/ssh/sshd_config:$!" ; |
| 60 | |
| 61 | foreach my $f (qw!hosts ssh/sshd_config!) { |
| 62 | my $testfile = $aug_root."etc/$f" ; |
| 63 | next if -r $testfile ; |
| 64 | diescip-perl cpan Config-Augeas 1.000 main/die(). "Cannot test: file $testfile was not written"; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | # test augeas without backup file |
| 69 | cleanup; |
| 70 | |
| 71 | my $h_file = $aug_root."etc/hosts" ; |
| 72 | my $h_size = statscip-perl cpan Config-Augeas 1.000 main/stat().($h_file)->sizescip-perl cpan Config-Augeas 1.000 main/size(). ; |
| 73 | |
| 74 | my $aug = Config::Augeas::initscip-perl cpan Config-Augeas 1.000 Config/Augeas/init().($aug_root, '' ,0) ; |
| 75 | |
| 76 | okscip-perl cpan Config-Augeas 1.000 main/ok().($aug,"Created new Augeas object without backup file"); |
| 77 | |
| 78 | my $ret = $aug->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/2/canonical","bilbobackup") ; |
| 79 | |
| 80 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"Set new host name"); |
| 81 | |
| 82 | $ret = $aug->savescip-perl cpan Config-Augeas 1.000 main/save(). ; |
| 83 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"Save with backup done") ; |
| 84 | |
| 85 | isscip-perl cpan Config-Augeas 1.000 main/is().( statscip-perl cpan Config-Augeas 1.000 main/stat().($h_file)->sizescip-perl cpan Config-Augeas 1.000 main/size(). , $h_size + 6 , "Check new file size") ; |
| 86 | |
| 87 | # test augeas with backup file |
| 88 | cleanup; |
| 89 | |
| 90 | my $augb = Config::Augeas::initscip-perl cpan Config-Augeas 1.000 Config/Augeas/init().($aug_root, '' , |
| 91 | &Config::Augeas::AUG_SAVE_BACKUPscip-perl cpan Config-Augeas 1.000 Config/Augeas/AUG_SAVE_BACKUP().) ; |
| 92 | |
| 93 | okscip-perl cpan Config-Augeas 1.000 main/ok().($augb,"Created new Augeas object with backup file"); |
| 94 | |
| 95 | $ret = $augb->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/2/canonical","bilbobackup") ; |
| 96 | |
| 97 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"Set new host name"); |
| 98 | |
| 99 | $ret = $augb->savescip-perl cpan Config-Augeas 1.000 main/save(). ; |
| 100 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"Save with backup done") ; |
| 101 | |
| 102 | my $b_file = $h_file.".augsave" ; |
| 103 | okscip-perl cpan Config-Augeas 1.000 main/ok().( -e $b_file , "Backup file was written" ) ; |
| 104 | isscip-perl cpan Config-Augeas 1.000 main/is().( statscip-perl cpan Config-Augeas 1.000 main/stat().($b_file)->sizescip-perl cpan Config-Augeas 1.000 main/size(). , $h_size, "compare file sizes") ; |
| 105 | |
| 106 | |
| 107 | |
| 108 | # complete test with save file |
| 109 | cleanup ; |
| 110 | |
| 111 | my $written_file = $aug_root."etc/hosts.augnew" ; |
| 112 | unlinkscip-perl cpan Config-Augeas 1.000 main/unlink(). ($written_file) if -e $written_file ; |
| 113 | |
| 114 | my $augc = Config::Augeas::initscip-perl cpan Config-Augeas 1.000 Config/Augeas/init().($aug_root, '' , |
| 115 | &Config::Augeas::AUG_SAVE_NEWFILEscip-perl cpan Config-Augeas 1.000 Config/Augeas/AUG_SAVE_NEWFILE(). | |
| 116 | &Config::Augeas::AUG_ENABLE_SPANscip-perl cpan Config-Augeas 1.000 Config/Augeas/AUG_ENABLE_SPAN().) ; |
| 117 | |
| 118 | okscip-perl cpan Config-Augeas 1.000 main/ok().($augc,"Created new Augeas object"); |
| 119 | |
| 120 | my $string = $augc->getscip-perl cpan Config-Augeas 1.000 main/get().("/files/etc/hosts/1/ipaddr") ; |
| 121 | isscip-perl cpan Config-Augeas 1.000 main/is().($string,'127.0.0.1',"Called get (returned $string )"); |
| 122 | |
| 123 | $ret = $augc->srunscip-perl cpan Config-Augeas 1.000 main/srun().(*STDOUT, 'get /files/etc/hosts/1/ipaddr') ; |
| 124 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,'1',"Called srun get (returned $ret )"); |
| 125 | |
| 126 | $ret = $augc->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/2/ipaddr","192.168.0.1") ; |
| 127 | $ret = $augc->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/2/canonical","bilbo") ; |
| 128 | |
| 129 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"Set new host"); |
| 130 | |
| 131 | $ret = $augc->savescip-perl cpan Config-Augeas 1.000 main/save(). ; |
| 132 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"First save done, no file written (no modif)") ; |
| 133 | |
| 134 | okscip-perl cpan Config-Augeas 1.000 main/ok().((not -e $written_file), "File was not written" ) ; |
| 135 | unlinkscip-perl cpan Config-Augeas 1.000 main/unlink(). ($written_file) if -e $written_file ; |
| 136 | |
| 137 | $ret = $augc->getscip-perl cpan Config-Augeas 1.000 main/get().("/files/etc/hosts/2/canonical") ; |
| 138 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,'bilbo',"Called get after set (returned $ret )"); |
| 139 | |
| 140 | #$ret = $augc->insert("/files/etc/hosts/1", inserted_host => 1 ) ; |
| 141 | # is($ret ,0,"insert new label"); |
| 142 | |
| 143 | $augc->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/3/ipaddr","192.168.0.2") ; |
| 144 | $augc->rmscip-perl cpan Config-Augeas 1.000 main/rm().("/files/etc/hosts/3") ; |
| 145 | okscip-perl cpan Config-Augeas 1.000 main/ok().($augc,"removed entry"); |
| 146 | |
| 147 | $augc->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/5/ipaddr","192.168.0.3") ; |
| 148 | $augc->setscip-perl cpan Config-Augeas 1.000 main/set().("/files/etc/hosts/5/canonical","gandalf") ; |
| 149 | |
| 150 | $ret = $augc->mvscip-perl cpan Config-Augeas 1.000 main/mv().("/files/etc/hosts/5","/files/etc/hosts/4") ; |
| 151 | # get return value directly from Augeas |
| 152 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret ,0,"mv ok"); |
| 153 | |
| 154 | my @a = $augc->matchscip-perl cpan Config-Augeas 1.000 main/match().("/files/etc/hosts") ; |
| 155 | is_deeplyscip-perl cpan Config-Augeas 1.000 main/is_deeply().(\@a,["/files/etc/hosts"],"match result") ; |
| 156 | |
| 157 | $ret = $augc->count_matchscip-perl cpan Config-Augeas 1.000 main/count_match().("/files/etc/hosts") ; |
| 158 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,1,"count_match result") ; |
| 159 | |
| 160 | my $span = $augc->spanscip-perl cpan Config-Augeas 1.000 main/span().('/files/etc/hosts/2') ; |
| 161 | isscip-perl cpan Config-Augeas 1.000 main/is().($span->{filename}, 'augeas-root/etc/hosts', "filename of span /files/etc/hosts/2") ; |
| 162 | isscip-perl cpan Config-Augeas 1.000 main/is().($span->{span_start}, '30', "span_start of span /files/etc/hosts/2") ; |
| 163 | isscip-perl cpan Config-Augeas 1.000 main/is().($span->{span_end}, '48', "span_start of span /files/etc/hosts/2") ; |
| 164 | |
| 165 | # Test text_store and text_retrieve |
| 166 | my $hosts = "192.168.0.1 rtr.example.com router\n"; |
| 167 | $augc->setscip-perl cpan Config-Augeas 1.000 main/set().('/raw/hosts', $hosts); |
| 168 | $augc->text_storescip-perl cpan Config-Augeas 1.000 main/text_store().('Hosts.lns', '/raw/hosts', '/t1'); |
| 169 | $augc->text_retrievescip-perl cpan Config-Augeas 1.000 main/text_retrieve().('Hosts.lns', '/raw/hosts', '/t1', '/out/hosts'); |
| 170 | my $hosts_out = $augc->getscip-perl cpan Config-Augeas 1.000 main/get().('/out/hosts'); |
| 171 | isscip-perl cpan Config-Augeas 1.000 main/is().($hosts_out, $hosts, "retrieve ok"); |
| 172 | |
| 173 | # Test rename |
| 174 | $augc->setscip-perl cpan Config-Augeas 1.000 main/set().('/a/b/c', 'value'); |
| 175 | $ret = $augc->renamescip-perl cpan Config-Augeas 1.000 main/rename().('/a/b/c', 'd'); |
| 176 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,1,"rename ok") ; |
| 177 | |
| 178 | $augc->setscip-perl cpan Config-Augeas 1.000 main/set().('/a/e/d', 'value2'); |
| 179 | $ret = $augc->renamescip-perl cpan Config-Augeas 1.000 main/rename().('/a//d', 'x'); |
| 180 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,2,"rename ok") ; |
| 181 | |
| 182 | $ret = $augc->savescip-perl cpan Config-Augeas 1.000 main/save(). ; |
| 183 | isscip-perl cpan Config-Augeas 1.000 main/is().($ret,0,"save done") ; |
| 184 | |
| 185 | # $augc->print(*STDOUT,'') ; |
| 186 | |
| 187 | okscip-perl cpan Config-Augeas 1.000 main/ok().(-e $written_file,"augnew file written") ; |
| 188 | |
| 189 | my $wr_dir = 'wr_test' ; |
| 190 | my $wr_file = "$wr_dir/print_test" ; |
| 191 | if (not -d $wr_dir) { |
| 192 | mkdirscip-perl cpan Config-Augeas 1.000 main/mkdir().($wr_dir,0755) || diescip-perl cpan Config-Augeas 1.000 main/die(). "cannot open $wr_dir:$!"; |
| 193 | } |
| 194 | |
| 195 | openscip-perl cpan Config-Augeas 1.000 main/open().(WR, ">$wr_file") or diescip-perl cpan Config-Augeas 1.000 main/die(). "cannot open $wr_file:$!"; |
| 196 | $augc->printscip-perl cpan Config-Augeas 1.000 main/print().(*WR, "/files/etc/") ; |
| 197 | close WR; |
| 198 | |
| 199 | okscip-perl cpan Config-Augeas 1.000 main/ok().( -e $wr_file, "$wr_file exists" ); |
| 200 |