cloudflare/Cloudflare-CPanel
Publicmirrored fromhttps://github.com/cloudflare/Cloudflare-CPanel
t/cloudflare-bin-cfadmin.t
90lines · modecode
Update cfadmin to use the correct serializer based on the cPanel version, and use the universal one if it is availabledaac30b
13 years ago
| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | use Test::More tests => 8 + 1; |
| 5 | use Test::NoWarnings; |
| 6 | use File::Temp; |
| 7 | |
| 8 | require '../cloudflare/bin/cfadmin'; |
| 9 | |
| 10 | cmp_ok( bin::cfadmin::__get_current_version(), '>=', 11.30, "bin::cfadmin::__get_current_version() returns a reasonable version (>= 11.30)" ); |
| 11 | |
| 12 | my $serializer_function = bin::cfadmin::__get_serializer_function(); |
| 13 | my $json_load_function = bin::cfadmin::__get_json_load_function(); |
| 14 | my $json_loadfile_function = bin::cfadmin::__get_json_loadfile_function(); |
| 15 | |
| 16 | # Test json functions |
| 17 | { |
| 18 | |
| 19 | my $ref = $json_load_function->('{"pig":"cow"}'); |
| 20 | is_deeply( $ref, { 'pig' => 'cow' }, "json load function works as expected" ); |
| 21 | |
| 22 | my ( $fh, $filename ) = File::Temp::tempfile(); |
| 23 | |
| 24 | print {$fh} '{"pig":"cow"}'; |
| 25 | |
| 26 | close($fh); |
| 27 | |
| 28 | my $ref2 = $json_loadfile_function->($filename); |
| 29 | is_deeply( $ref2, { 'pig' => 'cow' }, "json loadfile function works as expected" ); |
| 30 | |
| 31 | } |
| 32 | |
| 33 | # Test serializer functions |
| 34 | { |
| 35 | no warnings 'redefine'; |
| 36 | |
| 37 | local $INC{'Cpanel/AdminBin/Serializer.pm'} = ''; |
| 38 | local *bin::cfadmin::__get_current_version = sub { |
| 39 | 11.32; |
| 40 | }; |
| 41 | |
| 42 | my $ref = bin::cfadmin::__get_serializer_function(); |
| 43 | isa_ok( $ref, "CODE", "bin::cfadmin::__get_serializer_function returned a Storable object" ); |
| 44 | |
| 45 | my ( $fh, $filename ) = File::Temp::tempfile(); |
| 46 | if ( my $pid = fork() ) { |
| 47 | waitpid( $pid, 0 ); |
| 48 | } |
| 49 | else { |
| 50 | open( STDOUT, '>&=', fileno($fh) ); |
| 51 | $ref->( [ 'dog' => 1 ] ); |
| 52 | exit 0; |
| 53 | } |
| 54 | |
| 55 | seek( $fh, 0, 0 ); |
| 56 | my $data = Storable::fd_retrieve($fh); |
| 57 | is_deeply( $data, [ 'dog' => 1 ], "The Storable object works as expected" ); |
| 58 | |
| 59 | local *bin::cfadmin::__get_current_version = sub { |
| 60 | 11.38; |
| 61 | }; |
| 62 | |
| 63 | my $ref_2 = bin::cfadmin::__get_serializer_function(); |
| 64 | isa_ok( $ref_2, "CODE", "bin::cfadmin::__get_serializer_function returned a Storable object" ); |
| 65 | isnt( $ref, $ref_2, "The references are different" ); |
| 66 | |
| 67 | ( $fh, $filename ) = File::Temp::tempfile(); |
| 68 | if ( my $pid = fork() ) { |
| 69 | waitpid( $pid, 0 ); |
| 70 | } |
| 71 | else { |
| 72 | open( STDOUT, '>&=', fileno($fh) ); |
| 73 | $ref_2->( [ 'cow' => 1 ] ); |
| 74 | exit 0; |
| 75 | } |
| 76 | seek( $fh, 0, 0 ); |
| 77 | my $yaml; |
| 78 | { |
| 79 | local $/; |
| 80 | $yaml = readline($fh); |
| 81 | } |
| 82 | $data = Cpanel::YAML::Load($yaml); |
| 83 | is_deeply( $data, [ 'cow' => 1 ], "The Cpanel::YAML object works as expected" ); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | bin::cfadmin::__get_json_load_function(); |
| 89 | } |