Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add IsInventorySnapshot flag for change tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravga authored and NarineM committed Dec 20, 2017
1 parent e656fe9 commit 7204bee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
21 changes: 10 additions & 11 deletions source/code/plugins/changetracking_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ def self.instanceXMLtoHash(instanceXML)
def self.serviceXMLtoHash(serviceXML, isInventorySnapshot = false)
serviceHash = instanceXMLtoHash(serviceXML)
serviceHash["CollectionName"] = serviceHash["Name"]

if isInventorySnapshot
serviceHash["LastInventorySnapshotTime"] = @@lastInventorySnapshotTime
else
#InventoryChecksum should be calculated before InventorySnapshot is filled.
if isInventorySnapshot == false
serviceHash["InventoryChecksum"] = Digest::SHA256.hexdigest(serviceHash.to_json)
end
serviceHash["IsInventorySnapshot"] = isInventorySnapshot
serviceHash
end

Expand All @@ -62,12 +61,12 @@ def self.packageXMLtoHash(packageXML, isInventorySnapshot = false)
ret["Publisher"] = packageHash["Publisher"]
ret["Size"] = packageHash["Size"]
ret["Timestamp"] = OMS::Common.format_time(packageHash["InstalledOn"].to_i)

if isInventorySnapshot
ret["LastInventorySnapshotTime"] = @@lastInventorySnapshotTime
else
#InventoryChecksum should be calculated before InventorySnapshot is filled.
if isInventorySnapshot == false
ret["InventoryChecksum"] = Digest::SHA256.hexdigest(packageHash.to_json)
end
ret["IsInventorySnapshot"] = isInventorySnapshot

ret
end

Expand All @@ -84,11 +83,11 @@ def self.fileInventoryXMLtoHash(fileInventoryXML, isInventorySnapshot = false)
ret["DateModified"] = OMS::Common.format_time_str(fileInventoryHash["ModifiedDate"])
ret["DateCreated"] = OMS::Common.format_time_str(fileInventoryHash["CreatedDate"])

if isInventorySnapshot
ret["LastInventorySnapshotTime"] = @@lastInventorySnapshotTime
else
#InventoryChecksum should be calculated before InventorySnapshot is filled.
if isInventorySnapshot == false
ret["InventoryChecksum"] = Digest::SHA256.hexdigest(fileInventoryHash.to_json)
end
ret["IsInventorySnapshot"] = isInventorySnapshot
ret
end

