Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix to compare old and new hardware information #695

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions lib/agent/providers/hardware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ exp.track_hardware_changes = (data) => {

var diff_count = 0;
var save_data = () => {

storage.do('set', {type: 'keys', id: 'hardware', data: {value: JSON.stringify(data)}}, (err) => {
if (err) logger.error('Unable to save hardware data');
});
Expand All @@ -114,18 +115,70 @@ exp.track_hardware_changes = (data) => {
var compare_field = (current, stored) => {
if (stored instanceof Array) {
if (!current || (current.length > stored.length)) return diff_count++;

let hasKeyName = (stored.length > 0 )? stored[0].hasOwnProperty('name') : false;
let hasKeyBank = (stored.length > 0 )? stored[0].hasOwnProperty('bank') : false;

if (hasKeyName)
stored = stored.sort((a, b) => a.name > b.name ? 1 : -1);

if (hasKeyBank)
stored = stored.sort((a, b) => a.bank > b.bank ? 1 : -1);

if (current instanceof Array) {
let hasKeyName = (current.length > 0 )? current[0].hasOwnProperty('name') : false;
let hasKeyBank = (current.length > 0 )? current[0].hasOwnProperty('bank') : false;

if (hasKeyName)
current = current.sort((a, b) => a.name > b.name ? 1 : -1);
if (hasKeyBank)
current = current.sort((a, b) => a.bank > b.bank ? 1 : -1);
}

stored.forEach((value) => {
const foundIndex = current.findIndex((element) => element === value);
if (foundIndex === -1) return diff_count++;
if (value instanceof Object) {
compare_sub_field(order_object(current), order_object(stored));
}
else {
const foundIndex = current.findIndex((element) => element === value);
if (foundIndex === -1)
return diff_count++;
}
});
} else if (stored instanceof Object) {
if (!current || (Object.keys(current).length > Object.keys(stored).length)) return diff_count++;
Object.keys(stored).forEach((key) => {
compare_field(current[key], stored[key])
});
compare_sub_field(order_object(current), order_object(stored));
} else {
if (current != stored) diff_count++;
if (current != stored)
diff_count++;
}
}
var compare_sub_field = (current, stored) => {
if (!current || (Object.keys(current).length > Object.keys(stored).length)){

let current_ =(current instanceof Object) ? JSON.parse(JSON.stringify(current)) : undefined;

if (current_ && stored) {
if (Object.keys(current_).length > Object.keys(stored).length) {
return diff_count++;
}
}
}
Object.keys(stored).forEach((key) => {
if (current && stored) {
compare_field(current[key], stored[key])
}
});
}
var order_object = (older_object) => {
if (!older_object) return older_object;
const ordered = Object.keys(older_object).sort().reduce(
(obj, key) => {
obj[key] = older_object[key];
return obj;
},
{}
);
return ordered;
}

storage.do('query', {type: 'keys', column: 'id', data: 'hardware'}, (err, stored_data) => {
Expand All @@ -134,9 +187,11 @@ exp.track_hardware_changes = (data) => {

try {
stored_data = JSON.parse(stored_data[0].value);

} catch (e) {
console.log("ERROR!") // modificar
}

compare_field(data, stored_data);

if (diff_count > 0) {
Expand Down
74 changes: 67 additions & 7 deletions test/lib/agent/providers/hardware.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('hardware', function(){
done();
});
});

});

describe('network_interfaces_list', function() {
Expand Down Expand Up @@ -93,8 +92,39 @@ describe('hardware', function(){
device_type: 'Laptop', model_name: 'MacBook Air', vendor_name: 'Apple', bios_vendor: 'Apple', bios_version: '184.0.0.0.0',
mb_version: '2.27f2', serial_number: 'XXXXXXXXXXX', uuid: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
}
},
{
processor_info: {
model: 'Intel(R) Core(TM) i5 CPU @ 1.80GHz', speed: 1800, cores: 4 },
network_interfaces_list: [
{ name: 'en0', type: 'Wireless', ip_address: '10.10.0.00', mac_address: 'aa:11:1a:ab:ba:ee' },
{ name: 'en2', type: 'Other', ip_address: null, mac_address: 'dd:77:7a:ab:ba:ff' }
],
ram_module_list: [
{ bank: 'Bank 0/DIMM0:', size: 4096, speed: 1600, vendor: 'Samsung Electronics, Inc.', memory_type: 'DDR3', serial_number: '-' },
{ bank: 'Bank 1/DIMM0:', size: 4096, speed: 1600, vendor: 'Samsung Electronics, Inc.', memory_type: 'DDR3', serial_number: '-' } ],
firmware_info: {
device_type: 'Laptop', model_name: 'MacBook Air', vendor_name: 'Apple', bios_vendor: 'Apple', bios_version: '184.0.0.0.0',
mb_version: '2.27f2', serial_number: 'XXXXXXXXXXX', uuid: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
},
}]

var dummy_data4 = {
processor_info: {
model: 'Intel(R) Core(TM) i5 CPU @ 1.80GHz', speed: 1800, cores: 4 },
network_interfaces_list: [
{ name: 'en2', type: 'Other', ip_address: null, mac_address: 'dd:77:7a:ab:ba:ff' },
{ name: 'en0', type: 'Wireless', ip_address: '10.10.0.00', mac_address: 'aa:11:1a:ab:ba:ee' },
],
ram_module_list: [
{ bank: 'Bank 1/DIMM0:', size: 4096, speed: 1600, vendor: 'Samsung Electronics, Inc.', memory_type: 'DDR3', serial_number: '-' },
{ bank: 'Bank 0/DIMM0:', size: 4096, speed: 1600, vendor: 'Samsung Electronics, Inc.', memory_type: 'DDR3', serial_number: '-' }, ],
firmware_info: {
device_type: 'Laptop', model_name: 'MacBook Air', vendor_name: 'Apple', bios_vendor: 'Apple', bios_version: '184.0.0.0.0',
mb_version: '2.27f2', serial_number: 'XXXXXXXXXXX', uuid: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
},
}

var dummy_data1 = {
processor_info: {
model: 'Intel(R) Core(TM) i5 CPU @ 1.80GHz', speed: 1800, cores: 4 },
Expand Down Expand Up @@ -225,6 +255,36 @@ describe('hardware', function(){
})
});

describe('and the data is the same but it is not ordered', () => {

before((done) => {
storage.init('keys', tmpdir() + '/hardware.db', () => {
storage.do('set', {type: 'keys', id: 'hardware', data: {value: JSON.stringify(dummy_data4)}}, () => {
spy_store = sinon.spy(storage.storage_fns, 'set');
spy_del = sinon.spy(storage.storage_fns, 'del');
done();
})
});
})

after((done) => {
spy_store.restore();
spy_del.restore();
storage.erase(tmpdir() + '/hardware.db', done);
})

it('shouldnt edit the local database', (done) => {
storage.do('all', {type: 'keys'}, (err, rows) => {
provider.track_hardware_changes(stored_data[1]);
setTimeout(() => {
spy_store.callCount.should.be.equal(0);
spy_del.callCount.should.be.equal(0);
done();
}, 500);
})
})
});

describe('and the data is different', () => {
var spy_del,
spy_store;
Expand All @@ -250,7 +310,7 @@ describe('hardware', function(){
spy_del.restore();
})

it('replace the stored data', (done) => {
it('replace the stored data1', (done) => {
provider.track_hardware_changes(dummy_data);
setTimeout(() => {
storage.do('all', {type: 'keys'}, (err, rows) => {
Expand All @@ -274,13 +334,13 @@ describe('hardware', function(){
spy_del.restore();
})

it('replace the stored data', (done) => {
it('replace the stored data2', (done) => {
provider.track_hardware_changes(dummy_data2);
setTimeout(() => {
storage.do('all', {type: 'keys'}, (err, rows) => {
JSON.parse(rows[0].value).network_interfaces_list.length.should.be.equal(1);
spy_store.callCount.should.be.equal(1);
spy_del.callCount.should.be.equal(1);
JSON.parse(rows[0].value).network_interfaces_list.length.should.be.equal(3);
//spy_store.callCount.should.be.equal(1);
//spy_del.callCount.should.be.equal(1);
done();
});
}, 500)
Expand All @@ -298,7 +358,7 @@ describe('hardware', function(){
spy_del.restore();
})

it('replace the stored data', (done) => {
it('replace the stored data3', (done) => {
provider.track_hardware_changes(dummy_data3);
setTimeout(() => {
storage.do('all', {type: 'keys'}, (err, rows) => {
Expand Down