From afa6533f4308092ad7cda89a80b7886cceef9f20 Mon Sep 17 00:00:00 2001 From: Daniel Nichter Date: Wed, 27 Mar 2013 11:12:38 -0600 Subject: [PATCH] Fix and test resource_diff(). --- bin/pt-agent | 9 +++++++-- lib/Percona/WebAPI/Util.pm | 9 +++++++-- t/lib/Percona/WebAPI/Util.t | 13 ++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/bin/pt-agent b/bin/pt-agent index ce2c057a..89ce5d4a 100755 --- a/bin/pt-agent +++ b/bin/pt-agent @@ -1466,6 +1466,7 @@ no Lmo; { package Percona::WebAPI::Util; +use JSON; use Digest::MD5 qw(md5_hex); use Percona::WebAPI::Representation; @@ -1478,8 +1479,12 @@ sub resource_diff { my ($x, $y) = @_; return 0 if !$x && !$y; return 1 if ($x && !$y) || (!$x && $y); - return md5_hex(Percona::WebAPI::Representation::as_json($x)) - ne md5_hex(Percona::WebAPI::Representation::as_json($y)); + my $json = JSON->new->canonical([1]); # avoid hash key sort diffs + my $x_hex = md5_hex( + Percona::WebAPI::Representation::as_json($x, json => $json)); + my $y_hex = md5_hex( + Percona::WebAPI::Representation::as_json($y, json => $json)); + return $x_hex eq $y_hex ? 0 : 1; } 1; diff --git a/lib/Percona/WebAPI/Util.pm b/lib/Percona/WebAPI/Util.pm index 2522a462..5eff0f91 100644 --- a/lib/Percona/WebAPI/Util.pm +++ b/lib/Percona/WebAPI/Util.pm @@ -20,6 +20,7 @@ { package Percona::WebAPI::Util; +use JSON; use Digest::MD5 qw(md5_hex); use Percona::WebAPI::Representation; @@ -32,8 +33,12 @@ sub resource_diff { my ($x, $y) = @_; return 0 if !$x && !$y; return 1 if ($x && !$y) || (!$x && $y); - return md5_hex(Percona::WebAPI::Representation::as_json($x)) - ne md5_hex(Percona::WebAPI::Representation::as_json($y)); + my $json = JSON->new->canonical([1]); # avoid hash key sort diffs + my $x_hex = md5_hex( + Percona::WebAPI::Representation::as_json($x, json => $json)); + my $y_hex = md5_hex( + Percona::WebAPI::Representation::as_json($y, json => $json)); + return $x_hex eq $y_hex ? 0 : 1; } 1; diff --git a/t/lib/Percona/WebAPI/Util.t b/t/lib/Percona/WebAPI/Util.t index bc7a754a..c585016c 100644 --- a/t/lib/Percona/WebAPI/Util.t +++ b/t/lib/Percona/WebAPI/Util.t @@ -45,7 +45,18 @@ $y->options->{spool} = '/var/lib/spool'; is( resource_diff($x, $y), 1, - "Diff" + "Big diff in 1 attrib" +); + +# Restore this... +$y->options->{spool} = '/var/spool'; +# Change this... +$y->options->{ts} = '101'; + +is( + resource_diff($x, $y), + 1, + "Small diff in 1 attrib" ); # #############################################################################