Expand Down
38 changes: 28 additions & 10 deletions test/code/plugins/filter_changetracking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def test_serviceXMLtoHash
"Enabled"=> "false",
"Controller"=> "init",
"InventoryChecksum"=>
"6d53388bb96294678111ee5c81cde7ef32d7a3ecf6a320c65142516ee32a9967"
"6d53388bb96294678111ee5c81cde7ef32d7a3ecf6a320c65142516ee32a9967",
"IsInventorySnapshot"=> false
}
instanceXML = ChangeTracking::strToXML(instanceXMLstr).root
assert_equal("INSTANCE", instanceXML.name)
Expand Down Expand Up @@ -181,6 +182,7 @@ def test_packageXMLtoHash
"Timestamp"=> "2016-03-18T22:11:05.000Z",
"InventoryChecksum"=>
"17daf5f8849aa11a1afbe453e9cdf0ea2d9cb8a17488e3b9e0f54575e65497a8",
"IsInventorySnapshot"=> false,
"Architecture"=> "x86_64",
"Size"=> "38487871"
}
Expand Down Expand Up @@ -236,6 +238,7 @@ def test_fileInventoryXMLtoHash
"Group"=>"root",
"InventoryChecksum"=>
"0e084a89b6cce82c34eff883a0dd4deb0d510a1997e4a3e087c962a115369aea",
"IsInventorySnapshot"=>false,
"Mode"=>"644",
"Owner"=>"root",
"Size"=>"835"}
Expand All @@ -252,6 +255,7 @@ def test_transform_and_wrap_Package
[{"Architecture"=>"x86_64",
"CollectionName"=>"rsyslog",
"CurrentVersion"=>"7.4.7",
"IsInventorySnapshot"=>false,
"Name"=>"rsyslog",
"Publisher"=>"CentOS BuildSystem <http://bugs.centos.org>",
"Size"=>"2033615",
Expand Down Expand Up @@ -280,6 +284,7 @@ def test_transform_and_wrap_Service
"FilePath"=>"",
"Installed"=>"true",
"InstalledOn"=>"1458339065",
"IsInventorySnapshot"=>false,
"Name"=>"omsagent",
"PackageDescription"=>
"Microsoft Operations Management Suite for UNIX/Linux agent",
Expand Down Expand Up @@ -315,6 +320,7 @@ def test_transform_and_wrap_file_inventory
"DateModified"=>"2016-08-20T21:12:22.000Z",
"FileSystemPath"=>"/etc/yum.conf",
"Group"=>"root",
"IsInventorySnapshot"=>false,
"Mode"=>"644",
"Owner"=>"root",
"Size"=>"835"},
Expand All @@ -324,6 +330,7 @@ def test_transform_and_wrap_file_inventory
"DateModified"=>"2016-08-20T21:12:22.000Z",
"FileSystemPath"=>"/etc/yum1.conf",
"Group"=>"root",
"IsInventorySnapshot"=>false,
"Mode"=>"644",
"Owner"=>"root",
"Size"=>"835"}],
Expand Down Expand Up @@ -361,6 +368,7 @@ def test_transform_and_wrap_file_inventory
"FileContentBlobLink"=>" ",
"FileSystemPath"=>"/etc/yum.conf",
"Group"=>"root",
"IsInventorySnapshot"=>false,
"Mode"=>"644",
"Owner"=>"root",
"Size"=>"835"},
Expand All @@ -370,6 +378,7 @@ def test_transform_and_wrap_file_inventory
"DateModified"=>"2016-08-20T21:12:22.000Z",
"FileSystemPath"=>"/etc/yum1.conf",
"Group"=>"root",
"IsInventorySnapshot"=>false,
"Mode"=>"644",
"Owner"=>"root",
"Size"=>"835"}],
Expand Down Expand Up @@ -483,7 +492,8 @@ def test_filechangetracking_e2e_ForceSend
ret = ChangeTracking.transform_and_wrap(@fileinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
# Test that duplicates are removed as well. The test data has 1374 packages and 216 services with some duplicates.
assert_equal(1, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime") == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("InventoryChecksum") == false)

previousHash = ChangeTracking.getHash(@fileinventoryHashPath)
Expand All @@ -494,7 +504,8 @@ def test_filechangetracking_e2e_ForceSend
#2nd call should return empty since inventory did not change and time
ret = ChangeTracking.transform_and_wrap(@fileinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
assert_equal(1, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime"))
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot"))
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == true)
assert(ret["DataItems"][0]["Collections"][0].has_key?("InventoryChecksum") == false)

previousHash1 = ChangeTracking.getHash(@fileinventoryHashPath)
Expand Down Expand Up @@ -522,7 +533,8 @@ def test_servicechangetracking_e2e_ForceSend
ret = ChangeTracking.transform_and_wrap(@packageinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
# Test that duplicates are removed as well. The test data has 1374 packages and 216 services with some duplicates.
assert_equal(1371, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime") == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("InventoryChecksum") == false)

previousHash = ChangeTracking.getHash(@fileinventoryHashPath)
Expand All @@ -533,7 +545,8 @@ def test_servicechangetracking_e2e_ForceSend
#2nd call should return empty since inventory did not change and time
ret = ChangeTracking.transform_and_wrap(@packageinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
assert_equal(1371, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime"))
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == true)
assert(ret["DataItems"][0]["Collections"][0].has_key?("InventoryChecksum") == false)

previousHash1 = ChangeTracking.getHash(@fileinventoryHashPath)
Expand Down Expand Up @@ -561,7 +574,8 @@ def test_packagechangetracking_e2e_ForceSend
ret = ChangeTracking.transform_and_wrap(@serviceinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
# Test that duplicates are removed as well. The test data has 1374 packages and 216 services with some duplicates.
assert_equal(209, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime") == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("InventoryChecksum") == false)

previousHash = ChangeTracking.getHash(@fileinventoryHashPath)
Expand All @@ -572,7 +586,8 @@ def test_packagechangetracking_e2e_ForceSend
#2nd call should return empty since inventory did not change and time
ret = ChangeTracking.transform_and_wrap(@serviceinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
assert_equal(209, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime"))
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == true)
assert(ret["DataItems"][0]["Collections"][0].has_key?("InventoryChecksum") == false)

previousHash1 = ChangeTracking.getHash(@fileinventoryHashPath)
Expand All @@ -594,20 +609,23 @@ def test_packagechangetracking_Inventory_e2e
end
ret = ChangeTracking.transform_and_wrap(@serviceinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
assert_equal(209, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime"))
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == true)

#Case 2 - last inventory was sent a while ago, its time for inventory
previousTime = Time.now - (25*3600)
SetInventoryTimeStamp(@fileInventoryTimestampHashPath, previousTime)
ret = ChangeTracking.transform_and_wrap(@serviceinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
assert_equal(209, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime"))
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == true)

#case 3 - last inventry was sent moments ago, inventorry should not be sent, but a change should be found since there was no previous hashfile
SetInventoryTimeStamp(@fileInventoryTimestampHashPath, Time.now)
ret = ChangeTracking.transform_and_wrap(@serviceinventoryPath, @fileinventoryHashPath, @fileInventoryTimestampHashPath)
assert_equal(209, ret["DataItems"][0]["Collections"].size, "Got the wrong number of file inventory instances")
assert(ret["DataItems"][0]["Collections"][0].has_key?("LastInventorySnapshotTime") == false)
assert(ret["DataItems"][0]["Collections"][0].has_key?("IsInventorySnapshot") == true)
assert(ret["DataItems"][0]["Collections"][0]["IsInventorySnapshot"] == false)

#case 4 - last inventry was sent moments ago, inventorry should not be sent, and there is not change
previousTime = Time.now - (9*3600)
Expand Down

0 comments on commit 7204bee

Please sign in to comment.